|
| 1 | +import {Injectable} from 'angular2/core'; |
| 2 | + |
| 3 | +export enum MdInteractionType { |
| 4 | + KEYBOARD, |
| 5 | + MOUSE, |
| 6 | + TOUCH, |
| 7 | +} |
| 8 | + |
| 9 | +// Interface for the interaction mappings, which holds an index definition |
| 10 | +// to avoid implicitly warnings. |
| 11 | +interface MdInteractionMap { |
| 12 | + [index: string]: any; |
| 13 | +} |
| 14 | + |
| 15 | +@Injectable() |
| 16 | +export class MdInteraction { |
| 17 | + |
| 18 | + private _isBuffering: boolean = false; |
| 19 | + private _bufferTimeout: number; |
| 20 | + private _lastInteractionType: MdInteractionType; |
| 21 | + |
| 22 | + private _inputTypeMap: MdInteractionMap = { |
| 23 | + 'keydown': MdInteractionType.KEYBOARD, |
| 24 | + 'mousedown': MdInteractionType.MOUSE, |
| 25 | + 'mouseenter': MdInteractionType.MOUSE, |
| 26 | + 'touchstart': MdInteractionType.TOUCH, |
| 27 | + 'pointerdown': 'pointer', |
| 28 | + 'MSPointerDown': 'pointer', |
| 29 | +}; |
| 30 | + |
| 31 | + // IE dispatches `pointerdown` events, which need to be validated separately. |
| 32 | + // Index numbers referenced here: https://msdn.microsoft.com/library/windows/apps/hh466130.aspx |
| 33 | + private _iePointerMap: MdInteractionMap = { |
| 34 | + 2: MdInteractionType.TOUCH, |
| 35 | + 3: MdInteractionType.TOUCH, |
| 36 | + 4: MdInteractionType.MOUSE |
| 37 | + }; |
| 38 | + |
| 39 | + constructor() { |
| 40 | + let mouseEvent = 'PointerEvent' in window ? 'pointerdown' : 'mousedown'; |
| 41 | + |
| 42 | + document.body.addEventListener('keydown', (e: Event) => this._onInputEvent(e)); |
| 43 | + document.body.addEventListener(mouseEvent, (e: Event) => this._onInputEvent(e)); |
| 44 | + document.body.addEventListener('mouseenter', (e: Event) => this._onInputEvent(e)); |
| 45 | + |
| 46 | + if ('ontouchstart' in document.documentElement) { |
| 47 | + document.body.addEventListener('touchstart', (e: TouchEvent) => this._onTouchInputEvent(e)); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private _onInputEvent(event: Event) { |
| 52 | + if (this._isBuffering) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + let type = this._inputTypeMap[event.type]; |
| 57 | + |
| 58 | + if (type === 'pointer') { |
| 59 | + let pointerType = (<any> event)['pointerType']; |
| 60 | + type = (typeof pointerType === 'number') ? this._iePointerMap[pointerType] |
| 61 | + : this._parsePointerType(pointerType); |
| 62 | + } |
| 63 | + |
| 64 | + if (type !== undefined) { |
| 65 | + this._lastInteractionType = type; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private _onTouchInputEvent(event: TouchEvent) { |
| 70 | + clearTimeout(this._bufferTimeout); |
| 71 | + |
| 72 | + this._onInputEvent(event); |
| 73 | + this._isBuffering = true; |
| 74 | + |
| 75 | + // The timeout of 650ms is needed to delay the touchstart, because otherwise the touch will call |
| 76 | + // the `onInput` function multiple times. |
| 77 | + this._bufferTimeout = setTimeout(() => this._isBuffering = false, 650); |
| 78 | + } |
| 79 | + |
| 80 | + private _parsePointerType(pointerType: string) { |
| 81 | + switch (pointerType) { |
| 82 | + case 'mouse': |
| 83 | + return MdInteractionType.MOUSE; |
| 84 | + case 'touch': |
| 85 | + return MdInteractionType.TOUCH; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + getLastInteractionType(): MdInteractionType { |
| 90 | + return this._lastInteractionType; |
| 91 | + } |
| 92 | +} |
0 commit comments