From 92f244aa268be60109d0a6696dc2680d7f387234 Mon Sep 17 00:00:00 2001 From: Dzmitry Shylovich Date: Fri, 11 Nov 2016 00:54:31 +0300 Subject: [PATCH] refactor(events): remove facade and clean up --- .../src/dom/events/dom_events.ts | 1 - .../src/dom/events/event_manager.ts | 12 ++--- .../src/dom/events/hammer_common.ts | 7 ++- .../src/dom/events/hammer_gestures.ts | 1 - .../src/dom/events/key_events.ts | 50 ++++++++----------- 5 files changed, 30 insertions(+), 41 deletions(-) diff --git a/modules/@angular/platform-browser/src/dom/events/dom_events.ts b/modules/@angular/platform-browser/src/dom/events/dom_events.ts index 1200410458..e42d694338 100644 --- a/modules/@angular/platform-browser/src/dom/events/dom_events.ts +++ b/modules/@angular/platform-browser/src/dom/events/dom_events.ts @@ -7,7 +7,6 @@ */ import {Injectable} from '@angular/core'; - import {EventManagerPlugin} from './event_manager'; @Injectable() diff --git a/modules/@angular/platform-browser/src/dom/events/event_manager.ts b/modules/@angular/platform-browser/src/dom/events/event_manager.ts index 75dfe49b05..fdad32f09b 100644 --- a/modules/@angular/platform-browser/src/dom/events/event_manager.ts +++ b/modules/@angular/platform-browser/src/dom/events/event_manager.ts @@ -7,7 +7,7 @@ */ import {Inject, Injectable, NgZone, OpaqueToken} from '@angular/core'; - +import {getDOM} from '../dom_adapter'; /** * @stable @@ -27,12 +27,12 @@ export class EventManager { } addEventListener(element: HTMLElement, eventName: string, handler: Function): Function { - var plugin = this._findPluginFor(eventName); + const plugin = this._findPluginFor(eventName); return plugin.addEventListener(element, eventName, handler); } addGlobalEventListener(target: string, eventName: string, handler: Function): Function { - var plugin = this._findPluginFor(eventName); + const plugin = this._findPluginFor(eventName); return plugin.addGlobalEventListener(target, eventName, handler); } @@ -40,9 +40,9 @@ export class EventManager { /** @internal */ _findPluginFor(eventName: string): EventManagerPlugin { - var plugins = this._plugins; - for (var i = 0; i < plugins.length; i++) { - var plugin = plugins[i]; + const plugins = this._plugins; + for (let i = 0; i < plugins.length; i++) { + const plugin = plugins[i]; if (plugin.supports(eventName)) { return plugin; } diff --git a/modules/@angular/platform-browser/src/dom/events/hammer_common.ts b/modules/@angular/platform-browser/src/dom/events/hammer_common.ts index 63a4019abc..602edc4eee 100644 --- a/modules/@angular/platform-browser/src/dom/events/hammer_common.ts +++ b/modules/@angular/platform-browser/src/dom/events/hammer_common.ts @@ -8,7 +8,7 @@ import {EventManagerPlugin} from './event_manager'; -var _eventNames = { +const EVENT_NAMES = { // pan 'pan': true, 'panstart': true, @@ -46,11 +46,10 @@ var _eventNames = { 'tap': true, }; - -export class HammerGesturesPluginCommon extends EventManagerPlugin { +export abstract class HammerGesturesPluginCommon extends EventManagerPlugin { constructor() { super(); } supports(eventName: string): boolean { - return _eventNames.hasOwnProperty(eventName.toLowerCase()); + return EVENT_NAMES.hasOwnProperty(eventName.toLowerCase()); } } diff --git a/modules/@angular/platform-browser/src/dom/events/hammer_gestures.ts b/modules/@angular/platform-browser/src/dom/events/hammer_gestures.ts index c2cbb42c48..c92d077e9f 100644 --- a/modules/@angular/platform-browser/src/dom/events/hammer_gestures.ts +++ b/modules/@angular/platform-browser/src/dom/events/hammer_gestures.ts @@ -7,7 +7,6 @@ */ import {Inject, Injectable, OpaqueToken} from '@angular/core'; - import {HammerGesturesPluginCommon} from './hammer_common'; /** diff --git a/modules/@angular/platform-browser/src/dom/events/key_events.ts b/modules/@angular/platform-browser/src/dom/events/key_events.ts index 499204a638..e085fc96e7 100644 --- a/modules/@angular/platform-browser/src/dom/events/key_events.ts +++ b/modules/@angular/platform-browser/src/dom/events/key_events.ts @@ -7,23 +7,17 @@ */ import {Injectable, NgZone} from '@angular/core'; - -import {ListWrapper} from '../../facade/collection'; -import {isPresent} from '../../facade/lang'; import {getDOM} from '../dom_adapter'; - import {EventManagerPlugin} from './event_manager'; - -var modifierKeys = ['alt', 'control', 'meta', 'shift']; -var modifierKeyGetters: {[key: string]: (event: KeyboardEvent) => boolean} = { +const MODIFIER_KEYS = ['alt', 'control', 'meta', 'shift']; +const MODIFIER_KEY_GETTERS: {[key: string]: (event: KeyboardEvent) => boolean} = { 'alt': (event: KeyboardEvent) => event.altKey, 'control': (event: KeyboardEvent) => event.ctrlKey, 'meta': (event: KeyboardEvent) => event.metaKey, 'shift': (event: KeyboardEvent) => event.shiftKey }; - /** * @experimental */ @@ -31,15 +25,13 @@ var modifierKeyGetters: {[key: string]: (event: KeyboardEvent) => boolean} = { export class KeyEventsPlugin extends EventManagerPlugin { constructor() { super(); } - supports(eventName: string): boolean { - return isPresent(KeyEventsPlugin.parseEventName(eventName)); - } + supports(eventName: string): boolean { return KeyEventsPlugin.parseEventName(eventName) != null; } addEventListener(element: HTMLElement, eventName: string, handler: Function): Function { - var parsedEvent = KeyEventsPlugin.parseEventName(eventName); + const parsedEvent = KeyEventsPlugin.parseEventName(eventName); - var outsideHandler = KeyEventsPlugin.eventCallback( - element, parsedEvent['fullKey'], handler, this.manager.getZone()); + const outsideHandler = + KeyEventsPlugin.eventCallback(parsedEvent['fullKey'], handler, this.manager.getZone()); return this.manager.getZone().runOutsideAngular(() => { return getDOM().onAndCancel(element, parsedEvent['domEventName'], outsideHandler); @@ -47,19 +39,20 @@ export class KeyEventsPlugin extends EventManagerPlugin { } static parseEventName(eventName: string): {[key: string]: string} { - var parts: string[] = eventName.toLowerCase().split('.'); + const parts: string[] = eventName.toLowerCase().split('.'); - var domEventName = parts.shift(); + const domEventName = parts.shift(); if ((parts.length === 0) || !(domEventName === 'keydown' || domEventName === 'keyup')) { return null; } - var key = KeyEventsPlugin._normalizeKey(parts.pop()); + const key = KeyEventsPlugin._normalizeKey(parts.pop()); - var fullKey = ''; - modifierKeys.forEach(modifierName => { - if (parts.indexOf(modifierName) > -1) { - ListWrapper.remove(parts, modifierName); + let fullKey = ''; + MODIFIER_KEYS.forEach(modifierName => { + const index: number = parts.indexOf(modifierName); + if (index > -1) { + parts.splice(index, 1); fullKey += modifierName + '.'; } }); @@ -70,24 +63,24 @@ export class KeyEventsPlugin extends EventManagerPlugin { return null; } - var result: {[k: string]: string} = {}; + const result: {[k: string]: string} = {}; result['domEventName'] = domEventName; result['fullKey'] = fullKey; return result; } static getEventFullKey(event: KeyboardEvent): string { - var fullKey = ''; - var key = getDOM().getEventKey(event); + let fullKey = ''; + let key = getDOM().getEventKey(event); key = key.toLowerCase(); if (key === ' ') { key = 'space'; // for readability } else if (key === '.') { key = 'dot'; // because '.' is used as a separator in event names } - modifierKeys.forEach(modifierName => { + MODIFIER_KEYS.forEach(modifierName => { if (modifierName != key) { - var modifierGetter = modifierKeyGetters[modifierName]; + const modifierGetter = MODIFIER_KEY_GETTERS[modifierName]; if (modifierGetter(event)) { fullKey += modifierName + '.'; } @@ -97,8 +90,7 @@ export class KeyEventsPlugin extends EventManagerPlugin { return fullKey; } - static eventCallback(element: HTMLElement, fullKey: any, handler: Function, zone: NgZone): - Function { + static eventCallback(fullKey: any, handler: Function, zone: NgZone): Function { return (event: any /** TODO #9100 */) => { if (KeyEventsPlugin.getEventFullKey(event) === fullKey) { zone.runGuarded(() => handler(event)); @@ -108,7 +100,7 @@ export class KeyEventsPlugin extends EventManagerPlugin { /** @internal */ static _normalizeKey(keyName: string): string { - // TODO: switch to a StringMap if the mapping grows too much + // TODO: switch to a Map if the mapping grows too much switch (keyName) { case 'esc': return 'escape';