refactor(events): remove facade and clean up

This commit is contained in:
Dzmitry Shylovich 2016-11-11 00:54:31 +03:00 committed by Victor Berchet
parent 2a4bf9a0df
commit 92f244aa26
5 changed files with 30 additions and 41 deletions

View File

@ -7,7 +7,6 @@
*/ */
import {Injectable} from '@angular/core'; import {Injectable} from '@angular/core';
import {EventManagerPlugin} from './event_manager'; import {EventManagerPlugin} from './event_manager';
@Injectable() @Injectable()

View File

@ -7,7 +7,7 @@
*/ */
import {Inject, Injectable, NgZone, OpaqueToken} from '@angular/core'; import {Inject, Injectable, NgZone, OpaqueToken} from '@angular/core';
import {getDOM} from '../dom_adapter';
/** /**
* @stable * @stable
@ -27,12 +27,12 @@ export class EventManager {
} }
addEventListener(element: HTMLElement, eventName: string, handler: Function): Function { addEventListener(element: HTMLElement, eventName: string, handler: Function): Function {
var plugin = this._findPluginFor(eventName); const plugin = this._findPluginFor(eventName);
return plugin.addEventListener(element, eventName, handler); return plugin.addEventListener(element, eventName, handler);
} }
addGlobalEventListener(target: string, eventName: string, handler: Function): Function { addGlobalEventListener(target: string, eventName: string, handler: Function): Function {
var plugin = this._findPluginFor(eventName); const plugin = this._findPluginFor(eventName);
return plugin.addGlobalEventListener(target, eventName, handler); return plugin.addGlobalEventListener(target, eventName, handler);
} }
@ -40,9 +40,9 @@ export class EventManager {
/** @internal */ /** @internal */
_findPluginFor(eventName: string): EventManagerPlugin { _findPluginFor(eventName: string): EventManagerPlugin {
var plugins = this._plugins; const plugins = this._plugins;
for (var i = 0; i < plugins.length; i++) { for (let i = 0; i < plugins.length; i++) {
var plugin = plugins[i]; const plugin = plugins[i];
if (plugin.supports(eventName)) { if (plugin.supports(eventName)) {
return plugin; return plugin;
} }

View File

@ -8,7 +8,7 @@
import {EventManagerPlugin} from './event_manager'; import {EventManagerPlugin} from './event_manager';
var _eventNames = { const EVENT_NAMES = {
// pan // pan
'pan': true, 'pan': true,
'panstart': true, 'panstart': true,
@ -46,11 +46,10 @@ var _eventNames = {
'tap': true, 'tap': true,
}; };
export abstract class HammerGesturesPluginCommon extends EventManagerPlugin {
export class HammerGesturesPluginCommon extends EventManagerPlugin {
constructor() { super(); } constructor() { super(); }
supports(eventName: string): boolean { supports(eventName: string): boolean {
return _eventNames.hasOwnProperty(eventName.toLowerCase()); return EVENT_NAMES.hasOwnProperty(eventName.toLowerCase());
} }
} }

View File

@ -7,7 +7,6 @@
*/ */
import {Inject, Injectable, OpaqueToken} from '@angular/core'; import {Inject, Injectable, OpaqueToken} from '@angular/core';
import {HammerGesturesPluginCommon} from './hammer_common'; import {HammerGesturesPluginCommon} from './hammer_common';
/** /**

View File

@ -7,23 +7,17 @@
*/ */
import {Injectable, NgZone} from '@angular/core'; import {Injectable, NgZone} from '@angular/core';
import {ListWrapper} from '../../facade/collection';
import {isPresent} from '../../facade/lang';
import {getDOM} from '../dom_adapter'; import {getDOM} from '../dom_adapter';
import {EventManagerPlugin} from './event_manager'; import {EventManagerPlugin} from './event_manager';
const MODIFIER_KEYS = ['alt', 'control', 'meta', 'shift'];
var modifierKeys = ['alt', 'control', 'meta', 'shift']; const MODIFIER_KEY_GETTERS: {[key: string]: (event: KeyboardEvent) => boolean} = {
var modifierKeyGetters: {[key: string]: (event: KeyboardEvent) => boolean} = {
'alt': (event: KeyboardEvent) => event.altKey, 'alt': (event: KeyboardEvent) => event.altKey,
'control': (event: KeyboardEvent) => event.ctrlKey, 'control': (event: KeyboardEvent) => event.ctrlKey,
'meta': (event: KeyboardEvent) => event.metaKey, 'meta': (event: KeyboardEvent) => event.metaKey,
'shift': (event: KeyboardEvent) => event.shiftKey 'shift': (event: KeyboardEvent) => event.shiftKey
}; };
/** /**
* @experimental * @experimental
*/ */
@ -31,15 +25,13 @@ var modifierKeyGetters: {[key: string]: (event: KeyboardEvent) => boolean} = {
export class KeyEventsPlugin extends EventManagerPlugin { export class KeyEventsPlugin extends EventManagerPlugin {
constructor() { super(); } constructor() { super(); }
supports(eventName: string): boolean { supports(eventName: string): boolean { return KeyEventsPlugin.parseEventName(eventName) != null; }
return isPresent(KeyEventsPlugin.parseEventName(eventName));
}
addEventListener(element: HTMLElement, eventName: string, handler: Function): Function { addEventListener(element: HTMLElement, eventName: string, handler: Function): Function {
var parsedEvent = KeyEventsPlugin.parseEventName(eventName); const parsedEvent = KeyEventsPlugin.parseEventName(eventName);
var outsideHandler = KeyEventsPlugin.eventCallback( const outsideHandler =
element, parsedEvent['fullKey'], handler, this.manager.getZone()); KeyEventsPlugin.eventCallback(parsedEvent['fullKey'], handler, this.manager.getZone());
return this.manager.getZone().runOutsideAngular(() => { return this.manager.getZone().runOutsideAngular(() => {
return getDOM().onAndCancel(element, parsedEvent['domEventName'], outsideHandler); return getDOM().onAndCancel(element, parsedEvent['domEventName'], outsideHandler);
@ -47,19 +39,20 @@ export class KeyEventsPlugin extends EventManagerPlugin {
} }
static parseEventName(eventName: string): {[key: string]: string} { 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')) { if ((parts.length === 0) || !(domEventName === 'keydown' || domEventName === 'keyup')) {
return null; return null;
} }
var key = KeyEventsPlugin._normalizeKey(parts.pop()); const key = KeyEventsPlugin._normalizeKey(parts.pop());
var fullKey = ''; let fullKey = '';
modifierKeys.forEach(modifierName => { MODIFIER_KEYS.forEach(modifierName => {
if (parts.indexOf(modifierName) > -1) { const index: number = parts.indexOf(modifierName);
ListWrapper.remove(parts, modifierName); if (index > -1) {
parts.splice(index, 1);
fullKey += modifierName + '.'; fullKey += modifierName + '.';
} }
}); });
@ -70,24 +63,24 @@ export class KeyEventsPlugin extends EventManagerPlugin {
return null; return null;
} }
var result: {[k: string]: string} = {}; const result: {[k: string]: string} = {};
result['domEventName'] = domEventName; result['domEventName'] = domEventName;
result['fullKey'] = fullKey; result['fullKey'] = fullKey;
return result; return result;
} }
static getEventFullKey(event: KeyboardEvent): string { static getEventFullKey(event: KeyboardEvent): string {
var fullKey = ''; let fullKey = '';
var key = getDOM().getEventKey(event); let key = getDOM().getEventKey(event);
key = key.toLowerCase(); key = key.toLowerCase();
if (key === ' ') { if (key === ' ') {
key = 'space'; // for readability key = 'space'; // for readability
} else if (key === '.') { } else if (key === '.') {
key = 'dot'; // because '.' is used as a separator in event names key = 'dot'; // because '.' is used as a separator in event names
} }
modifierKeys.forEach(modifierName => { MODIFIER_KEYS.forEach(modifierName => {
if (modifierName != key) { if (modifierName != key) {
var modifierGetter = modifierKeyGetters[modifierName]; const modifierGetter = MODIFIER_KEY_GETTERS[modifierName];
if (modifierGetter(event)) { if (modifierGetter(event)) {
fullKey += modifierName + '.'; fullKey += modifierName + '.';
} }
@ -97,8 +90,7 @@ export class KeyEventsPlugin extends EventManagerPlugin {
return fullKey; return fullKey;
} }
static eventCallback(element: HTMLElement, fullKey: any, handler: Function, zone: NgZone): static eventCallback(fullKey: any, handler: Function, zone: NgZone): Function {
Function {
return (event: any /** TODO #9100 */) => { return (event: any /** TODO #9100 */) => {
if (KeyEventsPlugin.getEventFullKey(event) === fullKey) { if (KeyEventsPlugin.getEventFullKey(event) === fullKey) {
zone.runGuarded(() => handler(event)); zone.runGuarded(() => handler(event));
@ -108,7 +100,7 @@ export class KeyEventsPlugin extends EventManagerPlugin {
/** @internal */ /** @internal */
static _normalizeKey(keyName: string): string { 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) { switch (keyName) {
case 'esc': case 'esc':
return 'escape'; return 'escape';