UNPKG

3.03 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 /**
11 * @param {?} element
12 * @param {?=} opts
13 */
14 function Gesture(element, opts) {
15 if (opts === void 0) { opts = {}; }
16 this._callbacks = {};
17 this.isListening = false;
18 defaults(opts, {
19 domEvents: true
20 });
21 this.element = element;
22 // Map 'x' or 'y' string to hammerjs opts
23 this.direction = opts.direction || 'x';
24 opts.direction = this.direction === 'x' ?
25 DIRECTION_HORIZONTAL :
26 DIRECTION_VERTICAL;
27 this._options = opts;
28 }
29 /**
30 * @param {?} opts
31 * @return {?}
32 */
33 Gesture.prototype.options = function (opts) {
34 Object.assign(this._options, opts);
35 };
36 /**
37 * @param {?} type
38 * @param {?} cb
39 * @return {?}
40 */
41 Gesture.prototype.on = function (type, cb) {
42 if (type === 'pinch' || type === 'rotate') {
43 this._hammer.get(type).set({ enable: true });
44 }
45 this._hammer.on(type, cb);
46 (this._callbacks[type] || (this._callbacks[type] = [])).push(cb);
47 };
48 /**
49 * @param {?} type
50 * @param {?} cb
51 * @return {?}
52 */
53 Gesture.prototype.off = function (type, cb) {
54 this._hammer.off(type, this._callbacks[type] ? cb : null);
55 };
56 /**
57 * @return {?}
58 */
59 Gesture.prototype.listen = function () {
60 if (!this.isListening) {
61 this._hammer = Hammer(this.element, this._options);
62 }
63 this.isListening = true;
64 };
65 /**
66 * @return {?}
67 */
68 Gesture.prototype.unlisten = function () {
69 var /** @type {?} */ eventType;
70 var /** @type {?} */ i;
71 if (this._hammer && this.isListening) {
72 for (eventType in this._callbacks) {
73 for (i = 0; i < this._callbacks[eventType].length; i++) {
74 this._hammer.off(eventType, this._callbacks[eventType]);
75 }
76 }
77 this._hammer.destroy();
78 }
79 this._callbacks = {};
80 this._hammer = null;
81 this.isListening = false;
82 };
83 /**
84 * @return {?}
85 */
86 Gesture.prototype.destroy = function () {
87 this.unlisten();
88 this.element = this._options = null;
89 };
90 return Gesture;
91}());
92export { Gesture };
93function Gesture_tsickle_Closure_declarations() {
94 /** @type {?} */
95 Gesture.prototype._hammer;
96 /** @type {?} */
97 Gesture.prototype._options;
98 /** @type {?} */
99 Gesture.prototype._callbacks;
100 /** @type {?} */
101 Gesture.prototype.element;
102 /** @type {?} */
103 Gesture.prototype.direction;
104 /** @type {?} */
105 Gesture.prototype.isListening;
106}
107//# sourceMappingURL=gesture.js.map
\No newline at end of file