app.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. var _DEBUG = true;
  2. window.jsonParse=function(){var r="(?:-?\\b(?:0|[1-9][0-9]*)(?:\\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\\b)",k='(?:[^\\0-\\x08\\x0a-\\x1f"\\\\]|\\\\(?:["/\\\\bfnrt]|u[0-9A-Fa-f]{4}))';k='(?:"'+k+'*")';var s=new RegExp("(?:false|true|null|[\\{\\}\\[\\]]|"+r+"|"+k+")","g"),t=new RegExp("\\\\(?:([^u])|u(.{4}))","g"),u={'"':'"',"/":"/","\\":"\\",b:"\u0008",f:"\u000c",n:"\n",r:"\r",t:"\t"};function v(h,j,e){return j?u[j]:String.fromCharCode(parseInt(e,16))}var w=new String(""),x=Object.hasOwnProperty;return function(h,
  3. j){h=h.match(s);var e,c=h[0],l=false;if("{"===c)e={};else if("["===c)e=[];else{e=[];l=true}for(var b,d=[e],m=1-l,y=h.length;m<y;++m){c=h[m];var a;switch(c.charCodeAt(0)){default:a=d[0];a[b||a.length]=+c;b=void 0;break;case 34:c=c.substring(1,c.length-1);if(c.indexOf("\\")!==-1)c=c.replace(t,v);a=d[0];if(!b)if(a instanceof Array)b=a.length;else{b=c||w;break}a[b]=c;b=void 0;break;case 91:a=d[0];d.unshift(a[b||a.length]=[]);b=void 0;break;case 93:d.shift();break;case 102:a=d[0];a[b||a.length]=false;
  4. b=void 0;break;case 110:a=d[0];a[b||a.length]=null;b=void 0;break;case 116:a=d[0];a[b||a.length]=true;b=void 0;break;case 123:a=d[0];d.unshift(a[b||a.length]={});b=void 0;break;case 125:d.shift();break}}if(l){if(d.length!==1)throw new Error;e=e[0]}else if(d.length)throw new Error;if(j){var p=function(n,o){var f=n[o];if(f&&typeof f==="object"){var i=null;for(var g in f)if(x.call(f,g)&&f!==n){var q=p(f,g);if(q!==void 0)f[g]=q;else{i||(i=[]);i.push(g)}}if(i)for(g=i.length;--g>=0;)delete f[i[g]]}return j.call(n,
  5. o,f)};e=p({"":e},"")}return e}}();
  6. (function($){$.toJSON=function(o)
  7. {if(typeof(JSON)=='object'&&JSON.stringify)
  8. return JSON.stringify(o);var type=typeof(o);if(o===null)
  9. return"null";if(type=="undefined")
  10. return undefined;if(type=="number"||type=="boolean")
  11. return o+"";if(type=="string")
  12. return $.quoteString(o);if(type=='object')
  13. {if(typeof o.toJSON=="function")
  14. return $.toJSON(o.toJSON());if(o.constructor===Date)
  15. {var month=o.getUTCMonth()+1;if(month<10)month='0'+month;var day=o.getUTCDate();if(day<10)day='0'+day;var year=o.getUTCFullYear();var hours=o.getUTCHours();if(hours<10)hours='0'+hours;var minutes=o.getUTCMinutes();if(minutes<10)minutes='0'+minutes;var seconds=o.getUTCSeconds();if(seconds<10)seconds='0'+seconds;var milli=o.getUTCMilliseconds();if(milli<100)milli='0'+milli;if(milli<10)milli='0'+milli;return'"'+year+'-'+month+'-'+day+'T'+
  16. hours+':'+minutes+':'+seconds+'.'+milli+'Z"';}
  17. if(o.constructor===Array)
  18. {var ret=[];for(var i=0;i<o.length;i++)
  19. ret.push($.toJSON(o[i])||"null");return"["+ret.join(",")+"]";}
  20. var pairs=[];for(var k in o){var name;var type=typeof k;if(type=="number")
  21. name='"'+k+'"';else if(type=="string")
  22. name=$.quoteString(k);else
  23. continue;if(typeof o[k]=="function")
  24. continue;var val=$.toJSON(o[k]);pairs.push(name+":"+val);}
  25. return"{"+pairs.join(", ")+"}";}};$.evalJSON=function(src)
  26. {if(typeof(JSON)=='object'&&JSON.parse)
  27. return JSON.parse(src);return eval("("+src+")");};$.secureEvalJSON=function(src)
  28. {if(typeof(JSON)=='object'&&JSON.parse)
  29. return JSON.parse(src);var filtered=src;filtered=filtered.replace(/\\["\\\/bfnrtu]/g,'@');filtered=filtered.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']');filtered=filtered.replace(/(?:^|:|,)(?:\s*\[)+/g,'');if(/^[\],:{}\s]*$/.test(filtered))
  30. return eval("("+src+")");else
  31. throw new SyntaxError("Error parsing JSON, source is not valid.");};$.quoteString=function(string)
  32. {if(string.match(_escapeable))
  33. {return'"'+string.replace(_escapeable,function(a)
  34. {var c=_meta[a];if(typeof c==='string')return c;c=a.charCodeAt();return'\\u00'+Math.floor(c/16).toString(16)+(c%16).toString(16);})+'"';}
  35. return'"'+string+'"';};var _escapeable=/["\\\x00-\x1f\x7f-\x9f]/g;var _meta={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'};})(jQuery);
  36. /**
  37. * Init debug, grabs console object if accessible, or makes dummy debugger
  38. */
  39. var fb = _DEBUG && 'undefined' != typeof(console) ? console : {
  40. log : function(){},
  41. debug : function(){},
  42. info : function(){},
  43. warn : function(){},
  44. error : function(){},
  45. assert : function(){},
  46. dir : function(){},
  47. dirxml : function(){},
  48. trace : function(){},
  49. group : function(){},
  50. groupEnd : function(){},
  51. time : function(){},
  52. timeEnd : function(){},
  53. profile : function(){},
  54. profileEnd : function(){},
  55. count : function(){},
  56. msg : function(){}
  57. };
  58. /**
  59. * App namespace
  60. */
  61. var App = {
  62. Ajax: {},
  63. Env: {
  64. BROWSER: {
  65. type: 'unknown-type',
  66. version: 'version', // prefixed with type will be "unknown-version"
  67. os: 'unknown-os'
  68. },
  69. getWorldName: function() {
  70. return App.Env.world.toLowerCase();
  71. }
  72. },
  73. Core: {},
  74. Bash: {},
  75. Console: {},
  76. Constants: {},
  77. Actions: {},
  78. Helpers: {},
  79. Filters: {},
  80. i18n: {},
  81. HTML: {
  82. Build: {}
  83. },
  84. View:{
  85. // pages related views
  86. Pages: {
  87. USER: {},
  88. WEB_DOMAIN: {},
  89. MAIL: {},
  90. DB: {},
  91. DNS: {},
  92. IP: {},
  93. CRON: {}
  94. }
  95. },
  96. Messages: {},
  97. Model: {
  98. USER: {},
  99. WEB_DOMAIN: {},
  100. MAIL: {},
  101. DB: {},
  102. DNS: {},
  103. IP: {},
  104. CRON: {}
  105. },
  106. Cache: {
  107. clear: function(){} // stub method, will be used later
  108. },
  109. Pages: {
  110. USER: {},
  111. WEB_DOMAIN: {},
  112. MAIL: {},
  113. DB: {},
  114. DNS: {},
  115. IP: {},
  116. CRON: {}
  117. },
  118. Ref: {},
  119. Tmp: {},
  120. Thread: {
  121. run: function(delay, ref)
  122. {
  123. setTimeout(function()
  124. {
  125. ref();
  126. }, delay*10);
  127. }
  128. },
  129. Settings: {},
  130. Templates: {
  131. Templator: null,
  132. Tpl: {},
  133. _indexes: {}
  134. },
  135. Utils: {},
  136. Validate: {}
  137. };
  138. // Internals
  139. Array.prototype.set = function(key, value)
  140. {
  141. var index = this[0][key];
  142. this[1][index] = value;
  143. }
  144. Array.prototype.get = function(key){
  145. var index = this[0][key];
  146. return this[1][index];
  147. }
  148. Array.prototype.finalize = function()
  149. {
  150. this.shift();
  151. this[0] = this[0].join('');
  152. return this[0];
  153. }
  154. Array.prototype.done = function()
  155. {
  156. return this.join('');
  157. }
  158. String.prototype.wrapperize = function(key, ns)
  159. {
  160. var tpl = App.Templates.get(key, ns);
  161. tpl.set(':content', this);
  162. return tpl.finalize();
  163. }
  164. App.Ajax.request = function(jedi_method, data, callback)
  165. {
  166. App.Helpers.beforeAjax(jedi_method);
  167. $.ajax({
  168. url: function() {
  169. var url_parts = location.href.replace('#', '').split('/');
  170. if (url_parts[url_parts.length -1] == 'index.html') {
  171. url_parts[url_parts.length -1] = 'dispatch.php';
  172. }
  173. else {
  174. url_parts.push('dispatch.php');
  175. }
  176. return url_parts.join('/');
  177. }(),
  178. global: false,
  179. type: data.request_method || "POST",
  180. data: $.extend(data, {'jedi_method': jedi_method}),
  181. dataType: "text",
  182. async:true,
  183. success: function(reply)
  184. {
  185. timer.start();
  186. callback && callback(jsonParse(reply));
  187. App.Helpers.afterAjax();
  188. timer.stop(jedi_method);
  189. },
  190. error: function()
  191. {
  192. App.View.popup('error');
  193. }
  194. });
  195. }
  196. /**
  197. * Timer for profiling
  198. */
  199. var timer = {};
  200. timer.start = function()
  201. {
  202. timer.start_time = new Date();
  203. }
  204. timer.stop = function( msg )
  205. {
  206. timer.stop_time = new Date();
  207. timer.print( msg );
  208. }
  209. timer.print = function( msg )
  210. {
  211. var passed = timer.stop_time - timer.start_time;
  212. fb.info( (msg || '') + ': ' + passed / 1000 );
  213. }