-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathif.js
72 lines (65 loc) · 1.51 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
import FragmentFactory from '../../fragment/factory'
import {
getAttr,
remove,
replace,
createAnchor,
warn
} from '../../util/index'
export default {
priority: 2000,
bind () {
var el = this.el
if (!el.__vue__) {
// check else block
var next = el.nextElementSibling
if (next && getAttr(next, 'v-else') !== null) {
remove(next)
this.elseFactory = new FragmentFactory(this.vm, next)
}
// check main block
this.anchor = createAnchor('v-if')
replace(el, this.anchor)
this.factory = new FragmentFactory(this.vm, el)
} else {
process.env.NODE_ENV !== 'production' && warn(
'v-if="' + this.expression + '" cannot be ' +
'used on an instance root element.'
)
this.invalid = true
}
},
update (value) {
if (this.invalid) return
if (value) {
if (!this.frag) {
this.insert()
}
} else {
this.remove()
}
},
insert () {
if (this.elseFrag) {
this.elseFrag.remove()
this.elseFrag = null
}
this.frag = this.factory.create(this._host, this._scope, this._frag)
this.frag.before(this.anchor)
},
remove: function () {
if (this.frag) {
this.frag.remove()
this.frag = null
}
if (this.elseFactory && !this.elseFrag) {
this.elseFrag = this.elseFactory.create(this._host, this._scope, this._frag)
this.elseFrag.before(this.anchor)
}
},
unbind: function () {
if (this.frag) {
this.frag.destroy()
}
}
}