diff options
author | Pushkar Joshi | 2012-03-12 15:26:10 -0700 |
---|---|---|
committer | Pushkar Joshi | 2012-03-12 15:26:10 -0700 |
commit | 46b2e561fa6ca054cad58e4c372a598bbb7ee2c9 (patch) | |
tree | 38d1c8666098a10078834c1eaac3d455135bee12 /node_modules/montage/ui/skeleton/range-input.reel/range-input.js | |
parent | 7b4b068cadc0af8ec7e930e1c2b429e945f96984 (diff) | |
parent | 69d90467865a1384725b2301901be2180c5a841f (diff) | |
download | ninja-46b2e561fa6ca054cad58e4c372a598bbb7ee2c9.tar.gz |
Merge branch 'master' into brushtool
Diffstat (limited to 'node_modules/montage/ui/skeleton/range-input.reel/range-input.js')
-rw-r--r-- | node_modules/montage/ui/skeleton/range-input.reel/range-input.js | 234 |
1 files changed, 234 insertions, 0 deletions
diff --git a/node_modules/montage/ui/skeleton/range-input.reel/range-input.js b/node_modules/montage/ui/skeleton/range-input.reel/range-input.js new file mode 100644 index 00000000..5747ccd6 --- /dev/null +++ b/node_modules/montage/ui/skeleton/range-input.reel/range-input.js | |||
@@ -0,0 +1,234 @@ | |||
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 | /*global require,exports */ | ||
7 | var Montage = require("montage").Montage, | ||
8 | Component = require("ui/component").Component; | ||
9 | |||
10 | /** | ||
11 | * The input type="range" field | ||
12 | */ | ||
13 | var RangeInput = exports.RangeInput = Montage.create(Component, { | ||
14 | |||
15 | DEFAULT_WIDTH: {value: 300}, | ||
16 | HANDLE_ADJUST: {value: 5}, | ||
17 | |||
18 | hasTemplate: {value: true}, | ||
19 | |||
20 | // public API | ||
21 | _min: {value: null}, | ||
22 | min: { | ||
23 | get: function() { | ||
24 | return this._min; | ||
25 | }, | ||
26 | set: function(value) { | ||
27 | this._min = String.isString(value) ? parseFloat(value) : value; | ||
28 | this.needsDraw = true; | ||
29 | } | ||
30 | }, | ||
31 | |||
32 | _max: {value: null}, | ||
33 | max: { | ||
34 | get: function() { | ||
35 | return this._max; | ||
36 | }, | ||
37 | set: function(value) { | ||
38 | this._max = String.isString(value) ? parseFloat(value) : value; | ||
39 | this.needsDraw = true; | ||
40 | } | ||
41 | }, | ||
42 | |||
43 | _step: {value: null}, | ||
44 | step: { | ||
45 | get: function() { | ||
46 | return this._step; | ||
47 | }, | ||
48 | set: function(value) { | ||
49 | this._step = String.isString(value) ? parseFloat(value) : value; | ||
50 | this.needsDraw = true; | ||
51 | } | ||
52 | }, | ||
53 | |||
54 | /** Width of the slider in px. Default = 300 */ | ||
55 | _width: {value: null}, | ||
56 | width: { | ||
57 | get: function() { | ||
58 | return this._width; | ||
59 | }, | ||
60 | set: function(value) { | ||
61 | this._width = String.isString(value) ? parseFloat(value) : value; | ||
62 | this.needsDraw = true; | ||
63 | } | ||
64 | }, | ||
65 | |||
66 | |||
67 | _sliding: {value: false}, | ||
68 | |||
69 | percent: {value: null}, | ||
70 | _valueSyncedWithPosition: {value: null}, | ||
71 | _value: {value: null}, | ||
72 | value: { | ||
73 | get: function() { | ||
74 | return this._value; | ||
75 | }, | ||
76 | set: function(value, fromInput) { | ||
77 | this._value = String.isString(value) ? parseFloat(value) : value; | ||
78 | |||
79 | if(fromInput) { | ||
80 | this._valueSyncedWithPosition = true; | ||
81 | } else { | ||
82 | this._valueSyncedWithPosition = false; | ||
83 | this._calculatePositionFromValue(); | ||
84 | this.needsDraw = true; | ||
85 | } | ||
86 | } | ||
87 | }, | ||
88 | |||
89 | // private | ||
90 | sliderEl: {value: null, enumerable: false}, | ||
91 | handleEl: {value: null, enumerable: false}, | ||
92 | sliderLeft: {value: null, enumerable: false}, | ||
93 | sliderWidth: {value: null, enumerable: false}, | ||
94 | minX: {value: null, enumerable: false}, | ||
95 | maxX: {value: null, enumerable: false}, | ||
96 | |||
97 | _positionX: {value: null}, | ||
98 | positionX: { | ||
99 | enumerable: false, | ||
100 | get: function() { | ||
101 | return this._positionX; | ||
102 | }, | ||
103 | set: function(value, fromValue) { | ||
104 | |||
105 | if(value !== null && !isNaN(value)) { | ||
106 | this._positionX = value; | ||
107 | if(!fromValue) { | ||
108 | this._calculateValueFromPosition(); | ||
109 | this._valueSyncedWithPosition = true; | ||
110 | } | ||
111 | this.needsDraw = true; | ||
112 | } | ||
113 | |||
114 | } | ||
115 | }, | ||
116 | |||
117 | _calculateValueFromPosition: { | ||
118 | value: function() { | ||
119 | if(this.sliderWidth > 0) { | ||
120 | var percent = this.percent = (this.positionX / this.sliderWidth) * 100; | ||
121 | var value = (this.min + ((percent/100) * (this.max - this.min))); | ||
122 | Object.getPropertyDescriptor(this, "value").set.call(this, value, true); | ||
123 | } | ||
124 | |||
125 | } | ||
126 | }, | ||
127 | |||
128 | _calculatePositionFromValue: { | ||
129 | value: function() { | ||
130 | // unless the element is ready, we cannot position the handle | ||
131 | if(this.sliderWidth) { | ||
132 | var percent, value = this.value; | ||
133 | var range = (this.max - this.min); | ||
134 | percent = ((this.value-this.min)/range) * 100; | ||
135 | var positionX = (percent/100)*this.sliderWidth; | ||
136 | Object.getPropertyDescriptor(this, "positionX").set.call(this, positionX, true); | ||
137 | this.percent = percent; | ||
138 | this._valueSyncedWithPosition = true; | ||
139 | } else { | ||
140 | this._valueSyncedWithPosition = false; | ||
141 | } | ||
142 | } | ||
143 | }, | ||
144 | |||
145 | deserializedFromTemplate: { | ||
146 | value: function() { | ||
147 | |||
148 | // read initial values from the input type=range | ||
149 | |||
150 | this.min = this.min || this.element.getAttribute('min') || 0; | ||
151 | this.max = this.max || this.element.getAttribute('max') || 100; | ||
152 | this.step = this.step || this.element.getAttribute('step') || 1; | ||
153 | this.value = this.value || this.element.getAttribute('value') || 0; | ||
154 | |||
155 | |||
156 | } | ||
157 | }, | ||
158 | |||
159 | prepareForDraw: { | ||
160 | value: function() { | ||
161 | this.minX = this.sliderLeft = this.element.offsetLeft; | ||
162 | this.sliderWidth = (this.width || RangeInput.DEFAULT_WIDTH); //this.element.offsetWidth || 300; | ||
163 | this.element.style.width = (this.sliderWidth + RangeInput.HANDLE_ADJUST) + 'px'; | ||
164 | |||
165 | this.maxX = this.sliderLeft + this.sliderWidth; | ||
166 | |||
167 | if(!this._valueSyncedWithPosition) { | ||
168 | this._calculatePositionFromValue(); | ||
169 | } | ||
170 | } | ||
171 | }, | ||
172 | |||
173 | // @todo: Without prepareForActivationEvents, the translateComposer does not work | ||
174 | prepareForActivationEvents: { | ||
175 | value: function() { | ||
176 | this.translateComposer.addEventListener('translateStart', this, false); | ||
177 | this.translateComposer.addEventListener('translateEnd', this, false); | ||
178 | } | ||
179 | }, | ||
180 | |||
181 | handleTranslateStart: { | ||
182 | value: function(e) { | ||
183 | this._valueSyncedWithPosition = false; | ||
184 | this._sliding = true; | ||
185 | } | ||
186 | }, | ||
187 | |||
188 | handleTranslateEnd: { | ||
189 | value: function(e) { | ||
190 | |||
191 | if(this._sliding === true) { | ||
192 | this._sliding = false; | ||
193 | } else { | ||
194 | // do this only when the user clicks the slider directly instead of | ||
195 | // sliding the handle | ||
196 | var position = this.translateComposer._pointerX; | ||
197 | var positionX = ((position)- this.sliderLeft); | ||
198 | if(positionX > 0 && (positionX <= this.sliderWidth)) { | ||
199 | this.positionX = positionX; | ||
200 | } | ||
201 | |||
202 | } | ||
203 | |||
204< |