|
2 | 2 |
|
3 | 3 | import { resolveAsset } from 'core/util/options'
|
4 | 4 | import { mergeVNodeHook } from 'core/vdom/helpers'
|
| 5 | +import { emptyNode } from 'core/vdom/patch' |
5 | 6 |
|
6 | 7 | export default {
|
7 |
| - create: function bindDirectives (oldVnode: VNodeWithData, vnode: VNodeWithData) { |
8 |
| - let hasInsert = false |
9 |
| - forEachDirective(oldVnode, vnode, (def, dir) => { |
10 |
| - callHook(def, dir, 'bind', vnode, oldVnode) |
11 |
| - if (def.inserted) { |
12 |
| - hasInsert = true |
| 8 | + create: updateDirectives, |
| 9 | + update: updateDirectives, |
| 10 | + destroy: function unbindDirectives (vnode: VNodeWithData) { |
| 11 | + updateDirectives(vnode, emptyNode) |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +function updateDirectives ( |
| 16 | + oldVnode: VNodeWithData, |
| 17 | + vnode: VNodeWithData |
| 18 | +) { |
| 19 | + if (!oldVnode.data.directives && !vnode.data.directives) { |
| 20 | + return |
| 21 | + } |
| 22 | + const isCreate = oldVnode === emptyNode |
| 23 | + const oldDirs = normalizeDirectives(oldVnode.data.directives, oldVnode.context) |
| 24 | + const newDirs = normalizeDirectives(vnode.data.directives, vnode.context) |
| 25 | + |
| 26 | + const dirsWithInsert = [] |
| 27 | + const dirsWithPostpatch = [] |
| 28 | + |
| 29 | + let key, oldDir, dir |
| 30 | + for (key in newDirs) { |
| 31 | + oldDir = oldDirs[key] |
| 32 | + dir = newDirs[key] |
| 33 | + if (!oldDir) { |
| 34 | + // new directive, bind |
| 35 | + callHook(dir, 'bind', vnode, oldVnode) |
| 36 | + if (dir.def && dir.def.inserted) { |
| 37 | + dirsWithInsert.push(dir) |
| 38 | + } |
| 39 | + } else { |
| 40 | + // existing directive, update |
| 41 | + dir.oldValue = oldDir.value |
| 42 | + callHook(dir, 'update', vnode, oldVnode) |
| 43 | + if (dir.def && dir.def.componentUpdated) { |
| 44 | + dirsWithPostpatch.push(dir) |
13 | 45 | }
|
14 |
| - }) |
15 |
| - if (hasInsert) { |
16 |
| - mergeVNodeHook(vnode.data.hook || (vnode.data.hook = {}), 'insert', () => { |
17 |
| - applyDirectives(oldVnode, vnode, 'inserted') |
18 |
| - }, 'dir-insert') |
19 | 46 | }
|
20 |
| - }, |
21 |
| - update: function updateDirectives (oldVnode: VNodeWithData, vnode: VNodeWithData) { |
22 |
| - applyDirectives(oldVnode, vnode, 'update') |
23 |
| - // if old vnode has directives but new vnode doesn't |
24 |
| - // we need to teardown the directives on the old one. |
25 |
| - if (oldVnode.data.directives && !vnode.data.directives) { |
26 |
| - applyDirectives(oldVnode, oldVnode, 'unbind') |
| 47 | + } |
| 48 | + |
| 49 | + if (dirsWithInsert.length) { |
| 50 | + const callInsert = () => { |
| 51 | + dirsWithInsert.forEach(dir => { |
| 52 | + callHook(dir, 'inserted', vnode, oldVnode) |
| 53 | + }) |
| 54 | + } |
| 55 | + if (isCreate) { |
| 56 | + mergeVNodeHook(vnode.data.hook || (vnode.data.hook = {}), 'insert', callInsert, 'dir-insert') |
| 57 | + } else { |
| 58 | + callInsert() |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + if (dirsWithPostpatch.length) { |
| 63 | + mergeVNodeHook(vnode.data.hook || (vnode.data.hook = {}), 'postpatch', () => { |
| 64 | + dirsWithPostpatch.forEach(dir => { |
| 65 | + callHook(dir, 'componentUpdated', vnode, oldVnode) |
| 66 | + }) |
| 67 | + }, 'dir-postpatch') |
| 68 | + } |
| 69 | + |
| 70 | + if (!isCreate) { |
| 71 | + for (key in oldDirs) { |
| 72 | + if (!newDirs[key]) { |
| 73 | + // no longer present, unbind |
| 74 | + callHook(oldDirs[key], 'unbind', oldVnode) |
| 75 | + } |
27 | 76 | }
|
28 |
| - }, |
29 |
| - postpatch: function postupdateDirectives (oldVnode: VNodeWithData, vnode: VNodeWithData) { |
30 |
| - applyDirectives(oldVnode, vnode, 'componentUpdated') |
31 |
| - }, |
32 |
| - destroy: function unbindDirectives (vnode: VNodeWithData) { |
33 |
| - applyDirectives(vnode, vnode, 'unbind') |
34 | 77 | }
|
35 | 78 | }
|
36 | 79 |
|
37 | 80 | const emptyModifiers = Object.create(null)
|
38 | 81 |
|
39 |
| -function forEachDirective ( |
40 |
| - oldVnode: VNodeWithData, |
41 |
| - vnode: VNodeWithData, |
42 |
| - fn: Function |
43 |
| -) { |
44 |
| - const dirs = vnode.data.directives |
45 |
| - if (dirs) { |
46 |
| - for (let i = 0; i < dirs.length; i++) { |
47 |
| - const dir = dirs[i] |
48 |
| - const def = resolveAsset(vnode.context.$options, 'directives', dir.name, true) |
49 |
| - if (def) { |
50 |
| - const oldDirs = oldVnode && oldVnode.data.directives |
51 |
| - if (oldDirs) { |
52 |
| - dir.oldValue = oldDirs[i].value |
53 |
| - } |
54 |
| - if (!dir.modifiers) { |
55 |
| - dir.modifiers = emptyModifiers |
56 |
| - } |
57 |
| - fn(def, dir) |
58 |
| - } |
| 82 | +function normalizeDirectives ( |
| 83 | + dirs: ?Array<VNodeDirective>, |
| 84 | + vm: Component |
| 85 | +): { [key: string]: VNodeDirective } { |
| 86 | + const res = Object.create(null) |
| 87 | + if (!dirs) { |
| 88 | + return res |
| 89 | + } |
| 90 | + let i, dir |
| 91 | + for (i = 0; i < dirs.length; i++) { |
| 92 | + dir = dirs[i] |
| 93 | + res[getRawDirName(dir)] = dir |
| 94 | + if (!dir.modifiers) { |
| 95 | + dir.modifiers = emptyModifiers |
59 | 96 | }
|
| 97 | + dir.def = resolveAsset(vm.$options, 'directives', dir.name, true) |
60 | 98 | }
|
| 99 | + return res |
61 | 100 | }
|
62 | 101 |
|
63 |
| -function applyDirectives ( |
64 |
| - oldVnode: VNodeWithData, |
65 |
| - vnode: VNodeWithData, |
66 |
| - hook: string |
67 |
| -) { |
68 |
| - forEachDirective(oldVnode, vnode, (def, dir) => { |
69 |
| - callHook(def, dir, hook, vnode, oldVnode) |
70 |
| - }) |
| 102 | +function getRawDirName (dir: VNodeDirective): string { |
| 103 | + return dir.rawName || ( |
| 104 | + dir.name + ( |
| 105 | + dir.modifiers |
| 106 | + ? '.' + Object.keys(dir.modifiers).join('.') |
| 107 | + : '' |
| 108 | + ) |
| 109 | + ) |
71 | 110 | }
|
72 | 111 |
|
73 |
| -function callHook (def, dir, hook, vnode, oldVnode) { |
74 |
| - const fn = def && def[hook] |
| 112 | +function callHook (dir, hook, vnode, oldVnode) { |
| 113 | + const fn = dir.def && dir.def[hook] |
75 | 114 | if (fn) {
|
76 | 115 | fn(vnode.elm, dir, vnode, oldVnode)
|
77 | 116 | }
|
|
0 commit comments