UNPKG

6.29 kBJavaScriptView Raw
1import { defaults } from '../util/util';
2import { PanRecognizer } from './recognizers';
3import { pointerCoord } from '../util/dom';
4import { UIEventManager } from './ui-event-manager';
5/**
6 * @hidden
7 */
8var PanGesture = (function () {
9 /**
10 * @param {?} plt
11 * @param {?} element
12 * @param {?=} opts
13 */
14 function PanGesture(plt, element, opts) {
15 if (opts === void 0) { opts = {}; }
16 this.plt = plt;
17 this.element = element;
18 defaults(opts, {
19 threshold: 20,
20 maxAngle: 40,
21 direction: 'x',
22 zone: true,
23 capture: false,
24 passive: false,
25 });
26 this.events = new UIEventManager(plt);
27 if (opts.domController) {
28 this.debouncer = opts.domController.debouncer();
29 }
30 this.gestute = opts.gesture;
31 this.direction = opts.direction;
32 this.eventsConfig = {
33 element: this.element,
34 pointerDown: this.pointerDown.bind(this),
35 pointerMove: this.pointerMove.bind(this),
36 pointerUp: this.pointerUp.bind(this),
37 zone: opts.zone,
38 capture: opts.capture,
39 passive: opts.passive
40 };
41 if (opts.threshold > 0) {
42 this.detector = new PanRecognizer(opts.direction, opts.threshold, opts.maxAngle);
43 }
44 }
45 /**
46 * @return {?}
47 */
48 PanGesture.prototype.listen = function () {
49 if (!this.isListening) {
50 this.pointerEvents = this.events.pointerEvents(this.eventsConfig);
51 this.isListening = true;
52 }
53 };
54 /**
55 * @return {?}
56 */
57 PanGesture.prototype.unlisten = function () {
58 if (this.isListening) {
59 this.gestute && this.gestute.release();
60 this.events.unlistenAll();
61 this.isListening = false;
62 }
63 };
64 /**
65 * @return {?}
66 */
67 PanGesture.prototype.destroy = function () {
68 this.gestute && this.gestute.destroy();
69 this.gestute = null;
70 this.unlisten();
71 this.events.destroy();
72 this.events = this.element = this.gestute = null;
73 };
74 /**
75 * @param {?} ev
76 * @return {?}
77 */
78 PanGesture.prototype.pointerDown = function (ev) {
79 if (this.started) {
80 return;
81 }
82 if (!this.canStart(ev)) {
83 return false;
84 }
85 if (this.gestute) {
86 // Release fallback
87 this.gestute.release();
88 // Start gesture
89 if (!this.gestute.start()) {
90 return false;
91 }
92 }
93 this.started = true;
94 this.captured = false;
95 var /** @type {?} */ coord = pointerCoord(ev);
96 if (this.detector) {
97 this.detector.start(coord);
98 }
99 else {
100 if (!this.tryToCapture(ev)) {
101 this.started = false;
102 this.captured = false;
103 this.gestute.release();
104 return false;
105 }
106 }
107 return true;
108 };
109 /**
110 * @param {?} ev
111 * @return {?}
112 */
113 PanGesture.prototype.pointerMove = function (ev) {
114 var _this = this;
115 (void 0) /* assert */;
116 if (this.captured) {
117 this.debouncer.write(function () {
118 _this.onDragMove(ev);
119 });
120 return;
121 }
122 (void 0) /* assert */;
123 var /** @type {?} */ coord = pointerCoord(ev);
124 if (this.detector.detect(coord)) {
125 if (this.detector.pan() !== 0) {
126 if (!this.tryToCapture(ev)) {
127 this.abort(ev);
128 }
129 }
130 }
131 };
132 /**
133 * @param {?} ev
134 * @return {?}
135 */
136 PanGesture.prototype.pointerUp = function (ev) {
137 (void 0) /* assert */;
138 this.debouncer.cancel();
139 this.gestute && this.gestute.release();
140 if (this.captured) {
141 this.onDragEnd(ev);
142 }
143 else {
144 this.notCaptured(ev);
145 }
146 this.captured = false;
147 this.started = false;
148 };
149 /**
150 * @param {?} ev
151 * @return {?}
152 */
153 PanGesture.prototype.tryToCapture = function (ev) {
154 (void 0) /* assert */;
155 (void 0) /* assert */;
156 if (this.gestute && !this.gestute.capture()) {
157 return false;
158 }
159 this.onDragStart(ev);
160 this.captured = true;
161 return true;
162 };
163 /**
164 * @param {?} ev
165 * @return {?}
166 */
167 PanGesture.prototype.abort = function (ev) {
168 this.started = false;
169 this.captured = false;
170 this.gestute.release();
171 this.pointerEvents.stop();
172 this.notCaptured(ev);
173 };
174 /**
175 * @return {?}
176 */
177 PanGesture.prototype.getNativeElement = function () {
178 return this.element;
179 };
180 /**
181 * @param {?} _ev
182 * @return {?}
183 */
184 PanGesture.prototype.canStart = function (_ev) { return true; };
185 /**
186 * @param {?} _ev
187 * @return {?}
188 */
189 PanGesture.prototype.onDragStart = function (_ev) { };
190 /**
191 * @param {?} _ev
192 * @return {?}
193 */
194 PanGesture.prototype.onDragMove = function (_ev) { };
195 /**
196 * @param {?} _ev
197 * @return {?}
198 */
199 PanGesture.prototype.onDragEnd = function (_ev) { };
200 /**
201 * @param {?} _ev
202 * @return {?}
203 */
204 PanGesture.prototype.notCaptured = function (_ev) { };
205 return PanGesture;
206}());
207export { PanGesture };
208function PanGesture_tsickle_Closure_declarations() {
209 /** @type {?} */
210 PanGesture.prototype.debouncer;
211 /** @type {?} */
212 PanGesture.prototype.events;
213 /** @type {?} */
214 PanGesture.prototype.pointerEvents;
215 /** @type {?} */
216 PanGesture.prototype.detector;
217 /** @type {?} */
218 PanGesture.prototype.started;
219 /** @type {?} */
220 PanGesture.prototype.captured;
221 /** @type {?} */
222 PanGesture.prototype.isListening;
223 /** @type {?} */
224 PanGesture.prototype.gestute;
225 /** @type {?} */
226 PanGesture.prototype.direction;
227 /** @type {?} */
228 PanGesture.prototype.eventsConfig;
229 /** @type {?} */
230 PanGesture.prototype.plt;
231 /** @type {?} */
232 PanGesture.prototype.element;
233}
234//# sourceMappingURL=pan-gesture.js.map
\No newline at end of file