|
|
@@ -167,6 +167,96 @@
|
|
|
};
|
|
|
this.o2.runCallback = _runCallback;
|
|
|
|
|
|
+ var _getAllOptions = function(options){
|
|
|
+ return {
|
|
|
+ "noCache": !!(options && options.nocache),
|
|
|
+ "reload": !!(options && options.reload),
|
|
|
+ "sequence": !!(options && options.sequence),
|
|
|
+ "doc": (options && options.doc) || document,
|
|
|
+ "dom": (options && options.dom) || document.body,
|
|
|
+ "position": "beforeend" //'beforebegin' 'afterbegin' 'beforeend' 'afterend'
|
|
|
+ }
|
|
|
+ };
|
|
|
+ var _getCssOptions = function(options){
|
|
|
+ return {
|
|
|
+ "noCache": !!(options && options.nocache),
|
|
|
+ "reload": !!(options && options.reload),
|
|
|
+ "sequence": !!(options && options.sequence),
|
|
|
+ "doc": (options && options.doc) || document,
|
|
|
+ "dom": (options && options.dom) || null
|
|
|
+ }
|
|
|
+ };
|
|
|
+ var _getJsOptions = function(options){
|
|
|
+ return {
|
|
|
+ "noCache": !!(options && options.nocache),
|
|
|
+ "reload": !!(options && options.reload),
|
|
|
+ "sequence": !!(options && options.sequence),
|
|
|
+ "doc": (options && options.doc) || document
|
|
|
+ }
|
|
|
+ };
|
|
|
+ var _getHtmlOptions = function(options){
|
|
|
+ return {
|
|
|
+ "noCache": !!(options && options.nocache),
|
|
|
+ "reload": !!(options && options.reload),
|
|
|
+ "sequence": !!(options && options.sequence),
|
|
|
+ "doc": (options && options.doc) || document,
|
|
|
+ "dom": (options && options.dom) || null,
|
|
|
+ "position": "beforeend" //'beforebegin' 'afterbegin' 'beforeend' 'afterend'
|
|
|
+ }
|
|
|
+ };
|
|
|
+ var _xhr_get = function(url, success, failure){
|
|
|
+ var xhr = new _request();
|
|
|
+ xhr.open("GET", url, true);
|
|
|
+
|
|
|
+ var _checkCssLoaded= function(_, err){
|
|
|
+ if (!(xhr.readyState == 4 || err)) return;
|
|
|
+
|
|
|
+ _removeListener(xhr, 'readystatechange', _checkCssLoaded);
|
|
|
+ _removeListener(xhr, 'load', _checkCssLoaded);
|
|
|
+ _removeListener(xhr, 'error', _checkCssErrorLoaded);
|
|
|
+
|
|
|
+ if (err) {failure(xhr); return}
|
|
|
+ var status = xhr.status;
|
|
|
+ status = (status == 1223) ? 204 : status;
|
|
|
+ if ((status >= 200 && status < 300))
|
|
|
+ success(xhr);
|
|
|
+ else if ((status >= 300 && status < 400))
|
|
|
+ failure(xhr);
|
|
|
+ else
|
|
|
+ failure(xhr);
|
|
|
+ };
|
|
|
+ var _checkCssErrorLoaded= function(err){ _checkCssLoaded(err) };
|
|
|
+
|
|
|
+ if ("load" in xhr) _addListener(xhr, "load", _checkCssLoaded);
|
|
|
+ if ("error" in xhr) _addListener(xhr, "load", _checkCssErrorLoaded);
|
|
|
+ _addListener(xhr, "readystatechange", _checkCssLoaded);
|
|
|
+ xhr.send();
|
|
|
+ };
|
|
|
+
|
|
|
+ var _loadSequence = function(ms, cb, op, n, thisLoaded, loadSingle, uuid, fun){
|
|
|
+ loadSingle(ms[n], function(module){
|
|
|
+ if (module) thisLoaded.push(module);
|
|
|
+ n++;
|
|
|
+ if (fun) fun(module);
|
|
|
+ if (n===ms.length){
|
|
|
+ if (cb) cb(thisLoaded);
|
|
|
+ }else{
|
|
|
+ _loadSequence(ms, cb, op, n, thisLoaded, loadSingle, uuid, fun);
|
|
|
+ }
|
|
|
+ }, op, uuid);
|
|
|
+ };
|
|
|
+ var _loadDisarray = function(ms, cb, op, thisLoaded, loadSingle, uuid, fun){
|
|
|
+ var count=0;
|
|
|
+ for (var i=0; i<ms.length; i++){
|
|
|
+ loadSingle(ms[i], function(module){
|
|
|
+ if (module) thisLoaded.push(module);
|
|
|
+ count++;
|
|
|
+ if (fun) fun(module);
|
|
|
+ if (count===ms.length) if (cb) cb(thisLoaded);
|
|
|
+ }, op, uuid);
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
//use framework url
|
|
|
var _frameworks = {
|
|
|
"o2.core": ["/o2_core/o2/o2.core.js"],
|
|
|
@@ -184,97 +274,98 @@
|
|
|
};
|
|
|
var _loaded = {};
|
|
|
var _loadedCss = {};
|
|
|
+ var _loadedHtml = {};
|
|
|
|
|
|
- var _loadSingle = function(url, callback, reload){
|
|
|
- var addr_uri = _frameworks[url] || url;
|
|
|
- if (!_debug) if (addr_uri.indexOf("o2_lib")===-1) addr_uri = addr_uri.replace(/\.js/, ".min.js");
|
|
|
- addr_uri = (addr_uri.indexOf("?")!==-1) ? addr_uri+"&v="+this.o2.version.v : addr_uri+"?v="+this.o2.version.v;
|
|
|
-
|
|
|
+ var _loadSingle = function(module, callback, op){
|
|
|
+ var url = module;
|
|
|
+ var uuid = _uuid();
|
|
|
+ if (op.noCache) url = (url.indexOf("?")!==-1) ? url+"&v="+uuid : addr_uri+"?v="+uuid;
|
|
|
var key = encodeURIComponent(url);
|
|
|
+ if (!op.reload) if (_loaded[key]){ if (callback)callback(); return; }
|
|
|
|
|
|
- if (!reload) if (_loaded[key]){ if (callback)callback(); return; }
|
|
|
-
|
|
|
- var head = (document.head || document.getElementsByTagName("head")[0] || document.documentElement);
|
|
|
- var s = document.createElement('script');
|
|
|
- s.src = addr_uri;
|
|
|
+ var head = (op.doc.head || op.doc.getElementsByTagName("head")[0] || op.doc.documentElement);
|
|
|
+ var s = op.doc.createElement('script');
|
|
|
head.appendChild(s);
|
|
|
+ s.id = uuid;
|
|
|
+ s.src = url;
|
|
|
|
|
|
- var _checkScriptLoaded = function(_, isAbort){
|
|
|
+ var _checkScriptLoaded = function(_, isAbort, err){
|
|
|
if (isAbort || !s.readyState || s.readyState === "loaded" || s.readyState === "complete") {
|
|
|
- _loaded[key] = true;
|
|
|
- _removeListener(s, 'DOMContentLoaded', _checkScriptLoaded);
|
|
|
+ var scriptObj = {"module": module, "id": uuid, "script": s, "doc": op.doc};
|
|
|
+ if (!err) _loaded[key] = scriptObj;
|
|
|
_removeListener(s, 'readystatechange', _checkScriptLoaded);
|
|
|
- if (!isAbort) if (callback)callback();
|
|
|
+ _removeListener(s, 'load', _checkScriptLoaded);
|
|
|
+ _removeListener(s, 'error', _checkScriptErrorLoaded);
|
|
|
+ if (!isAbort || err){
|
|
|
+ if (err){
|
|
|
+ if (s) head.removeChild(s);
|
|
|
+ if (callback)callback();
|
|
|
+ }else{
|
|
|
+ head.removeChild(s);
|
|
|
+ if (callback)callback(scriptObj);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
};
|
|
|
+ var _checkScriptErrorLoaded = function(e, err){
|
|
|
+ console.log("Error: load javascript module: "+module);
|
|
|
+ _checkScriptLoaded(e, true, "error");
|
|
|
+ };
|
|
|
|
|
|
if ('onreadystatechange' in s) _addListener(s, 'readystatechange', _checkScriptLoaded);
|
|
|
_addListener(s, 'load', _checkScriptLoaded);
|
|
|
+ _addListener(s, 'error', _checkScriptErrorLoaded);
|
|
|
};
|
|
|
- var _load = function(urls, callback, reload, doc){
|
|
|
- var urltype = _typeOf(urls);
|
|
|
- var modules;
|
|
|
- if (urltype==="array"){
|
|
|
- modules = [];
|
|
|
- for (var i=0; i<urls.length; i++){
|
|
|
- var url = urls[i];
|
|
|
- var module = _frameworks[url] || url;
|
|
|
- if (_typeOf(module)==="array"){
|
|
|
- modules.concat(module)
|
|
|
- }else{
|
|
|
- modules.push(url)
|
|
|
- }
|
|
|
- }
|
|
|
- }else{
|
|
|
- modules = _frameworks[urls] || urls;
|
|
|
- }
|
|
|
|
|
|
- var type = _typeOf(modules);
|
|
|
- if (type==="array"){
|
|
|
- var thisLoaded = [];
|
|
|
- for (var i=0; i<modules.length; i++){
|
|
|
- _loadSingle(modules[i], function(){
|
|
|
- thisLoaded.push(modules[i]);
|
|
|
- if (thisLoaded.length===modules.length){
|
|
|
- if (callback) callback();
|
|
|
- }
|
|
|
- }, reload, doc);
|
|
|
+ var _load = function(urls, options, callback){
|
|
|
+ var ms = (_typeOf(urls)==="array") ? urls : [urls];
|
|
|
+ var op = (_typeOf(options)==="object") ? _getJsOptions(options) : _getJsOptions(null);
|
|
|
+ var cb = (_typeOf(options)==="function") ? options : callback;
|
|
|
+
|
|
|
+ var modules = [];
|
|
|
+ for (var i=0; i<ms.length; i++){
|
|
|
+ var url = ms[i];
|
|
|
+ var module = _frameworks[url] || url;
|
|
|
+ if (_typeOf(module)==="array"){
|
|
|
+ modules = modules.concat(module)
|
|
|
+ }else{
|
|
|
+ modules.push(module)
|
|
|
}
|
|
|
}
|
|
|
- if (type==="string"){
|
|
|
- _loadSingle(modules, callback, reload);
|
|
|
+ var thisLoaded = [];
|
|
|
+ if (op.sequence){
|
|
|
+ _loadSequence(modules, cb, op, 0, thisLoaded, _loadSingle);
|
|
|
+ }else{
|
|
|
+ _loadDisarray(modules, cb, op, thisLoaded, _loadSingle);
|
|
|
}
|
|
|
};
|
|
|
this.o2.load = _load;
|
|
|
|
|
|
- var _loadSingleCss = function(url, callback, uuid, reload, sourceDoc){
|
|
|
- var key = encodeURIComponent(url);
|
|
|
- if (!reload) if (_loadedCss[key]){ if (callback)callback(_loadedCss[key]); return; }
|
|
|
-
|
|
|
- var cssurl = _frameworks[url] || url;
|
|
|
+ var _loadSingleCss = function(module, callback, op, uuid){
|
|
|
+ var url = module;
|
|
|
+ var uid = _uuid();
|
|
|
+ if (op.noCache) url = (url.indexOf("?")!==-1) ? url+"&v="+uid : url+"?v="+uid;
|
|
|
|
|
|
- var xhr = new _request();
|
|
|
- xhr.open("GET", cssurl, true);
|
|
|
+ var key = encodeURIComponent(url);
|
|
|
+ if (!op.reload) if (_loadedCss[key]){ if (callback)callback(_loadedCss[key]); return; }
|
|
|
|
|
|
var success = function(xhr){
|
|
|
var cssText = xhr.responseText;
|
|
|
try{
|
|
|
if (cssText){
|
|
|
- if (uuid){
|
|
|
+ if (op.dom){
|
|
|
var rex = new RegExp("(.+)(?=\\{)", "g");
|
|
|
var match;
|
|
|
while ((match = rex.exec(cssText)) !== null) {
|
|
|
var prefix = "." + uuid + " ";
|
|
|
var rule = prefix + match[0];
|
|
|
- cssText = cssText.substring(0, match.index) + rule + cssText.substring(rex.lastIndex, cssText.length)
|
|
|
+ cssText = cssText.substring(0, match.index) + rule + cssText.substring(rex.lastIndex, cssText.length);
|
|
|
rex.lastIndex = rex.lastIndex + prefix.length;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- var doc = sourceDoc || document;
|
|
|
- var style = doc.createElement("style");
|
|
|
+ var style = op.doc.createElement("style");
|
|
|
style.setAttribute("type", "text/css");
|
|
|
- var head = (document.head || document.getElementsByTagName("head")[0] || document.documentElement);
|
|
|
+ var head = (op.doc.head || op.doc.getElementsByTagName("head")[0] || op.doc.documentElement);
|
|
|
head.appendChild(style);
|
|
|
if(style.styleSheet){
|
|
|
var setFunc = function(){
|
|
|
@@ -286,41 +377,24 @@
|
|
|
setFunc();
|
|
|
}
|
|
|
}else{
|
|
|
- var cssTextNode = doc.createTextNode(cssText);
|
|
|
+ var cssTextNode = op.doc.createTextNode(cssText);
|
|
|
style.appendChild(cssTextNode);
|
|
|
}
|
|
|
- style.sheet.cssRules[0].cssText = "#layout_xxx {\n" +
|
|
|
- " width: 500px;\n" +
|
|
|
- " height: 300px;\n" +
|
|
|
- " background: #FFCCBA;\n" +
|
|
|
- "}"
|
|
|
}
|
|
|
- _loadedCss[key] = style;
|
|
|
- if (callback) callback(style);
|
|
|
+ style.id = uid;
|
|
|
+ var styleObj = {"module": module, "id": uid, "style": style, "doc": op.doc};
|
|
|
+ _loadedCss[key] = styleObj;
|
|
|
+ if (callback) callback(styleObj);
|
|
|
}catch (e){
|
|
|
if (callback) callback();
|
|
|
return;
|
|
|
}
|
|
|
};
|
|
|
var failure = function(xhr){
|
|
|
+ console.log("Error: load css module: "+module);
|
|
|
if (callback) callback();
|
|
|
};
|
|
|
- var onreadystatechange= function(){
|
|
|
- if (xhr.readyState != 4) return;
|
|
|
-
|
|
|
- var status = xhr.status;
|
|
|
- status = (status == 1223) ? 204 : status;
|
|
|
- //var response = {text: xhr.responseText || '', xml: xhr.responseXML};
|
|
|
- if ((status >= 200 && status < 300))
|
|
|
- success(xhr);
|
|
|
- else if ((status >= 300 && status < 400))
|
|
|
- failure(xhr);
|
|
|
- else
|
|
|
- failure(xhr);
|
|
|
- };
|
|
|
- _addListener(xhr, "readystatechange", onreadystatechange);
|
|
|
- //xhr.onreadystatechange = onreadystatechange;
|
|
|
- xhr.send();
|
|
|
+ _xhr_get(url, success, failure);
|
|
|
};
|
|
|
|
|
|
var _parseDomString = function(dom, fn, sourceDoc){
|
|
|
@@ -337,32 +411,88 @@
|
|
|
if (domType==="element") _parseDomElement(dom, fn);
|
|
|
if (domType==="array") for (var i=0; i<dom.length; i++) _parseDom(dom[i], fn, sourceDoc);
|
|
|
};
|
|
|
- var _loadCss = function(urls, dom, callback, reload, sourceDoc){
|
|
|
- var uuid = "";
|
|
|
- if (dom){
|
|
|
- uuid = "css"+_uuid();
|
|
|
- _parseDom(dom, function(node){ node.className += ((node.className) ? " "+uuid : uuid)}, sourceDoc);
|
|
|
+ var _loadCss = function(modules, options, callback){
|
|
|
+ var ms = (_typeOf(modules)==="array") ? modules : [modules];
|
|
|
+ var op = (_typeOf(options)==="object") ? _getCssOptions(options) : _getCssOptions(null);
|
|
|
+ var cb = (_typeOf(options)==="function") ? options : callback;
|
|
|
+
|
|
|
+ var uuid = "css"+_uuid();
|
|
|
+ if (op.dom) _parseDom(op.dom, function(node){ node.className += ((node.className) ? " "+uuid : uuid)}, op.doc);
|
|
|
+
|
|
|
+ var thisLoaded = [];
|
|
|
+ if (op.sequence){
|
|
|
+ _loadSequence(ms, cb, op, 0, thisLoaded, _loadSingleCss, uuid);
|
|
|
+ }else{
|
|
|
+ _loadDisarray(ms, cb, op, thisLoaded, _loadSingleCss, uuid);
|
|
|
}
|
|
|
- modules = _frameworks[urls] || urls;
|
|
|
- var type = _typeOf(modules);
|
|
|
- if (type==="array"){
|
|
|
- var thisLoaded = [];
|
|
|
- var styleList = [];
|
|
|
- for (var i=0; i<modules.length; i++){
|
|
|
- _loadSingleCss(modules[i], function(style){
|
|
|
- thisLoaded.push(modules[i]);
|
|
|
- if (style) styleList.push(styleList);
|
|
|
- if (thisLoaded.length===modules.length){
|
|
|
- if (callback) callback(styleList);
|
|
|
- }
|
|
|
- }, uuid, reload, sourceDoc);
|
|
|
+ };
|
|
|
+ var _removeCss = function(module){
|
|
|
+ var k = encodeURIComponent(module);
|
|
|
+ var removeCss = _loadedCss[k];
|
|
|
+ if (!removeCss) for (key in _loadedCss){
|
|
|
+ if (_loadedCss[key].id==module){
|
|
|
+ removeCss = _loadedCss[key];
|
|
|
+ k = key;
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
- if (type==="string"){
|
|
|
- _loadSingleCss(modules, callback, uuid, reload, sourceDoc);
|
|
|
+ if (removeCss){
|
|
|
+ delete _loadedCss[k];
|
|
|
+ var styleNode = removeCss.doc.getElementById(removeCss.id);
|
|
|
+ if (styleNode) styleNode.parentNode.removeChild(styleNode);
|
|
|
+ removeCss = null;
|
|
|
}
|
|
|
};
|
|
|
this.o2.loadCss = _loadCss;
|
|
|
+ this.o2.removeCss = _removeCss;
|
|
|
+ Element.prototype.loadCss = function(modules, options, callback){
|
|
|
+ var op = (_typeOf(options)==="object") ? options : {};
|
|
|
+ var cb = (_typeOf(options)==="function") ? options : callback;
|
|
|
+ op.dom = this;
|
|
|
+ _loadCss(modules, op, cb);
|
|
|
+ };
|
|
|
+
|
|
|
+ _loadSingleHtml = function(module, callback, op){
|
|
|
+ var url = module;
|
|
|
+ var uid = _uuid();
|
|
|
+ if (op.noCache) url = (url.indexOf("?")!==-1) ? url+"&v="+uid : url+"?v="+uid;
|
|
|
+ var key = encodeURIComponent(url);
|
|
|
+ if (!op.reload) if (_loadedHtml[key]){ if (callback)callback(_loadedHtml[key]); return; }
|
|
|
+
|
|
|
+ var success = function(xhr){
|
|
|
+ var htmlObj = {"module": module, "id": uid, "data": xhr.responseText, "doc": op.doc};
|
|
|
+ _loadedHtml[key] = htmlObj;
|
|
|
+ if (callback) callback(htmlObj);
|
|
|
+ };
|
|
|
+ var failure = function(){
|
|
|
+ console.log("Error: load html module: "+module);
|
|
|
+ if (callback) callback();
|
|
|
+ };
|
|
|
+ _xhr_get(url, success, failure);
|
|
|
+ };
|
|
|
+
|
|
|
+ var _injectHtml = function(op, data){
|
|
|
+ if (op.dom) _parseDom(op.dom, function(node){ node.insertAdjacentHTML(op.position, data) }, op.doc);
|
|
|
+ };
|
|
|
+ var _loadHtml = function(modules, options, callback){
|
|
|
+ var ms = (_typeOf(modules)==="array") ? modules : [modules];
|
|
|
+ var op = (_typeOf(options)==="object") ? _getHtmlOptions(options) : _getHtmlOptions(null);
|
|
|
+ var cb = (_typeOf(options)==="function") ? options : callback;
|
|
|
+
|
|
|
+ var thisLoaded = [];
|
|
|
+ if (op.sequence){
|
|
|
+ _loadSequence(ms, cb, op, 0, thisLoaded, _loadSingleHtml, null, function(html){ if (html) _injectHtml(op, html.data ); });
|
|
|
+ }else{
|
|
|
+ _loadDisarray(ms, cb, op, thisLoaded, _loadSingleHtml, null, function(html){ if (html) _injectHtml(op, html.data ); });
|
|
|
+ }
|
|
|
+ };
|
|
|
+ this.o2.loadHtml = _loadHtml;
|
|
|
+ Element.prototype.loadHtml = function(modules, options, callback){
|
|
|
+ var op = (_typeOf(options)==="object") ? options : {};
|
|
|
+ var cb = (_typeOf(options)==="function") ? options : callback;
|
|
|
+ op.dom = this;
|
|
|
+ _loadHtml(modules, op, cb);
|
|
|
+ };
|
|
|
|
|
|
var _dom = {
|
|
|
ready: false,
|