Skip to content

Commit

Permalink
refactor: remove deprecated EventManager method `addGlobalEventList…
Browse files Browse the repository at this point in the history
…ener` (#49645)

This commit removed the deprecated `EventManager` method  `addGlobalEventListener`.

BREAKING CHANGE: Deprecated `EventManager` method `addGlobalEventListener` has been removed as it is not used by Ivy.

PR Close #49645
  • Loading branch information
alan-agius4 authored and atscott committed Mar 30, 2023
1 parent e883198 commit 2703fd6
Show file tree
Hide file tree
Showing 15 changed files with 8 additions and 103 deletions.
2 changes: 0 additions & 2 deletions goldens/public-api/platform-browser/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ export const EVENT_MANAGER_PLUGINS: InjectionToken<EventManagerPlugin[]>;
export class EventManager {
constructor(plugins: EventManagerPlugin[], _zone: NgZone);
addEventListener(element: HTMLElement, eventName: string, handler: Function): Function;
// @deprecated
addGlobalEventListener(target: string, eventName: string, handler: Function): Function;
getZone(): NgZone;
// (undocumented)
static ɵfac: i0.ɵɵFactoryDeclaration<EventManager, never>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -866,9 +866,6 @@
{
"name": "dashCaseToCamelCase"
},
{
"name": "decoratePreventDefault"
},
{
"name": "deepForEach"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -638,9 +638,6 @@
{
"name": "currentConsumer"
},
{
"name": "decoratePreventDefault"
},
{
"name": "deepForEach"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -872,9 +872,6 @@
{
"name": "currentConsumer"
},
{
"name": "decoratePreventDefault"
},
{
"name": "deepForEach"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -842,9 +842,6 @@
{
"name": "currentConsumer"
},
{
"name": "decoratePreventDefault"
},
{
"name": "deepForEach"
},
Expand Down
3 changes: 0 additions & 3 deletions packages/core/test/bundling/router/bundle.golden_symbols.json
Original file line number Diff line number Diff line change
Expand Up @@ -1091,9 +1091,6 @@
{
"name": "decodeQuery"
},
{
"name": "decoratePreventDefault"
},
{
"name": "deepForEach"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -578,9 +578,6 @@
{
"name": "currentConsumer"
},
{
"name": "decoratePreventDefault"
},
{
"name": "deepForEach"
},
Expand Down
3 changes: 0 additions & 3 deletions packages/core/test/bundling/todo/bundle.golden_symbols.json
Original file line number Diff line number Diff line change
Expand Up @@ -758,9 +758,6 @@
{
"name": "currentConsumer"
},
{
"name": "decoratePreventDefault"
},
{
"name": "deepForEach"
},
Expand Down
10 changes: 7 additions & 3 deletions packages/platform-browser/src/dom/dom_renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {ɵgetDOM as getDOM} from '@angular/common';
import {APP_ID, CSP_NONCE, Inject, Injectable, InjectionToken, OnDestroy, Optional, Renderer2, RendererFactory2, RendererStyleFlags2, RendererType2, ViewEncapsulation} from '@angular/core';

import {EventManager} from './events/event_manager';
Expand Down Expand Up @@ -288,10 +289,13 @@ class DefaultDomRenderer2 implements Renderer2 {
() => void {
(typeof ngDevMode === 'undefined' || ngDevMode) && checkNoSyntheticProp(event, 'listener');
if (typeof target === 'string') {
return <() => void>this.eventManager.addGlobalEventListener(
target, event, decoratePreventDefault(callback));
target = getDOM().getGlobalEventTarget(document, target);
if (!target) {
throw new Error(`Unsupported event target ${target} for event ${event}`);
}
}
return <() => void>this.eventManager.addEventListener(

return this.eventManager.addEventListener(
target, event, decoratePreventDefault(callback)) as () => void;
}
}
Expand Down
24 changes: 0 additions & 24 deletions packages/platform-browser/src/dom/events/event_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* found in the LICENSE file at https://angular.io/license
*/

import {ɵgetDOM as getDOM} from '@angular/common';
import {Inject, Injectable, InjectionToken, NgZone} from '@angular/core';

/**
Expand Down Expand Up @@ -52,21 +51,6 @@ export class EventManager {
return plugin.addEventListener(element, eventName, handler);
}

/**
* Registers a global handler for an event in a target view.
*
* @param target A target for global event notifications. One of "window", "document", or "body".
* @param eventName The name of the event to listen for.
* @param handler A function to call when the notification occurs. Receives the
* event object as an argument.
* @returns A callback function that can be used to remove the handler.
* @deprecated No longer being used in Ivy code. To be removed in version 14.
*/
addGlobalEventListener(target: string, eventName: string, handler: Function): Function {
const plugin = this._findPluginFor(eventName);
return plugin.addGlobalEventListener(target, eventName, handler);
}

/**
* Retrieves the compilation zone in which event listeners are registered.
*/
Expand Down Expand Up @@ -102,12 +86,4 @@ export abstract class EventManagerPlugin {
abstract supports(eventName: string): boolean;

abstract addEventListener(element: HTMLElement, eventName: string, handler: Function): Function;

addGlobalEventListener(element: string, eventName: string, handler: Function): Function {
const target: HTMLElement = getDOM().getGlobalEventTarget(this._doc, element);
if (!target) {
throw new Error(`Unsupported event target ${target} for event ${eventName}`);
}
return this.addEventListener(target, eventName, handler);
}
}
20 changes: 0 additions & 20 deletions packages/platform-browser/test/dom/events/event_manager_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,26 +75,6 @@ describe('EventManager', () => {
expect(receivedEvent).toBe(dispatchedEvent);
});

it('should add and remove global event listeners', () => {
const element = el('<div><div></div></div>');
doc.body.appendChild(element);
const dispatchedEvent = createMouseEvent('click');
let receivedEvent: any /** TODO #9100 */ = null;
const handler = (e: any /** TODO #9100 */) => {
receivedEvent = e;
};
const manager = new EventManager([domEventPlugin], new FakeNgZone());

const remover = manager.addGlobalEventListener('document', 'click', handler);
getDOM().dispatchEvent(element, dispatchedEvent);
expect(receivedEvent).toBe(dispatchedEvent);

receivedEvent = null;
remover();
getDOM().dispatchEvent(element, dispatchedEvent);
expect(receivedEvent).toBe(null);
});

it('should keep zone when addEventListener', () => {
const Zone = (window as any)['Zone'];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ import {HammerGestureConfig, HammerGesturesPlugin,} from '@angular/platform-brow
plugin = new HammerGesturesPlugin(document, new HammerGestureConfig(), fakeConsole);
});

it('should implement addGlobalEventListener', () => {
spyOn(plugin, 'addEventListener').and.callFake(() => () => {});

expect(() => {
plugin.addGlobalEventListener('document', 'swipe', () => {});
}).not.toThrowError();
});

it('should warn user and do nothing when Hammer.js not loaded', () => {
expect(plugin.supports('swipe')).toBe(false);
expect(fakeConsole.warn)
Expand Down
11 changes: 0 additions & 11 deletions packages/platform-browser/test/dom/events/key_events_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,6 @@ import {KeyEventsPlugin} from '@angular/platform-browser/src/dom/events/key_even
.toEqual(KeyEventsPlugin.parseEventName('keyup.control.escape'));
});

if (!isNode) {
it('should implement addGlobalEventListener', () => {
const plugin = new KeyEventsPlugin(document);

spyOn(plugin, 'addEventListener').and.callFake(() => () => {});

expect(() => plugin.addGlobalEventListener('window', 'keyup.control.esc', () => {}))
.not.toThrowError();
});
}

it('should match key field', () => {
const baseKeyboardEvent = {
isTrusted: true,
Expand Down
9 changes: 0 additions & 9 deletions packages/platform-server/src/server_events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,4 @@ export class ServerEventManagerPlugin /* extends EventManagerPlugin which is pri
addEventListener(element: HTMLElement, eventName: string, handler: Function): Function {
return getDOM().onAndCancel(element, eventName, handler);
}

/** @deprecated No longer being used in Ivy code. To be removed in version 14. */
addGlobalEventListener(element: string, eventName: string, handler: Function): Function {
const target: HTMLElement = getDOM().getGlobalEventTarget(this.doc, element);
if (!target) {
throw new Error(`Unsupported event target ${target} for event ${eventName}`);
}
return this.addEventListener(target, eventName, handler);
}
}
6 changes: 1 addition & 5 deletions packages/platform-server/src/server_renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,7 @@ class DefaultServerRenderer2 implements Renderer2 {
target: 'document'|'window'|'body'|any, eventName: string,
callback: (event: any) => boolean): () => void {
checkNoSyntheticProp(eventName, 'listener');
if (typeof target === 'string') {
return <() => void>this.eventManager.addGlobalEventListener(
target, eventName, this.decoratePreventDefault(callback));
}
return <() => void>this.eventManager.addEventListener(
return this.eventManager.addEventListener(
target, eventName, this.decoratePreventDefault(callback)) as () => void;
}

Expand Down

0 comments on commit 2703fd6

Please sign in to comment.