ResourceLoader.js
2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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)
}
};