@whitesev/domutils 1.4.3 → 1.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.amd.js +79 -61
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +79 -61
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +79 -61
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +79 -61
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +79 -61
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +79 -61
- package/dist/index.umd.js.map +1 -1
- package/dist/types/src/DOMUtilsEvent.d.ts +18 -18
- package/dist/types/src/types/DOMUtilsEvent.d.ts +12 -1
- package/package.json +1 -1
- package/src/DOMUtilsEvent.ts +135 -103
- package/src/types/DOMUtilsEvent.d.ts +12 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { DOMUtils_Event, DOMUtils_EventType, DOMUtilsElementEventType, DOMUtilsEventListenerOptionsAttribute } from "./types/DOMUtilsEvent";
|
|
1
|
+
import type { DOMUtils_Event, DOMUtils_EventType, DOMUtilsElementEventType, DOMUtilsEventListenerOption, DOMUtilsEventListenerOptionsAttribute } from "./types/DOMUtilsEvent";
|
|
2
2
|
import type { DOMUtilsTargetElementType } from "./types/global";
|
|
3
3
|
import type { WindowApiOption } from "./types/WindowApi";
|
|
4
4
|
import { WindowApi } from "./WindowApi";
|
|
@@ -23,7 +23,7 @@ export declare class DOMUtilsEvent {
|
|
|
23
23
|
* console.log("事件触发",event)
|
|
24
24
|
* })
|
|
25
25
|
*/
|
|
26
|
-
on<T extends DOMUtils_EventType>(element: DOMUtilsElementEventType, eventType: T | T[], callback: (event: DOMUtils_Event[T]) => void, option?:
|
|
26
|
+
on<T extends DOMUtils_EventType>(element: DOMUtilsElementEventType, eventType: T | T[], callback: (this: HTMLElement, event: DOMUtils_Event[T]) => void, option?: DOMUtilsEventListenerOption | boolean): void;
|
|
27
27
|
/**
|
|
28
28
|
* 绑定事件
|
|
29
29
|
* @param element 需要绑定的元素|元素数组|window
|
|
@@ -42,7 +42,7 @@ export declare class DOMUtilsEvent {
|
|
|
42
42
|
* console.log("事件触发",event)
|
|
43
43
|
* })
|
|
44
44
|
*/
|
|
45
|
-
on<T extends Event>(element: DOMUtilsElementEventType, eventType: string, callback: (event: T) => void, option?:
|
|
45
|
+
on<T extends Event>(element: DOMUtilsElementEventType, eventType: string, callback: (this: HTMLElement, event: T) => void, option?: DOMUtilsEventListenerOption | boolean): void;
|
|
46
46
|
/**
|
|
47
47
|
* 绑定事件
|
|
48
48
|
* @param element 需要绑定的元素|元素数组|window
|
|
@@ -67,7 +67,7 @@ export declare class DOMUtilsEvent {
|
|
|
67
67
|
* console.log("事件触发",event)
|
|
68
68
|
* })
|
|
69
69
|
*/
|
|
70
|
-
on<T extends DOMUtils_EventType>(element: DOMUtilsElementEventType, eventType: T | T[], selector: string | string[] | undefined | null, callback: (event: DOMUtils_Event[T]) => void, option?:
|
|
70
|
+
on<T extends DOMUtils_EventType>(element: DOMUtilsElementEventType, eventType: T | T[], selector: string | string[] | undefined | null, callback: (this: HTMLElement, event: DOMUtils_Event[T]) => void, option?: DOMUtilsEventListenerOption | boolean): void;
|
|
71
71
|
/**
|
|
72
72
|
* 绑定事件
|
|
73
73
|
* @param element 需要绑定的元素|元素数组|window
|
|
@@ -92,7 +92,7 @@ export declare class DOMUtilsEvent {
|
|
|
92
92
|
* console.log("事件触发",event)
|
|
93
93
|
* })
|
|
94
94
|
*/
|
|
95
|
-
on<T extends Event>(element: DOMUtilsElementEventType, eventType: string, selector: string | string[] | (() => string | string[]) | undefined | null, callback: (event: T) => void, option?:
|
|
95
|
+
on<T extends Event>(element: DOMUtilsElementEventType, eventType: string, selector: string | string[] | (() => string | string[]) | undefined | null, callback: (this: HTMLElement, event: T) => void, option?: DOMUtilsEventListenerOption | boolean): void;
|
|
96
96
|
/**
|
|
97
97
|
* 取消绑定事件
|
|
98
98
|
* @param element 需要取消绑定的元素|元素数组
|
|
@@ -102,11 +102,11 @@ export declare class DOMUtilsEvent {
|
|
|
102
102
|
* + capture 如果在添加事件监听器时指定了useCapture为true,则在移除事件监听器时也必须指定为true
|
|
103
103
|
* @param filter (可选)过滤函数,对元素属性上的事件进行过滤出想要删除的事件
|
|
104
104
|
* @example
|
|
105
|
-
* // 取消监听元素a.xx
|
|
105
|
+
* // 取消监听元素a.xx所有的click事件
|
|
106
106
|
* DOMUtils.off(document.querySelector("a.xx"),"click")
|
|
107
107
|
* DOMUtils.off("a.xx","click")
|
|
108
108
|
*/
|
|
109
|
-
off<T extends DOMUtils_EventType>(element: DOMUtilsElementEventType, eventType: T | T[], callback?: (event: DOMUtils_Event[T]) => void, option?: boolean |
|
|
109
|
+
off<T extends DOMUtils_EventType>(element: DOMUtilsElementEventType, eventType: T | T[], callback?: (this: HTMLElement, event: DOMUtils_Event[T]) => void, option?: boolean | EventListenerOptions, filter?: (value: DOMUtilsEventListenerOptionsAttribute, index: number, array: DOMUtilsEventListenerOptionsAttribute[]) => boolean): void;
|
|
110
110
|
/**
|
|
111
111
|
* 取消绑定事件
|
|
112
112
|
* @param element 需要取消绑定的元素|元素数组
|
|
@@ -120,7 +120,7 @@ export declare class DOMUtilsEvent {
|
|
|
120
120
|
* DOMUtils.off(document.querySelector("a.xx"),"click")
|
|
121
121
|
* DOMUtils.off("a.xx","click")
|
|
122
122
|
*/
|
|
123
|
-
off<T extends Event>(element: DOMUtilsElementEventType, eventType: string, callback?: (event: T) => void, option?: boolean |
|
|
123
|
+
off<T extends Event>(element: DOMUtilsElementEventType, eventType: string, callback?: (this: HTMLElement, event: T) => void, option?: boolean | EventListenerOptions, filter?: (value: DOMUtilsEventListenerOptionsAttribute, index: number, array: DOMUtilsEventListenerOptionsAttribute[]) => boolean): void;
|
|
124
124
|
/**
|
|
125
125
|
* 取消绑定事件
|
|
126
126
|
* @param element 需要取消绑定的元素|元素数组
|
|
@@ -135,7 +135,7 @@ export declare class DOMUtilsEvent {
|
|
|
135
135
|
* DOMUtils.off(document.querySelector("a.xx"),"click tap hover")
|
|
136
136
|
* DOMUtils.off("a.xx",["click","tap","hover"])
|
|
137
137
|
*/
|
|
138
|
-
off<T extends DOMUtils_EventType>(element: DOMUtilsElementEventType, eventType: T | T[], selector?: DOMUtilsEventListenerOptionsAttribute["selector"] | undefined, callback?: (event: DOMUtils_Event[T]) => void, option?: boolean |
|
|
138
|
+
off<T extends DOMUtils_EventType>(element: DOMUtilsElementEventType, eventType: T | T[], selector?: DOMUtilsEventListenerOptionsAttribute["selector"] | undefined, callback?: (this: HTMLElement, event: DOMUtils_Event[T]) => void, option?: boolean | EventListenerOptions, filter?: (value: DOMUtilsEventListenerOptionsAttribute, index: number, array: DOMUtilsEventListenerOptionsAttribute[]) => boolean): void;
|
|
139
139
|
/**
|
|
140
140
|
* 取消绑定事件
|
|
141
141
|
* @param element 需要取消绑定的元素|元素数组
|
|
@@ -150,7 +150,7 @@ export declare class DOMUtilsEvent {
|
|
|
150
150
|
* DOMUtils.off(document.querySelector("a.xx"),"click tap hover")
|
|
151
151
|
* DOMUtils.off("a.xx",["click","tap","hover"])
|
|
152
152
|
*/
|
|
153
|
-
off<T extends Event>(element: DOMUtilsElementEventType, eventType: string, selector?: DOMUtilsEventListenerOptionsAttribute["selector"] | undefined, callback?: (event: T) => void, option?: boolean |
|
|
153
|
+
off<T extends Event>(element: DOMUtilsElementEventType, eventType: string, selector?: DOMUtilsEventListenerOptionsAttribute["selector"] | undefined, callback?: (this: HTMLElement, event: T) => void, option?: boolean | EventListenerOptions, filter?: (value: DOMUtilsEventListenerOptionsAttribute, index: number, array: DOMUtilsEventListenerOptionsAttribute[]) => boolean): void;
|
|
154
154
|
/**
|
|
155
155
|
* 取消绑定所有的事件
|
|
156
156
|
* @param element 需要取消绑定的元素|元素数组
|
|
@@ -216,7 +216,7 @@ export declare class DOMUtilsEvent {
|
|
|
216
216
|
* console.log("触发click事件成功")
|
|
217
217
|
* })
|
|
218
218
|
* */
|
|
219
|
-
click(element: DOMUtilsTargetElementType | typeof globalThis | Window, handler?: (event: DOMUtils_Event["click"]) => void, details?: any, useDispatchToTriggerEvent?: boolean): void;
|
|
219
|
+
click(element: DOMUtilsTargetElementType | typeof globalThis | Window, handler?: (this: HTMLElement, event: DOMUtils_Event["click"]) => void, details?: any, useDispatchToTriggerEvent?: boolean): void;
|
|
220
220
|
/**
|
|
221
221
|
* 绑定或触发元素的blur事件
|
|
222
222
|
* @param element 目标元素
|
|
@@ -231,7 +231,7 @@ export declare class DOMUtilsEvent {
|
|
|
231
231
|
* console.log("触发blur事件成功")
|
|
232
232
|
* })
|
|
233
233
|
* */
|
|
234
|
-
blur(element: DOMUtilsTargetElementType | typeof globalThis | Window, handler?: (event: DOMUtils_Event["blur"]) => void, details?: object, useDispatchToTriggerEvent?: boolean): void;
|
|
234
|
+
blur(element: DOMUtilsTargetElementType | typeof globalThis | Window, handler?: (this: HTMLElement, event: DOMUtils_Event["blur"]) => void, details?: object, useDispatchToTriggerEvent?: boolean): void;
|
|
235
235
|
/**
|
|
236
236
|
* 绑定或触发元素的focus事件
|
|
237
237
|
* @param element 目标元素
|
|
@@ -246,7 +246,7 @@ export declare class DOMUtilsEvent {
|
|
|
246
246
|
* console.log("触发focus事件成功")
|
|
247
247
|
* })
|
|
248
248
|
* */
|
|
249
|
-
focus(element: DOMUtilsTargetElementType | typeof globalThis | Window, handler?: (event: DOMUtils_Event["focus"]) => void, details?: object, useDispatchToTriggerEvent?: boolean): void;
|
|
249
|
+
focus(element: DOMUtilsTargetElementType | typeof globalThis | Window, handler?: (this: HTMLElement, event: DOMUtils_Event["focus"]) => void, details?: object, useDispatchToTriggerEvent?: boolean): void;
|
|
250
250
|
/**
|
|
251
251
|
* 当鼠标移入或移出元素时触发事件
|
|
252
252
|
* @param element 当前元素
|
|
@@ -261,7 +261,7 @@ export declare class DOMUtilsEvent {
|
|
|
261
261
|
* console.log("移入/移除");
|
|
262
262
|
* })
|
|
263
263
|
*/
|
|
264
|
-
hover(element: DOMUtilsTargetElementType, handler: (event: DOMUtils_Event["hover"]) => void, option?: boolean |
|
|
264
|
+
hover(element: DOMUtilsTargetElementType, handler: (this: HTMLElement, event: DOMUtils_Event["hover"]) => void, option?: boolean | DOMUtilsEventListenerOption): void;
|
|
265
265
|
/**
|
|
266
266
|
* 当按键松开时触发事件
|
|
267
267
|
* keydown - > keypress - > keyup
|
|
@@ -277,7 +277,7 @@ export declare class DOMUtilsEvent {
|
|
|
277
277
|
* console.log("按键松开");
|
|
278
278
|
* })
|
|
279
279
|
*/
|
|
280
|
-
keyup(element: DOMUtilsTargetElementType | Window | typeof globalThis, handler: (event: DOMUtils_Event["keyup"]) => void, option?: boolean |
|
|
280
|
+
keyup(element: DOMUtilsTargetElementType | Window | typeof globalThis, handler: (this: HTMLElement, event: DOMUtils_Event["keyup"]) => void, option?: boolean | DOMUtilsEventListenerOption): void;
|
|
281
281
|
/**
|
|
282
282
|
* 当按键按下时触发事件
|
|
283
283
|
* keydown - > keypress - > keyup
|
|
@@ -293,7 +293,7 @@ export declare class DOMUtilsEvent {
|
|
|
293
293
|
* console.log("按键按下");
|
|
294
294
|
* })
|
|
295
295
|
*/
|
|
296
|
-
keydown(element: DOMUtilsTargetElementType | Window | typeof globalThis, handler: (event: DOMUtils_Event["keydown"]) => void, option?: boolean |
|
|
296
|
+
keydown(element: DOMUtilsTargetElementType | Window | typeof globalThis, handler: (this: HTMLElement, event: DOMUtils_Event["keydown"]) => void, option?: boolean | DOMUtilsEventListenerOption): void;
|
|
297
297
|
/**
|
|
298
298
|
* 当按键按下时触发事件
|
|
299
299
|
* keydown - > keypress - > keyup
|
|
@@ -309,7 +309,7 @@ export declare class DOMUtilsEvent {
|
|
|
309
309
|
* console.log("按键按下");
|
|
310
310
|
* })
|
|
311
311
|
*/
|
|
312
|
-
keypress(element: DOMUtilsTargetElementType | Window | typeof globalThis, handler: (event: DOMUtils_Event["keypress"]) => void, option?: boolean |
|
|
312
|
+
keypress(element: DOMUtilsTargetElementType | Window | typeof globalThis, handler: (this: HTMLElement, event: DOMUtils_Event["keypress"]) => void, option?: boolean | DOMUtilsEventListenerOption): void;
|
|
313
313
|
/**
|
|
314
314
|
* 监听某个元素键盘按键事件或window全局按键事件
|
|
315
315
|
* 按下有值的键时触发,按下Ctrl\Alt\Shift\Meta是无值键。按下先触发keydown事件,再触发keypress事件。
|
|
@@ -373,7 +373,7 @@ export declare class DOMUtilsEvent {
|
|
|
373
373
|
搜索 170
|
|
374
374
|
收藏 171
|
|
375
375
|
**/
|
|
376
|
-
listenKeyboard(element: DOMUtilsTargetElementType | Window | Node | typeof globalThis, eventName:
|
|
376
|
+
listenKeyboard(element: DOMUtilsTargetElementType | Window | Node | typeof globalThis, eventName: "keyup" | "keypress" | "keydown" | undefined, callback: (keyName: string, keyValue: number, otherCodeList: string[], event: KeyboardEvent) => void, options?: DOMUtilsEventListenerOption | boolean): {
|
|
377
377
|
removeListen(): void;
|
|
378
378
|
};
|
|
379
379
|
/**
|
|
@@ -211,7 +211,7 @@ export declare interface DOMUtilsEventListenerOptionsAttribute {
|
|
|
211
211
|
/**
|
|
212
212
|
* 属性配置
|
|
213
213
|
*/
|
|
214
|
-
option:
|
|
214
|
+
option: DOMUtilsEventListenerOption;
|
|
215
215
|
/**
|
|
216
216
|
* 用户添加的事件
|
|
217
217
|
*/
|
|
@@ -222,6 +222,17 @@ export declare interface DOMUtilsEventListenerOptionsAttribute {
|
|
|
222
222
|
selector?: string[];
|
|
223
223
|
}
|
|
224
224
|
|
|
225
|
+
/**
|
|
226
|
+
* 事件的额外配置
|
|
227
|
+
*/
|
|
228
|
+
export declare type DOMUtilsEventListenerOption = AddEventListenerOptions & {
|
|
229
|
+
/**
|
|
230
|
+
* 是否使用 event.composedPath() 来代替 event.target
|
|
231
|
+
*
|
|
232
|
+
* 一般用于设置了selector参数
|
|
233
|
+
*/
|
|
234
|
+
isComposedPath?: boolean;
|
|
235
|
+
};
|
|
225
236
|
export declare type DOMUtilsElementEventType =
|
|
226
237
|
| HTMLElement
|
|
227
238
|
| string
|
package/package.json
CHANGED
package/src/DOMUtilsEvent.ts
CHANGED
|
@@ -6,6 +6,7 @@ import type {
|
|
|
6
6
|
DOMUtils_Event,
|
|
7
7
|
DOMUtils_EventType,
|
|
8
8
|
DOMUtilsElementEventType,
|
|
9
|
+
DOMUtilsEventListenerOption,
|
|
9
10
|
DOMUtilsEventListenerOptionsAttribute,
|
|
10
11
|
} from "./types/DOMUtilsEvent";
|
|
11
12
|
import type { DOMUtilsTargetElementType } from "./types/global";
|
|
@@ -38,8 +39,8 @@ export class DOMUtilsEvent {
|
|
|
38
39
|
on<T extends DOMUtils_EventType>(
|
|
39
40
|
element: DOMUtilsElementEventType,
|
|
40
41
|
eventType: T | T[],
|
|
41
|
-
callback: (event: DOMUtils_Event[T]) => void,
|
|
42
|
-
option?:
|
|
42
|
+
callback: (this: HTMLElement, event: DOMUtils_Event[T]) => void,
|
|
43
|
+
option?: DOMUtilsEventListenerOption | boolean
|
|
43
44
|
): void;
|
|
44
45
|
/**
|
|
45
46
|
* 绑定事件
|
|
@@ -62,8 +63,8 @@ export class DOMUtilsEvent {
|
|
|
62
63
|
on<T extends Event>(
|
|
63
64
|
element: DOMUtilsElementEventType,
|
|
64
65
|
eventType: string,
|
|
65
|
-
callback: (event: T) => void,
|
|
66
|
-
option?:
|
|
66
|
+
callback: (this: HTMLElement, event: T) => void,
|
|
67
|
+
option?: DOMUtilsEventListenerOption | boolean
|
|
67
68
|
): void;
|
|
68
69
|
/**
|
|
69
70
|
* 绑定事件
|
|
@@ -93,8 +94,8 @@ export class DOMUtilsEvent {
|
|
|
93
94
|
element: DOMUtilsElementEventType,
|
|
94
95
|
eventType: T | T[],
|
|
95
96
|
selector: string | string[] | undefined | null,
|
|
96
|
-
callback: (event: DOMUtils_Event[T]) => void,
|
|
97
|
-
option?:
|
|
97
|
+
callback: (this: HTMLElement, event: DOMUtils_Event[T]) => void,
|
|
98
|
+
option?: DOMUtilsEventListenerOption | boolean
|
|
98
99
|
): void;
|
|
99
100
|
/**
|
|
100
101
|
* 绑定事件
|
|
@@ -124,8 +125,8 @@ export class DOMUtilsEvent {
|
|
|
124
125
|
element: DOMUtilsElementEventType,
|
|
125
126
|
eventType: string,
|
|
126
127
|
selector: string | string[] | (() => string | string[]) | undefined | null,
|
|
127
|
-
callback: (event: T) => void,
|
|
128
|
-
option?:
|
|
128
|
+
callback: (this: HTMLElement, event: T) => void,
|
|
129
|
+
option?: DOMUtilsEventListenerOption | boolean
|
|
129
130
|
): void;
|
|
130
131
|
on<T extends Event>(
|
|
131
132
|
element:
|
|
@@ -139,9 +140,17 @@ export class DOMUtilsEvent {
|
|
|
139
140
|
| null
|
|
140
141
|
| typeof globalThis,
|
|
141
142
|
eventType: DOMUtils_EventType | DOMUtils_EventType[] | string,
|
|
142
|
-
selector:
|
|
143
|
-
|
|
144
|
-
|
|
143
|
+
selector:
|
|
144
|
+
| string
|
|
145
|
+
| undefined
|
|
146
|
+
| string[]
|
|
147
|
+
| ((this: HTMLElement, event: T) => void)
|
|
148
|
+
| null,
|
|
149
|
+
callback?:
|
|
150
|
+
| ((this: HTMLElement, event: T) => void)
|
|
151
|
+
| DOMUtilsEventListenerOption
|
|
152
|
+
| boolean,
|
|
153
|
+
option?: DOMUtilsEventListenerOption | boolean
|
|
145
154
|
) {
|
|
146
155
|
/**
|
|
147
156
|
* 获取option配置
|
|
@@ -152,10 +161,11 @@ export class DOMUtilsEvent {
|
|
|
152
161
|
function getOption(
|
|
153
162
|
args: IArguments,
|
|
154
163
|
startIndex: number,
|
|
155
|
-
option:
|
|
164
|
+
option: DOMUtilsEventListenerOption
|
|
156
165
|
) {
|
|
157
|
-
|
|
158
|
-
|
|
166
|
+
let currentParam = args[startIndex];
|
|
167
|
+
if (typeof currentParam === "boolean") {
|
|
168
|
+
option.capture = currentParam;
|
|
159
169
|
if (typeof args[startIndex + 1] === "boolean") {
|
|
160
170
|
option.once = args[startIndex + 1];
|
|
161
171
|
}
|
|
@@ -163,14 +173,16 @@ export class DOMUtilsEvent {
|
|
|
163
173
|
option.passive = args[startIndex + 2];
|
|
164
174
|
}
|
|
165
175
|
} else if (
|
|
166
|
-
typeof
|
|
167
|
-
("capture" in
|
|
168
|
-
"once" in
|
|
169
|
-
"passive" in
|
|
176
|
+
typeof currentParam === "object" &&
|
|
177
|
+
("capture" in currentParam ||
|
|
178
|
+
"once" in currentParam ||
|
|
179
|
+
"passive" in currentParam ||
|
|
180
|
+
"isComposedPath" in currentParam)
|
|
170
181
|
) {
|
|
171
|
-
option.capture =
|
|
172
|
-
option.once =
|
|
173
|
-
option.passive =
|
|
182
|
+
option.capture = currentParam.capture;
|
|
183
|
+
option.once = currentParam.once;
|
|
184
|
+
option.passive = currentParam.passive;
|
|
185
|
+
option.isComposedPath = currentParam.isComposedPath;
|
|
174
186
|
}
|
|
175
187
|
return option;
|
|
176
188
|
}
|
|
@@ -205,26 +217,28 @@ export class DOMUtilsEvent {
|
|
|
205
217
|
selectorList.push(selector);
|
|
206
218
|
}
|
|
207
219
|
// 事件回调
|
|
208
|
-
let
|
|
220
|
+
let listenerCallBack: (this: HTMLElement, event: Event) => void =
|
|
221
|
+
callback as any;
|
|
209
222
|
// 事件配置
|
|
210
|
-
let
|
|
223
|
+
let listenerOption: DOMUtilsEventListenerOption = {
|
|
211
224
|
capture: false,
|
|
212
225
|
once: false,
|
|
213
226
|
passive: false,
|
|
227
|
+
isComposedPath: false,
|
|
214
228
|
};
|
|
215
229
|
if (typeof selector === "function") {
|
|
216
230
|
/* 这是为没有selector的情况 */
|
|
217
|
-
|
|
218
|
-
|
|
231
|
+
listenerCallBack = selector as any;
|
|
232
|
+
listenerOption = getOption(args, 3, listenerOption);
|
|
219
233
|
} else {
|
|
220
234
|
/* 这是存在selector的情况 */
|
|
221
|
-
|
|
235
|
+
listenerOption = getOption(args, 4, listenerOption);
|
|
222
236
|
}
|
|
223
237
|
/**
|
|
224
238
|
* 如果是once,那么删除该监听和元素上的事件和监听
|
|
225
239
|
*/
|
|
226
240
|
function checkOptionOnceToRemoveEventListener() {
|
|
227
|
-
if (
|
|
241
|
+
if (listenerOption.once) {
|
|
228
242
|
DOMUtilsContext.off(
|
|
229
243
|
element,
|
|
230
244
|
eventType as any,
|
|
@@ -240,38 +254,41 @@ export class DOMUtilsEvent {
|
|
|
240
254
|
* @param event
|
|
241
255
|
*/
|
|
242
256
|
function domUtilsEventCallBack(event: Event) {
|
|
243
|
-
let
|
|
257
|
+
let eventTarget = listenerOption.isComposedPath
|
|
258
|
+
? (event.composedPath()[0] as HTMLElement)
|
|
259
|
+
: (event.target as HTMLElement);
|
|
244
260
|
if (selectorList.length) {
|
|
245
|
-
/*
|
|
261
|
+
/* 存在子元素选择器 */
|
|
246
262
|
let totalParent = DOMUtilsCommonUtils.isWin(elementItem)
|
|
247
263
|
? DOMUtilsContext.windowApi.document.documentElement
|
|
248
264
|
: elementItem;
|
|
249
265
|
for (let index = 0; index < selectorList.length; index++) {
|
|
250
266
|
const selectorItem = selectorList[index];
|
|
251
|
-
if (
|
|
267
|
+
if (eventTarget.matches(selectorItem)) {
|
|
252
268
|
/* 当前目标可以被selector所匹配到 */
|
|
253
|
-
|
|
269
|
+
listenerCallBack.call(eventTarget, event as any);
|
|
254
270
|
checkOptionOnceToRemoveEventListener();
|
|
255
271
|
break;
|
|
256
|
-
} else
|
|
257
|
-
target.closest(selectorItem) &&
|
|
258
|
-
totalParent.contains(target.closest(selectorItem))
|
|
259
|
-
) {
|
|
272
|
+
} else {
|
|
260
273
|
/* 在上层与主元素之间寻找可以被selector所匹配到的 */
|
|
261
|
-
let
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
274
|
+
let $closestMatches = eventTarget.closest(
|
|
275
|
+
selectorItem
|
|
276
|
+
) as HTMLElement | null;
|
|
277
|
+
if ($closestMatches && totalParent.contains($closestMatches)) {
|
|
278
|
+
/* event的target值不能直接修改 */
|
|
279
|
+
OriginPrototype.Object.defineProperty(event, "target", {
|
|
280
|
+
get() {
|
|
281
|
+
return $closestMatches;
|
|
282
|
+
},
|
|
283
|
+
});
|
|
284
|
+
listenerCallBack.call($closestMatches, event as any);
|
|
285
|
+
checkOptionOnceToRemoveEventListener();
|
|
286
|
+
break;
|
|
287
|
+
}
|
|
271
288
|
}
|
|
272
289
|
}
|
|
273
290
|
} else {
|
|
274
|
-
|
|
291
|
+
listenerCallBack.call(elementItem, event as any);
|
|
275
292
|
checkOptionOnceToRemoveEventListener();
|
|
276
293
|
}
|
|
277
294
|
}
|
|
@@ -281,7 +298,7 @@ export class DOMUtilsEvent {
|
|
|
281
298
|
elementItem.addEventListener(
|
|
282
299
|
eventName,
|
|
283
300
|
domUtilsEventCallBack,
|
|
284
|
-
|
|
301
|
+
listenerOption
|
|
285
302
|
);
|
|
286
303
|
/* 获取对象上的事件 */
|
|
287
304
|
let elementEvents: {
|
|
@@ -292,9 +309,9 @@ export class DOMUtilsEvent {
|
|
|
292
309
|
elementEvents[eventName] = elementEvents[eventName] || [];
|
|
293
310
|
elementEvents[eventName].push({
|
|
294
311
|
selector: selectorList,
|
|
295
|
-
option:
|
|
312
|
+
option: listenerOption,
|
|
296
313
|
callback: domUtilsEventCallBack,
|
|
297
|
-
originCallBack:
|
|
314
|
+
originCallBack: listenerCallBack,
|
|
298
315
|
});
|
|
299
316
|
/* 覆盖事件 */
|
|
300
317
|
// @ts-ignore
|
|
@@ -311,15 +328,15 @@ export class DOMUtilsEvent {
|
|
|
311
328
|
* + capture 如果在添加事件监听器时指定了useCapture为true,则在移除事件监听器时也必须指定为true
|
|
312
329
|
* @param filter (可选)过滤函数,对元素属性上的事件进行过滤出想要删除的事件
|
|
313
330
|
* @example
|
|
314
|
-
* // 取消监听元素a.xx
|
|
331
|
+
* // 取消监听元素a.xx所有的click事件
|
|
315
332
|
* DOMUtils.off(document.querySelector("a.xx"),"click")
|
|
316
333
|
* DOMUtils.off("a.xx","click")
|
|
317
334
|
*/
|
|
318
335
|
off<T extends DOMUtils_EventType>(
|
|
319
336
|
element: DOMUtilsElementEventType,
|
|
320
337
|
eventType: T | T[],
|
|
321
|
-
callback?: (event: DOMUtils_Event[T]) => void,
|
|
322
|
-
option?: boolean |
|
|
338
|
+
callback?: (this: HTMLElement, event: DOMUtils_Event[T]) => void,
|
|
339
|
+
option?: boolean | EventListenerOptions,
|
|
323
340
|
filter?: (
|
|
324
341
|
value: DOMUtilsEventListenerOptionsAttribute,
|
|
325
342
|
index: number,
|
|
@@ -342,8 +359,8 @@ export class DOMUtilsEvent {
|
|
|
342
359
|
off<T extends Event>(
|
|
343
360
|
element: DOMUtilsElementEventType,
|
|
344
361
|
eventType: string,
|
|
345
|
-
callback?: (event: T) => void,
|
|
346
|
-
option?: boolean |
|
|
362
|
+
callback?: (this: HTMLElement, event: T) => void,
|
|
363
|
+
option?: boolean | EventListenerOptions,
|
|
347
364
|
filter?: (
|
|
348
365
|
value: DOMUtilsEventListenerOptionsAttribute,
|
|
349
366
|
index: number,
|
|
@@ -368,8 +385,8 @@ export class DOMUtilsEvent {
|
|
|
368
385
|
element: DOMUtilsElementEventType,
|
|
369
386
|
eventType: T | T[],
|
|
370
387
|
selector?: DOMUtilsEventListenerOptionsAttribute["selector"] | undefined,
|
|
371
|
-
callback?: (event: DOMUtils_Event[T]) => void,
|
|
372
|
-
option?: boolean |
|
|
388
|
+
callback?: (this: HTMLElement, event: DOMUtils_Event[T]) => void,
|
|
389
|
+
option?: boolean | EventListenerOptions,
|
|
373
390
|
filter?: (
|
|
374
391
|
value: DOMUtilsEventListenerOptionsAttribute,
|
|
375
392
|
index: number,
|
|
@@ -394,8 +411,8 @@ export class DOMUtilsEvent {
|
|
|
394
411
|
element: DOMUtilsElementEventType,
|
|
395
412
|
eventType: string,
|
|
396
413
|
selector?: DOMUtilsEventListenerOptionsAttribute["selector"] | undefined,
|
|
397
|
-
callback?: (event: T) => void,
|
|
398
|
-
option?: boolean |
|
|
414
|
+
callback?: (this: HTMLElement, event: T) => void,
|
|
415
|
+
option?: boolean | EventListenerOptions,
|
|
399
416
|
filter?: (
|
|
400
417
|
value: DOMUtilsEventListenerOptionsAttribute,
|
|
401
418
|
index: number,
|
|
@@ -417,11 +434,14 @@ export class DOMUtilsEvent {
|
|
|
417
434
|
selector?:
|
|
418
435
|
| DOMUtilsEventListenerOptionsAttribute["selector"]
|
|
419
436
|
| undefined
|
|
420
|
-
| ((event: T) => void),
|
|
421
|
-
callback?:
|
|
437
|
+
| ((this: HTMLElement, event: T) => void),
|
|
438
|
+
callback?:
|
|
439
|
+
| ((this: HTMLElement, event: T) => void)
|
|
440
|
+
| boolean
|
|
441
|
+
| EventListenerOptions,
|
|
422
442
|
option?:
|
|
423
443
|
| boolean
|
|
424
|
-
|
|
|
444
|
+
| EventListenerOptions
|
|
425
445
|
| ((
|
|
426
446
|
value: DOMUtilsEventListenerOptionsAttribute,
|
|
427
447
|
index: number,
|
|
@@ -444,13 +464,14 @@ export class DOMUtilsEvent {
|
|
|
444
464
|
startIndex: number,
|
|
445
465
|
option: EventListenerOptions
|
|
446
466
|
) {
|
|
447
|
-
|
|
448
|
-
|
|
467
|
+
let currentParam: boolean | EventListenerOptions = args1[startIndex];
|
|
468
|
+
if (typeof currentParam === "boolean") {
|
|
469
|
+
option.capture = currentParam;
|
|
449
470
|
} else if (
|
|
450
|
-
typeof
|
|
451
|
-
"capture" in
|
|
471
|
+
typeof currentParam === "object" &&
|
|
472
|
+
"capture" in currentParam
|
|
452
473
|
) {
|
|
453
|
-
option.capture =
|
|
474
|
+
option.capture = currentParam.capture;
|
|
454
475
|
}
|
|
455
476
|
return option;
|
|
456
477
|
}
|
|
@@ -485,20 +506,33 @@ export class DOMUtilsEvent {
|
|
|
485
506
|
/**
|
|
486
507
|
* 事件的回调函数
|
|
487
508
|
*/
|
|
488
|
-
let
|
|
509
|
+
let listenerCallBack: (this: HTMLElement, event: T) => void =
|
|
510
|
+
callback as any;
|
|
489
511
|
|
|
490
512
|
/**
|
|
491
513
|
* 事件的配置
|
|
492
514
|
*/
|
|
493
|
-
let
|
|
515
|
+
let listenerOption: EventListenerOptions = {
|
|
494
516
|
capture: false,
|
|
495
517
|
};
|
|
496
518
|
if (typeof selector === "function") {
|
|
497
519
|
/* 这是为没有selector的情况 */
|
|
498
|
-
|
|
499
|
-
|
|
520
|
+
listenerCallBack = selector;
|
|
521
|
+
listenerOption = getOption(args, 3, listenerOption);
|
|
500
522
|
} else {
|
|
501
|
-
|
|
523
|
+
listenerOption = getOption(args, 4, listenerOption);
|
|
524
|
+
}
|
|
525
|
+
// 是否移除所有事件
|
|
526
|
+
let isRemoveAll = false;
|
|
527
|
+
if (args.length === 2) {
|
|
528
|
+
// 目标函数、事件名
|
|
529
|
+
isRemoveAll = true;
|
|
530
|
+
} else if (
|
|
531
|
+
(args.length === 3 && typeof args[2] === "string") ||
|
|
532
|
+
Array.isArray(args[2])
|
|
533
|
+
) {
|
|
534
|
+
// 目标函数、事件名、子元素选择器
|
|
535
|
+
isRemoveAll = true;
|
|
502
536
|
}
|
|
503
537
|
elementList.forEach((elementItem) => {
|
|
504
538
|
/* 获取对象上的事件 */
|
|
@@ -512,34 +546,32 @@ export class DOMUtilsEvent {
|
|
|
512
546
|
}
|
|
513
547
|
for (let index = 0; index < handlers.length; index++) {
|
|
514
548
|
let handler = handlers[index];
|
|
515
|
-
let flag =
|
|
516
|
-
if (
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
549
|
+
let flag = true;
|
|
550
|
+
if (
|
|
551
|
+
flag &&
|
|
552
|
+
listenerCallBack &&
|
|
553
|
+
handler.originCallBack !== listenerCallBack
|
|
554
|
+
) {
|
|
555
|
+
// callback不同
|
|
556
|
+
flag = false;
|
|
557
|
+
}
|
|
558
|
+
if (flag && selectorList.length && Array.isArray(handler.selector)) {
|
|
520
559
|
if (
|
|
521
|
-
|
|
522
|
-
JSON.stringify(handler.selector) === JSON.stringify(selectorList)
|
|
560
|
+
JSON.stringify(handler.selector) !== JSON.stringify(selectorList)
|
|
523
561
|
) {
|
|
524
|
-
//
|
|
525
|
-
flag =
|
|
562
|
+
// 子元素选择器不同
|
|
563
|
+
flag = false;
|
|
526
564
|
}
|
|
527
565
|
}
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
handler.callback === _callback_ ||
|
|
532
|
-
handler.originCallBack === _callback_
|
|
533
|
-
) {
|
|
534
|
-
/* callback不为空,且callback相同 */
|
|
535
|
-
flag = true;
|
|
566
|
+
if (flag && listenerOption.capture !== handler.option.capture) {
|
|
567
|
+
// 事件的配置项不同
|
|
568
|
+
flag = false;
|
|
536
569
|
}
|
|
537
|
-
|
|
538
|
-
if (flag) {
|
|
570
|
+
if (flag || isRemoveAll) {
|
|
539
571
|
elementItem.removeEventListener(
|
|
540
572
|
eventName,
|
|
541
573
|
handler.callback,
|
|
542
|
-
|
|
574
|
+
handler.option
|
|
543
575
|
);
|
|
544
576
|
handlers.splice(index--, 1);
|
|
545
577
|
}
|
|
@@ -830,7 +862,7 @@ export class DOMUtilsEvent {
|
|
|
830
862
|
* */
|
|
831
863
|
click(
|
|
832
864
|
element: DOMUtilsTargetElementType | typeof globalThis | Window,
|
|
833
|
-
handler?: (event: DOMUtils_Event["click"]) => void,
|
|
865
|
+
handler?: (this: HTMLElement, event: DOMUtils_Event["click"]) => void,
|
|
834
866
|
details?: any,
|
|
835
867
|
useDispatchToTriggerEvent?: boolean
|
|
836
868
|
) {
|
|
@@ -880,7 +912,7 @@ export class DOMUtilsEvent {
|
|
|
880
912
|
* */
|
|
881
913
|
blur(
|
|
882
914
|
element: DOMUtilsTargetElementType | typeof globalThis | Window,
|
|
883
|
-
handler?: (event: DOMUtils_Event["blur"]) => void,
|
|
915
|
+
handler?: (this: HTMLElement, event: DOMUtils_Event["blur"]) => void,
|
|
884
916
|
details?: object,
|
|
885
917
|
useDispatchToTriggerEvent?: boolean
|
|
886
918
|
) {
|
|
@@ -935,7 +967,7 @@ export class DOMUtilsEvent {
|
|
|
935
967
|
* */
|
|
936
968
|
focus(
|
|
937
969
|
element: DOMUtilsTargetElementType | typeof globalThis | Window,
|
|
938
|
-
handler?: (event: DOMUtils_Event["focus"]) => void,
|
|
970
|
+
handler?: (this: HTMLElement, event: DOMUtils_Event["focus"]) => void,
|
|
939
971
|
details?: object,
|
|
940
972
|
useDispatchToTriggerEvent?: boolean
|
|
941
973
|
) {
|
|
@@ -985,8 +1017,8 @@ export class DOMUtilsEvent {
|
|
|
985
1017
|
*/
|
|
986
1018
|
hover(
|
|
987
1019
|
element: DOMUtilsTargetElementType,
|
|
988
|
-
handler: (event: DOMUtils_Event["hover"]) => void,
|
|
989
|
-
option?: boolean |
|
|
1020
|
+
handler: (this: HTMLElement, event: DOMUtils_Event["hover"]) => void,
|
|
1021
|
+
option?: boolean | DOMUtilsEventListenerOption
|
|
990
1022
|
) {
|
|
991
1023
|
let DOMUtilsContext = this;
|
|
992
1024
|
if (typeof element === "string") {
|
|
@@ -1022,8 +1054,8 @@ export class DOMUtilsEvent {
|
|
|
1022
1054
|
*/
|
|
1023
1055
|
keyup(
|
|
1024
1056
|
element: DOMUtilsTargetElementType | Window | typeof globalThis,
|
|
1025
|
-
handler: (event: DOMUtils_Event["keyup"]) => void,
|
|
1026
|
-
option?: boolean |
|
|
1057
|
+
handler: (this: HTMLElement, event: DOMUtils_Event["keyup"]) => void,
|
|
1058
|
+
option?: boolean | DOMUtilsEventListenerOption
|
|
1027
1059
|
) {
|
|
1028
1060
|
let DOMUtilsContext = this;
|
|
1029
1061
|
if (element == null) {
|
|
@@ -1058,8 +1090,8 @@ export class DOMUtilsEvent {
|
|
|
1058
1090
|
*/
|
|
1059
1091
|
keydown(
|
|
1060
1092
|
element: DOMUtilsTargetElementType | Window | typeof globalThis,
|
|
1061
|
-
handler: (event: DOMUtils_Event["keydown"]) => void,
|
|
1062
|
-
option?: boolean |
|
|
1093
|
+
handler: (this: HTMLElement, event: DOMUtils_Event["keydown"]) => void,
|
|
1094
|
+
option?: boolean | DOMUtilsEventListenerOption
|
|
1063
1095
|
) {
|
|
1064
1096
|
let DOMUtilsContext = this;
|
|
1065
1097
|
if (element == null) {
|
|
@@ -1094,8 +1126,8 @@ export class DOMUtilsEvent {
|
|
|
1094
1126
|
*/
|
|
1095
1127
|
keypress(
|
|
1096
1128
|
element: DOMUtilsTargetElementType | Window | typeof globalThis,
|
|
1097
|
-
handler: (event: DOMUtils_Event["keypress"]) => void,
|
|
1098
|
-
option?: boolean |
|
|
1129
|
+
handler: (this: HTMLElement, event: DOMUtils_Event["keypress"]) => void,
|
|
1130
|
+
option?: boolean | DOMUtilsEventListenerOption
|
|
1099
1131
|
) {
|
|
1100
1132
|
let DOMUtilsContext = this;
|
|
1101
1133
|
if (element == null) {
|
|
@@ -1186,7 +1218,7 @@ export class DOMUtilsEvent {
|
|
|
1186
1218
|
otherCodeList: string[],
|
|
1187
1219
|
event: KeyboardEvent
|
|
1188
1220
|
) => void,
|
|
1189
|
-
options?:
|
|
1221
|
+
options?: DOMUtilsEventListenerOption | boolean
|
|
1190
1222
|
): {
|
|
1191
1223
|
removeListen(): void;
|
|
1192
1224
|
} {
|