UNPKG

11.2 kBJavaScriptView Raw
1import { Inject, Injectable, forwardRef } from '@angular/core';
2import { App } from '../components/app/app';
3/**
4 * @hidden
5 */
6export var GESTURE_GO_BACK_SWIPE = 'goback-swipe';
7/**
8 * @hidden
9 */
10export var GESTURE_MENU_SWIPE = 'menu-swipe';
11/**
12 * @hidden
13 */
14export var GESTURE_ITEM_SWIPE = 'item-swipe';
15/**
16 * @hidden
17 */
18export var GESTURE_REFRESHER = 'refresher';
19/**
20 * @hidden
21 */
22export var GESTURE_TOGGLE = 'toggle';
23/**
24 * @hidden
25 */
26export var GESTURE_PRIORITY_SLIDING_ITEM = -10;
27/**
28 * @hidden
29 */
30export var GESTURE_PRIORITY_REFRESHER = 0;
31/**
32 * @hidden
33 */
34export var GESTURE_PRIORITY_MENU_SWIPE = 10;
35/**
36 * @hidden
37 */
38export var GESTURE_PRIORITY_GO_BACK_SWIPE = 20;
39/**
40 * @hidden
41 */
42export var GESTURE_PRIORITY_TOGGLE = 30;
43/**
44 * @hidden
45 */
46export var BLOCK_ALL = {
47 disable: [GESTURE_MENU_SWIPE, GESTURE_GO_BACK_SWIPE],
48 disableScroll: true
49};
50/**
51 * @hidden
52 */
53var GestureController = (function () {
54 /**
55 * @param {?} _app
56 */
57 function GestureController(_app) {
58 this._app = _app;
59 this.id = 1;
60 this.requestedStart = {};
61 this.disabledGestures = {};
62 this.disabledScroll = new Set();
63 this.capturedID = null;
64 }
65 /**
66 * @param {?} opts
67 * @return {?}
68 */
69 GestureController.prototype.createGesture = function (opts) {
70 if (!opts.name) {
71 throw new Error('name is undefined');
72 }
73 return new GestureDelegate(opts.name, this.newID(), this, opts.priority || 0, !!opts.disableScroll);
74 };
75 /**
76 * @param {?=} opts
77 * @return {?}
78 */
79 GestureController.prototype.createBlocker = function (opts) {
80 if (opts === void 0) { opts = {}; }
81 return new BlockerDelegate(this.newID(), this, opts.disable, !!opts.disableScroll);
82 };
83 /**
84 * @return {?}
85 */
86 GestureController.prototype.newID = function () {
87 var /** @type {?} */ id = this.id;
88 this.id++;
89 return id;
90 };
91 /**
92 * @param {?} gestureName
93 * @param {?} id
94 * @param {?} priority
95 * @return {?}
96 */
97 GestureController.prototype.start = function (gestureName, id, priority) {
98 if (!this.canStart(gestureName)) {
99 delete this.requestedStart[id];
100 return false;
101 }
102 this.requestedStart[id] = priority;
103 return true;
104 };
105 /**
106 * @param {?} gestureName
107 * @param {?} id
108 * @param {?} priority
109 * @return {?}
110 */
111 GestureController.prototype.capture = function (gestureName, id, priority) {
112 if (!this.start(gestureName, id, priority)) {
113 return false;
114 }
115 var /** @type {?} */ requestedStart = this.requestedStart;
116 var /** @type {?} */ maxPriority = -10000;
117 for (var /** @type {?} */ gestureID in requestedStart) {
118 maxPriority = Math.max(maxPriority, requestedStart[gestureID]);
119 }
120 if (maxPriority === priority) {
121 this.capturedID = id;
122 this.requestedStart = {};
123 (void 0) /* console.debug */;
124 return true;
125 }
126 delete requestedStart[id];
127 (void 0) /* console.debug */;
128 return false;
129 };
130 /**
131 * @param {?} id
132 * @return {?}
133 */
134 GestureController.prototype.release = function (id) {
135 delete this.requestedStart[id];
136 if (this.capturedID && id === this.capturedID) {
137 this.capturedID = null;
138 }
139 };
140 /**
141 * @param {?} gestureName
142 * @param {?} id
143 * @return {?}
144 */
145 GestureController.prototype.disableGesture = function (gestureName, id) {
146 var /** @type {?} */ set = this.disabledGestures[gestureName];
147 if (!set) {
148 set = new Set();
149 this.disabledGestures[gestureName] = set;
150 }
151 set.add(id);
152 };
153 /**
154 * @param {?} gestureName
155 * @param {?} id
156 * @return {?}
157 */
158 GestureController.prototype.enableGesture = function (gestureName, id) {
159 var /** @type {?} */ set = this.disabledGestures[gestureName];
160 if (set) {
161 set.delete(id);
162 }
163 };
164 /**
165 * @param {?} id
166 * @return {?}
167 */
168 GestureController.prototype.disableScroll = function (id) {
169 var /** @type {?} */ isEnabled = !this.isScrollDisabled();
170 this.disabledScroll.add(id);
171 if (this._app && isEnabled && this.isScrollDisabled()) {
172 (void 0) /* console.debug */;
173 this._app._setDisableScroll(true);
174 }
175 };
176 /**
177 * @param {?} id
178 * @return {?}
179 */
180 GestureController.prototype.enableScroll = function (id) {
181 var /** @type {?} */ isDisabled = this.isScrollDisabled();
182 this.disabledScroll.delete(id);
183 if (this._app && isDisabled && !this.isScrollDisabled()) {
184 (void 0) /* console.debug */;
185 this._app._setDisableScroll(false);
186 }
187 };
188 /**
189 * @param {?} gestureName
190 * @return {?}
191 */
192 GestureController.prototype.canStart = function (gestureName) {
193 if (this.capturedID) {
194 (void 0) /* console.debug */;
195 // a gesture already captured
196 return false;
197 }
198 if (this.isDisabled(gestureName)) {
199 (void 0) /* console.debug */;
200 return false;
201 }
202 return true;
203 };
204 /**
205 * @return {?}
206 */
207 GestureController.prototype.isCaptured = function () {
208 return !!this.capturedID;
209 };
210 /**
211 * @return {?}
212 */
213 GestureController.prototype.isScrollDisabled = function () {
214 return this.disabledScroll.size > 0;
215 };
216 /**
217 * @param {?} gestureName
218 * @return {?}
219 */
220 GestureController.prototype.isDisabled = function (gestureName) {
221 var /** @type {?} */ disabled = this.disabledGestures[gestureName];
222 return !!(disabled && disabled.size > 0);
223 };
224 return GestureController;
225}());
226export { GestureController };
227GestureController.decorators = [
228 { type: Injectable },
229];
230/**
231 * @nocollapse
232 */
233GestureController.ctorParameters = function () { return [
234 { type: App, decorators: [{ type: Inject, args: [forwardRef(function () { return App; }),] },] },
235]; };
236function GestureController_tsickle_Closure_declarations() {
237 /** @type {?} */
238 GestureController.decorators;
239 /**
240 * @nocollapse
241 * @type {?}
242 */
243 GestureController.ctorParameters;
244 /** @type {?} */
245 GestureController.prototype.id;
246 /** @type {?} */
247 GestureController.prototype.requestedStart;
248 /** @type {?} */
249 GestureController.prototype.disabledGestures;
250 /** @type {?} */
251 GestureController.prototype.disabledScroll;
252 /** @type {?} */
253 GestureController.prototype.capturedID;
254 /** @type {?} */
255 GestureController.prototype._app;
256}
257/**
258 * @hidden
259 */
260var GestureDelegate = (function () {
261 /**
262 * @param {?} name
263 * @param {?} id
264 * @param {?} controller
265 * @param {?} priority
266 * @param {?} disableScroll
267 */
268 function GestureDelegate(name, id, controller, priority, disableScroll) {
269 this.name = name;
270 this.id = id;
271 this.controller = controller;
272 this.priority = priority;
273 this.disableScroll = disableScroll;
274 }
275 /**
276 * @return {?}
277 */
278 GestureDelegate.prototype.canStart = function () {
279 if (!this.controller) {
280 (void 0) /* assert */;
281 return false;
282 }
283 return this.controller.canStart(this.name);
284 };
285 /**
286 * @return {?}
287 */
288 GestureDelegate.prototype.start = function () {
289 if (!this.controller) {
290 (void 0) /* assert */;
291 return false;
292 }
293 return this.controller.start(this.name, this.id, this.priority);
294 };
295 /**
296 * @return {?}
297 */
298 GestureDelegate.prototype.capture = function () {
299 if (!this.controller) {
300 (void 0) /* assert */;
301 return false;
302 }
303 var /** @type {?} */ captured = this.controller.capture(this.name, this.id, this.priority);
304 if (captured && this.disableScroll) {
305 this.controller.disableScroll(this.id);
306 }
307 return captured;
308 };
309 /**
310 * @return {?}
311 */
312 GestureDelegate.prototype.release = function () {
313 if (!this.controller) {
314 (void 0) /* assert */;
315 return;
316 }
317 this.controller.release(this.id);
318 if (this.disableScroll) {
319 this.controller.enableScroll(this.id);
320 }
321 };
322 /**
323 * @return {?}
324 */
325 GestureDelegate.prototype.destroy = function () {
326 this.release();
327 this.controller = null;
328 };
329 return GestureDelegate;
330}());
331export { GestureDelegate };
332function GestureDelegate_tsickle_Closure_declarations() {
333 /** @type {?} */
334 GestureDelegate.prototype.name;
335 /** @type {?} */
336 GestureDelegate.prototype.id;
337 /** @type {?} */
338 GestureDelegate.prototype.controller;
339 /** @type {?} */
340 GestureDelegate.prototype.priority;
341 /** @type {?} */
342 GestureDelegate.prototype.disableScroll;
343}
344/**
345 * @hidden
346 */
347var BlockerDelegate = (function () {
348 /**
349 * @param {?} id
350 * @param {?} controller
351 * @param {?} disable
352 * @param {?} disableScroll
353 */
354 function BlockerDelegate(id, controller, disable, disableScroll) {
355 this.id = id;
356 this.controller = controller;
357 this.disable = disable;
358 this.disableScroll = disableScroll;
359 this.blocked = false;
360 }
361 /**
362 * @return {?}
363 */
364 BlockerDelegate.prototype.block = function () {
365 var _this = this;
366 if (!this.controller) {
367 (void 0) /* assert */;
368 return;
369 }
370 if (this.disable) {
371 this.disable.forEach(function (gesture) {
372 _this.controller.disableGesture(gesture, _this.id);
373 });
374 }
375 if (this.disableScroll) {
376 this.controller.disableScroll(this.id);
377 }
378 this.blocked = true;
379 };
380 /**
381 * @return {?}
382 */
383 BlockerDelegate.prototype.unblock = function () {
384 var _this = this;
385 if (!this.controller) {
386 (void 0) /* assert */;
387 return;
388 }
389 if (this.disable) {
390 this.disable.forEach(function (gesture) {
391 _this.controller.enableGesture(gesture, _this.id);
392 });
393 }
394 if (this.disableScroll) {
395 this.controller.enableScroll(this.id);
396 }
397 this.blocked = false;
398 };
399 /**
400 * @return {?}
401 */
402 BlockerDelegate.prototype.destroy = function () {
403 this.unblock();
404 this.controller = null;
405 };
406 return BlockerDelegate;
407}());
408export { BlockerDelegate };
409function BlockerDelegate_tsickle_Closure_declarations() {
410 /** @type {?} */
411 BlockerDelegate.prototype.blocked;
412 /** @type {?} */
413 BlockerDelegate.prototype.id;
414 /** @type {?} */
415 BlockerDelegate.prototype.controller;
416 /** @type {?} */
417 BlockerDelegate.prototype.disable;
418 /** @type {?} */
419 BlockerDelegate.prototype.disableScroll;
420}
421//# sourceMappingURL=gesture-controller.js.map
\No newline at end of file