-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
160 lines (130 loc) · 4.33 KB
/
index.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import './polyfill';
import * as utils from './utils';
import { add, remove } from './delegate';
import { $Event, $Callback } from './events';
import { triggerEvent, triggerDelegateEvent } from './trigger';
import { VueConstructor } from 'vue/types/vue';
import { VNodeDirective } from 'vue/types/vnode';
interface Modifiers { [key: string]: boolean; }
interface DalegateOn {
el: HTMLElement;
type: string;
selector?: string;
fn: $Callback;
option?: Modifiers;
}
interface DalegateOff {
el: HTMLElement;
type?: string;
selector?: string;
fn?: $Callback;
capture?: boolean;
}
const functionMap = new Map<$Callback, $Callback>();
/**
* Wrapper the function
* @param {$Callback} callback
* @param {Modifiers} modifiers
* @param {(() => void)} once
* @returns {$Callback}
*/
export function fnWrapper(callback: $Callback, modifiers: Modifiers, once?: () => void): $Callback {
return function packFn(event: $Event) {
let self = true, left = true, right = true, esc = true, enter = true;
if (modifiers.self) {
self = event.currentTarget === event.target;
}
if (modifiers.left) {
left = event.button === 0;
}
if (modifiers.right) {
right = event.button === 2;
}
if (modifiers.esc) {
esc = event.key === 'Escape';
}
if (modifiers.enter) {
enter = event.key === 'Enter';
}
if (self && left && right && esc && enter) {
const ans = callback(event);
event.result = ans;
if (modifiers.stop) {
event.stopPropagation();
}
if (modifiers.prevent) {
event.preventDefault();
}
if (modifiers.once && once) {
once();
}
return ans;
}
};
}
/**
* Fix the type of input event
* @param {string} type
* @returns {string}
*/
function fixType(type: string): string {
const match = type && type.match(/^[a-z]+/i);
if (!match) {
throw (new Error('(delegate) Illegal Event'));
}
return match[0];
}
/**
* directive bind event
* @param {HTMLElement} el
* @param {VNodeDirective} binding
*/
function bind(el: HTMLElement, binding: VNodeDirective): void {
const value = utils.isArray(binding.value) ? binding.value : ['', binding.value];
const [selector, callback] = value as [string, $Callback];
if (utils.isNull(callback)) {
throw new Error('(delegate) must input callback');
}
const handler = fnWrapper(callback, binding.modifiers, () => unbind(el, binding));
const type = fixType(binding.arg);
functionMap.set(callback, handler);
add(el, type, selector, handler, binding.modifiers.capture);
}
/**
* directive unbind event
* @param {HTMLElement} el
* @param {VNodeDirective} binding
*/
function unbind(el: HTMLElement, binding: VNodeDirective): void {
const value = utils.isArray(binding.value) ? binding.value : ['*', binding.value];
const [selector, callback] = value as [string, $Callback];
const handler = functionMap.get(callback);
const type = fixType(binding.arg);
functionMap.delete(callback);
remove(el, type, selector, handler, binding.modifiers.capture);
}
export default {
install(App: VueConstructor) {
App.directive('delegate', {
bind,
unbind,
});
App.prototype.delegateOn = function({ el, type, selector = '', fn, option = {} }: DalegateOn) {
const handler = fnWrapper(fn, option, () => this.delegateOff({ el, type, selector, fn, capture: option.capture }));
functionMap.set(fn, handler);
add(el, type, selector, handler, option.capture || false);
};
App.prototype.delegateOff = (opt: HTMLElement | DalegateOff) => {
const { el, type, selector, fn, capture } = (opt instanceof Element)
? { el: opt, type: '', selector: '*', fn: undefined, capture: undefined }
: opt;
const handler = fn ? functionMap.get(fn) : undefined;
if (fn) {
functionMap.delete(fn);
}
remove(el, type, selector, handler, capture);
};
App.prototype.triggerEvent = triggerEvent;
App.prototype.trigger$Event = triggerDelegateEvent;
},
};