OLD | NEW |
1 // Copyright (c) 2011 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! |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 = { |
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 */ |
| 255 valueMap: undefined, |
| 256 |
| 257 /** |
| 258 * If true, the associated pref will be modified on each onchange event; |
| 259 * otherwise, the pref will only be modified on the onmouseup event after |
| 260 * the drag. |
| 261 */ |
| 262 continuous: true, |
251 | 263 |
252 /** | 264 /** |
253 * Initialization function for the cr.ui framework. | 265 * Initialization function for the cr.ui framework. |
254 */ | 266 */ |
255 decorate: function() { | 267 decorate: function() { |
256 this.type = 'range'; | 268 this.type = 'range'; |
257 PrefNumeric.prototype.decorate.call(this); | |
258 var self = this; | |
259 | 269 |
260 // Additionally change the indicator as well. | 270 // Update the UI when the pref changes. |
261 Preferences.getInstance().addEventListener(this.pref, | 271 Preferences.getInstance().addEventListener( |
262 function(event) { | 272 this.pref, this.onPrefChange_.bind(this)); |
263 self.updateIndicator(); | |
264 }); | |
265 | 273 |
266 // Listen to user events. | 274 // Listen to user events. |
267 this.addEventListener('input', | 275 // TODO(jhawkins): Add onmousewheel handling once the associated WK bug is |
268 function(e) { | 276 // fixed. |
269 this.updateIndicator(); | 277 // https://bugs.webkit.org/show_bug.cgi?id=52256 |
270 }); | 278 this.onchange = this.onChange_.bind(this); |
| 279 this.onkeyup = this.onmouseup = this.onInputUp_.bind(this); |
271 }, | 280 }, |
272 | 281 |
273 updateIndicator: function() { | 282 /** |
274 if ($(this.id + '-value')) { | 283 * Event listener that updates the UI when the underlying pref changes. |
275 $(this.id + '-value').textContent = this.value; | 284 * @param {Event} event The event that details the pref change. |
276 } | 285 * @private |
277 } | 286 */ |
| 287 onPrefChange_: function(event) { |
| 288 var value = event.value && event.value['value'] != undefined ? |
| 289 event.value['value'] : event.value; |
| 290 if (value != undefined) |
| 291 this.value = this.valueMap ? this.valueMap.indexOf(value) : value; |
| 292 }, |
| 293 |
| 294 /** |
| 295 * onchange handler that sets the pref when the user changes the value of |
| 296 * the input element. |
| 297 * @private |
| 298 */ |
| 299 onChange_: function(event) { |
| 300 if (this.continuous) |
| 301 this.setRangePref_(); |
| 302 |
| 303 this.notifyChange(this, this.mapValueToRange_(this.value)); |
| 304 }, |
| 305 |
| 306 /** |
| 307 * Sets the integer value of |pref| to the value of this element. |
| 308 * @private |
| 309 */ |
| 310 setRangePref_: function() { |
| 311 Preferences.setIntegerPref( |
| 312 this.pref, this.mapValueToRange_(this.value), this.metric); |
| 313 }, |
| 314 |
| 315 /** |
| 316 * onkeyup/onmouseup handler that modifies the pref if |continuous| is |
| 317 * false. |
| 318 * @private |
| 319 */ |
| 320 onInputUp_: function(event) { |
| 321 if (!this.continuous) |
| 322 this.setRangePref_(); |
| 323 }, |
| 324 |
| 325 /** |
| 326 * Maps the value of this element into the range provided by the client, |
| 327 * represented by |valueMap|. |
| 328 * @param {number} value The value to map. |
| 329 * @private |
| 330 */ |
| 331 mapValueToRange_: function(value) { |
| 332 return this.valueMap ? this.valueMap[value] : value; |
| 333 }, |
| 334 |
| 335 /** |
| 336 * Called when the client has specified non-continuous mode and the value of |
| 337 * the range control changes. |
| 338 * @param {Element} el This element. |
| 339 * @param {number} value The value of this element. |
| 340 */ |
| 341 notifyChange: function(el, value) { |
| 342 }, |
278 }; | 343 }; |
279 | 344 |
| 345 /** |
| 346 * The preference name. |
| 347 * @type {string} |
| 348 */ |
| 349 cr.defineProperty(PrefRange, 'pref', cr.PropertyKind.ATTR); |
| 350 |
| 351 /** |
| 352 * The user metric string. |
| 353 * @type {string} |
| 354 */ |
| 355 cr.defineProperty(PrefRange, 'metric', cr.PropertyKind.ATTR); |
| 356 |
280 ///////////////////////////////////////////////////////////////////////////// | 357 ///////////////////////////////////////////////////////////////////////////// |
281 // PrefSelect class: | 358 // PrefSelect class: |
282 | 359 |
283 // Define a constructor that uses a select element as its underlying element. | 360 // Define a constructor that uses a select element as its underlying element. |
284 var PrefSelect = cr.ui.define('select'); | 361 var PrefSelect = cr.ui.define('select'); |
285 | 362 |
286 PrefSelect.prototype = { | 363 PrefSelect.prototype = { |
287 // Set up the prototype chain | 364 // Set up the prototype chain |
288 __proto__: HTMLSelectElement.prototype, | 365 __proto__: HTMLSelectElement.prototype, |
289 | 366 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 case 'number': | 415 case 'number': |
339 Preferences.setIntegerPref(self.pref, | 416 Preferences.setIntegerPref(self.pref, |
340 self.options[self.selectedIndex].value, self.metric); | 417 self.options[self.selectedIndex].value, self.metric); |
341 break; | 418 break; |
342 case 'boolean': | 419 case 'boolean': |
343 var option = self.options[self.selectedIndex]; | 420 var option = self.options[self.selectedIndex]; |
344 var value = (option.value == 'true') ? true : false; | 421 var value = (option.value == 'true') ? true : false; |
345 Preferences.setBooleanPref(self.pref, value, self.metric); | 422 Preferences.setBooleanPref(self.pref, value, self.metric); |
346 break; | 423 break; |
347 case 'string': | 424 case 'string': |
| 425 case undefined: // Assume the pref is a string. |
348 Preferences.setStringPref(self.pref, | 426 Preferences.setStringPref(self.pref, |
349 self.options[self.selectedIndex].value, self.metric); | 427 self.options[self.selectedIndex].value, self.metric); |
350 break; | 428 break; |
351 } | 429 } |
352 }); | 430 }); |
353 }, | 431 }, |
354 }; | 432 }; |
355 | 433 |
356 /** | 434 /** |
357 * The preference name. | 435 * The preference name. |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
427 PrefCheckbox: PrefCheckbox, | 505 PrefCheckbox: PrefCheckbox, |
428 PrefNumber: PrefNumber, | 506 PrefNumber: PrefNumber, |
429 PrefNumeric: PrefNumeric, | 507 PrefNumeric: PrefNumeric, |
430 PrefRadio: PrefRadio, | 508 PrefRadio: PrefRadio, |
431 PrefRange: PrefRange, | 509 PrefRange: PrefRange, |
432 PrefSelect: PrefSelect, | 510 PrefSelect: PrefSelect, |
433 PrefTextField: PrefTextField | 511 PrefTextField: PrefTextField |
434 }; | 512 }; |
435 | 513 |
436 }); | 514 }); |
OLD | NEW |