o2.js 37 KB

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