1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 cr.define('options', function() {
6 var Preferences = options.Preferences;
9 * Allows an element to be disabled for several reasons.
10 * The element is disabled if at least one reason is true, and the reasons
11 * can be set separately.
13 * @param {!HTMLElement} el The element to update.
14 * @param {string} reason The reason for disabling the element.
15 * @param {boolean} disabled Whether the element should be disabled or enabled
16 * for the given |reason|.
18 function updateDisabledState(el, reason, disabled) {
19 if (!el.disabledReasons)
20 el.disabledReasons = {};
22 if (el.disabled && (Object.keys(el.disabledReasons).length == 0)) {
23 // The element has been previously disabled without a reason, so we add
24 // one to keep it disabled.
25 el.disabledReasons.other = true;
29 // If the element is not disabled, there should be no reason, except for
31 delete el.disabledReasons.other;
32 if (Object.keys(el.disabledReasons).length > 0)
33 console.error('Element is not disabled but should be');
37 el.disabledReasons[reason] = true;
39 delete el.disabledReasons[reason];
41 el.disabled = Object.keys(el.disabledReasons).length > 0;
44 /////////////////////////////////////////////////////////////////////////////
45 // PrefInputElement class:
48 * Define a constructor that uses an input element as its underlying element.
50 * @extends {HTMLInputElement}
52 var PrefInputElement = cr.ui.define('input');
54 PrefInputElement.prototype = {
55 // Set up the prototype chain
56 __proto__: HTMLInputElement.prototype,
59 * Initialization function for the cr.ui framework.
61 decorate: function() {
64 // Listen for user events.
65 this.addEventListener('change', this.handleChange.bind(this));
67 // Listen for pref changes.
68 Preferences.getInstance().addEventListener(this.pref, function(event) {
69 if (event.value.uncommitted && !self.dialogPref)
71 self.updateStateFromPref(event);
72 updateDisabledState(self, 'notUserModifiable', event.value.disabled);
73 self.controlledBy = event.value.controlledBy;
78 * Handle changes to the input element's state made by the user. If a custom
79 * change handler does not suppress it, a default handler is invoked that
80 * updates the associated pref.
81 * @param {Event} event Change event.
84 handleChange: function(event) {
85 if (!this.customChangeHandler(event))
86 this.updatePrefFromState();
90 * Handles changes to the pref. If a custom change handler does not suppress
91 * it, a default handler is invoked that updates the input element's state.
92 * @param {Event} event Pref change event.
95 updateStateFromPref: function(event) {
96 if (!this.customPrefChangeHandler(event))
97 this.value = event.value.value;
101 * An abstract method for all subclasses to override to update their
102 * preference from existing state.
105 updatePrefFromState: assertNotReached,
108 * See |updateDisabledState| above.
110 setDisabled: function(reason, disabled) {
111 updateDisabledState(this, reason, disabled);
115 * Custom change handler that is invoked first when the user makes changes
116 * to the input element's state. If it returns false, a default handler is
117 * invoked next that updates the associated pref. If it returns true, the
118 * default handler is suppressed (i.e., this works like stopPropagation or
120 * @param {Event} event Input element change event.
122 customChangeHandler: function(event) {
127 * Custom change handler that is invoked first when the preference
128 * associated with the input element changes. If it returns false, a default
129 * handler is invoked next that updates the input element. If it returns
130 * true, the default handler is suppressed.
131 * @param {Event} event Input element change event.
133 customPrefChangeHandler: function(event) {
139 * The name of the associated preference.
141 cr.defineProperty(PrefInputElement, 'pref', cr.PropertyKind.ATTR);
144 * The data type of the associated preference, only relevant for derived
145 * classes that support different data types.
147 cr.defineProperty(PrefInputElement, 'dataType', cr.PropertyKind.ATTR);
150 * Whether this input element is part of a dialog. If so, changes take effect
151 * in the settings UI immediately but are only actually committed when the
152 * user confirms the dialog. If the user cancels the dialog instead, the
153 * changes are rolled back in the settings UI and never committed.
155 cr.defineProperty(PrefInputElement, 'dialogPref', cr.PropertyKind.BOOL_ATTR);
158 * Whether the associated preference is controlled by a source other than the
159 * user's setting (can be 'policy', 'extension', 'recommended' or unset).
161 cr.defineProperty(PrefInputElement, 'controlledBy', cr.PropertyKind.ATTR);
164 * The user metric string.
166 cr.defineProperty(PrefInputElement, 'metric', cr.PropertyKind.ATTR);
168 /////////////////////////////////////////////////////////////////////////////
169 // PrefCheckbox class:
172 * Define a constructor that uses an input element as its underlying element.
174 * @extends {options.PrefInputElement}
176 var PrefCheckbox = cr.ui.define('input');
178 PrefCheckbox.prototype = {
179 // Set up the prototype chain
180 __proto__: PrefInputElement.prototype,
183 * Initialization function for the cr.ui framework.
185 decorate: function() {
186 PrefInputElement.prototype.decorate.call(this);
187 this.type = 'checkbox';
189 // Consider a checked dialog checkbox as a 'suggestion' which is committed
190 // once the user confirms the dialog.
191 if (this.dialogPref && this.checked)
192 this.updatePrefFromState();
196 * Update the associated pref when when the user makes changes to the
200 updatePrefFromState: function() {
201 var value = this.inverted_pref ? !this.checked : this.checked;
202 Preferences.setBooleanPref(this.pref, value,
203 !this.dialogPref, this.metric);
207 updateStateFromPref: function(event) {
208 if (!this.customPrefChangeHandler(event))
209 this.defaultPrefChangeHandler(event);
213 * @param {Event} event A pref change event.
215 defaultPrefChangeHandler: function(event) {
216 var value = Boolean(event.value.value);
217 this.checked = this.inverted_pref ? !value : value;
222 * Whether the mapping between checkbox state and associated pref is inverted.
224 cr.defineProperty(PrefCheckbox, 'inverted_pref', cr.PropertyKind.BOOL_ATTR);
226 /////////////////////////////////////////////////////////////////////////////
229 // Define a constructor that uses an input element as its underlying element.
230 var PrefNumber = cr.ui.define('input');
232 PrefNumber.prototype = {
233 // Set up the prototype chain
234 __proto__: PrefInputElement.prototype,
237 * Initialization function for the cr.ui framework.
239 decorate: function() {
240 PrefInputElement.prototype.decorate.call(this);
241 this.type = 'number';
245 * Update the associated pref when the user inputs a number.
248 updatePrefFromState: function() {
249 if (this.validity.valid) {
250 Preferences.setIntegerPref(this.pref, this.value,
251 !this.dialogPref, this.metric);
256 /////////////////////////////////////////////////////////////////////////////
259 //Define a constructor that uses an input element as its underlying element.
260 var PrefRadio = cr.ui.define('input');
262 PrefRadio.prototype = {
263 // Set up the prototype chain
264 __proto__: PrefInputElement.prototype,
267 * Initialization function for the cr.ui framework.
269 decorate: function() {
270 PrefInputElement.prototype.decorate.call(this);
275 * Update the associated pref when when the user selects the radio button.
278 updatePrefFromState: function() {
279 if (this.value == 'true' || this.value == 'false') {
280 Preferences.setBooleanPref(this.pref,
281 this.value == String(this.checked),
282 !this.dialogPref, this.metric);
284 Preferences.setIntegerPref(this.pref, this.value,
285 !this.dialogPref, this.metric);
290 updateStateFromPref: function(event) {
291 if (!this.customPrefChangeHandler(event))
292 this.checked = this.value == String(event.value.value);
296 /////////////////////////////////////////////////////////////////////////////
300 * Define a constructor that uses an input element as its underlying element.
302 * @extends {options.PrefInputElement}
304 var PrefRange = cr.ui.define('input');
306 PrefRange.prototype = {
307 // Set up the prototype chain
308 __proto__: PrefInputElement.prototype,
311 * The map from slider position to corresponding pref value.
316 * Initialization function for the cr.ui framework.
318 decorate: function() {
319 PrefInputElement.prototype.decorate.call(this);
322 // Listen for user events.
323 // TODO(jhawkins): Add onmousewheel handling once the associated WK bug is
325 // https://bugs.webkit.org/show_bug.cgi?id=52256
326 this.addEventListener('keyup', this.handleRelease_.bind(this));
327 this.addEventListener('mouseup', this.handleRelease_.bind(this));
328 this.addEventListener('touchcancel', this.handleRelease_.bind(this));
329 this.addEventListener('touchend', this.handleRelease_.bind(this));
333 * Update the associated pref when when the user releases the slider.
336 updatePrefFromState: function() {
337 Preferences.setIntegerPref(
339 this.mapPositionToPref(parseInt(this.value, 10)),
345 handleChange: function() {
346 // Ignore changes to the slider position made by the user while the slider
347 // has not been released.
351 * Handle changes to the slider position made by the user when the slider is
352 * released. If a custom change handler does not suppress it, a default
353 * handler is invoked that updates the associated pref.
354 * @param {Event} event Change event.
357 handleRelease_: function(event) {
358 if (!this.customChangeHandler(event))
359 this.updatePrefFromState();
363 * Handles changes to the pref associated with the slider. If a custom
364 * change handler does not suppress it, a default handler is invoked that
365 * updates the slider position.
368 updateStateFromPref: function(event) {
369 if (this.customPrefChangeHandler(event))
371 var value = event.value.value;
372 this.value = this.valueMap ? this.valueMap.indexOf(value) : value;
376 * Map slider position to the range of values provided by the client,
377 * represented by |valueMap|.
378 * @param {number} position The slider position to map.
380 mapPositionToPref: function(position) {
381 return this.valueMap ? this.valueMap[position] : position;
385 /////////////////////////////////////////////////////////////////////////////
388 // Define a constructor that uses a select element as its underlying element.
389 var PrefSelect = cr.ui.define('select');
391 PrefSelect.prototype = {
392 // Set up the prototype chain
393 __proto__: PrefInputElement.prototype,
396 * Update the associated pref when when the user selects an item.
399 updatePrefFromState: function() {
400 var value = this.options[this.selectedIndex].value;
401 switch (this.dataType) {
403 Preferences.setIntegerPref(this.pref, value,
404 !this.dialogPref, this.metric);
407 Preferences.setDoublePref(this.pref, value,
408 !this.dialogPref, this.metric);
411 Preferences.setBooleanPref(this.pref, value == 'true',
412 !this.dialogPref, this.metric);
415 Preferences.setStringPref(this.pref, value,
416 !this.dialogPref, this.metric);
419 console.error('Unknown data type for <select> UI element: ' +
425 updateStateFromPref: function(event) {
426 if (this.customPrefChangeHandler(event))
429 // Make sure the value is a string, because the value is stored as a
430 // string in the HTMLOptionElement.
431 var value = String(event.value.value);
434 for (var i = 0; i < this.options.length; i++) {
435 if (this.options[i].value == value) {
436 this.selectedIndex = i;
441 // Item not found, select first item.
443 this.selectedIndex = 0;
445 // The "onchange" event automatically fires when the user makes a manual
446 // change. It should never be fired for a programmatic change. However,
447 // these two lines were here already and it is hard to tell who may be
450 this.onchange(event);
454 /////////////////////////////////////////////////////////////////////////////
455 // PrefTextField class:
457 // Define a constructor that uses an input element as its underlying element.
458 var PrefTextField = cr.ui.define('input');
460 PrefTextField.prototype = {
461 // Set up the prototype chain
462 __proto__: PrefInputElement.prototype,
465 * Initialization function for the cr.ui framework.
467 decorate: function() {
468 PrefInputElement.prototype.decorate.call(this);
471 // Listen for user events.
472 window.addEventListener('unload', function() {
473 if (document.activeElement == self)
479 * Update the associated pref when when the user inputs text.
482 updatePrefFromState: function(event) {
483 switch (this.dataType) {
485 Preferences.setIntegerPref(this.pref, this.value,
486 !this.dialogPref, this.metric);
489 Preferences.setDoublePref(this.pref, this.value,
490 !this.dialogPref, this.metric);
493 Preferences.setURLPref(this.pref, this.value,
494 !this.dialogPref, this.metric);
497 Preferences.setStringPref(this.pref, this.value,
498 !this.dialogPref, this.metric);
504 /////////////////////////////////////////////////////////////////////////////
505 // PrefPortNumber class:
507 // Define a constructor that uses an input element as its underlying element.
508 var PrefPortNumber = cr.ui.define('input');
510 PrefPortNumber.prototype = {
511 // Set up the prototype chain
512 __proto__: PrefTextField.prototype,
515 * Initialization function for the cr.ui framework.
517 decorate: function() {
520 self.dataType = 'number';
521 PrefTextField.prototype.decorate.call(this);
522 self.oninput = function() {
523 // Note that using <input type="number"> is insufficient to restrict
524 // the input as it allows negative numbers and does not limit the
525 // number of charactes typed even if a range is set. Furthermore,
526 // it sometimes produces strange repaint artifacts.
527 var filtered = self.value.replace(/[^0-9]/g, '');
528 if (filtered != self.value)
529 self.value = filtered;
534 /////////////////////////////////////////////////////////////////////////////
537 // Define a constructor that uses a button element as its underlying element.
538 var PrefButton = cr.ui.define('button');
540 PrefButton.prototype = {
541 // Set up the prototype chain
542 __proto__: HTMLButtonElement.prototype,
545 * Initialization function for the cr.ui framework.
547 decorate: function() {
550 // Listen for pref changes.
551 // This element behaves like a normal button and does not affect the
552 // underlying preference; it just becomes disabled when the preference is
553 // managed, and its value is false. This is useful for buttons that should
554 // be disabled when the underlying Boolean preference is set to false by a
555 // policy or extension.
556 Preferences.getInstance().addEventListener(this.pref, function(event) {
557 updateDisabledState(self, 'notUserModifiable',
558 event.value.disabled && !event.value.value);
559 self.controlledBy = event.value.controlledBy;
564 * See |updateDisabledState| above.
566 setDisabled: function(reason, disabled) {
567 updateDisabledState(this, reason, disabled);
572 * The name of the associated preference.
574 cr.defineProperty(PrefButton, 'pref', cr.PropertyKind.ATTR);
577 * Whether the associated preference is controlled by a source other than the
578 * user's setting (can be 'policy', 'extension', 'recommended' or unset).
580 cr.defineProperty(PrefButton, 'controlledBy', cr.PropertyKind.ATTR);
584 PrefCheckbox: PrefCheckbox,
585 PrefInputElement: PrefInputElement,
586 PrefNumber: PrefNumber,
587 PrefRadio: PrefRadio,
588 PrefRange: PrefRange,
589 PrefSelect: PrefSelect,
590 PrefTextField: PrefTextField,
591 PrefPortNumber: PrefPortNumber,
592 PrefButton: PrefButton