2019-08-31 20:36:36 +00:00
|
|
|
/**
|
|
|
|
Runtime helper for applying directives to a vnode. Example usage:
|
|
|
|
|
|
|
|
const comp = resolveComponent('comp')
|
|
|
|
const foo = resolveDirective('foo')
|
|
|
|
const bar = resolveDirective('bar')
|
|
|
|
|
2019-10-21 14:00:23 +00:00
|
|
|
return withDirectives(h(comp), [
|
2019-08-31 20:36:36 +00:00
|
|
|
[foo, this.x],
|
|
|
|
[bar, this.y]
|
2019-09-03 22:11:04 +00:00
|
|
|
])
|
2019-08-31 20:36:36 +00:00
|
|
|
*/
|
|
|
|
|
2020-03-18 15:43:32 +00:00
|
|
|
import { VNode } from './vnode'
|
|
|
|
import { isFunction, EMPTY_OBJ, makeMap } from '@vue/shared'
|
2019-08-31 20:36:36 +00:00
|
|
|
import { warn } from './warning'
|
2020-03-16 22:36:19 +00:00
|
|
|
import { ComponentInternalInstance, Data } from './component'
|
2020-03-16 16:47:49 +00:00
|
|
|
import { currentRenderingInstance } from './componentRenderUtils'
|
2019-09-06 16:58:31 +00:00
|
|
|
import { callWithAsyncErrorHandling, ErrorCodes } from './errorHandling'
|
2020-03-16 16:47:49 +00:00
|
|
|
import { ComponentPublicInstance } from './componentProxy'
|
2019-08-31 20:36:36 +00:00
|
|
|
|
2020-06-11 20:37:14 +00:00
|
|
|
export interface DirectiveBinding<V = any> {
|
2020-03-16 16:47:49 +00:00
|
|
|
instance: ComponentPublicInstance | null
|
2020-06-11 20:37:14 +00:00
|
|
|
value: V
|
|
|
|
oldValue: V | null
|
2019-08-31 20:36:36 +00:00
|
|
|
arg?: string
|
2019-10-11 21:55:20 +00:00
|
|
|
modifiers: DirectiveModifiers
|
2020-06-11 20:37:14 +00:00
|
|
|
dir: ObjectDirective<any, V>
|
2019-08-31 20:36:36 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 20:37:14 +00:00
|
|
|
export type DirectiveHook<T = any, Prev = VNode<any, T> | null, V = any> = (
|
2019-10-11 21:55:20 +00:00
|
|
|
el: T,
|
2020-06-11 20:37:14 +00:00
|
|
|
binding: DirectiveBinding<V>,
|
2019-10-11 21:55:20 +00:00
|
|
|
vnode: VNode<any, T>,
|
2020-03-18 16:30:20 +00:00
|
|
|
prevVNode: Prev
|
2019-08-31 20:36:36 +00:00
|
|
|
) => void
|
|
|
|
|
2020-03-16 22:36:19 +00:00
|
|
|
export type SSRDirectiveHook = (
|
|
|
|
binding: DirectiveBinding,
|
|
|
|
vnode: VNode
|
|
|
|
) => Data | undefined
|
|
|
|
|
2020-06-11 20:37:14 +00:00
|
|
|
export interface ObjectDirective<T = any, V = any> {
|
|
|
|
beforeMount?: DirectiveHook<T, null, V>
|
|
|
|
mounted?: DirectiveHook<T, null, V>
|
|
|
|
beforeUpdate?: DirectiveHook<T, VNode<any, T>, V>
|
|
|
|
updated?: DirectiveHook<T, VNode<any, T>, V>
|
|
|
|
beforeUnmount?: DirectiveHook<T, null, V>
|
|
|
|
unmounted?: DirectiveHook<T, null, V>
|
2020-03-16 22:36:19 +00:00
|
|
|
getSSRProps?: SSRDirectiveHook
|
2019-08-31 20:36:36 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 20:37:14 +00:00
|
|
|
export type FunctionDirective<T = any, V = any> = DirectiveHook<T, any, V>
|
2019-10-16 06:12:26 +00:00
|
|
|
|
2020-06-11 20:37:14 +00:00
|
|
|
export type Directive<T = any, V = any> =
|
|
|
|
| ObjectDirective<T, V>
|
|
|
|
| FunctionDirective<T, V>
|
2019-10-16 06:12:26 +00:00
|
|
|
|
2019-10-26 20:00:07 +00:00
|
|
|
export type DirectiveModifiers = Record<string, boolean>
|
2019-08-31 20:36:36 +00:00
|
|
|
|
2019-10-26 20:00:07 +00:00
|
|
|
export type VNodeDirectiveData = [
|
|
|
|
unknown,
|
|
|
|
string | undefined,
|
|
|
|
DirectiveModifiers
|
|
|
|
]
|
2019-08-31 20:36:36 +00:00
|
|
|
|
2019-10-18 16:34:45 +00:00
|
|
|
const isBuiltInDirective = /*#__PURE__*/ makeMap(
|
|
|
|
'bind,cloak,else-if,else,for,html,if,model,on,once,pre,show,slot,text'
|
|
|
|
)
|
|
|
|
|
|
|
|
export function validateDirectiveName(name: string) {
|
|
|
|
if (isBuiltInDirective(name)) {
|
|
|
|
warn('Do not use built-in directive ids as custom directive id: ' + name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-02 16:09:29 +00:00
|
|
|
// Directive, value, argument, modifiers
|
2019-09-06 16:58:31 +00:00
|
|
|
export type DirectiveArguments = Array<
|
2019-09-02 16:09:29 +00:00
|
|
|
| [Directive]
|
|
|
|
| [Directive, any]
|
|
|
|
| [Directive, any, string]
|
|
|
|
| [Directive, any, string, DirectiveModifiers]
|
|
|
|
>
|
2019-08-31 20:36:36 +00:00
|
|
|
|
2020-04-30 21:04:35 +00:00
|
|
|
/**
|
|
|
|
* Adds directives to a VNode.
|
|
|
|
*/
|
2019-10-26 20:32:27 +00:00
|
|
|
export function withDirectives<T extends VNode>(
|
|
|
|
vnode: T,
|
|
|
|
directives: DirectiveArguments
|
|
|
|
): T {
|
2020-03-16 16:47:49 +00:00
|
|
|
const internalInstance = currentRenderingInstance
|
|
|
|
if (internalInstance === null) {
|
|
|
|
__DEV__ && warn(`withDirectives can only be used inside render functions.`)
|
|
|
|
return vnode
|
|
|
|
}
|
|
|
|
const instance = internalInstance.proxy
|
2020-03-18 15:43:32 +00:00
|
|
|
const bindings: DirectiveBinding[] = vnode.dirs || (vnode.dirs = [])
|
2019-10-26 20:00:07 +00:00
|
|
|
for (let i = 0; i < directives.length; i++) {
|
|
|
|
let [dir, value, arg, modifiers = EMPTY_OBJ] = directives[i]
|
|
|
|
if (isFunction(dir)) {
|
|
|
|
dir = {
|
|
|
|
mounted: dir,
|
|
|
|
updated: dir
|
|
|
|
} as ObjectDirective
|
|
|
|
}
|
2020-03-18 15:43:32 +00:00
|
|
|
bindings.push({
|
2019-10-26 20:00:07 +00:00
|
|
|
dir,
|
2020-03-16 16:47:49 +00:00
|
|
|
instance,
|
2019-10-26 20:00:07 +00:00
|
|
|
value,
|
|
|
|
oldValue: void 0,
|
|
|
|
arg,
|
|
|
|
modifiers
|
2020-03-18 15:43:32 +00:00
|
|
|
})
|
2019-08-31 20:36:36 +00:00
|
|
|
}
|
|
|
|
return vnode
|
|
|
|
}
|
|
|
|
|
2019-08-31 21:06:39 +00:00
|
|
|
export function invokeDirectiveHook(
|
2019-09-02 16:09:29 +00:00
|
|
|
vnode: VNode,
|
2020-03-18 15:43:32 +00:00
|
|
|
prevVNode: VNode | null,
|
|
|
|
instance: ComponentInternalInstance | null,
|
|
|
|
name: keyof ObjectDirective
|
2019-08-31 21:06:39 +00:00
|
|
|
) {
|
2020-03-18 15:43:32 +00:00
|
|
|
const bindings = vnode.dirs!
|
|
|
|
const oldBindings = prevVNode && prevVNode.dirs!
|
|
|
|
for (let i = 0; i < bindings.length; i++) {
|
|
|
|
const binding = bindings[i]
|
|
|
|
if (oldBindings) {
|
|
|
|
binding.oldValue = oldBindings[i].value
|
|
|
|
}
|
|
|
|
const hook = binding.dir[name] as DirectiveHook | undefined
|
|
|
|
if (hook) {
|
|
|
|
callWithAsyncErrorHandling(hook, instance, ErrorCodes.DIRECTIVE_HOOK, [
|
|
|
|
vnode.el,
|
|
|
|
binding,
|
|
|
|
vnode,
|
|
|
|
prevVNode
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}
|
2019-08-31 21:06:39 +00:00
|
|
|
}
|