diff options
author | Pierre Frisch | 2011-12-22 07:25:50 -0800 |
---|---|---|
committer | Valerio Virgillito | 2012-01-27 11:18:17 -0800 |
commit | b89a7ee8b956c96a1dcee995ea840feddc5d4b27 (patch) | |
tree | 0f3136ab0ecdbbbed6a83576581af0a53124d6f1 /js/components/SliderBase.js | |
parent | 2401f05d1f4b94d45e4568b81fc73e67b969d980 (diff) | |
download | ninja-b89a7ee8b956c96a1dcee995ea840feddc5d4b27.tar.gz |
First commit of Ninja to ninja-internal
Signed-off-by: Valerio Virgillito <rmwh84@motorola.com>
Diffstat (limited to 'js/components/SliderBase.js')
-rw-r--r-- | js/components/SliderBase.js | 282 |
1 files changed, 282 insertions, 0 deletions
diff --git a/js/components/SliderBase.js b/js/components/SliderBase.js new file mode 100644 index 00000000..9f007456 --- /dev/null +++ b/js/components/SliderBase.js | |||
@@ -0,0 +1,282 @@ | |||
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 | // Slider, HotText and HotTextUnit will extend this class. | ||
8 | var Montage = require("montage/core/core").Montage; | ||
9 | var Component = require("montage/ui/component").Component; | ||
10 | |||
11 | var SliderBase = exports.SliderBase = Montage.create(Component, { | ||
12 | |||
13 | _hasFocus: { | ||
14 | enumerable: false, | ||
15 | value: false | ||
16 | }, | ||
17 | |||
18 | _touchIdentifier: { | ||
19 | enumerable: false, | ||
20 | value: null | ||
21 | }, | ||
22 | |||
23 | _xStart: { | ||
24 | enumerable: false, | ||
25 | value: 0 | ||
26 | }, | ||
27 | |||
28 | _yStart: { | ||
29 | enumerable: false, | ||
30 | value: 0 | ||
31 | }, | ||
32 | |||
33 | _previousValue: { | ||
34 | enumerable: false, | ||
35 | value: null | ||
36 | }, | ||
37 | |||
38 | _isMouseDown: { | ||
39 | enumerable: true, | ||
40 | value: false | ||
41 | }, | ||
42 | |||
43 | // We need to calculate some metrics on the first draw after prepareForDraw because | ||
44 | // styles values are not available during prepareForDraw | ||
45 | _firstTime: { | ||
46 | enumerable: false, | ||
47 | value: true | ||
48 | }, | ||
49 | |||
50 | _enabled: { | ||
51 | enumerable: true, | ||
52 | value: true | ||
53 | }, | ||
54 | |||
55 | enabled: { | ||
56 | enumerable: true, | ||
57 | get: function() { | ||
58 | return this._enabled; | ||
59 | }, | ||
60 | set: function(value) { | ||
61 | |||
62 | if (value !== this._enabled) { | ||
63 | this._enabled = value; | ||
64 | if(this._enabled) | ||
65 | { | ||
66 | this.element.classList.remove("disabled"); | ||
67 | |||
68 | this.element.addEventListener("blur", this); | ||
69 | this.element.addEventListener("focus", this); | ||
70 | |||
71 | this.element.addEventListener("touchstart", this, false); | ||
72 | this.element.addEventListener("mousedown", this, false); | ||
73 | } | ||
74 | else | ||
75 | { | ||
76 | this.element.classList.add("disabled"); | ||
77 | |||
78 | this.element.removeEventListener("blur", this); | ||
79 | this.element.removeEventListener("focus", this); | ||
80 | |||
81 | this.element.removeEventListener("touchstart", this, false); | ||
82 | this.element.removeEventListener("mousedown", this, false); | ||
83 | } | ||
84 | this.needsDraw = true; | ||
85 | } | ||
86 | } | ||
87 | }, | ||
88 | |||
89 | // Internal flags to determine what the change/changing events will contain | ||
90 | _eventType: { | ||
91 | enumerable: false, | ||
92 | value: "change" | ||
93 | }, | ||
94 | |||
95 | _wasSetByCode: { | ||
96 | enumerable: false, | ||
97 | value: true | ||
98 | }, | ||
99 | |||
100 | _setEventFlags: { | ||
101 | value: function (eventType, wasSetByCode) { | ||
102 | this._eventType = eventType; | ||
103 | this._wasSetByCode = wasSetByCode; | ||
104 | } | ||
105 | }, | ||
106 | |||
107 | _value: { | ||
108 | enumerable: false, | ||
109 | value: 0 | ||
110 | }, | ||
111 | |||
112 | value: { | ||
113 | enumerable: true, | ||
114 | get: function() { | ||
115 | return this._value; | ||
116 | }, | ||
117 | set: function(value) { | ||
118 | if (isNaN(value)) { | ||
119 | return; | ||
120 | } | ||
121 | |||
122 | if (value < this._minValue) { | ||
123 | value = this._minValue; | ||
124 | } | ||
125 | else if (value > this._maxValue) { | ||
126 | value = this._maxValue; | ||
127 | } | ||
128 | |||
129 | if (value !== this._value) { | ||
130 | this._value = value; | ||
131 | this.needsDraw = true; | ||
132 | this._dispatchActionEvent(); | ||
133 | } | ||
134 | } | ||
135 | }, | ||
136 | |||
137 | _minValue: { | ||
138 | enumerable: false, | ||
139 | value: 0 | ||
140 | }, | ||
141 | |||
142 | minValue: { | ||
143 | get: function() { | ||
144 | return this._minValue; | ||
145 | }, | ||
146 | set: function(value) { | ||
147 | if (value !== this._minValue) { | ||
148 | this._minValue = value; | ||
149 | this._firstTime = true; // Force layout recalculation. | ||
150 | if (this._value < value) | ||
151 | { | ||
152 | this.value = value; | ||
153 | } | ||
154 | this.needsDraw = true; | ||
155 | } | ||
156 | } | ||
157 | }, | ||
158 | |||
159 | _maxValue: { | ||
160 | enumerable: false, | ||
161 | value: 100 | ||
162 | }, | ||
163 | |||
164 | maxValue: { | ||
165 | get: function() { | ||
166 | return this._maxValue; | ||
167 | }, | ||
168 | set: function(value) { | ||
169 | if (value !== this._maxValue) { | ||
170 | this._maxValue = value; | ||
171 | this._firstTime = true; // Force layout recalculation. | ||
172 | if (this._value > value) | ||
173 | { | ||
174 | this.value = value; | ||
175 | } | ||
176 | this.needsDraw = true; | ||
177 | } | ||
178 | } | ||
179 | }, | ||
180 | |||
181 | _valueCoef: { | ||
182 | value: 1 | ||
183 | }, | ||
184 | |||
185 | _valueFromPageOffset: { | ||
186 | value: function(offset, pageY, isShiftKeyPressed) { | ||
187 | // Implement in subclass. | ||
188 | } | ||
189 | }, | ||
190 | |||
191 | handleMousedown: { | ||
192 | value: function (event) { | ||
193 | // event.preventDefault(); // Commenting out -- other elements need to blur when we are moused down | ||
194 | var clickPoint = webkitConvertPointFromPageToNode(this.element, new WebKitPoint(event.pageX, event.pageY)); | ||
195 | this._xStart = clickPoint.x; | ||
196 | this._yStart = clickPoint.y; | ||
197 | this._previousValue = this.value; | ||
198 | this._isMouseDown = true; | ||
199 | document.addEventListener("mousemove", this, false); | ||
200 | document.addEventListener("mouseup", this, false); | ||
201 | } | ||
202 | }, | ||
203 | |||
204 | handleTouchstart: { | ||
205 | value: function (event) { | ||
206 | event.preventDefault(); // Not sure how to let other elements blur if we need to override mobile behavior | ||
207 | var touch = event.targetTouches[0]; | ||
208 | this._touchIdentifier = touch.identifier; | ||
209 | var clickPoint = webkitConvertPointFromPageToNode(this.element, new WebKitPoint(touch.pageX,touch.pageY)); | ||
210 | this._xStart = clickPoint.x; | ||
211 | this._yStart = clickPoint.y; | ||
212 | this._previousValue = this.value; | ||
213 | this._isMouseDown = true; | ||
214 | document.addEventListener("touchmove", this, false); | ||
215 | document.addEventListener("touchend", this, false); | ||
216 | } | ||
217 | }, | ||
218 | |||
219 | handleMousemove: { | ||
220 | value: function (event) { | ||
221 | event.target.style.cursor = "pointer"; | ||
222 | this._setEventFlags("changing", false); | ||
223 | this._valueFromPageOffset(event.pageX, event.pageY, event.shiftKey); | ||
224 | } | ||
225 | }, | ||
226 | |||
227 | handleTouchmove: { | ||
228 | value: function (event) { | ||
229 | for(var i=0, iTouch; iTouch = event.changedTouches[i]; i++) { | ||
230 | if (iTouch.identifier === this._touchIdentifier) { | ||
231 | this._setEventFlags("changing", false); | ||
232 | this._valueFromPageOffset(iTouch.pageX, iTouch.pageY, false); | ||
233 | break; | ||
234 | } | ||
235 | } | ||
236 | } | ||
237 | }, | ||
238 | |||
239 | handleMouseup: { | ||
240 | value: function (event) { | ||
241 | document.removeEventListener("mousemove", this); | ||
242 | document.removeEventListener("mouseup", this); | ||
243 | this._isMouseDown = false; | ||
244 | event.target.style.cursor = "default"; | ||
245 |