Component.js
12.7 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
define([
'juicer',
'mUtil',
'i18n',
'zepto'
], function (juicer, mUtil) {
'use strict';
var isString = mUtil.isString,
isElement = mUtil.isElement,
isObject = mUtil.isObject,
isArray = mUtil.isArray;
var comStatus = {
UNLOAD: 1,
LOAD: 2,
DATALOAD: 3
};
var F = function (opts) {
opts = opts || {};
var evt = { // 事件handlers
handlers: {},
once: {}
};
var always = function () { return true; };
// 自定义绑定事件 非dom事件
this.$on = function (evtName, handler) {
evt.handlers[evtName] = handler;
};
// 只执行一次的事件
this.$once = function (evtName, handler) {
evt.once[evtName] = handler;
};
// 取消绑定
this.$off = function (evtName) {
if(evt.once[evtName]) {
delete evt.once[evtName];
} else if(evt.handlers[evtName]) {
delete evt.handlers[evtName];
} else if(!evtName) {
evt.handlers = {};
evt.once = {};
}
};
// 触发自定义绑定事件
this.$emit = function (evtName) {
var args = [].slice.call(arguments, 1);
if(evt.once[evtName]) {
evt.once[evtName].apply(this, args);
delete evt.once[evtName];
} else if(evt.handlers[evtName]) {
evt.handlers[evtName].apply(this, args);
}
};
//初始化生命周期方法
this.beforeMount = always;
this.mounted = always;
// dataModel 转化为 viewModel
this.transferToVM = always;
// 组件模板和css
this.tpl = "";
this.css = "";
this.components = {};
this.status = comStatus.UNLOAD;
this.id = F.util.getId(opts.el);
this.pageid = opts.pageid || mUtil.getCurrentPageId();
this.$container = $(opts.container || "body");
this.updateStatus = function (status) {
this.status = status;
}
// 组件render
this.render = function () {
var that = this,
util = F.util;
// 组件类型
opts.type = opts.type || that.type;
// url: 后端请求获取dataModel的方式
// option: 前端直接调用组件所传option,即viewModel
return $.when(util.initData(opts.url || opts.option || {}), util.getTpl(opts.type, this.tpl), util.getCss(this.css))
.then(function (result) {
var $el = that.$container.find("[id='" + that.id + "']");
var pageid = that.pageid;
if(opts.url || opts.needTransfer) {
that.transferToVM(result);
} else {
that.viewModel = $.extend(true, that.viewModel, result);
}
that.viewModel = util.formatVM(that.viewModel, pageid, function (key) {
var keysOfSkipedVarParse = that.keysOfSkipedVarParse || [];
return !!~keysOfSkipedVarParse.indexOf(key);
});
that.id = result.id || that.id;
if(pageid) {
F.instances[pageid] = F.instances[pageid] || {};
F.instances[pageid][that.id] = that;
} else {
F.instances[that.id] = that;
}
that.components = util.map(that.components, function (com) {
com.props = com.props || {};
com.uid = com.props.uid = "_" + mUtil.UUID();
com.content = juicer(com.tmpl, com.props);
return com;
});
var thrunk = that.beforeMount();
//beforeMount()可能返回值:undefined,true,Deferred
thrunk = typeof(thrunk) == 'object' ? thrunk : $.Deferred().resolve();
return thrunk.then(function(){
// 扩展viewModel
$.extend(true, that.viewModel, { id: that.id, compType: opts.type });
that.$el = util.render($el, $.extend(true, {}, that.viewModel, that.components));
that.$comp = that.$el.children(".wev-comp-" + that.type);
that.components = util.map(that.components, function (com) {
com.$el = that.$el.find("#" + com.uid);
return com;
});
Object.keys(that.components).forEach(function (k) {
that.components[k].mounted();
});
return that.mounted();
});
});
};
};
F.tpls = {}; // 缓存组件的模板
F.instances = {}; //缓存插件
F.util = {
getId: function (el) {
if(isString(el)) return el;
if(isElement(el)) return el.id;
return el;
},
getTpl: function (type, modName) {
var deferred = $.Deferred();
if(type in F.tpls) return deferred.resolve();
if(!modName) return (F.tpls[type] = juicer(""));
require([modName], function (template) {
F.tpls[type] = juicer(mUtil.replaceI18n(template));
deferred.resolve();
});
return deferred;
},
getCss: function (modName) {
var deferred = $.Deferred();
if(!modName) return deferred.resolve();
if(isString(modName)) {
modName = ['css!' + modName];
} else if(isArray(modName)) {
modName = modName.map(function (name) {
return 'css!' + name;
});
}
require(modName, function () {
deferred.resolve();
});
return deferred;
},
render: function (el, vm) {
var tpl = F.tpls[vm.compType],
html = tpl.render(vm);
return $(el).html(html);
},
initData: function (data, fn) {
var deferred = $.Deferred();
if(isObject(data)) return deferred.resolve(data);
mUtil.getJSON(data, function (result) {
deferred.resolve(result.data);
}, function (e) {
var mec_id = data.split("mec_id=")[1];
mUtil.console.error("无法获取到组件id为 " + mec_id + " 的相关数据");
});
return deferred;
},
map: function (o, map) {
var n = {};
Object.keys(o).map(function (k) {
n[k] = map(o[k]);
});
return n;
},
formatVM: function (vm, pageid, skip) {
var t = this;
var datasets = this.getDataSet(pageid);
var languageId = mUtil.getUserLanguage();
var needParseVar = mUtil.containsVarParser(JSON.stringify(vm));
vm = mUtil.replaceJSON(vm, function (str) {
str = mUtil.replaceVariables(str, pageid);//替换页面变量和系统变量
str = t.replaceMutilLanguage(str, languageId);
if(datasets.length) {
datasets.forEach(function (dataset) {
str = dataset.replace(str);
});
}
needParseVar && (str = mUtil.replaceVarParser(str));
return str;
}, skip);
vm.needParseVar = needParseVar;
return vm;
},
replaceMutilLanguage: function (str, languageId) { //替换字符串里的包含的多语言值,如果不包含,返回原字符串
var t = this;
(str.match(/~`~`(.|\n)+?`~`~/g) || []).map(function (val) {
str = str.replace(val, t.replaceStandMutilLanguage(val, languageId));
});
return str;
},
replaceStandMutilLanguage: function (mval, languageId) { //替换标准多语言值
var symbol = "`~`";
var values = mval.replace(/^~`~`|`~`~$/g, "").split(symbol);
if(values.length == 1 && values[0] === mval) return mval;
var firstMutilValue = '';
for(var i = 0; i < values.length; i++) {
var val = values[i];
var langid = val.replace(/([\d]{1,2}) [\s\S]*/, "$1");
val = val.replace(/[\d]{1,2} /, "").trim();
if(val && !firstMutilValue) {
firstMutilValue = val;
}
if(val && languageId == langid) {
mval = val;
}
}
return mval.indexOf('~') >= 0 ? firstMutilValue : mval;
},
getDataSet: function (pageid) {
var coms = F.instances[pageid];
if(!coms) return [];
return Object.keys(coms).filter(function (comId) {
var com = coms[comId];
return com.type === "DataSet";
}).map(function (comId) {
return coms[comId];
});
}
};
return {
super: function (component, opts) {
F.call(component, opts);
},
init: function (Component) {
// 寄生组合继承
function Super() { }
Super.prototype = F.prototype; // 只保留F.prototype的公有方法和属性
Component.prototype = new Super();
return Component;
},
getInstance: function (id, pageid) {
pageid = pageid || mUtil.getCurrentPageId();
if(!pageid) return F.instances[id];
return F.instances[pageid][id];
},
getPageComs: function (pageid) {
if (!pageid) return [];
var comsMap = F.instances[pageid] || {};
return Object.keys(comsMap).map(function (comId) {
return comsMap[comId];
});
},
load: function (components, onload, onComLoad) {
var index = -1;
var order = {};
var len = components.length;
components.forEach(function (com) {
if(!com.type) return;
var priority = order[com.priority] || [];
com.index = priority.push(com) - 1;
com.ready = $.Deferred();
com.render = $.Deferred();
order[com.priority] = priority;
require([com.type], function (Component) {
var component = new Component(com);
component.$once("loaded", function (index) {
// 组件加载完成后 触发的回调
component.updateStatus(comStatus.LOAD);
if(mUtil.isFunction(onComLoad)) {
setTimeout(function () {
onComLoad(component, index);
}, 0);
}
if(len - 1 !== index) return;
// 所有组件加载完成 触发后的回调
mUtil.isFunction(onload) && setTimeout(onload, 0);
});
com.ready.then(function () {
return component.render().then(function () {
component.$emit("loaded", ++index);
component.$el.data("loaded", true);
com.render.resolve();
});
});
});
});
Object.keys(order).sort(function (a, b) { return a - b }).reduce(function (prev, now) {
prev = prev || $.Deferred().resolve();
return prev.then(function () {
var deferreds = (order[now] || []).map(function (c) {
c.ready.resolve();
return c.render;
});
return $.when.apply($.when, deferreds);
});
}, "");
},
preload: function (components) {
components.forEach(function (com) {
require([com.type]);
});
},
replaceMutilLanguage: function (content, languageid) {
return F.util.replaceMutilLanguage(content, languageid);
},
render: function (el, vm){
return F.util.render(el, vm);
},
status: comStatus
};
});