-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdirective.ts
59 lines (54 loc) · 1.69 KB
/
directive.ts
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
import Vue from 'vue';
import { parentMethods } from './utils/tools';
import { camelToKebabCase, vueVersion } from './utils/utilities';
export interface DirectiveOptions {
name: string;
deep?: boolean;
local?: boolean;
twoWay?: boolean;
params?: string[];
priority?: number;
terminal?: boolean;
paramWatchers?: Object;
acceptStatement?: boolean;
}
export default function Directive(options: DirectiveOptions | string) {
return function (target: any): any {
if (typeof options === 'string') {
options = { name: options };
}
let local = options.local;
delete options.local;
let instance = new target();
parentMethods(target, instance);
if (options.params && !options.paramWatchers) options.paramWatchers = {};
for (let key in instance) {
let isFunc = typeof instance[key] === 'function';
if (isFunc) {
if (~['bind', 'update', 'unbind'].indexOf(key)) {
options[key] = instance[key];
} else if (~['inserted', 'componentUpdated'].indexOf(key) && vueVersion === 2) {
options[key] = instance[key];
} else if (options.paramWatchers && ~options.params.indexOf(key)) {
options.paramWatchers[key] = instance[key];
}
}
}
if (!options.name) {
console.warn(`[vue-ts-decorate] Property 'name' must be set in directives`);
}
let attr: string = camelToKebabCase(options.name);
delete options.name;
let updateFunc: boolean | Function | DirectiveOptions = Object.keys(options).length === 1 && (<any>options).update;
if (local) {
return { [attr]: updateFunc || options };
} else {
if (typeof updateFunc !== 'boolean') {
Vue.directive(attr, updateFunc);
} else {
Vue.directive(attr, options);
}
return Vue.directive(attr);
}
};
}