o2.js 33 KB

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