2016-06-23 09:47:54 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
|
*/
|
|
|
|
|
|
2016-08-25 00:50:16 -07:00
|
|
|
import {Inject, Injectable, OpaqueToken} from '@angular/core';
|
2016-06-08 16:38:52 -07:00
|
|
|
|
|
|
|
|
import {isPresent} from '../../facade/lang';
|
|
|
|
|
|
2015-02-09 15:11:31 +01:00
|
|
|
import {HammerGesturesPluginCommon} from './hammer_common';
|
2016-04-07 11:57:49 -07:00
|
|
|
|
2016-06-27 12:27:23 -07:00
|
|
|
/**
|
|
|
|
|
* A DI token that you can use to provide{@link HammerGestureConfig} to Angular. Use it to configure
|
|
|
|
|
* Hammer gestures.
|
|
|
|
|
*
|
|
|
|
|
* @experimental
|
|
|
|
|
*/
|
2016-06-08 16:38:52 -07:00
|
|
|
export const HAMMER_GESTURE_CONFIG: OpaqueToken = new OpaqueToken('HammerGestureConfig');
|
2016-04-07 11:57:49 -07:00
|
|
|
|
|
|
|
|
export interface HammerInstance {
|
|
|
|
|
on(eventName: string, callback: Function): void;
|
|
|
|
|
off(eventName: string, callback: Function): void;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-27 12:27:23 -07:00
|
|
|
/**
|
|
|
|
|
* @experimental
|
|
|
|
|
*/
|
2016-04-07 11:57:49 -07:00
|
|
|
@Injectable()
|
|
|
|
|
export class HammerGestureConfig {
|
|
|
|
|
events: string[] = [];
|
|
|
|
|
|
|
|
|
|
overrides: {[key: string]: Object} = {};
|
|
|
|
|
|
|
|
|
|
buildHammer(element: HTMLElement): HammerInstance {
|
|
|
|
|
var mc = new Hammer(element);
|
|
|
|
|
|
|
|
|
|
mc.get('pinch').set({enable: true});
|
|
|
|
|
mc.get('rotate').set({enable: true});
|
|
|
|
|
|
|
|
|
|
for (let eventName in this.overrides) {
|
|
|
|
|
mc.get(eventName).set(this.overrides[eventName]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return mc;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-02-09 15:11:31 +01:00
|
|
|
|
2015-09-04 14:23:13 -07:00
|
|
|
@Injectable()
|
2015-02-09 15:11:31 +01:00
|
|
|
export class HammerGesturesPlugin extends HammerGesturesPluginCommon {
|
2016-04-07 11:57:49 -07:00
|
|
|
constructor(@Inject(HAMMER_GESTURE_CONFIG) private _config: HammerGestureConfig) { super(); }
|
|
|
|
|
|
2015-05-18 11:57:20 -07:00
|
|
|
supports(eventName: string): boolean {
|
2016-04-07 11:57:49 -07:00
|
|
|
if (!super.supports(eventName) && !this.isCustomEvent(eventName)) return false;
|
2015-02-09 15:11:31 +01:00
|
|
|
|
2016-06-08 15:45:15 -07:00
|
|
|
if (!isPresent((window as any /** TODO #???? */)['Hammer'])) {
|
2016-08-25 00:50:16 -07:00
|
|
|
throw new Error(`Hammer.js is not loaded, can not bind ${eventName} event`);
|
2015-02-09 15:11:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-25 14:47:25 -08:00
|
|
|
addEventListener(element: HTMLElement, eventName: string, handler: Function): Function {
|
2015-02-09 15:11:31 +01:00
|
|
|
var zone = this.manager.getZone();
|
|
|
|
|
eventName = eventName.toLowerCase();
|
|
|
|
|
|
2016-04-07 11:57:49 -07:00
|
|
|
return zone.runOutsideAngular(() => {
|
2015-02-09 15:11:31 +01:00
|
|
|
// Creating the manager bind events, must be done outside of angular
|
2016-04-07 11:57:49 -07:00
|
|
|
var mc = this._config.buildHammer(element);
|
2016-06-08 16:38:52 -07:00
|
|
|
var callback = function(eventObj: any /** TODO #???? */) {
|
|
|
|
|
zone.runGuarded(function() { handler(eventObj); });
|
|
|
|
|
};
|
2016-02-23 18:50:09 +09:00
|
|
|
mc.on(eventName, callback);
|
|
|
|
|
return () => { mc.off(eventName, callback); };
|
2015-02-09 15:11:31 +01:00
|
|
|
});
|
|
|
|
|
}
|
2016-04-07 11:57:49 -07:00
|
|
|
|
|
|
|
|
isCustomEvent(eventName: string): boolean { return this._config.events.indexOf(eventName) > -1; }
|
2015-02-09 15:11:31 +01:00
|
|
|
}
|