-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy path_index.js
367 lines (339 loc) · 11.2 KB
/
_index.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
import { avalon, isObject, platform } from '../seed/core'
import { cssDiff } from '../directives/css'
import { dumpTree, groupTree, getRange } from '../renders/share'
var legalTags = { wbr: 1, xmp: 1, template: 1 }
var events = 'onInit,onReady,onViewChange,onDispose,onEnter,onLeave'
var componentEvents = avalon.oneObject(events)
function toObject(value) {
var value = platform.toJson(value)
if (Array.isArray(value)) {
var v = {}
value.forEach(function(el) {
el && avalon.shadowCopy(v, el)
})
return v
}
return value
}
var componentQueue = []
avalon.directive('widget', {
delay: true,
priority: 4,
deep: true,
init: function() {
//cached属性必须定义在组件容器里面,不是template中
var vdom = this.node
this.cacheVm = !!vdom.props.cached
if (vdom.dom && vdom.nodeName === '#comment') {
var comment = vdom.dom
}
var oldValue = this.getValue()
var value = toObject(oldValue)
//外部VM与内部VM
// ===创建组件的VM==BEGIN===
var is = vdom.props.is || value.is
this.is = is
var Component = avalon.components[is]
//外部传入的总大于内部
if (!('fragment' in this)) {
if (!vdom.isVoidTag) { //提取组件容器内部的东西作为模板
var text = vdom.children[0]
if (text && text.nodeValue) {
this.fragment = text.nodeValue
} else {
this.fragment = avalon.vdom(vdom.children, 'toHTML')
}
} else {
this.fragment = false
}
}
//如果组件还没有注册,那么将原元素变成一个占位用的注释节点
if (!Component) {
this.readyState = 0
vdom.nodeName = '#comment'
vdom.nodeValue = 'unresolved component placeholder'
delete vdom.dom
avalon.Array.ensure(componentQueue, this)
return
}
this.readyState = 1
//如果是非空元素,比如说xmp, ms-*, template
var id = value.id || value.$id
var hasCache = avalon.vmodels[id]
var fromCache = false
var component = new Component()
if (hasCache) {
comVm = hasCache
this.comVm = comVm
replaceRoot(this, comVm.$render)
fromCache = true
} else {
console.log(value)
var comVm = component.__getVm(value)
fireComponentHook(comVm, vdom, 'Init')
this.comVm = comVm
var boss = avalon.scan(component.template, comVm)
comVm.$render = boss
replaceRoot(this, boss)
var nodesWithSlot = []
var directives = []
if (this.fragment || component.soleSlot) {
var curVM = this.fragment ? this.vm : comVm
var curText = this.fragment || '{{##' + component.soleSlot + '}}'
var childBoss = avalon.scan('<div>' + curText + '</div>', curVM, function() {
nodesWithSlot = this.root.children
})
directives = childBoss.directives
for (var i in childBoss) {
delete childBoss[i]
}
}
boss.directives.push.apply(boss.directives, directives)
var arraySlot = [],
objectSlot = {}
//从用户写的元素内部 收集要移动到 新创建的组件内部的元素
if (component.soleSlot) {
arraySlot = nodesWithSlot
} else {
nodesWithSlot.forEach(function(el, i) { //要求带slot属性
if (el.slot) {
var nodes = getRange(nodesWithSlot, el)
nodes.push(nodes.end)
nodes.unshift(el)
objectSlot[el.slot] = nodes
} else if (el.props) {
var name = el.props.slot
if (name) {
delete el.props.slot
if (Array.isArray(objectSlot[name])) {
objectSlot[name].push(el)
} else {
objectSlot[name] = [el]
}
}
}
})
}
//将原来元素的所有孩子,全部移动新的元素的第一个slot的位置上
if (component.soleSlot) {
insertArraySlot(boss.vnodes, arraySlot)
} else {
insertObjectSlot(boss.vnodes, objectSlot)
}
}
if (comment) {
var dom = avalon.vdom(vdom, 'toDOM')
comment.parentNode.replaceChild(dom, comment)
comVm.$element = boss.root.dom = dom
delete this.reInit
}
//处理DOM节点
dumpTree(vdom.dom)
groupTree(vdom.dom, vdom.children)
if (fromCache) {
fireComponentHook(comVm, vdom, 'Enter')
} else {
fireComponentHook(comVm, vdom, 'Ready')
}
this.beforeDispose = function() {
if (!this.cacheVm) {
fireComponentHook(comVm, vdom, 'Dispose')
comVm.$hashcode = false
delete avalon.vmodels[comVm.$id]
this.boss.dispose()
} else {
fireComponentHook(comVm, vdom, 'Leave')
}
}
},
diff: function(newVal, oldVal) {
if (cssDiff.call(this, newVal, oldVal)) {
return true
}
},
update: function(vdom, value) {
this.oldValue = value //★★防止递归
switch (this.readyState) {
case 0:
if (this.reInit) {
this.init()
}
break
case 1:
this.readyState++
break
default:
this.readyState++
var comVm = this.comVm
avalon.viewChanging = true
avalon.transaction(function() {
for (var i in value) {
if (comVm.hasOwnProperty(i)) {
comVm[i] = value[i]
}
}
})
//要保证要先触发孩子的ViewChange 然后再到它自己的ViewChange
fireComponentHook(comVm, vdom, 'ViewChange')
delete avalon.viewChanging
break
}
}
})
function replaceRoot(instance, boss) {
instance.boss = boss
var root = boss.root
var vdom = instance.node
for (var i in root) {
vdom[i] = root[i]
}
boss.root = vdom
boss.vnodes[0] = vdom
}
function fireComponentHook(vm, vdom, name) {
var list = vm.$events['on' + name]
if (list) {
list.forEach(function(el) {
el.callback.call(vm, {
type: name.toLowerCase(),
target: vdom.dom,
vmodel: vm
})
})
}
}
function collectHooks(a, list) {
for (var i in a) {
if (componentEvents[i]) {
if (typeof a[i] === 'function' &&
i.indexOf('on') === 0) {
list.unshift({
type: i,
cb: a[i]
})
}
delete a[i]
}
}
}
function resetParentChildren(nodes, arr) {
var dir = arr && arr[0] && arr[0].forDir
if (dir) {
dir.parentChildren = nodes
}
}
function insertArraySlot(nodes, arr) {
for (var i = 0, el; el = nodes[i]; i++) {
if (el.nodeName === 'slot') {
resetParentChildren(nodes, arr)
nodes.splice.apply(nodes, [i, 1].concat(arr))
break
} else if (el.children) {
insertArraySlot(el.children, arr)
}
}
}
function insertObjectSlot(nodes, obj) {
for (var i = 0, el; el = nodes[i]; i++) {
if (el.nodeName === 'slot') {
var name = el.props.name
resetParentChildren(nodes, obj[name])
nodes.splice.apply(nodes, [i, 1].concat(obj[name]))
continue
} else if (el.children) {
insertObjectSlot(el.children, obj)
}
}
}
function Avalon() {}
Avalon.prototype = {
constructor: Avalon,
__getVm: function(props) {
var hooks = []
var ctor = this.constructor
var def = avalon.mix(true, {}, this, this.defaults, (ctor.defaults || {}))
delete def.__getVm
delete def.id
delete def.is
delete def.template
delete def.soleSlot
collectHooks(def, hooks)
if (props) {
collectHooks(props, hooks)
for (var i in def) {
var value = props[i]
if (value != null) {
if (value && typeof value === 'object') {
def[i] = avalon.mix(true, Array.isArray(value) ? [] : {}, value)
} else {
def[i] = value
}
}
}
} else {
props = {}
}
var is = ctor.name || '$'
def.$id = props.id || props.$id || avalon.makeHashCode(is)
var vm = avalon.define(def)
hooks.forEach(function(el) {
vm.$watch(el.type, el.cb)
})
return vm
}
}
avalon.Avalon = Avalon
avalon.createComponent = (function() {
function obj(o) {
var __ = function() {}
__.prototype = o
return new __
}
function extend(child, parent) {
//这里要创建对象就是防止对parent产生污染
var proto = child.prototype = obj(parent.prototype);
proto.constructor = child
}
return function(className, define, superClass) {
className = avalon.camelize(className)
var body = ['function ' + className + '(props){',
'\t__parent.call(this)',
'\tthis.template = ' + avalon.quote(define.template),
(define.soleSlot ?
'\tthis.soleSlot = ' + avalon.quote(define.soleSlot) : ''),
'}',
'__extends(' + className + ',__parent)',
'return ' + className
].join('\n')
var factory = Function('__parent', '__extends', body)
var parentClass = Avalon
if (superClass) {
parentClass = superClass
}
var c = factory(parentClass, extend)
var m = superClass ? superClass.defaults : {}
c.defaults = avalon.mix({}, m, define.defaults)
return c
}
})()
avalon.components = {}
avalon.component = function(name, component) {
/**
* template: string
* defaults: object
* soleSlot: string
*/
if (component !== 'function') { //2.2.1
component = avalon.createComponent(name, component)
}
avalon.components[name] = component
for (var el, i = 0; el = componentQueue[i]; i++) {
if (el.is === name) {
componentQueue.splice(i, 1)
el.reInit = true
delete el.value
el.update()
i--;
}
}
}