diff options
Diffstat (limited to 'node_modules/montage/ui/composer/long-press-composer.js')
-rw-r--r-- | node_modules/montage/ui/composer/long-press-composer.js | 232 |
1 files changed, 232 insertions, 0 deletions
diff --git a/node_modules/montage/ui/composer/long-press-composer.js b/node_modules/montage/ui/composer/long-press-composer.js new file mode 100644 index 00000000..717bb42e --- /dev/null +++ b/node_modules/montage/ui/composer/long-press-composer.js | |||
@@ -0,0 +1,232 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | /** | ||
7 | @module montage/ui/composer/long-press-composer | ||
8 | @requires montage | ||
9 | @requires montage/ui/composer/composer | ||
10 | */ | ||
11 | var Montage = require("montage").Montage, | ||
12 | Composer = require("ui/composer/composer").Composer; | ||
13 | /** | ||
14 | @class module:montage/ui/composer/long-press-composer.LongPressComposer | ||
15 | @extends module:montage/ui/composer/composer.Composer | ||
16 | */ | ||
17 | exports.LongPressComposer = Montage.create(Composer,/** @lends module:montage/ui/event/composer/long-press-composer.LongPressComposer# */ { | ||
18 | |||
19 | /** | ||
20 | Description TODO | ||
21 | @private | ||
22 | */ | ||
23 | _motionThreshold: { | ||
24 | value: 10 | ||
25 | }, | ||
26 | /** | ||
27 | Description TODO | ||
28 | @private | ||
29 | */ | ||
30 | _longpressTimeOut: { | ||
31 | value: 1500 | ||
32 | }, | ||
33 | |||
34 | _longPressTimer: { | ||
35 | value: null | ||
36 | }, | ||
37 | |||
38 | _fingerId: { | ||
39 | value: null | ||
40 | }, | ||
41 | |||
42 | _X: { | ||
43 | value: null | ||
44 | }, | ||
45 | |||
46 | _Y: { | ||
47 | value: null | ||
48 | }, | ||
49 | |||
50 | /** | ||
51 | Description TODO | ||
52 | @function | ||
53 | */ | ||
54 | load: { | ||
55 | value: function () { | ||
56 | if (window.Touch) { | ||
57 | this._element.addEventListener("touchstart", this); | ||
58 | this._element.addEventListener("touchmove", this); | ||
59 | this._element.addEventListener("touchend", this); | ||
60 | } else { | ||
61 | this._element.addEventListener("mousedown", this); | ||
62 | } | ||
63 | } | ||
64 | }, | ||
65 | /** | ||
66 | Description TODO | ||
67 | @function | ||
68 | */ | ||
69 | unload: { | ||
70 | value: function () { | ||
71 | if (window.Touch) { | ||
72 | this._element.removeEventListener("touchstart", this); | ||
73 | this._element.removeEventListener("touchmove", this); | ||
74 | this._element.removeEventListener("touchend", this); | ||
75 | } | ||
76 | else { | ||
77 | this._element.removeEventListener("mousedown", this); | ||
78 | } | ||
79 | } | ||
80 | }, | ||
81 | /** | ||
82 | Description TODO | ||
83 | @function | ||
84 | @param {Event} event This event. | ||
85 | */ | ||
86 | handleTouchstart: { | ||
87 | value: function (event) { | ||
88 | var self = this; | ||
89 | /* If two longpress on same target then the first one will be cleared*/ | ||
90 | if (this._longpressTimer) { | ||
91 | clearTimeout(this._longpressTimer); | ||
92 | this._longpressTimer = null; | ||
93 | } | ||
94 | this._fingerId = event.changedTouches[0].identifier; | ||
95 | this._X = event.changedTouches[0].clientX; | ||
96 | this._Y = event.changedTouches[0].clientY; | ||
97 | this._longpressTimer = null; | ||
98 | this._longpressTimer = setTimeout(function () { | ||
99 | // FIXME should be dispatched against something else | ||
100 | self._dispatchLongPress(self._element); | ||
101 | }, this._longpressTimeOut); | ||
102 | } | ||
103 | }, | ||
104 | /** | ||
105 | Description TODO | ||
106 | @function | ||
107 | @param {Event} event This event. | ||
108 | */ | ||
109 | handleTouchmove: { | ||
110 | value: function (event) { | ||
111 | var i, deltaX, deltaY, len; | ||
112 | /* the longpresstimer is checked so that the flushing of the timer occurs only once even though touchmoves are received */ | ||
113 | if (this._longpressTimer) { | ||
114 | len = event.changedTouches.length; | ||
115 | for (i = 0; i < len; i++) { | ||
116 | /*Checked if two fingers on same target and both are moved */ | ||
117 | if (this._fingerId === event.changedTouches[i].identifier) { | ||
118 | deltaX = Math.abs(event.changedTouches[i].clientX - this._X); | ||
119 | deltaY = Math.abs(event.changedTouches[i].clientY - this._Y); | ||
120 | /* Clearing timer only on some considerable motion */ | ||
121 | if (deltaX > this._motionThreshold || deltaY > this._motionThreshold) { | ||
122 | this._clearLongpress(event.currentTarget); | ||
123 | break; | ||
124 | } | ||
125 | } | ||
126 | } | ||
127 | } | ||
128 | } | ||
129 | }, | ||
130 | /** | ||
131 | Description TODO | ||
132 | @function | ||
133 | @param {Event} event This event. | ||
134 | */ | ||
135 | handleTouchend: { | ||
136 | value: function (event) { | ||
137 | var target = event.currentTarget; | ||
138 | /* longpresstimer checked because end occurs after its move then timer is already cleared. */ | ||
139 | if (this._longpressTimer && this._fingerId === event.changedTouches[0].identifier) { | ||
140 | this._clearLongpress(target); | ||
141 | } | ||
142 | } | ||
143 | }, | ||
144 | /** | ||
145 | Description TODO | ||
146 | @function | ||
147 | @param {Event} event This event. | ||
148 | */ | ||
149 | handleMousedown: { | ||
150 | value: function (event) { | ||
151 | var target = event.currentTarget; | ||
152 | var self = this; | ||
153 | target.addEventListener("mousemove", this); | ||
154 | target.addEventListener("mouseup", this); | ||
155 | target.addEventListener("mouseout", this); | ||
156 | this._fingerId = 0; | ||
157 | this._X = event.clientX; | ||
158 | this._Y = event.clientY; | ||
159 | this._longpressTimer = setTimeout(function () { | ||
160 | self._dispatchLongPress(target); | ||
161 | }, this._longpressTimeOut); | ||
162 | } | ||
163 | }, | ||
164 | /** | ||
165 | Description TODO | ||
166 | @function | ||
167 | @param {Event} event This event. | ||
168 | */ | ||
169 | handleMouseup: { | ||
170 | value: function (event) { | ||
171 | this._clearLongpress(event.currentTarget); | ||
172 | } | ||
173 | }, | ||
174 | /** | ||
175 | Description TODO | ||
176 | @function | ||
177 | @param {Event} event This event. | ||
178 | */ | ||
179 | handleMouseout: { | ||
180 | value: function (event) { | ||
181 | this._clearLongpress(event.currentTarget); | ||
182 | } | ||
183 | }, | ||
184 | /** | ||
185 | Description TODO | ||
186 | @function | ||
187 | @param {Event} event This event. | ||
188 | */ | ||
189 | handleMousemove: { | ||
190 | value: function (event) { | ||
191 | this._clearLongpress(event.currentTarget); | ||
192 | } | ||
193 | }, | ||
194 | /** | ||
195 | Description TODO | ||
196 | @private | ||
197 | */ | ||
198 | _dispatchLongPress: { | ||
199 | value: function (target) { | ||
200 | longPressEvent = document.createEvent("CustomEvent"); | ||
201 | longPressEvent.clientX = this._X; | ||
202 | longPressEvent.clientY = this._Y; | ||
203 | longPressEvent.identifier = this._fingerId; | ||
204 | longPressEvent.initCustomEvent("longpress", true, true, null); | ||
205 | this.dispatchEvent(longPressEvent); | ||
206 | this._clearLongpress(target); | ||
207 | } | ||
208 | }, | ||
209 | /** | ||
210 | Description TODO | ||
211 | @private | ||
212 | */ | ||
213 | _clearLongpress: { | ||
214 | value: function (target) { | ||
215 | if (this._longpressTimer) { | ||
216 | clearTimeout(this._longpressTimer); | ||
217 | this._longpressTimer = null; | ||
218 |