OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 cr.define('options', function() { | 5 cr.define('options', function() { |
6 | 6 |
7 var Preferences = options.Preferences; | 7 var Preferences = options.Preferences; |
8 ///////////////////////////////////////////////////////////////////////////// | 8 ///////////////////////////////////////////////////////////////////////////// |
9 // PrefCheckbox class: | 9 // PrefCheckbox class: |
10 // TODO(jhawkins): Refactor all this copy-pasted code! | 10 // TODO(jhawkins): Refactor all this copy-pasted code! |
11 | 11 |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
238 }); | 238 }); |
239 } | 239 } |
240 }; | 240 }; |
241 | 241 |
242 ///////////////////////////////////////////////////////////////////////////// | 242 ///////////////////////////////////////////////////////////////////////////// |
243 // PrefRange class: | 243 // PrefRange class: |
244 | 244 |
245 // Define a constructor that uses an input element as its underlying element. | 245 // Define a constructor that uses an input element as its underlying element. |
246 var PrefRange = cr.ui.define('input'); | 246 var PrefRange = cr.ui.define('input'); |
247 | 247 |
248 PrefRange.prototype = { | 248 PrefRange.prototype = { |
csilv
2011/01/11 21:48:43
Just curious... I believe PrefRange is used in CrO
James Hawkins
2011/01/11 23:45:54
The only thing I removed from this class was updat
| |
249 // Set up the prototype chain | 249 // Set up the prototype chain |
250 __proto__: PrefNumeric.prototype, | 250 __proto__: HTMLInputElement.prototype, |
251 | |
252 /** | |
253 * The map from input range value to the corresponding preference value. | |
254 * @private | |
255 */ | |
256 valueMap_: undefined, | |
257 | |
258 /** | |
259 * If true, the associated pref will be modified on each onchange event; | |
260 * otherwise, the pref will only be modified on the onmouseup event after | |
arv (Not doing code reviews)
2011/01/11 21:50:35
Using mouseup seems wrong since it will not work w
James Hawkins
2011/01/11 23:45:54
Not wrong, just not completely right ;-) Added ke
| |
261 * the drag. | |
262 * @private | |
263 */ | |
264 continuous_: true, | |
251 | 265 |
252 /** | 266 /** |
253 * Initialization function for the cr.ui framework. | 267 * Initialization function for the cr.ui framework. |
254 */ | 268 */ |
255 decorate: function() { | 269 decorate: function() { |
256 this.type = 'range'; | 270 this.type = 'range'; |
257 PrefNumeric.prototype.decorate.call(this); | |
258 var self = this; | |
259 | 271 |
260 // Additionally change the indicator as well. | 272 // Update the UI when the pref changes. |
261 Preferences.getInstance().addEventListener(this.pref, | 273 Preferences.getInstance().addEventListener( |
262 function(event) { | 274 this.pref, this.onPrefChange_.bind(this)); |
263 self.updateIndicator(); | |
264 }); | |
265 | 275 |
266 // Listen to user events. | 276 // Listen to user events. |
267 this.addEventListener('input', | 277 this.onchange = this.onChange_.bind(this); |
268 function(e) { | 278 this.onmouseup = this.onMouseUp_.bind(this); |
269 this.updateIndicator(); | |
270 }); | |
271 }, | 279 }, |
272 | 280 |
273 updateIndicator: function() { | 281 /** |
274 if ($(this.id + '-value')) { | 282 * Event listener that updates the UI when the underlying pref changes. |
275 $(this.id + '-value').textContent = this.value; | 283 * @param {Event} event The event that details the pref change. |
276 } | 284 * @private |
277 } | 285 */ |
286 onPrefChange_: function(event) { | |
287 var value = event.value && event.value['value'] != undefined ? | |
arv (Not doing code reviews)
2011/01/11 21:50:35
I see these lines all over. Maybe we could provide
James Hawkins
2011/01/11 23:45:54
I have a plan to refactor this, though it's large
| |
288 event.value['value'] : event.value; | |
289 if (value != undefined) | |
290 this.value = this.valueMap ? this.valueMap.indexOf(value) : value; | |
291 }, | |
292 | |
293 /** | |
294 * onchange handler that sets the pref when the user changes the value of | |
295 * the input element. | |
296 * @private | |
297 */ | |
298 onChange_: function(event) { | |
299 if (this.continuous) | |
300 this.setRangePref_(); | |
301 | |
302 this.notifyChange(this, this.mapValueToRange_(this.value)); | |
303 }, | |
304 | |
305 /** | |
306 * Sets the integer value of |pref| to the value of this element. | |
307 * @private | |
308 */ | |
309 setRangePref_: function() { | |
310 Preferences.setIntegerPref( | |
311 this.pref, this.mapValueToRange_(this.value), this.metric); | |
312 }, | |
313 | |
314 /** | |
315 * onmouseup handler that modifies the pref if |continuous_| is false. | |
316 * @private | |
317 */ | |
318 onMouseUp_: function() { | |
319 if (!this.continuous) | |
arv (Not doing code reviews)
2011/01/11 21:50:35
You also need to handle keyboard (and mousewheel?)
James Hawkins
2011/01/11 23:45:54
Done.
| |
320 this.setRangePref_(); | |
321 }, | |
322 | |
323 /** | |
324 * Maps the value of this element into the range provided by the client, | |
325 * represented by |valueMap|. | |
326 * @param {Integer} value The value to map. | |
arv (Not doing code reviews)
2011/01/11 21:50:35
{number}
JS has no int type
James Hawkins
2011/01/11 23:45:54
Done.
| |
327 * @private | |
328 */ | |
329 mapValueToRange_: function(value) { | |
330 return this.valueMap ? this.valueMap[value] : value; | |
331 }, | |
332 | |
333 /** | |
334 * Get and set the value map for the range element. | |
335 * @type {Array} | |
336 */ | |
337 get valueMap() { | |
arv (Not doing code reviews)
2011/01/11 21:50:35
Just use a plain old property if there are no side
James Hawkins
2011/01/11 23:45:54
Done.
| |
338 return this.valueMap_; | |
339 }, | |
340 set valueMap(valueMap) { | |
341 this.valueMap_ = valueMap; | |
342 }, | |
343 | |
344 /** | |
345 * Get and set the continuous_ flag. | |
346 * @type {Boolean} | |
347 */ | |
348 get continuous() { | |
349 return this.continuous_; | |
arv (Not doing code reviews)
2011/01/11 21:50:35
same here
James Hawkins
2011/01/11 23:45:54
Done.
| |
350 }, | |
351 set continuous(continuous) { | |
352 this.continuous_ = continuous; | |
353 }, | |
354 | |
355 /** | |
356 * Called when the client has specified non-continuous mode and the value of | |
357 * the range control changes. | |
358 * @param {Element} el This element. | |
359 * @param {Integer} value The value of this element. | |
360 */ | |
361 notifyChange: function(el, value) { | |
362 }, | |
278 }; | 363 }; |
279 | 364 |
365 /** | |
366 * The preference name. | |
367 * @type {string} | |
368 */ | |
369 cr.defineProperty(PrefRange, 'pref', cr.PropertyKind.ATTR); | |
370 | |
371 /** | |
372 * The user metric string. | |
373 * @type {string} | |
374 */ | |
375 cr.defineProperty(PrefRange, 'metric', cr.PropertyKind.ATTR); | |
376 | |
280 ///////////////////////////////////////////////////////////////////////////// | 377 ///////////////////////////////////////////////////////////////////////////// |
281 // PrefSelect class: | 378 // PrefSelect class: |
282 | 379 |
283 // Define a constructor that uses an select element as its underlying element. | 380 // Define a constructor that uses an select element as its underlying element. |
284 var PrefSelect = cr.ui.define('select'); | 381 var PrefSelect = cr.ui.define('select'); |
285 | 382 |
286 PrefSelect.prototype = { | 383 PrefSelect.prototype = { |
287 // Set up the prototype chain | 384 // Set up the prototype chain |
288 __proto__: HTMLSelectElement.prototype, | 385 __proto__: HTMLSelectElement.prototype, |
289 | 386 |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
424 PrefCheckbox: PrefCheckbox, | 521 PrefCheckbox: PrefCheckbox, |
425 PrefNumber: PrefNumber, | 522 PrefNumber: PrefNumber, |
426 PrefNumeric: PrefNumeric, | 523 PrefNumeric: PrefNumeric, |
427 PrefRadio: PrefRadio, | 524 PrefRadio: PrefRadio, |
428 PrefRange: PrefRange, | 525 PrefRange: PrefRange, |
429 PrefSelect: PrefSelect, | 526 PrefSelect: PrefSelect, |
430 PrefTextField: PrefTextField | 527 PrefTextField: PrefTextField |
431 }; | 528 }; |
432 | 529 |
433 }); | 530 }); |
OLD | NEW |