diff options
Diffstat (limited to 'node_modules/montage/ui/composer/press-composer.js')
-rw-r--r-- | node_modules/montage/ui/composer/press-composer.js | 126 |
1 files changed, 111 insertions, 15 deletions
diff --git a/node_modules/montage/ui/composer/press-composer.js b/node_modules/montage/ui/composer/press-composer.js index 4a6b99b2..680c301d 100644 --- a/node_modules/montage/ui/composer/press-composer.js +++ b/node_modules/montage/ui/composer/press-composer.js | |||
@@ -20,11 +20,11 @@ var PressComposer = exports.PressComposer = Montage.create(Composer,/** @lends m | |||
20 | 20 | ||
21 | /** | 21 | /** |
22 | @event | 22 | @event |
23 | @name pressstart | 23 | @name pressStart |
24 | @param {Event} event | 24 | @param {Event} event |
25 | 25 | ||
26 | Dispatched when a press begins. It is ended by either a {@link press} or | 26 | Dispatched when a press begins. It is ended by either a {@link press} or |
27 | {@link presscancel} event. | 27 | {@link pressCancel} event. |
28 | */ | 28 | */ |
29 | 29 | ||
30 | /** | 30 | /** |
@@ -37,7 +37,15 @@ var PressComposer = exports.PressComposer = Montage.create(Composer,/** @lends m | |||
37 | 37 | ||
38 | /** | 38 | /** |
39 | @event | 39 | @event |
40 | @name presscancel | 40 | @name longPress |
41 | @param {Event} event | ||
42 | |||
43 | Dispatched when a press lasts for longer than (@link longPressTimeout} | ||
44 | */ | ||
45 | |||
46 | /** | ||
47 | @event | ||
48 | @name pressCancel | ||
41 | @param {Event} event | 49 | @param {Event} event |
42 | 50 | ||
43 | Dispatched when a press is canceled. This could be because the pointer | 51 | Dispatched when a press is canceled. This could be because the pointer |
@@ -67,6 +75,35 @@ var PressComposer = exports.PressComposer = Montage.create(Composer,/** @lends m | |||
67 | } | 75 | } |
68 | }, | 76 | }, |
69 | 77 | ||
78 | /** | ||
79 | Cancel the current press. | ||
80 | |||
81 | Can be used in a "longPress" event handler to prevent the "press" event | ||
82 | being fired. | ||
83 | @returns Boolean true if a press was canceled, false if the composer was | ||
84 | already in a unpressed or canceled state. | ||
85 | */ | ||
86 | cancelPress: { | ||
87 | value: function() { | ||
88 | if (this._state === PressComposer.PRESSED) { | ||
89 | this._dispatchPressCancel(); | ||
90 | this._endInteraction(); | ||
91 | return true; | ||
92 | } | ||
93 | return false; | ||
94 | } | ||
95 | }, | ||
96 | |||
97 | // Optimisation so that we don't set a timeout if we do not need to | ||
98 | addEventListener: { | ||
99 | value: function(type, listener, useCapture) { | ||
100 | Composer.addEventListener.call(this, type, listener, useCapture); | ||
101 | if (type === "longPress") { | ||
102 | this._shouldDispatchLongPress = true; | ||
103 | } | ||
104 | } | ||
105 | }, | ||
106 | |||
70 | UNPRESSED: { | 107 | UNPRESSED: { |
71 | value: 0 | 108 | value: 0 |
72 | }, | 109 | }, |
@@ -87,6 +124,34 @@ var PressComposer = exports.PressComposer = Montage.create(Composer,/** @lends m | |||
87 | } | 124 | } |
88 | }, | 125 | }, |
89 | 126 | ||
127 | _shouldDispatchLongPress: { | ||
128 | enumerable: false, | ||
129 | value: false | ||
130 | }, | ||
131 | |||
132 | _longPressTimeout: { | ||
133 | enumerable: false, | ||
134 | value: 1000 | ||
135 | }, | ||
136 | /** | ||
137 | How long a press has to last for a longPress event to be dispatched | ||
138 | */ | ||
139 | longPressTimeout: { | ||
140 | get: function() { | ||
141 | return this._longPressTimeout; | ||
142 | }, | ||
143 | set: function(value) { | ||
144 | if (this._longPressTimeout !== value) { | ||
145 | this._longPressTimeout = value; | ||
146 | } | ||
147 | } | ||
148 | }, | ||
149 | |||
150 | _longPressTimer: { | ||
151 | enumberable: false, | ||
152 | value: null | ||
153 | }, | ||
154 | |||
90 | // Magic | 155 | // Magic |
91 | 156 | ||
92 | /** | 157 | /** |
@@ -140,7 +205,7 @@ var PressComposer = exports.PressComposer = Montage.create(Composer,/** @lends m | |||
140 | 205 | ||
141 | this.component.eventManager.claimPointer(this._observedPointer, this); | 206 | this.component.eventManager.claimPointer(this._observedPointer, this); |
142 | 207 | ||
143 | this._dispatchPressstart(event); | 208 | this._dispatchPressStart(event); |
144 | } | 209 | } |
145 | }, | 210 | }, |
146 | 211 | ||
@@ -165,12 +230,12 @@ var PressComposer = exports.PressComposer = Montage.create(Composer,/** @lends m | |||
165 | while (target !== this._element && target && target.parentNode) { | 230 | while (target !== this._element && target && target.parentNode) { |
166 | target = target.parentNode; | 231 | target = target.parentNode; |
167 | } | 232 | } |
168 | isTarget = target === this.component.element; | 233 | isTarget = target === this._element; |
169 | 234 | ||
170 | if (isSurrendered && event.type === "click") { | 235 | if (isSurrendered && event.type === "click") { |
171 | // Pointer surrendered, so prevent the default action | 236 | // Pointer surrendered, so prevent the default action |
172 | event.preventDefault(); | 237 | event.preventDefault(); |
173 | // No need to dispatch an event as presscancel was dispatched | 238 | // No need to dispatch an event as pressCancel was dispatched |
174 | // in surrenderPointer, just end the interaction. | 239 | // in surrenderPointer, just end the interaction. |
175 | this._endInteraction(event); | 240 | this._endInteraction(event); |
176 | return; | 241 | return; |
@@ -183,7 +248,7 @@ var PressComposer = exports.PressComposer = Montage.create(Composer,/** @lends m | |||
183 | } | 248 | } |
184 | 249 | ||
185 | if (!isSurrendered && !isTarget && event.type === "mouseup") { | 250 | if (!isSurrendered && !isTarget && event.type === "mouseup") { |
186 | this._dispatchPresscancel(event); | 251 | this._dispatchPressCancel(event); |
187 | this._endInteraction(event); | 252 | this._endInteraction(event); |
188 | return; | 253 | return; |
189 | } | 254 | } |
@@ -247,7 +312,7 @@ var PressComposer = exports.PressComposer = Montage.create(Composer,/** @lends m | |||
247 | return false; | 312 | return false; |
248 | } | 313 | } |
249 | 314 | ||
250 | this._dispatchPresscancel(); | 315 | this._dispatchPressCancel(); |
251 | return true; | 316 | return true; |
252 | } | 317 | } |
253 | }, | 318 | }, |
@@ -280,7 +345,7 @@ var PressComposer = exports.PressComposer = Montage.create(Composer,/** @lends m | |||
280 | value: function(event) { | 345 | value: function(event) { |
281 | if (this._observedPointer === null || this._changedTouchisObserved(event.changedTouches) !== false) { | 346 | if (this._observedPointer === null || this._changedTouchisObserved(event.changedTouches) !== false) { |
282 | if (this.component.eventManager.isPointerClaimedByComponent(this._observedPointer, this)) { | 347 | if (this.component.eventManager.isPointerClaimedByComponent(this._observedPointer, this)) { |
283 | this._dispatchPresscancel(event); | 348 | this._dispatchPressCancel(event); |
284 | } | 349 | } |
285 | this._endInteraction(event); | 350 | this._endInteraction(event); |
286 | } | 351 | } |
@@ -331,14 +396,21 @@ var PressComposer = exports.PressComposer = Montage.create(Composer,/** @lends m | |||
331 | }, | 396 | }, |
332 | 397 | ||
333 | /** | 398 | /** |
334 | Dispatch the pressstart event | 399 | Dispatch the pressStart event |
335 | @private | 400 | @private |
336 | */ | 401 | */ |
337 | _dispatchPressstart: { | 402 | _dispatchPressStart: { |
338 | enumerable: false, | 403 | enumerable: false, |
339 | value: function (event) { | 404 | value: function (event) { |
340 | this._state = PressComposer.PRESSED; | 405 | this._state = PressComposer.PRESSED; |
341 | this.dispatchEvent(this._createPressEvent("pressstart", event)); | 406 | this.dispatchEvent(this._createPressEvent("pressStart", event)); |
407 | |||
408 | if (this._shouldDispatchLongPress) { | ||
409 | var self = this; | ||
410 | this._longPressTimer = setTimeout(function () { | ||
411 | self._dispatchLongPress(); | ||
412 | }, this._longPressTimeout); | ||
413 | } | ||
342 | } | 414 | } |
343 | }, | 415 | }, |
344 | 416 | ||
@@ -349,20 +421,44 @@ var PressComposer = exports.PressComposer = Montage.create(Composer,/** @lends m | |||
349 | _dispatchPress: { | 421 | _dispatchPress: { |
350 | enumerable: false, | 422 | enumerable: false, |
351 | value: function (event) { | 423 | value: function (event) { |
424 | if (this._shouldDispatchLongPress) { | ||
425 | clearTimeout(this._longPressTimer); | ||
426 | this._longPressTimer = null; | ||
427 | } | ||
428 | |||
352 | this.dispatchEvent(this._createPressEvent("press", event)); | 429 | this.dispatchEvent(this._createPress |