o2.js 39 KB

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