forked from ionic-team/ionic-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgesture.ts
More file actions
76 lines (64 loc) · 1.79 KB
/
gesture.ts
File metadata and controls
76 lines (64 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import {defaults, assign} from '../util';
import {Hammer, DIRECTION_HORIZONTAL, DIRECTION_VERTICAL} from './hammer';
/**
* A gesture recognizer class.
*
* TODO(mlynch): Re-enable the DOM event simulation that was causing issues (or verify hammer does this already, it might);
*/
export class Gesture {
private _hammer: any;
private _options: any;
private _callbacks: any = {};
public element: HTMLElement;
public direction: string;
public isListening: boolean = false;
constructor(element, opts: any = {}) {
defaults(opts, {
domEvents: true
});
this.element = element;
// Map 'x' or 'y' string to hammerjs opts
this.direction = opts.direction || 'x';
opts.direction = this.direction === 'x' ?
DIRECTION_HORIZONTAL :
DIRECTION_VERTICAL;
this._options = opts;
}
options(opts: any) {
assign(this._options, opts);
}
on(type: string, cb: Function) {
if(type == 'pinch' || type == 'rotate') {
this._hammer.get('pinch').set({enable: true});
}
this._hammer.on(type, cb);
(this._callbacks[type] || (this._callbacks[type] = [])).push(cb);
}
off(type: string, cb: Function) {
this._hammer.off(type, this._callbacks[type] ? cb : null);
}
listen() {
if (!this.isListening) {
this._hammer = Hammer(this.element, this._options);
}
this.isListening = true;
}
unlisten() {
var type, i;
if (this._hammer && this.isListening) {
for (type in this._callbacks) {
for (i = 0; i < this._callbacks[type].length; i++) {
this._hammer.off(type, this._callbacks[type]);
}
}
this._hammer.destroy();
}
this._callbacks = {};
this._hammer = null;
this.isListening = false;
}
destroy() {
this.unlisten();
this.element = this._options = null;
}
}