update: 增加事件 options 参数

This commit is contained in:
yanmao 2022-01-20 23:34:41 +08:00
parent 592bfdd610
commit 6c410ada92
4 changed files with 34 additions and 16 deletions

View File

@ -442,11 +442,11 @@ class NodeEntry implements NodeInterface {
on<R = any, F extends EventListener<R> = EventListener<R>>(
eventType: string,
listener: F,
rewrite?: boolean | undefined,
options?: boolean | AddEventListenerOptions,
): NodeInterface {
this.each((node, i) => {
node.addEventListener(eventType, listener, false);
if (this.events[i]) this.events[i].on(eventType, listener, rewrite);
node.addEventListener(eventType, listener, options);
if (this.events[i]) this.events[i].on(eventType, listener, options);
});
return this;
}
@ -457,10 +457,15 @@ class NodeEntry implements NodeInterface {
* @param {Function} listener
* @return
*/
off(eventType: string, listener: EventListener): NodeInterface {
off(
eventType: string,
listener: EventListener,
options?: boolean | EventListenerOptions,
): NodeInterface {
this.each((node, i) => {
node.removeEventListener(eventType, listener, false);
if (this.events[i]) this.events[i].off(eventType, listener);
node.removeEventListener(eventType, listener, options);
if (this.events[i])
this.events[i].off(eventType, listener, options);
});
return this;
}

View File

@ -15,13 +15,13 @@ class Event implements EventInterface {
on<R = any, F extends EventListener<R> = EventListener<R>>(
eventType: string,
listener: F,
rewrite?: boolean,
options?: boolean | AddEventListenerOptions,
): void {
if (!this.listeners[eventType] || rewrite) {
if (!this.listeners[eventType]) {
this.listeners[eventType] = [];
}
this.listeners[eventType].push(listener);
if (typeof options !== 'object' || !options.once)
this.listeners[eventType].push(listener);
}
/**

View File

@ -24,14 +24,18 @@ export interface EventInterface {
on<R = any, F extends EventListener<R> = EventListener<R>>(
eventType: string,
listener: F,
rewrite?: boolean,
options?: boolean | AddEventListenerOptions,
): void;
/**
*
* @param eventType
* @param listener
*/
off(eventType: string, listener: EventListener): void;
off(
eventType: string,
listener: EventListener,
options?: boolean | EventListenerOptions,
): void;
/**
*
* @param eventType
@ -285,7 +289,7 @@ export interface NodeInterface {
on<R = any, F extends EventListener<R> = EventListener<R>>(
eventType: string,
listener: F,
rewrite?: boolean | undefined,
options?: boolean | AddEventListenerOptions,
): NodeInterface;
/**
@ -294,7 +298,11 @@ export interface NodeInterface {
* @param {Function} listener
* @return {NodeInterface}
*/
off(eventType: string, listener: EventListener): NodeInterface;
off(
eventType: string,
listener: EventListener,
options?: boolean | EventListenerOptions,
): NodeInterface;
/**
*

View File

@ -190,6 +190,7 @@ class CollapseComponent implements CollapseComponentInterface {
node.attributes({
'data-name': escape(name),
});
node.removeAllEvents();
node.on(
'click',
(event: MouseEvent) => {
@ -198,7 +199,9 @@ class CollapseComponent implements CollapseComponentInterface {
event.preventDefault();
if (onSelect) onSelect(event, data);
},
true,
{
once: true,
},
);
node.on(
'mouseenter',
@ -209,7 +212,9 @@ class CollapseComponent implements CollapseComponentInterface {
.removeClass('data-mention-item-active');
node.addClass('data-mention-item-active');
},
true,
{
once: true,
},
);
return node;
};