aboutsummaryrefslogtreecommitdiff
path: root/node_modules/vue/src/core/vdom/modules
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/vue/src/core/vdom/modules')
-rw-r--r--node_modules/vue/src/core/vdom/modules/directives.js119
-rw-r--r--node_modules/vue/src/core/vdom/modules/index.js7
-rw-r--r--node_modules/vue/src/core/vdom/modules/ref.js45
3 files changed, 171 insertions, 0 deletions
diff --git a/node_modules/vue/src/core/vdom/modules/directives.js b/node_modules/vue/src/core/vdom/modules/directives.js
new file mode 100644
index 00000000..639982fa
--- /dev/null
+++ b/node_modules/vue/src/core/vdom/modules/directives.js
@@ -0,0 +1,119 @@
+/* @flow */
+
+import { emptyNode } from 'core/vdom/patch'
+import { resolveAsset, handleError } from 'core/util/index'
+import { mergeVNodeHook } from 'core/vdom/helpers/index'
+
+export default {
+ create: updateDirectives,
+ update: updateDirectives,
+ destroy: function unbindDirectives (vnode: VNodeWithData) {
+ updateDirectives(vnode, emptyNode)
+ }
+}
+
+function updateDirectives (oldVnode: VNodeWithData, vnode: VNodeWithData) {
+ if (oldVnode.data.directives || vnode.data.directives) {
+ _update(oldVnode, vnode)
+ }
+}
+
+function _update (oldVnode, vnode) {
+ const isCreate = oldVnode === emptyNode
+ const isDestroy = vnode === emptyNode
+ const oldDirs = normalizeDirectives(oldVnode.data.directives, oldVnode.context)
+ const newDirs = normalizeDirectives(vnode.data.directives, vnode.context)
+
+ const dirsWithInsert = []
+ const dirsWithPostpatch = []
+
+ let key, oldDir, dir
+ for (key in newDirs) {
+ oldDir = oldDirs[key]
+ dir = newDirs[key]
+ if (!oldDir) {
+ // new directive, bind
+ callHook(dir, 'bind', vnode, oldVnode)
+ if (dir.def && dir.def.inserted) {
+ dirsWithInsert.push(dir)
+ }
+ } else {
+ // existing directive, update
+ dir.oldValue = oldDir.value
+ callHook(dir, 'update', vnode, oldVnode)
+ if (dir.def && dir.def.componentUpdated) {
+ dirsWithPostpatch.push(dir)
+ }
+ }
+ }
+
+ if (dirsWithInsert.length) {
+ const callInsert = () => {
+ for (let i = 0; i < dirsWithInsert.length; i++) {
+ callHook(dirsWithInsert[i], 'inserted', vnode, oldVnode)
+ }
+ }
+ if (isCreate) {
+ mergeVNodeHook(vnode, 'insert', callInsert)
+ } else {
+ callInsert()
+ }
+ }
+
+ if (dirsWithPostpatch.length) {
+ mergeVNodeHook(vnode, 'postpatch', () => {
+ for (let i = 0; i < dirsWithPostpatch.length; i++) {
+ callHook(dirsWithPostpatch[i], 'componentUpdated', vnode, oldVnode)
+ }
+ })
+ }
+
+ if (!isCreate) {
+ for (key in oldDirs) {
+ if (!newDirs[key]) {
+ // no longer present, unbind
+ callHook(oldDirs[key], 'unbind', oldVnode, oldVnode, isDestroy)
+ }
+ }
+ }
+}
+
+const emptyModifiers = Object.create(null)
+
+function normalizeDirectives (
+ dirs: ?Array<VNodeDirective>,
+ vm: Component
+): { [key: string]: VNodeDirective } {
+ const res = Object.create(null)
+ if (!dirs) {
+ // $flow-disable-line
+ return res
+ }
+ let i, dir
+ for (i = 0; i < dirs.length; i++) {
+ dir = dirs[i]
+ if (!dir.modifiers) {
+ // $flow-disable-line
+ dir.modifiers = emptyModifiers
+ }
+ res[getRawDirName(dir)] = dir
+ dir.def = resolveAsset(vm.$options, 'directives', dir.name, true)
+ }
+ // $flow-disable-line
+ return res
+}
+
+function getRawDirName (dir: VNodeDirective): string {
+ return dir.rawName || `${dir.name}.${Object.keys(dir.modifiers || {}).join('.')}`
+}
+
+function callHook (dir, hook, vnode, oldVnode, isDestroy) {
+ const fn = dir.def && dir.def[hook]
+ if (fn) {
+ try {
+ fn(vnode.elm, dir, vnode, oldVnode, isDestroy)
+ } catch (e) {
+ handleError(e, vnode.context, `directive ${dir.name} ${hook} hook`)
+ }
+ }
+}
diff --git a/node_modules/vue/src/core/vdom/modules/index.js b/node_modules/vue/src/core/vdom/modules/index.js
new file mode 100644
index 00000000..727c8646
--- /dev/null
+++ b/node_modules/vue/src/core/vdom/modules/index.js
@@ -0,0 +1,7 @@
+import directives from './directives'
+import ref from './ref'
+
+export default [
+ ref,
+ directives
+]
diff --git a/node_modules/vue/src/core/vdom/modules/ref.js b/node_modules/vue/src/core/vdom/modules/ref.js
new file mode 100644
index 00000000..aa9bdcfc
--- /dev/null
+++ b/node_modules/vue/src/core/vdom/modules/ref.js
@@ -0,0 +1,45 @@
+/* @flow */
+
+import { remove, isDef } from 'shared/util'
+
+export default {
+ create (_: any, vnode: VNodeWithData) {
+ registerRef(vnode)
+ },
+ update (oldVnode: VNodeWithData, vnode: VNodeWithData) {
+ if (oldVnode.data.ref !== vnode.data.ref) {
+ registerRef(oldVnode, true)
+ registerRef(vnode)
+ }
+ },
+ destroy (vnode: VNodeWithData) {
+ registerRef(vnode, true)
+ }
+}
+
+export function registerRef (vnode: VNodeWithData, isRemoval: ?boolean) {
+ const key = vnode.data.ref
+ if (!isDef(key)) return
+
+ const vm = vnode.context
+ const ref = vnode.componentInstance || vnode.elm
+ const refs = vm.$refs
+ if (isRemoval) {
+ if (Array.isArray(refs[key])) {
+ remove(refs[key], ref)
+ } else if (refs[key] === ref) {
+ refs[key] = undefined
+ }
+ } else {
+ if (vnode.data.refInFor) {
+ if (!Array.isArray(refs[key])) {
+ refs[key] = [ref]
+ } else if (refs[key].indexOf(ref) < 0) {
+ // $flow-disable-line
+ refs[key].push(ref)
+ }
+ } else {
+ refs[key] = ref
+ }
+ }
+}