mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
refactor(core): Add global event delegation provider (#56247)
This replaces all addEventListener calls with a stashing function, and installs an event listener on the document body to retrieve the stashed function; PR Close #56247
This commit is contained in:
committed by
Jessica Janiuk
parent
3bfba7ec15
commit
40fb81fd0e
@@ -49,6 +49,7 @@ import {BrowserGetTestability} from './browser/testability';
|
||||
import {BrowserXhr} from './browser/xhr';
|
||||
import {DomRendererFactory2} from './dom/dom_renderer';
|
||||
import {DomEventsPlugin} from './dom/events/dom_events';
|
||||
import {EventDelegationPlugin} from './dom/events/event_delegation';
|
||||
import {EVENT_MANAGER_PLUGINS, EventManager} from './dom/events/event_manager';
|
||||
import {KeyEventsPlugin} from './dom/events/key_events';
|
||||
import {SharedStylesHost} from './dom/shared_styles_host';
|
||||
@@ -240,6 +241,11 @@ const BROWSER_MODULE_PROVIDERS: Provider[] = [
|
||||
deps: [DOCUMENT, NgZone, PLATFORM_ID],
|
||||
},
|
||||
{provide: EVENT_MANAGER_PLUGINS, useClass: KeyEventsPlugin, multi: true, deps: [DOCUMENT]},
|
||||
{
|
||||
provide: EVENT_MANAGER_PLUGINS,
|
||||
useClass: EventDelegationPlugin,
|
||||
multi: true,
|
||||
},
|
||||
DomRendererFactory2,
|
||||
SharedStylesHost,
|
||||
EventManager,
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google LLC All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {Inject, Injectable, inject, ɵGLOBAL_EVENT_DELEGATION} from '@angular/core';
|
||||
import {EventManagerPlugin} from './event_manager';
|
||||
import {DOCUMENT} from '@angular/common';
|
||||
|
||||
@Injectable()
|
||||
export class EventDelegationPlugin extends EventManagerPlugin {
|
||||
private delegate = inject(ɵGLOBAL_EVENT_DELEGATION, {optional: true});
|
||||
constructor(@Inject(DOCUMENT) doc: any) {
|
||||
super(doc);
|
||||
}
|
||||
|
||||
override supports(eventName: string): boolean {
|
||||
// If `GlobalDelegationEventPlugin` implementation is not provided,
|
||||
// this plugin is kept disabled.
|
||||
return this.delegate ? this.delegate.supports(eventName) : false;
|
||||
}
|
||||
|
||||
override addEventListener(element: HTMLElement, eventName: string, handler: Function): Function {
|
||||
return this.delegate!.addEventListener(element, eventName, handler);
|
||||
}
|
||||
|
||||
removeEventListener(element: HTMLElement, eventName: string, callback: Function): void {
|
||||
return this.delegate!.removeEventListener(element, eventName, callback);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user