-
Notifications
You must be signed in to change notification settings - Fork 21
/
web-model.js
233 lines (218 loc) · 9.99 KB
/
web-model.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
/**
* @model.js
* @author xboxyan
* @created: 20-11-13
*/
(function () {
// 事件
const eventList = ['submit', 'click', 'dblclick', 'input', 'change', 'focus', 'blur', 'keydown', 'keyup', 'keypress', 'scroll', 'submit', 'invalid', 'mousedown', 'mousemove', 'mouseup', 'mouseenter', 'mouseleave', 'drag', 'dragstart', 'dragenter', 'dragover', 'dragleave', 'dragend', 'drop']
// 表单绑定
const modelMap = [
{
selector: 'input:not([type=checkbox]):not([type=radio]),textarea',
attrName: 'value',
attrValue: (name) => '${' + name + '}',
event: 'input',
fn: (name) => `this.${name}=$this.value`
},
{
selector: 'select',
attrName: 'value',
attrValue: (name) => '${' + name + '}',
event: 'change',
fn: (name) => `this.${name}=$this.value`
},
{
selector: '[type=radio]',
attrName: 'checked',
attrValue: (name, value) => `\${${name}==${value}}`,
event: 'change',
fn: (name) => `this.${name}=$this.value`
},
{
selector: '[type=checkbox]',
attrName: 'checked',
attrValue: (name, value, isArray) => isArray ? `\${${name}.includes(${value})}` : `\${${name}}`,
event: 'change',
fn: (name, isArray) => isArray ? `function(){ if(this.${name}.includes($this.value)){ this.${name} = this.${name}.filter(el => el!==$this.value) } else { this.${name} = this.${name}.concat($this.value)}}` : `this.${name}=$this.checked`
},
]
// parseFor "(item, index) in items" => {items:[item,index],items:items}
function parseFor(strFor) {
// 是否是对象
const isObject = strFor.includes(' of ');
const reg = /\s(?:in|of)\s/g;
const [keys, obj] = strFor.match(reg) ? strFor.split(reg) : ["item", strFor];
const items = Number(obj) > 0 ? `[${'null,'.repeat(Number(obj) - 1)}null]` : obj;
const params = keys.split(/[\(|\)|,\s?]/g).filter(Boolean);
return { isArrray: !isObject, items, params }
}
// 模板渲染之前
HTMLTemplateElement.prototype.interpolateBefore = function (fragment, { data, options: { computed }, rule }) {
// 表单绑定 $model="val"
const modelEls = fragment.content.querySelectorAll(`[\\${rule}model]`);
modelEls.forEach(el => {
let name = el.getAttribute(`${rule}model`);
const value = el.getAttribute('value');
const reg = /\$\{([^\}]+)\}/g;
const attrvalue = reg.test(value) ? value.replace(reg, '$1') : `'${value}'`;
// const forEl = el.closest(`[\\${rule}for]`);
// if (forEl) {
// const strFor = forEl.getAttribute(`${rule}for`);
// const { isArrray, items, params } = parseFor(strFor);
// const item = params[0] || (isArrray ? 'item' : 'value');
// const index = isArrray ? '__index__' : (params[1] || 'name');
// name = name.replaceAll(item+'.',`${items}[${index}].`);
// }
modelMap.forEach(item => {
if (el.matches(item.selector)) {
if (el.tagName === 'SELECT' && !data[name]) {
el.insertAdjacentHTML('afterbegin', '<option value="">请选择</option>');
}
const isArray = Array.isArray(data[name]);
el.setAttribute(item.attrName, item.attrValue(name, attrvalue, isArray));
el.setAttribute('v-on:'+item.event, item.fn(name, isArray));
el.removeAttribute(`${rule}model`);
}
})
})
// on
// const onEls = this.$fragment.content.querySelectorAll(`[\\${rule}on],[my-on]`);
// onEls.forEach(el => {
// const forEl = el.closest(`[\\${rule}for]`);
// if (forEl) {
// const strFor = forEl.getAttribute(`${rule}for`);
// const { isArrray, params } = parseFor(strFor);
// const name = isArrray ? '__index__' : (params[1] || 'name');
// const index = (isArrray?params[1]:params[2])||'index';
// const attr = el.attributes[`my-on`] || el.attributes[`${rule}on`];
// if (attr) {
// attr.value = attr.value.replace(RegExp(`(${name}|${index})`),'${$1}');
// }
// }
// })
// computed
Object.keys(computed).forEach(el => {
Object.defineProperty(data, el, {
writable: false,
enumerable: true,
value: computed[el].bind(data)
})
})
}
// 模板渲染之后
HTMLTemplateElement.prototype.interpolateAfter = function (fragment, { options: { methods }, rule }) {
const app = this.app;
// select
const selectEls = fragment.content.querySelectorAll(`select`);
selectEls.forEach(el => {
if (!el.value) {
el.selectedOptions[0].disabled = true;
}
})
// 绑定事件 @click="fn"
const eventEls = fragment.content.querySelectorAll(eventList.map( el => `[\\@${el}]`).join(',') + `,[v-on\\:input],[v-on\\:change]`);
eventEls.forEach(el => {
const attrs = Array.from(el.attributes).filter( attr => eventList.includes(attr.name.split(RegExp(`(?:@|v-on:|${rule}-on)`))[1]) )
attrs.forEach(attr => {
if (methods && Object.keys(methods).length && app) {
const name = attr.name.split(RegExp(`(?:@|v-on:|${rule}-on)`))[1];
const events = attr.value;
const fn = function (event) {
if (events.includes('(') || events.includes('this')) {
const names = Object.keys(methods);
const vals = Object.values(methods);
if (events.startsWith('function')) {
// $this , $event 内置DOM原生对象
return Function(...names, '$this', '$event', '"use strict";(' + events + ').call(this)').call(app.vm, ...vals, this, event);
}
return Function(...names, '$this', '$event', '"use strict";' + events.replace(/\(/, '.call(this,')).call(app.vm, ...vals, this, event);
} else {
return methods[events].call(app.vm);
}
// console.warn(name+'事件未定义,请在 methods 中添加');
}
if (name === 'input' && el.tagName === 'INPUT') {
el.addEventListener(name, () => {
this.timer && clearTimeout(this.timer);
this.timer = setTimeout(() => {
fn.call(el)
}, 100)
});
} else {
el.addEventListener(name, fn);
}
//el.removeAttribute(attr.name);
}
})
})
}
// 模板引擎 model
HTMLElement.prototype.model = function ({ data, created, methods = {}, computed = {}, watch = {} }) {
if (!this.html) {
throw new Error('请先引入 web-diff.js');
}
if (!this.template) {
this.template = document.querySelector(`template[is="${this.id}"]`);
}
if (this.template) {
if (!this.template.render) {
throw new Error('请先引入 web-template.js');
}
const app = this;
if (!app.vm) {
this.template.app = app;
app.html(app.template.render(data, { methods, watch, computed }).content);
// 数据更新
let timer = null;
const delay = (cb) => {
timer && clearTimeout(timer);
timer = setTimeout(() => {
cb && cb(data);
})
}
// 监听
const watches = {};
Object.keys(watch).forEach(el => {
watches[el] = data[el];
})
const handler = {
get(target, key) {
if (target[key] == null && methods[key]) {
return methods[key];
}
if (typeof target[key] === 'object' && target[key] !== null) {
return new Proxy(target[key], handler);
}
return Reflect.get(target, key);
},
set(target, key, value) {
if (methods[key]) {
return;
}
if (target[key] !== value) {
delay((data) => {
if (watches[key] !== data[key] && watch[key]) {
watch[key](watches[key],data[key]);
watches[key] = data[key];
}
app.html(app.template.render(data, { methods, watch, computed }).content);
})
}
return Reflect.set(target, key, value);
}
}
Object.defineProperty(app, 'vm', {
writable: false,
value: new Proxy(data, handler)
})
// 初始化完成
created && created.call(app.vm);
} else {
throw new Error('不能重复调用model');
}
} else {
throw new Error(`没有找到 is 属性为 ${this.id} 的模板`);
}
}
})()