o2.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /** ***** BEGIN LICENSE BLOCK *****
  2. * |------------------------------------------------------------------------------|
  3. * | O2OA 活力办公 创意无限 o2.js |
  4. * |------------------------------------------------------------------------------|
  5. * | Distributed under the AGPL license: |
  6. * |------------------------------------------------------------------------------|
  7. * | Copyright © 2018, o2oa.net, o2server.io O2 Team |
  8. * | All rights reserved. |
  9. * |------------------------------------------------------------------------------|
  10. *
  11. * This file is part of O2OA.
  12. *
  13. * O2OA is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * O2OA is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with Foobar. If not, see <https://www.gnu.org/licenses/>.
  25. *
  26. * ***** END LICENSE BLOCK ******/
  27. /* load o2 Core
  28. * |------------------------------------------------------------------------------|
  29. * |addReady: o2.addReady(fn), |
  30. * |------------------------------------------------------------------------------|
  31. * |load: o2.load(urls, callback, reload) |
  32. * |loadCss: o2.loadCss(urls, dom, callback, reload, doc) |
  33. * |------------------------------------------------------------------------------|
  34. * |typeOf: o2.typeOf(o) |
  35. * |------------------------------------------------------------------------------|
  36. * |uuid: o2.uuid() |
  37. * |------------------------------------------------------------------------------|
  38. */
  39. (function(){
  40. var _href = window.location.href;
  41. var _debug = (_href.indexOf("debugger")!==-1);
  42. var _par = _href.substr(_href.lastIndexOf("?")+1, _href.length);
  43. var _lp = "zh-cn";
  44. if (_par){
  45. var _parList = _par.split("&");
  46. for (var i=0; i<_parList.length; i++){
  47. var _v = _parList[i];
  48. var _kv = _v.split("=");
  49. if (_kv[0].toLowerCase()==="lg") _lp = _kv[1];
  50. }
  51. }
  52. this.o2 = {
  53. "version": {
  54. "v": '2.0.0',
  55. "build": "2018.11.22",
  56. "info": "O2OA 活力办公 创意无限. Copyright © 2018, o2oa.net O2 Team All rights reserved."
  57. },
  58. "session": {
  59. "isDebugger": _debug,
  60. "path": "/o2_core/o2"
  61. },
  62. "language": _lp,
  63. "splitStr": /(,\s*){1}|(;\s*){1}/g
  64. };
  65. var _attempt = function(){
  66. for (var i = 0, l = arguments.length; i < l; i++){
  67. try {
  68. arguments[i]();
  69. return arguments[i];
  70. } catch (e){}
  71. }
  72. return null;
  73. };
  74. var _typeOf = function(item){
  75. if (item == null) return 'null';
  76. if (item.$family != null) return item.$family();
  77. if (item.constructor == window.Array) return "array";
  78. if (item.nodeName){
  79. if (item.nodeType == 1) return 'element';
  80. if (item.nodeType == 3) return (/\S/).test(item.nodeValue) ? 'textnode' : 'whitespace';
  81. } else if (typeof item.length == 'number'){
  82. if (item.callee) return 'arguments';
  83. }
  84. return typeof item;
  85. };
  86. this.o2.typeOf = _typeOf;
  87. var _addListener = function(dom, type, fn){
  88. if (type == 'unload'){
  89. var old = fn, self = this;
  90. fn = function(){
  91. _removeListener(dom, 'unload', fn);
  92. old();
  93. };
  94. }
  95. if (dom.addEventListener) dom.addEventListener(type, fn, !!arguments[2]);
  96. else dom.attachEvent('on' + type, fn);
  97. };
  98. var _removeListener = function(dom, type, fn){
  99. if (dom.removeEventListener) dom.removeEventListener(type, fn, !!arguments[2]);
  100. else dom.detachEvent('on' + type, fn);
  101. };
  102. //http request class
  103. var _request = (function(){
  104. var XMLHTTP = function(){ return new XMLHttpRequest(); };
  105. var MSXML2 = function(){ return new ActiveXObject('MSXML2.XMLHTTP'); };
  106. var MSXML = function(){ return new ActiveXObject('Microsoft.XMLHTTP'); };
  107. return _attempt(XMLHTTP, MSXML2, MSXML);
  108. })();
  109. var _returnBase = function(number, base) {
  110. return (number).toString(base).toUpperCase();
  111. };
  112. var _getIntegerBits = function(val, start, end){
  113. var base16 = _returnBase(val, 16);
  114. var quadArray = new Array();
  115. var quadString = '';
  116. var i = 0;
  117. for (i = 0; i < base16.length; i++) {
  118. quadArray.push(base16.substring(i, i + 1));
  119. }
  120. for (i = Math.floor(start / 4); i <= Math.floor(end / 4); i++) {
  121. if (!quadArray[i] || quadArray[i] == '')
  122. quadString += '0';
  123. else
  124. quadString += quadArray[i];
  125. }
  126. return quadString;
  127. };
  128. var _rand = function(max) {
  129. return Math.floor(Math.random() * (max + 1));
  130. };
  131. //uuid
  132. var _uuid = function(){
  133. var dg = new Date(1582, 10, 15, 0, 0, 0, 0);
  134. var dc = new Date();
  135. var t = dc.getTime() - dg.getTime();
  136. var tl = _getIntegerBits(t, 0, 31);
  137. var tm = _getIntegerBits(t, 32, 47);
  138. var thv = _getIntegerBits(t, 48, 59) + '1';
  139. var csar = _getIntegerBits(_rand(4095), 0, 7);
  140. var csl = _getIntegerBits(_rand(4095), 0, 7);
  141. var n = _getIntegerBits(_rand(8191), 0, 7)
  142. + _getIntegerBits(_rand(8191), 8, 15)
  143. + _getIntegerBits(_rand(8191), 0, 7)
  144. + _getIntegerBits(_rand(8191), 8, 15)
  145. + _getIntegerBits(_rand(8191), 0, 15);
  146. return tl + tm + thv + csar + csl + n;
  147. };
  148. this.o2.uuid = _uuid;
  149. var _runCallback = function(callback, key, par){
  150. if (typeOf(callback).toLowerCase() === 'function'){
  151. if (key.toLowerCase()==="success") callback.apply(callback, par);
  152. }else{
  153. if (typeOf(callback).toLowerCase()==='object'){
  154. var name = ("on-"+key).camelCase();
  155. if (callback[name]) callback[name].apply(callback, par);
  156. }
  157. }
  158. };
  159. this.o2.runCallback = _runCallback;
  160. //load js, css, html adn all.
  161. var _getAllOptions = function(options){
  162. var doc = (options && options.doc) || document;
  163. if (!doc.unid) doc.unid = _uuid();
  164. return {
  165. "noCache": !!(options && options.nocache),
  166. "reload": !!(options && options.reload),
  167. "sequence": !!(options && options.sequence),
  168. "doc": doc,
  169. "dom": (options && options.dom) || document.body,
  170. "bind": (options && options.bind) || null,
  171. "position": "beforeend" //'beforebegin' 'afterbegin' 'beforeend' 'afterend'
  172. }
  173. };
  174. var _getCssOptions = function(options){
  175. var doc = (options && options.doc) || document;
  176. if (!doc.unid) doc.unid = _uuid();
  177. return {
  178. "noCache": !!(options && options.nocache),
  179. "reload": !!(options && options.reload),
  180. "sequence": !!(options && options.sequence),
  181. "doc": doc,
  182. "dom": (options && options.dom) || null
  183. }
  184. };
  185. var _getJsOptions = function(options){
  186. var doc = (options && options.doc) || document;
  187. if (!doc.unid) doc.unid = _uuid();
  188. return {
  189. "noCache": !!(options && options.nocache),
  190. "reload": !!(options && options.reload),
  191. "sequence": (!(options && options.sequence == false)),
  192. "doc": doc
  193. }
  194. };
  195. var _getHtmlOptions = function(options){
  196. var doc = (options && options.doc) || document;
  197. if (!doc.unid) doc.unid = _uuid();
  198. return {
  199. "noCache": !!(options && options.nocache),
  200. "reload": !!(options && options.reload),
  201. "sequence": !!(options && options.sequence),
  202. "doc": doc,
  203. "dom": (options && options.dom) || null,
  204. "bind": (options && options.bind) || null,
  205. "position": "beforeend" //'beforebegin' 'afterbegin' 'beforeend' 'afterend'
  206. }
  207. };
  208. var _xhr_get = function(url, success, failure, completed){
  209. var xhr = new _request();
  210. xhr.open("GET", url, true);
  211. var _checkCssLoaded= function(_, err){
  212. if (!(xhr.readyState == 4)) return;
  213. if (err){
  214. if (completed) completed(xhr);
  215. return;
  216. }
  217. _removeListener(xhr, 'readystatechange', _checkCssLoaded);
  218. _removeListener(xhr, 'load', _checkCssLoaded);
  219. _removeListener(xhr, 'error', _checkCssErrorLoaded);
  220. if (err) {failure(xhr); return}
  221. var status = xhr.status;
  222. status = (status == 1223) ? 204 : status;
  223. if ((status >= 200 && status < 300))
  224. success(xhr);
  225. else if ((status >= 300 && status < 400))
  226. failure(xhr);
  227. else
  228. failure(xhr);
  229. if (completed) completed(xhr);
  230. };
  231. var _checkCssErrorLoaded= function(err){ _checkCssLoaded(err) };
  232. if ("load" in xhr) _addListener(xhr, "load", _checkCssLoaded);
  233. if ("error" in xhr) _addListener(xhr, "load", _checkCssErrorLoaded);
  234. _addListener(xhr, "readystatechange", _checkCssLoaded);
  235. xhr.send();
  236. };
  237. var _loadSequence = function(ms, cb, op, n, thisLoaded, loadSingle, uuid, fun){
  238. loadSingle(ms[n], function(module){
  239. if (module) thisLoaded.push(module);
  240. n++;
  241. if (fun) fun(module);
  242. if (n===ms.length){
  243. if (cb) cb(thisLoaded);
  244. }else{
  245. _loadSequence(ms, cb, op, n, thisLoaded, loadSingle, uuid, fun);
  246. }
  247. }, op, uuid);
  248. };
  249. var _loadDisarray = function(ms, cb, op, thisLoaded, loadSingle, uuid, fun){
  250. var count=0;
  251. for (var i=0; i<ms.length; i++){
  252. loadSingle(ms[i], function(module){
  253. if (module) thisLoaded.push(module);
  254. count++;
  255. if (fun) fun(module);
  256. if (count===ms.length) if (cb) cb(thisLoaded);
  257. }, op, uuid);
  258. }
  259. };
  260. //load js
  261. //use framework url
  262. var _frameworks = {
  263. "o2.core": ["/o2_core/o2/o2.core.js"],
  264. "o2.more": ["/o2_core/o2/o2.more.js"],
  265. "ie_adapter": ["/o2_lib/o2/ie_adapter.js"],
  266. "jquery": ["/o2_lib/jquery/jquery.min.js"],
  267. "mootools": ["/o2_lib/mootools/mootools-1.6.0_all.js"],
  268. "ckeditor": ["/o2_lib/htmleditor/ckeditor/ckeditor.js"],
  269. "raphael": ["/o2_lib/raphael/raphael.js"],
  270. "d3": ["/o2_lib/d3/d3.min.js"],
  271. "ace": ["/o2_lib/ace/src-noconflict/ace.js","/o2_lib/ace/src-noconflict/ext-language_tools.js"],
  272. "JSBeautifier": ["/o2_lib/JSBeautifier/beautify.js"],
  273. "JSBeautifier_css": ["/o2_lib/JSBeautifier/beautify-css.js"],
  274. "JSBeautifier_html": ["/o2_lib/JSBeautifier/beautify-html.js"],
  275. "JSONTemplate": ["/o2_lib/mootools/plugin/Template.js"],
  276. "kity": ["/o2_lib/kityminder/kity/kity.min.js"],
  277. "kityminder": ["/o2_lib/kityminder/core/dist/kityminder.core.js"]
  278. };
  279. var _loaded = {};
  280. var _loadedCss = {};
  281. var _loadedHtml = {};
  282. var _loadCssRunning = {};
  283. var _loadCssQueue = [];
  284. var _loadSingle = function(module, callback, op){
  285. var url = module;
  286. var uuid = _uuid();
  287. if (op.noCache) url = (url.indexOf("?")!==-1) ? url+"&v="+uuid : addr_uri+"?v="+uuid;
  288. var key = encodeURIComponent(url+op.doc.unid);
  289. if (!op.reload) if (_loaded[key]){
  290. if (callback)callback(); return;
  291. }
  292. var head = (op.doc.head || op.doc.getElementsByTagName("head")[0] || op.doc.documentElement);
  293. var s = op.doc.createElement('script');
  294. head.appendChild(s);
  295. s.id = uuid;
  296. s.src = url;
  297. var _checkScriptLoaded = function(_, isAbort, err){
  298. if (isAbort || !s.readyState || s.readyState === "loaded" || s.readyState === "complete") {
  299. var scriptObj = {"module": module, "id": uuid, "script": s, "doc": op.doc};
  300. if (!err) _loaded[key] = scriptObj;
  301. _removeListener(s, 'readystatechange', _checkScriptLoaded);
  302. _removeListener(s, 'load', _checkScriptLoaded);
  303. _removeListener(s, 'error', _checkScriptErrorLoaded);
  304. if (!isAbort || err){
  305. if (err){
  306. if (s) head.removeChild(s);
  307. if (callback)callback();
  308. }else{
  309. head.removeChild(s);
  310. if (callback)callback(scriptObj);
  311. }
  312. }
  313. }
  314. };
  315. var _checkScriptErrorLoaded = function(e, err){
  316. console.log("Error: load javascript module: "+module);
  317. _checkScriptLoaded(e, true, "error");
  318. };
  319. if ('onreadystatechange' in s) _addListener(s, 'readystatechange', _checkScriptLoaded);
  320. _addListener(s, 'load', _checkScriptLoaded);
  321. _addListener(s, 'error', _checkScriptErrorLoaded);
  322. };
  323. var _load = function(urls, options, callback){
  324. var ms = (_typeOf(urls)==="array") ? urls : [urls];
  325. var op = (_typeOf(options)==="object") ? _getJsOptions(options) : _getJsOptions(null);
  326. var cb = (_typeOf(options)==="function") ? options : callback;
  327. var modules = [];
  328. for (var i=0; i<ms.length; i++){
  329. var url = ms[i];
  330. var module = _frameworks[url] || url;
  331. if (_typeOf(module)==="array"){
  332. modules = modules.concat(module)
  333. }else{
  334. modules.push(module)
  335. }
  336. }
  337. var thisLoaded = [];
  338. if (op.sequence){
  339. _loadSequence(modules, cb, op, 0, thisLoaded, _loadSingle);
  340. }else{
  341. _loadDisarray(modules, cb, op, thisLoaded, _loadSingle);
  342. }
  343. };
  344. this.o2.load = _load;
  345. //load css
  346. var _loadSingleCss = function(module, callback, op, uuid){
  347. var url = module;
  348. var uid = _uuid();
  349. if (op.noCache) url = (url.indexOf("?")!==-1) ? url+"&v="+uid : url+"?v="+uid;
  350. var key = encodeURIComponent(url+op.doc.unid);
  351. if (_loadCssRunning[key]){
  352. _loadCssQueue.push(function(){
  353. _loadSingleCss(module, callback, op, uuid);
  354. });
  355. return;
  356. }
  357. if (_loadedCss[key]) uuid = _loadedCss[key]["class"];
  358. if (op.dom) _parseDom(op.dom, function(node){ if (node.className.indexOf(uuid) == -1) node.className += ((node.className) ? " "+uuid : uuid);}, op.doc);
  359. var completed = function(){
  360. if (_loadCssRunning[key]){
  361. _loadCssRunning[key] = false;
  362. delete _loadCssRunning[key];
  363. }
  364. if (_loadCssQueue && _loadCssQueue.length){
  365. (_loadCssQueue.shift())();
  366. }
  367. };
  368. if (_loadedCss[key])if (!op.reload){
  369. if (callback)callback(_loadedCss[key]);
  370. completed();
  371. return;
  372. }
  373. var success = function(xhr){
  374. var cssText = xhr.responseText;
  375. try{
  376. if (cssText){
  377. if (op.bind) cssText = cssText.bindJson(op.bind);
  378. if (op.dom){
  379. var rex = new RegExp("(.+)(?=\\{)", "g");
  380. var match;
  381. while ((match = rex.exec(cssText)) !== null) {
  382. var prefix = "." + uuid + " ";
  383. var rule = prefix + match[0];
  384. cssText = cssText.substring(0, match.index) + rule + cssText.substring(rex.lastIndex, cssText.length);
  385. rex.lastIndex = rex.lastIndex + prefix.length;
  386. }
  387. }
  388. var style = op.doc.createElement("style");
  389. style.setAttribute("type", "text/css");
  390. var head = (op.doc.head || op.doc.getElementsByTagName("head")[0] || op.doc.documentElement);
  391. head.appendChild(style);
  392. if(style.styleSheet){
  393. var setFunc = function(){
  394. style.styleSheet.cssText = cssText;
  395. };
  396. if(style.styleSheet.disabled){
  397. setTimeout(setFunc, 10);
  398. }else{
  399. setFunc();
  400. }
  401. }else{
  402. var cssTextNode = op.doc.createTextNode(cssText);
  403. style.appendChild(cssTextNode);
  404. }
  405. }
  406. style.id = uid;
  407. var styleObj = {"module": module, "id": uid, "style": style, "doc": op.doc, "class": uuid};
  408. _loadedCss[key] = styleObj;
  409. if (callback) callback(styleObj);
  410. }catch (e){
  411. if (callback) callback();
  412. return;
  413. }
  414. };
  415. var failure = function(xhr){
  416. console.log("Error: load css module: "+module);
  417. if (callback) callback();
  418. };
  419. _loadCssRunning[key] = true;
  420. _xhr_get(url, success, failure, completed);
  421. };
  422. var _parseDomString = function(dom, fn, sourceDoc){
  423. var doc = sourceDoc || document;
  424. var list = doc.querySelectorAll(dom);
  425. if (list.length) for (var i=0; i<list.length; i++) _parseDomElement(list[i], fn);
  426. };
  427. var _parseDomElement = function(dom, fn){
  428. if (fn) fn(dom);
  429. };
  430. var _parseDom = function(dom, fn, sourceDoc){
  431. var domType = _typeOf(dom);
  432. if (domType==="string") _parseDomString(dom, fn, sourceDoc);
  433. if (domType==="element") _parseDomElement(dom, fn);
  434. if (domType==="array") for (var i=0; i<dom.length; i++) _parseDom(dom[i], fn, sourceDoc);
  435. };
  436. var _loadCss = function(modules, options, callback){
  437. var ms = (_typeOf(modules)==="array") ? modules : [modules];
  438. var op = (_typeOf(options)==="object") ? _getCssOptions(options) : _getCssOptions(null);
  439. var cb = (_typeOf(options)==="function") ? options : callback;
  440. var uuid = "css"+_uuid();
  441. var thisLoaded = [];
  442. if (op.sequence){
  443. _loadSequence(ms, cb, op, 0, thisLoaded, _loadSingleCss, uuid);
  444. }else{
  445. _loadDisarray(ms, cb, op, thisLoaded, _loadSingleCss, uuid);
  446. }
  447. };
  448. var _removeCss = function(module, doc){
  449. var thisDoc = doc || document;
  450. var k = encodeURIComponent(module+(thisDoc.unid||""));
  451. var removeCss = _loadedCss[k];
  452. if (!removeCss) for (key in _loadedCss){
  453. if (_loadedCss[key].id==module){
  454. removeCss = _loadedCss[key];
  455. k = key;
  456. break;
  457. }
  458. }
  459. if (removeCss){
  460. delete _loadedCss[k];
  461. var styleNode = removeCss.doc.getElementById(removeCss.id);
  462. if (styleNode) styleNode.parentNode.removeChild(styleNode);
  463. removeCss = null;
  464. }
  465. };
  466. this.o2.loadCss = _loadCss;
  467. this.o2.removeCss = _removeCss;
  468. Element.prototype.loadCss = function(modules, options, callback){
  469. var op = (_typeOf(options)==="object") ? options : {};
  470. var cb = (_typeOf(options)==="function") ? options : callback;
  471. op.dom = this;
  472. _loadCss(modules, op, cb);
  473. };
  474. //load html
  475. _loadSingleHtml = function(module, callback, op){
  476. var url = module;
  477. var uid = _uuid();
  478. if (op.noCache) url = (url.indexOf("?")!==-1) ? url+"&v="+uid : url+"?v="+uid;
  479. var key = encodeURIComponent(url+op.doc.unid);
  480. if (!op.reload) if (_loadedHtml[key]){ if (callback)callback(_loadedHtml[key]); return; }
  481. var success = function(xhr){
  482. var htmlObj = {"module": module, "id": uid, "data": xhr.responseText, "doc": op.doc};
  483. _loadedHtml[key] = htmlObj;
  484. if (callback) callback(htmlObj);
  485. };
  486. var failure = function(){
  487. console.log("Error: load html module: "+module);
  488. if (callback) callback();
  489. };
  490. _xhr_get(url, success, failure);
  491. };
  492. var _injectHtml = function(op, data){
  493. if (op.bind) data = data.bindJson(op.bind);
  494. if (op.dom) _parseDom(op.dom, function(node){ node.insertAdjacentHTML(op.position, data) }, op.doc);
  495. };
  496. var _loadHtml = function(modules, options, callback){
  497. var ms = (_typeOf(modules)==="array") ? modules : [modules];
  498. var op = (_typeOf(options)==="object") ? _getHtmlOptions(options) : _getHtmlOptions(null);
  499. var cb = (_typeOf(options)==="function") ? options : callback;
  500. var thisLoaded = [];
  501. if (op.sequence){
  502. _loadSequence(ms, cb, op, 0, thisLoaded, _loadSingleHtml, null, function(html){ if (html) _injectHtml(op, html.data ); });
  503. }else{
  504. _loadDisarray(ms, cb, op, thisLoaded, _loadSingleHtml, null, function(html){ if (html) _injectHtml(op, html.data ); });
  505. }
  506. };
  507. this.o2.loadHtml = _loadHtml;
  508. Element.prototype.loadHtml = function(modules, options, callback){
  509. var op = (_typeOf(options)==="object") ? options : {};
  510. var cb = (_typeOf(options)==="function") ? options : callback;
  511. op.dom = this;
  512. _loadHtml(modules, op, cb);
  513. };
  514. //load all
  515. _loadAll = function(modules, options, callback){
  516. //var ms = (_typeOf(modules)==="array") ? modules : [modules];
  517. var op = (_typeOf(options)==="object") ? _getAllOptions(options) : _getAllOptions(null);
  518. var cb = (_typeOf(options)==="function") ? options : callback;
  519. var ms, htmls, styles, sctipts;
  520. var _htmlLoaded=(!modules.html), _cssLoaded=(!modules.css), _jsLoaded=(!modules.js);
  521. var _checkloaded = function(){
  522. if (_htmlLoaded && _cssLoaded && _jsLoaded) if (cb) cb(htmls, styles, sctipts);
  523. };
  524. if (modules.html){
  525. _loadHtml(modules.html, op, function(h){
  526. htmls = h;
  527. _htmlLoaded = true;
  528. _checkloaded();
  529. });
  530. }
  531. if (modules.css){
  532. _loadCss(modules.css, op, function(s){
  533. styles = s;
  534. _cssLoaded = true;
  535. _checkloaded();
  536. });
  537. }
  538. if (modules.js){
  539. _load(modules.js, op, function(s){
  540. sctipts = s;
  541. _jsLoaded = true;
  542. _checkloaded();
  543. });
  544. }
  545. };
  546. this.o2.loadAll = _loadAll;
  547. Element.prototype.loadAll = function(modules, options, callback){
  548. var op = (_typeOf(options)==="object") ? options : {};
  549. var cb = (_typeOf(options)==="function") ? options : callback;
  550. op.dom = this;
  551. _loadAll(modules, op, cb);
  552. };
  553. //json template
  554. _parseText = function(html, json){
  555. var _ht = html;
  556. var regexp = /(text\{).+?\}/g;
  557. var r = _ht.match(regexp);
  558. if(r) if (r.length){
  559. for (var i=0; i<r.length; i++){
  560. var text = r[i].substr(0,r[i].lastIndexOf("}"));
  561. text = text.substr(text.indexOf("{")+1,text.length);
  562. var value = _jsonText(json ,text);
  563. _ht = _ht.replace(/(text\{).+?\}/,value);
  564. }
  565. }
  566. return _ht;
  567. };
  568. _parseEach = function(html, json){
  569. var _ht = html;
  570. var regexp = /(\{each\([\s\S]+\)\})[\s\S]+?(\{endEach\})/g;
  571. var r = _ht.match(regexp);
  572. if(r){
  573. if (r.length){
  574. for (var i=0; i<r.length; i++){
  575. var eachItemsStr = r[i].substr(0,r[i].indexOf(")"));
  576. eachItemsStr = eachItemsStr.substr(eachItemsStr.indexOf("(")+1,eachItemsStr.length);
  577. var pars = eachItemsStr.split(/,[\s]*/g);
  578. eachItemsPar = pars[0];
  579. eachItemsCount = pars[1].toInt();
  580. var eachItems = _jsonText(json ,eachItemsPar);
  581. if (eachItems) if (eachItemsCount==0) eachItemsCount = eachItems.length;
  582. var eachContentStr = r[i].substr(0,r[i].lastIndexOf("{endEach}"));
  583. eachContentStr = eachContentStr.substr(eachContentStr.indexOf("}")+1,eachContentStr.length);
  584. var eachContent = [];
  585. if (eachItems){
  586. for (var n=0; n<Math.min(eachItems.length, eachItemsCount); n++){
  587. var item = eachItems[n];
  588. if (item){
  589. var tmpEachContentStr = eachContentStr;
  590. var textReg = /(eachText\{).+?\}/g;
  591. texts = tmpEachContentStr.match(textReg);
  592. if (texts){
  593. if (texts.length){
  594. for (var j=0; j<texts.length; j++){
  595. var text = texts[j].substr(0,texts[j].lastIndexOf("}"));
  596. text = text.substr(text.indexOf("{")+1,text.length);
  597. var value = _jsonText(item ,text);
  598. tmpEachContentStr = tmpEachContentStr.replace(/(eachText\{).+?\}/,value);
  599. }
  600. }
  601. }
  602. eachContent.push(tmpEachContentStr);
  603. }
  604. }
  605. }
  606. _ht = _ht.replace(/(\{each\([\s\S]+\)\})[\s\S]+?(\{endEach\})/,eachContent.join(""));
  607. }
  608. }
  609. }
  610. return _ht;
  611. };
  612. _jsonText = function(json, text){
  613. var $ = json;
  614. var f = eval("(x = function($){\n return "+text+";\n})");
  615. returnValue = f.apply(json, [$]);
  616. if (returnValue===undefined) returnValue="";
  617. returnValue = returnValue.toString();
  618. return returnValue || "";
  619. };
  620. _bindJson = function(str, json){
  621. return _parseEach(_parseText(str, json), json);
  622. };
  623. o2.bindJson = _bindJson;
  624. String.prototype.bindJson = function(json){
  625. return _parseEach(_parseText(this, json), json);
  626. };
  627. //dom ready
  628. var _dom = {
  629. ready: false,
  630. loaded: false,
  631. checks: [],
  632. shouldPoll: false,
  633. timer: null,
  634. testElement: document.createElement('div'),
  635. readys: [],
  636. domready: function(){
  637. clearTimeout(_dom.timer);
  638. if (_dom.ready) return;
  639. _dom.loaded = _dom.ready = true;
  640. _removeListener(document, 'DOMContentLoaded', _dom.checkReady);
  641. _removeListener(document, 'readystatechange', _dom.check);
  642. _dom.onReady();
  643. },
  644. check: function(){
  645. for (var i = _dom.checks.length; i--;) if (_dom.checks[i]() && window.MooTools && o2.core && o2.more){
  646. _dom.domready();
  647. return true;
  648. }
  649. return false;
  650. },
  651. poll: function(){
  652. clearTimeout(_dom.timer);
  653. if (!_dom.check()) _dom.timer = setTimeout(_dom.poll, 10);
  654. },
  655. /*<ltIE8>*/
  656. // doScroll technique by Diego Perini http://javascript.nwbox.com/IEContentLoaded/
  657. // testElement.doScroll() throws when the DOM is not ready, only in the top window
  658. doScrollWorks: function(){
  659. try {
  660. _dom.testElement.doScroll();
  661. return true;
  662. } catch (e){}
  663. return false;
  664. },
  665. /*</ltIE8>*/
  666. onReady: function(){
  667. for (var i=0; i<_dom.readys.length; i++){
  668. this.readys[i].apply(window);
  669. }
  670. },
  671. addReady: function(fn){
  672. if (_dom.loaded){
  673. if (fn) fn.apply(window);
  674. }else{
  675. if (fn) _dom.readys.push(fn);
  676. }
  677. return _dom;
  678. },
  679. checkReady: function(){
  680. _dom.checks.push(function(){return true});
  681. _dom.check();
  682. }
  683. };
  684. var _loadO2 = function(){
  685. this.o2.load("o2.core", _dom.check);
  686. this.o2.load("o2.more", _dom.check);
  687. };
  688. _addListener(document, 'DOMContentLoaded', _dom.checkReady);
  689. /*<ltIE8>*/
  690. // If doScroll works already, it can't be used to determine domready
  691. // e.g. in an iframe
  692. if (_dom.testElement.doScroll && !_dom.doScrollWorks()){
  693. _dom.checks.push(_dom.doScrollWorks);
  694. _dom.shouldPoll = true;
  695. }
  696. /*</ltIE8>*/
  697. if (document.readyState) _dom.checks.push(function(){
  698. var state = document.readyState;
  699. return (state == 'loaded' || state == 'complete');
  700. });
  701. if ('onreadystatechange' in document) _addListener(document, 'readystatechange', _dom.check);
  702. else _dom.shouldPoll = true;
  703. if (_dom.shouldPoll) _dom.poll();
  704. if (!window.MooTools){
  705. this.o2.load("mootools", function(){ _loadO2(); _dom.check(); });
  706. }else{
  707. _loadO2();
  708. }
  709. this.o2.addReady = function(fn){ _dom.addReady.call(_dom, fn); };
  710. })();