-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathrecognize-gesture.js
58 lines (53 loc) · 1.79 KB
/
recognize-gesture.js
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
import Modifier from "ember-modifier";
import { registerDestructor } from "@ember/destroyable";
function cleanup(instance) {
if (instance.manager !== null) {
instance.manager.destroy();
instance.manager = null;
}
}
export default class RecognizeGestureModifier extends Modifier {
constructor(owner) {
super(...arguments);
this.gestures = owner.lookup("service:-gestures");
registerDestructor(this, cleanup);
}
modify(element, positional, named) {
const gestureNames = [...positional];
this.recognizers = this.gestures.retrieve(gestureNames);
const managerOptions =
named && Object.keys(named).length > 0
? Object.assign({}, named)
: { domEvents: true };
managerOptions.useCapture = this.gestures.useCapture;
if (this.recognizers) {
element.style["touch-action"] = "manipulation";
element.style["-ms-touch-action"] = "manipulation";
this.recognizers.then((recognizers) => {
if (this.isDestroyed) return;
this.sortRecognizers(recognizers);
this.manager = new Hammer.Manager(element, managerOptions);
recognizers.forEach((recognizer) => {
this.manager.add(recognizer);
});
});
}
}
// Move each recognizer after all recognizers it excludes in the list - why?
sortRecognizers(recognizers) {
for (let i = 0; i < recognizers.length; i++) {
const r = recognizers[i];
let currentIndex = i;
if (r.exclude.length) {
for (let j = 0; j < r.exclude.length; j++) {
const newIndex = recognizers.indexOf(r.exclude[j]);
if (newIndex > 0 && currentIndex < newIndex) {
recognizers.splice(currentIndex, 1);
recognizers.splice(newIndex, 0, r);
currentIndex = newIndex;
}
}
}
}
}
}