-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathif.js
124 lines (112 loc) · 2.99 KB
/
if.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
var _ = require('../util')
var compile = require('../compiler/compile')
var templateParser = require('../parsers/template')
var transition = require('../transition')
module.exports = {
bind: function () {
var el = this.el
if (!el.__vue__) {
this.start = _.createAnchor('v-if-start')
this.end = _.createAnchor('v-if-end')
_.replace(el, this.end)
_.before(this.start, this.end)
if (el.tagName === 'TEMPLATE') {
this.template = templateParser.parse(el, true)
} else {
this.template = document.createDocumentFragment()
this.template.appendChild(templateParser.clone(el))
}
// compile the nested partial
this.linker = compile(
this.template,
this.vm.$options,
true
)
} else {
this.invalid = true
_.warn(
'v-if="' + this.expression + '" cannot be ' +
'used on an already mounted instance.'
)
}
},
update: function (value) {
if (this.invalid) return
if (value) {
// avoid duplicate compiles, since update() can be
// called with different truthy values
if (!this.unlink) {
this.compile()
}
} else {
this.teardown()
}
},
compile: function () {
var vm = this.vm
var frag = templateParser.clone(this.template)
// the linker is not guaranteed to be present because
// this function might get called by v-partial
this.unlink = this.linker(vm, frag)
transition.blockAppend(frag, this.end, vm)
// call attached for all the child components created
// during the compilation
if (_.inDoc(vm.$el)) {
var children = this.getContainedComponents()
if (children) children.forEach(callAttach)
}
},
teardown: function () {
if (!this.unlink) return
// collect children beforehand
var children
if (_.inDoc(this.vm.$el)) {
children = this.getContainedComponents()
}
transition.blockRemove(this.start, this.end, this.vm)
if (children) children.forEach(callDetach)
this.unlink()
this.unlink = null
},
getContainedComponents: function () {
var vm = this.vm
var start = this.start.nextSibling
var end = this.end
var selfCompoents =
vm._children.length &&
vm._children.filter(contains)
var transComponents =
vm._transCpnts &&
vm._transCpnts.filter(contains)
function contains (c) {
var cur = start
var next
while (next !== end) {
next = cur.nextSibling
if (cur.contains(c.$el)) {
return true
}
cur = next
}
return false
}
return selfCompoents
? transComponents
? selfCompoents.concat(transComponents)
: selfCompoents
: transComponents
},
unbind: function () {
if (this.unlink) this.unlink()
}
}
function callAttach (child) {
if (!child._isAttached) {
child._callHook('attached')
}
}
function callDetach (child) {
if (child._isAttached) {
child._callHook('detached')
}
}