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 {EventManagerPlugin} from './event_manager';
@Injectable()

View File

@ -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;
}

View File

@ -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());
}
}

View File

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

View File

@ -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';