-
Notifications
You must be signed in to change notification settings - Fork 0
/
mVue.js
306 lines (280 loc) · 7.34 KB
/
mVue.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
class Dep {
constructor() {
this.subs = []
}
//订阅
addSub(watcher) {
this.subs.push(watcher) //存放所有的watcher
}
// 发布
notify() {
this.subs.forEach((watcher) => {
watcher.update()
})
}
}
//观察者类
class Watcher {
constructor(vm, expr, callback) {
this.vm = vm
this.expr = expr
this.callback = callback
// 默认存放一个老值
this.oldValue = this.get()
}
get() {
Dep.target = this
let value = compileUtils.getVal(this.vm, this.expr)
Dep.target = null
return value
}
update() {
//更新数据 数据变化后 会调用观察者的update方法
let newValue = compileUtils.getVal(this.vm, this.expr)
if (newValue !== this.oldValue) {
this.callback(newValue)
}
}
}
//数据劫持类
class Observer {
constructor(data) {
this.observer(data)
}
observer(data) {
if (data && typeof data === 'object') {
for (let key in data) {
this.defineReactive(data, key, data[key])
}
}
}
defineReactive(obj, key, value) {
this.observer(value)
let dep = new Dep()
Object.defineProperty(obj, key, {
get() {
Dep.target && dep.addSub(Dep.target)
return value
},
set: (newValue) => {
if (newValue !== value) {
this.observer(newValue)
value = newValue
dep.notify()
}
},
})
}
}
//处理指令的工具类
const compileUtils = {
html(node, expr, vm) {
let fn = this.updater.htmlUpdate
new Watcher(vm, expr, (newValue) => {
fn(node, newValue)
})
value = this.getVal(vm, expr)
fn(node, value)
},
getContentValue(vm, expr) {
return expr.replace(/\{\{(.+?)\}\}/g, (...args) => {
return this.getVal(vm, args[1])
})
},
text(node, expr, vm) {
let fn = this.updater.textUpdate
let content
//如果是直接以 {{}}的
if (/\{\{(.+?)\}\}/g.test(expr)) {
content = expr.replace(/\{\{(.+?)\}\}/g, (...args) => {
new Watcher(vm, args[1], () => {
console.log(expr)
fn(node, this.getContentValue(vm, expr)) //返回一个全的字符串
})
let value = this.getVal(vm, args[1])
return value
})
fn(node, content)
} else {
//如果是以 v-text的指令的
let value = this.getVal(vm, expr)
new Watcher(vm, expr, (newValue) => {
fn(node, newValue)
})
fn(node, value)
}
},
on(node, expr, vm, eventName) {
node.addEventListener(eventName, (event) => {
vm[expr].call(vm, event)
})
},
model(node, expr, vm) {
let fn = this.updater.modelUpdate
//添加一个观察者
new Watcher(vm, expr, (newValue) => {
fn(node, newValue)
})
node.addEventListener('input', (e) => {
let value = e.currentTarget.value
this.setValue(vm, expr, value)
})
let value = this.getVal(vm, expr)
fn(node, value)
},
bind(node, expr, vm) {},
updater: {
htmlUpdate(node, value) {
node.innerHTML = value
},
textUpdate(node, value) {
node.textContent = value
},
modelUpdate(node, value) {
node.value = value
},
bindUpdate(node, value) {},
},
setValue(vm, expr, value) {
expr.split('.').reduce((data, current, index, arr) => {
if (index === arr.length - 1) {
return (data[current] = value)
}
return data[current]
}, vm.$data)
},
getVal(vm, expr) {
// expr person.name
return expr.split('.').reduce((data, current) => {
return data[current]
}, vm.$data)
},
}
//编译器类
class Compiler {
constructor(el, vm) {
this.el = this.isElementNode(el) ? el : document.querySelector(el)
this.vm = vm
this.data = vm.$data
let fragment = this.node2fragment(this.el)
//进行编译
this.compiler(fragment)
this.el.appendChild(fragment)
}
//判断是不是一个指令的方法
isDirective(name) {
//判断是不是一个指令(是不是以v-开头)
return name.startsWith('v-')
}
//编译元素
compileElement(node) {
//如果是元素获取节点上的属性
let attributes = node.attributes
;[...attributes].forEach((attr) => {
let { name, value: expr } = attr
//如果是一个指令
if (this.isDirective(name)) {
//分割字符串
let [, Directive] = name.split('-')
let [DirectiveName, eventName] = Directive.split(':')
compileUtils[DirectiveName](node, expr, this.vm, eventName)
}
})
}
//编译文本的方法
compileText(node) {
let content = node.textContent
//console.log(content)
if (/\{\{(.+?)\}\}/.test(content)) {
//调用工具类的text方法
compileUtils['text'](node, content, this.vm)
}
}
compiler(node) {
let childerNodes = node.childNodes
;[...childerNodes].forEach((node) => {
//如果是一个元素节点
if (this.isElementNode(node)) {
this.compileElement(node)
//递归调用 如果该元素中还存在子节点
this.compiler(node)
} else {
//如果是一个文本节点
this.compileText(node)
}
})
}
isElementNode(element) {
//判断是不是一个元素节点
return element.nodeType === 1
}
//将元素先保存到文档碎片中(内存),避免每次更新渲染回流和重绘
node2fragment(node) {
//创建一个文档碎片
let fragment = document.createDocumentFragment()
let firstChild
while ((firstChild = node.firstChild)) {
//appendChild具有移动性
fragment.appendChild(firstChild)
}
return fragment
}
}
//创建一个基类
class Vue {
//构造器
constructor(options) {
//console.log('执行构造器方法。')
this.$el = options.el
this.$data = options.data
let computed = options.computed
let methods = options.methods
//判断是否存在el元素
if (this.$el) {
new Observer(this.$data)
for (let key in computed) {
//如果属性的值是一个函数
if (typeof computed[key] === 'function') {
Object.defineProperty(this.$data, key, {
get: () => {
return computed[key].call(this)
},
})
} else {
//如果属性的值是一个对象
Object.defineProperty(this.$data, key, {
get: () => {
return computed[key].get.call(this)
},
set: (newValue) => {
computed[key].set.call(this, newValue)
},
})
}
}
for (let key in methods) {
Object.defineProperty(this, key, {
get() {
return methods[key]
},
})
}
//代理
this.proxyVm(this.$data)
//调用编译器,进行编译
new Compiler(this.$el, this) //将元素和实例传给编译器
}
}
proxyVm(data) {
for (let key in data) {
Object.defineProperty(this, key, {
get() {
return data[key]
},
set(newValue) {
data[key] = newValue
},
})
}
}
}