app.js 6.0 KB

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