UNPKG

2.24 kBJavaScriptView Raw
1import { defaults } from '../util/util';
2import { DIRECTION_HORIZONTAL, DIRECTION_VERTICAL, Hammer } from './hammer';
3/**
4 * @hidden
5 * A gesture recognizer class.
6 *
7 * TODO(mlynch): Re-enable the DOM event simulation that was causing issues (or verify hammer does this already, it might);
8 */
9var Gesture = (function () {
10 function Gesture(element, opts) {
11 if (opts === void 0) { opts = {}; }
12 this._callbacks = {};
13 this.isListening = false;
14 defaults(opts, {
15 domEvents: true
16 });
17 this.element = element;
18 // Map 'x' or 'y' string to hammerjs opts
19 this.direction = opts.direction || 'x';
20 opts.direction = this.direction === 'x' ?
21 DIRECTION_HORIZONTAL :
22 DIRECTION_VERTICAL;
23 this._options = opts;
24 }
25 Gesture.prototype.options = function (opts) {
26 Object.assign(this._options, opts);
27 };
28 Gesture.prototype.on = function (type, cb) {
29 if (type === 'pinch' || type === 'rotate') {
30 this._hammer.get(type).set({ enable: true });
31 }
32 this._hammer.on(type, cb);
33 (this._callbacks[type] || (this._callbacks[type] = [])).push(cb);
34 };
35 Gesture.prototype.off = function (type, cb) {
36 this._hammer.off(type, this._callbacks[type] ? cb : null);
37 };
38 Gesture.prototype.listen = function () {
39 if (!this.isListening) {
40 this._hammer = Hammer(this.element, this._options);
41 }
42 this.isListening = true;
43 };
44 Gesture.prototype.unlisten = function () {
45 var eventType;
46 var i;
47 if (this._hammer && this.isListening) {
48 for (eventType in this._callbacks) {
49 for (i = 0; i < this._callbacks[eventType].length; i++) {
50 this._hammer.off(eventType, this._callbacks[eventType]);
51 }
52 }
53 this._hammer.destroy();
54 }
55 this._callbacks = {};
56 this._hammer = null;
57 this.isListening = false;
58 };
59 Gesture.prototype.destroy = function () {
60 this.unlisten();
61 this.element = this._options = null;
62 };
63 return Gesture;
64}());
65export { Gesture };
66//# sourceMappingURL=gesture.js.map
\No newline at end of file