ResourceLoader.js 2.17 KB
var ResourceLoader = {
	'loadedType' : []
};

ResourceLoader.loadResource = function(mec_type, callbackFn){
	var that = this;
	var NOT_AMD_LOAD = ['echarts.common.min.js', 'snap.svg.0.3.0.min_wev8.js', 'idangerous.swiper-2.7.min_wev8.js'];

	if(that.isLoaded(mec_type)){
		if(typeof(callbackFn) == "function"){
			callbackFn.call(callbackFn);
		}
	}else{
		require(["mec/plugin"], function (instances) {
			instances.getComponent(mec_type, function (component) {
				component.loadStyle();
			});
		});
		
		require(["utils"], function (_u) {
			_u.api("designer", {
				action: "getPluginDesignResource",
				data: { mec_type: mec_type }
			}, function (res) {
				var resources = res.data;

				for (var i = 0; i < resources.length; i++) {
					var r = resources[i];
					var id = r.path.replace(/\/.*\/([^\/]+)$/g, '$1');
					var type = r["type"];
					var content = r["content"];
					content += "\n/*# sourceURL=" + id + " */";

					if (type == "js") {
						that.runJs(content, NOT_AMD_LOAD.indexOf(id) != -1);
					} else if (type == "css") {
						var isrun = ~(r.path || "").indexOf("/mobilemode/css/mec/run"); // 判断是否为mec run中的css 是则添加scope

						that.runCss(content, id, isrun);
					}
				}
				that.setLoaded(mec_type);

				if (typeof (callbackFn) == "function") {
					callbackFn.call(callbackFn);
				}
			});
		});
	}
};

ResourceLoader.isLoaded = function(mec_type){
	
	for(var i = 0; i < this.loadedType.length; i++){
		if(this.loadedType[i] == mec_type){
			return true;
		}
	}
	
	return false;
};

ResourceLoader.setLoaded = function(mec_type){
	this.loadedType.push(mec_type);
};

ResourceLoader.runCss = function (_css, id, isrun) {
	var _u = require("utils");
	if(isrun) {
		return _u.scopeScreenCss(_u.replaceCssVarInIE(_css), id);
	}
	
	return _u.importCssText(_u.replaceCssVarInIE(_css), id);
};

ResourceLoader.runJs = function (_js, notAMDLoad) {
	if(notAMDLoad) { // 与require有冲突时的加载方式
		_js = "\
var r = require;\
var d = define;\
window.require = undefined;\
window.define = undefined;"
+ _js
+ "\
window.require = r;\
window.define = d;";
	}

    if (window.execScript) {
        window.execScript(_js)
    } else {
        window.eval(_js)
    }
};