-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvue-lite.js
374 lines (359 loc) · 12.3 KB
/
vue-lite.js
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
/**
* @Vue lite
* @author github.com/lucky51
* @date 2018-09-09
*/
//evevt subscriber
let Subscriber = function (callback, execlines){
this.func = callback;
this.lines = execlines;
}
Subscriber.prototype={
constructor:Subscriber,
line :function () {
var gen; var args = arguments;
if (arguments.length === 0) return;
else {
gen = function* () {
for (let index = 0; index < args.length; index++) {
const element = args[index];
if (typeof element === 'function') {
if (index === args.length - 1) return yield element;
else {
var val = yield element;
}
};
}
}
}
return function () {
var initdata = null;
if (arguments.length === 1) {
initdata = arguments[0];
}
return async function (callback) {
var gfun = gen();
var result;
try {
while (!((result = gfun.next()).done)) {
var nx = result.value;
if (typeof nx === 'function') {
var backrs = nx(initdata);
if (backrs['then']) {
initdata = await backrs;
} else {
initdata = backrs;
}
}
}
callback(initdata);
} catch (error) {
callback(error);
}
}
}
},
update(val){
if(this.lines){
this.line(...this.lines)(val)((result)=>{
this.func(result);
});
}else{
this.func(val);
}
}
}
//event publisher
let Publisher = function(){
this.subs = [];
}
Publisher.prototype={
constructor:Publisher,
notify(val){
this.subs.forEach((item,idx,arr)=>{
item.update(val);
});
},
addSub(sub){
this.subs.push(sub);
}
}
/**
* Vuelite constructor
* @param {*} opts
*/
let VueLite = function(opts){
this.taget = null;
this._data = opts["data"];
this._directives ={
"v-model":(el,data,exp)=>{
el.value = this.execExp2(exp, data, function () {
data.taget = new Subscriber(function (newVal) {
el.value = newVal;
},
[
function (newVal2) {
return data.execExp2(exp, data);
}
]);
},
function () {
data.taget = null;
});
el.addEventListener("keyup",(event)=>{
this.setExpData(event.target.value, data, exp);
});
// add watcher
return false;
},
"v-repeat": (el, data, exp) => {
//resolve exp old /^(var\s+)?\s*(\(\s*[_a-zA-Z]+[_a-zA-Z0-9]*\s*\,\s*[_a-zA-Z]+[_a-zA-Z0-9]*\s*\))*?\s+in\s+[\S\s]+/
//xx in arry cloneNode
if (/^(var)?\s*[_$a-zA-Z]+[_$a-zA-Z0-9]*\s+in\s+[\S\s]+/.test(exp) ||
/^(var\s+)?\s*\((\s*[_$a-zA-Z]+[_$a-zA-Z0-9]*\s*\,\s*)*\s*[_$a-zA-Z]+[_$a-zA-Z0-9]*\s*\)\s+in\s+[\S\s]+/g.test(exp)) {
var param1, param2,exp;
if (exp.indexOf('(') > -1) {
var splt = exp.split('in');
var lr = splt[0].replace(/[\s\(\)]/g, '').replace(/var/,'');
if(lr.indexOf(',')>-1){
var lrarr =lr.split(',');
param1 = lrarr[0];
param2 = lrarr[1];
}else{
param1 =lr
}
exp = splt[1].replace(/[\s]/g,'');
}else{
var result = this.split(exp, ' ');
param1 = result[0], exp = result[result.length - 1];
if (result[0] === ('var')) {
param1 = result[1];
}
}
var resolveData = this.execExp2(exp, this),counter=0;
if (typeof resolveData !== "object" && typeof resolveData !== 'string') throw 'data type is not iterable';
var temp = el.cloneNode(true);
for (let index in resolveData) {
let element = resolveData[index];
if (typeof resolveData === 'object' && !Array.isArray(resolveData)){
if(!resolveData.hasOwnProperty(index))continue;
}
var tmpele = temp.cloneNode(true);
this.findChildNode(tmpele, (sub) => {
var res = this.getExpMustache(sub.textContent, (txt, content) => {
var tmpresult = (new Function(param1, param2, `return ${content};`)).call(this, element, index);
return tmpresult;
})
sub.textContent = res;
});
if (counter === 0) {
this.replaceElement(tmpele, el);
el = tmpele;
} else {
this.insertAfter(tmpele, el);
el = tmpele;
}
counter++;
}
}
}
};
this.$el = this.toDom(opts.el);
this.toObserve(this._data);
this.bindtoSelf(this._data);
this.replaceExp(this,this.$el);
}
VueLite.Global={
}
VueLite.prototype={
constructor:VueLite,
split:(str,chr,opts)=>{
var arr = String.prototype.split.call(str,chr);
return arr.filter((item)=>{
return item!==' ';
});
},
replaceElement:(newEl,targetEl)=>{
targetEl.parentNode.replaceChild(newEl, targetEl);
},
insertAfter: (newEl, targetEl)=>{
var parentEl = targetEl.parentNode;
if (parentEl.lastChild == targetEl) {
parentEl.appendChild(newEl);
} else {
parentEl.insertBefore(newEl, targetEl.nextSibling);
}
},
bindDirectives(attrs,elem){
var obj = {};
var isExist = false;
Array.prototype.map.call(attrs ,(item)=>{
var tmp ={};
tmp[item.nodeName] = item.value ;
Object.assign(obj, tmp);
return tmp;
});
for (const item in obj) {
obj.hasOwnProperty(item) &&
(function(vm){
return (isExist =vm._directives.hasOwnProperty(item));
}(this)) &&
this._directives[item](elem, this, obj[item])
}
return isExist;
},
setExpData(val ,data ,exp){
if(val ===undefined) throw TypeError("val type error.");
var expclips = exp.split('.');
var value = data;
for (const key in expclips) {
var tmp = value[expclips[key]];
if (typeof value === 'object' && tmp === undefined) throw `resolve error. ${key} => ${expclips[key]}`;
if (tmp === undefined) {
value[expclips[key]] = val;
break;
};
if (typeof tmp !== 'object') {
value[expclips[key]] = val;
value = tmp;
break;
}
else {
value = tmp;
}
}
return value;
},
resolveDynamicExp(){
},
execExp2(exp,data,callback1,callback2){
var resolveExp = new Function(`with(this){return ${exp};}`);
callback1 &&callback1();
var result = resolveExp.call(data);
callback2 && callback2();
return result;
},
execExp(exp,data,callback1,callback2){
var expclips =exp.split('.');
var value = data;
for (const key in expclips) {
callback1&&callback1();
var tmp =value[expclips[key]];
callback2&& callback2();
if (typeof value==='object' && tmp===undefined) throw `resolve error. ${key} => ${expclips[key]}`;
if(tmp===undefined) break;
if(typeof tmp !=='object'){
value = tmp;
break;
}
else{
value = tmp;
}
}
return value;
},
//get mustache expression
getExpMustache(txt,callback){
let reg = /\{\{(.*?)\}\}/g;
var expresult = [];
return txt.replace(reg, (exp1, expcontent, idx) => {
expresult.push([exp1, expcontent]);
return callback(exp1,expcontent);
});
},
getExpValue(node,prop,txt, data){
let result ;
let regarr=[];
this.getExpMustache(txt, (mus,content)=>{
let value = this.execExp2(content, data, () => {
this.taget = new Subscriber( (newVal)=> {
node[prop] = this.execExp2(content,data) ;
});
}, () => {
this.taget = null;
});
regarr.push([value, mus]);
});
if(regarr.length>0){
result = txt;
for (const item of regarr) {
result = String.prototype.replace.call(result,item[1],item[0])
}
}
return result;
},
findChildNode(parent, callbackMustache,callbackAttribute){
if (!parent.childNodes) return;
Array.from(parent.childNodes).forEach((ele, idx, arr) => {
//text node
if (ele.nodeType === 3) {
callbackMustache&& callbackMustache(ele);
} // element node
else if (ele.nodeType === 1) {
var isnext = ele.attributes.length > 0 && callbackAttribute && callbackAttribute(ele, ele.attributes) ;
if(!isnext){
this.replaceExp(this, ele);
}
}
});
}
,
replaceExp(vm,el){
this.findChildNode(el,(ele)=>{
var regresult = this.getExpValue(ele, "textContent", ele.textContent, vm._data);
if (regresult) {
ele.textContent = regresult;
}
},
(ele,attrs)=>{
return this.bindDirectives(attrs, ele);
});
},
toDom(sel){
return document.querySelector(sel);
},
toObserve(data){
let self = this;
for (const key in data) {
if(typeof data!=='object')return;
var _publisher = new Publisher();
if (data.hasOwnProperty(key)) {
let element = data[key];
Object.defineProperty(data,key,{
enumerable:true,
configurable:true,
get(){
if(self.taget !==null){
_publisher.addSub(self.taget);
}
return element;
},
set(newVal){
element = newVal;
_publisher.notify(newVal);
}
});
if(typeof element ==='object' && Object.prototype.toString.call(element)==='[object Object]'){
this.toObserve(element);
}
}
}
},
bindtoSelf(taget){
for (const item in taget) {
if (taget.hasOwnProperty(item)) {
const element = taget[item];
Object.defineProperty(this,item,{
configurable:true,
enumerable:true,
get(){
return element;
},
set(newVal){
taget[item] = newVal;
}
})
}
}
}
}