Index: chrome/browser/resources/options2/pref_ui.js |
diff --git a/chrome/browser/resources/options2/pref_ui.js b/chrome/browser/resources/options2/pref_ui.js |
index 82bab8613980c09ad549c69850c6a79aa8e32b15..1d48ebc80776dd7c9a35e2c65b5e654ea9b57d55 100644 |
--- a/chrome/browser/resources/options2/pref_ui.js |
+++ b/chrome/browser/resources/options2/pref_ui.js |
@@ -2,8 +2,6 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-// TODO(jhawkins): Add dialog-pref support to all preference controls. |
- |
cr.define('options', function() { |
var Preferences = options.Preferences; |
@@ -48,15 +46,8 @@ cr.define('options', function() { |
* @param {!Event} event The pref change event. |
*/ |
function updateElementState_(el, event) { |
- el.controlledBy = null; |
- |
- if (!event.value) |
- return; |
- |
updateDisabledState_(el, 'notUserModifiable', event.value.disabled); |
- |
el.controlledBy = event.value.controlledBy; |
- |
OptionsPage.updateManagedBannerVisibility(); |
} |
@@ -72,12 +63,6 @@ cr.define('options', function() { |
__proto__: HTMLInputElement.prototype, |
/** |
- * The stored value of the preference that this checkbox controls. |
- * @type {boolean} |
- */ |
- prefValue_: null, |
- |
- /** |
* Initialization function for the cr.ui framework. |
*/ |
decorate: function() { |
@@ -88,54 +73,31 @@ cr.define('options', function() { |
// Listen to pref changes. |
Preferences.getInstance().addEventListener(this.pref, function(event) { |
- self.prefValue_ = Boolean(event.value.value); |
- self.resetPrefState(); |
- |
+ var value = event.value.value; |
+ self.checked = self.inverted_pref ? !value : value; |
updateElementState_(self, event); |
}); |
// Listen to user events. |
- this.addEventListener('change', function(e) { |
- if (self.customChangeHandler(e)) |
+ this.addEventListener('change', function(event) { |
+ if (this.customChangeHandler(event)) |
James Hawkins
2012/08/02 20:33:32
We have an inconsistency here: we use |self| above
bartfab (slow)
2012/08/31 14:14:30
Done.
|
return; |
- if (!this.dialogPref) |
- this.updatePreferenceValue_(); |
+ var value = this.inverted_pref ? !this.checked : this.checked; |
+ switch (this.valueType) { |
+ case 'number': |
+ Preferences.setIntegerPref(this.pref, Number(value), |
+ !this.dialogPref, this.metric); |
+ break; |
+ case 'boolean': |
+ Preferences.setBooleanPref(this.pref, value, |
+ !this.dialogPref, this.metric); |
+ break; |
+ } |
}); |
}, |
/** |
- * Update the preference value based on the checkbox state. |
- * @private |
- */ |
- updatePreferenceValue_: function() { |
- var value = this.inverted_pref ? !this.checked : this.checked; |
- switch (this.valueType) { |
- case 'number': |
- Preferences.setIntegerPref(this.pref, Number(value), this.metric); |
- break; |
- case 'boolean': |
- Preferences.setBooleanPref(this.pref, value, this.metric); |
- break; |
- } |
- }, |
- |
- /** |
- * Called by SettingsDialog to save the preference. |
- */ |
- savePrefState: function() { |
- this.updatePreferenceValue_(); |
- }, |
- |
- /** |
- * Called by SettingsDialog to reset the UI to match the current preference |
- * value. |
- */ |
- resetPrefState: function() { |
- this.checked = this.inverted_pref ? !this.prefValue_ : this.prefValue_; |
- }, |
- |
- /** |
* Sets up options in checkbox element. |
* @param {String} valueType The preference type for this checkbox. |
*/ |
@@ -170,7 +132,7 @@ cr.define('options', function() { |
/** |
* A special preference type specific to dialogs. These preferences are reset |
James Hawkins
2012/08/02 20:33:32
This sentence now doesn't make as much sense. I'd
bartfab (slow)
2012/08/31 14:14:30
Done.
|
- * when the dialog is shown and are not saved until the user confirms the |
+ * when the dialog is closed and are not saved until the user confirms the |
* dialog. |
* @type {boolean} |
*/ |
@@ -222,38 +184,24 @@ cr.define('options', function() { |
if (self.customChangeHandler(event)) |
return; |
self.checked = String(event.value.value) == self.value; |
- self.storedValue_ = self.checked; |
updateElementState_(self, event); |
}); |
- // Dialog preferences are not saved until savePrefState() is explicitly |
- // called. |
- if (!this.dialogPref) |
- this.onchange = this.savePrefState.bind(this); |
- }, |
- |
- /** |
- * Resets the input to the stored value. |
- */ |
- resetPrefState: function() { |
- this.checked = this.storedValue_; |
- }, |
+ // Listen to user events. |
+ this.addEventListener('change', function(event) { |
+ if (this.customChangeHandler(event)) |
+ return; |
- /** |
- * Saves the value of the input back into the preference. May be called |
- * directly to save dialog preferences. |
- */ |
- savePrefState: function() { |
- this.storedValue_ = this.checked; |
- if (this.value == 'true' || this.value == 'false') { |
- var value = String(this.value); |
- Preferences.setBooleanPref(this.pref, value == String(this.checked), |
- this.metric); |
- } else { |
- Preferences.setIntegerPref(this.pref, parseInt(this.value, 10), |
- this.metric); |
- } |
+ if (this.value == 'true' || this.value == 'false') { |
+ var value = String(this.value); |
+ Preferences.setBooleanPref(this.pref, value == String(this.checked), |
+ !this.dialogPref, this.metric); |
+ } else { |
+ Preferences.setIntegerPref(this.pref, parseInt(this.value, 10), |
+ !this.dialogPref, this.metric); |
+ } |
+ }); |
}, |
/** |
@@ -283,7 +231,7 @@ cr.define('options', function() { |
/** |
* A special preference type specific to dialogs. These preferences are reset |
- * when the dialog is shown and are not saved until the user confirms the |
+ * when the dialog is closed and are not saved until the user confirms the |
* dialog. |
* @type {boolean} |
*/ |
@@ -329,7 +277,8 @@ cr.define('options', function() { |
this.addEventListener('change', |
function(e) { |
if (this.validity.valid) { |
- Preferences.setIntegerPref(self.pref, self.value, self.metric); |
+ Preferences.setIntegerPref(self.pref, self.value, |
+ !self.dialogPref, self.metric); |
} |
}); |
}, |
@@ -349,6 +298,14 @@ cr.define('options', function() { |
cr.defineProperty(PrefNumeric, 'pref', cr.PropertyKind.ATTR); |
/** |
+ * A special preference type specific to dialogs. These preferences are reset |
+ * when the dialog is closed and are not saved until the user confirms the |
+ * dialog. |
+ * @type {boolean} |
+ */ |
+ cr.defineProperty(PrefRadio, 'dialogPref', cr.PropertyKind.BOOL_ATTR); |
+ |
+ /** |
* Whether the preference is controlled by something else than the user's |
* settings (either 'policy' or 'extension'). |
* @type {string} |
@@ -382,7 +339,8 @@ cr.define('options', function() { |
this.addEventListener('input', |
function(e) { |
if (this.validity.valid) { |
- Preferences.setIntegerPref(self.pref, self.value, self.metric); |
+ Preferences.setIntegerPref(self.pref, self.value, |
+ !self.dialogPref, self.metric); |
} |
}); |
}, |
@@ -442,8 +400,7 @@ cr.define('options', function() { |
*/ |
onPrefChange_: function(event) { |
var value = event.value.value; |
- if (value != undefined) |
- this.value = this.valueMap ? this.valueMap.indexOf(value) : value; |
+ this.value = this.valueMap ? this.valueMap.indexOf(value) : value; |
}, |
/** |
@@ -464,11 +421,8 @@ cr.define('options', function() { |
* @private |
*/ |
setRangePref_: function() { |
- Preferences.setIntegerPref( |
- this.pref, this.mapValueToRange_(this.value), this.metric); |
- |
- if (this.notifyPrefChange) |
- this.notifyPrefChange(this, this.mapValueToRange_(this.value)); |
+ Preferences.setIntegerPref(this.pref, this.mapValueToRange_(this.value), |
+ !this.dialogPref, this.metric); |
}, |
/** |
@@ -515,6 +469,14 @@ cr.define('options', function() { |
cr.defineProperty(PrefRange, 'pref', cr.PropertyKind.ATTR); |
/** |
+ * A special preference type specific to dialogs. These preferences are reset |
+ * when the dialog is closed and are not saved until the user confirms the |
+ * dialog. |
+ * @type {boolean} |
+ */ |
+ cr.defineProperty(PrefRadio, 'dialogPref', cr.PropertyKind.BOOL_ATTR); |
+ |
+ /** |
* Whether the preference is controlled by something else than the user's |
* settings (either 'policy' or 'extension'). |
* @type {string} |
@@ -538,12 +500,6 @@ cr.define('options', function() { |
__proto__: HTMLSelectElement.prototype, |
/** |
- * @type {string} The stored value of the preference that this select |
- * controls. |
- */ |
- prefValue_: null, |
- |
- /** |
* Initialization function for the cr.ui framework. |
*/ |
decorate: function() { |
@@ -556,72 +512,54 @@ cr.define('options', function() { |
value = event.value.value.toString(); |
updateElementState_(self, event); |
- self.prefValue_ = value; |
- self.resetPrefState(); |
- }); |
- // Listen to user events. |
- this.addEventListener('change', function(event) { |
- if (!self.dialogPref) |
- self.updatePreference_(self.prefValue_); |
- }); |
- }, |
- |
- /** |
- * Resets the input to the stored value. |
- */ |
- resetPrefState: function() { |
- var found = false; |
- for (var i = 0; i < this.options.length; i++) { |
- if (this.options[i].value == this.prefValue_) { |
- this.selectedIndex = i; |
- found = true; |
+ var found = false; |
+ for (var i = 0; i < self.options.length; i++) { |
+ if (self.options[i].value == self.prefValue_) { |
+ self.selectedIndex = i; |
+ found = true; |
+ } |
} |
- } |
- // Item not found, select first item. |
- if (!found) |
- this.selectedIndex = 0; |
+ // Item not found, select first item. |
+ if (!found) |
+ self.selectedIndex = 0; |
- if (this.onchange) |
- this.onchange(event); |
- }, |
+ if (this.onchange) |
+ self.onchange(event); |
+ }); |
- /** |
- * Updates the preference to the currently selected value. |
- */ |
- updatePreference_: function() { |
- if (!this.dataType) { |
- console.error('undefined data type for <select> pref'); |
- return; |
- } |
- |
- var prefValue = this.options[this.selectedIndex].value; |
- switch (this.dataType) { |
- case 'number': |
- Preferences.setIntegerPref(this.pref, prefValue, this.metric); |
- break; |
- case 'double': |
- Preferences.setDoublePref(this.pref, prefValue, this.metric); |
- break; |
- case 'boolean': |
- var value = (prefValue == 'true'); |
- Preferences.setBooleanPref(this.pref, value, this.metric); |
- break; |
- case 'string': |
- Preferences.setStringPref(this.pref, prefValue, this.metric); |
- break; |
- default: |
- console.error('unknown data type for <select> pref: ' + |
- this.dataType); |
- } |
- }, |
+ // Listen to user events. |
+ this.addEventListener('change', function(event) { |
+ if (!this.dataType) { |
+ console.error('undefined data type for <select> pref'); |
+ return; |
+ } |
- /** |
- * Called by SettingsDialog to save the stored value to preferences. |
- */ |
- savePrefState: function() { |
- this.updatePreference_(); |
+ var prefValue = this.options[this.selectedIndex].value; |
+ switch (this.dataType) { |
+ case 'number': |
+ Preferences.setIntegerPref(this.pref, prefValue, |
+ !this.dialogPref, this.metric); |
+ break; |
+ case 'double': |
+ Preferences.setDoublePref(this.pref, prefValue, |
+ !this.dialogPref, this.metric); |
+ break; |
+ case 'boolean': |
+ var value = (prefValue == 'true'); |
+ Preferences.setBooleanPref(this.pref, value, |
+ !this.dialogPref, this.metric); |
+ break; |
+ case 'string': |
+ Preferences.setStringPref(this.pref, prefValue, |
+ !this.dialogPref, this.metric); |
+ break; |
+ default: |
+ console.error('unknown data type for <select> pref: ' + |
+ this.dataType); |
+ } |
+ }); |
}, |
/** |
@@ -647,7 +585,7 @@ cr.define('options', function() { |
/** |
* A special preference type specific to dialogs. These preferences are reset |
- * when the dialog is shown and are not saved until the user confirms the |
+ * when the dialog is closed and are not saved until the user confirms the |
* dialog. |
* @type {boolean} |
*/ |
@@ -676,40 +614,6 @@ cr.define('options', function() { |
__proto__: HTMLInputElement.prototype, |
/** |
- * @type {string} The stored value of the preference that this text field |
- * controls. |
- */ |
- prefValue_: null, |
- |
- /** |
- * Saves the value of the input back into the preference. May be called |
- * directly to save dialog preferences. |
- */ |
- savePrefState: function() { |
- switch (this.dataType) { |
- case 'number': |
- Preferences.setIntegerPref(this.pref, this.value, this.metric); |
- break; |
- case 'double': |
- Preferences.setDoublePref(this.pref, this.value, this.metric); |
- break; |
- case 'url': |
- Preferences.setURLPref(this.pref, this.value, this.metric); |
- break; |
- default: |
- Preferences.setStringPref(this.pref, this.value, this.metric); |
- break; |
- } |
- }, |
- |
- /** |
- * Resets the input to the stored value. |
- */ |
- resetPrefState: function() { |
- this.value = this.prefValue_; |
- }, |
- |
- /** |
* Initialization function for the cr.ui framework. |
*/ |
decorate: function() { |
@@ -721,13 +625,29 @@ cr.define('options', function() { |
self.value = event.value.value; |
updateElementState_(self, event); |
- |
- self.prefValue_ = self.value; |
}); |
// Listen to user events. |
- if (!this.dialogPref) |
- this.addEventListener('change', this.savePrefState.bind(this)); |
+ this.addEventListener('change', function(event) { |
+ switch (this.dataType) { |
James Hawkins
2012/08/02 20:33:32
I'm not a fan of inlining large functions, so plea
bartfab (slow)
2012/08/31 14:14:30
Done.
|
+ case 'number': |
+ Preferences.setIntegerPref(this.pref, this.value, |
+ !this.dialogPref, this.metric); |
+ break; |
+ case 'double': |
+ Preferences.setDoublePref(this.pref, this.value, |
+ !this.dialogPref, this.metric); |
+ break; |
+ case 'url': |
+ Preferences.setURLPref(this.pref, this.value, |
+ !this.dialogPref, this.metric); |
+ break; |
+ default: |
+ Preferences.setStringPref(this.pref, this.value, |
+ !this.dialogPref, this.metric); |
+ break; |
+ } |
+ }); |
window.addEventListener('unload', |
function() { |
@@ -752,7 +672,7 @@ cr.define('options', function() { |
/** |
* A special preference type specific to dialogs. These preferences are reset |
- * when the dialog is shown and are not saved until the user confirms the |
+ * when the dialog is closed and are not saved until the user confirms the |
* dialog. |
* @type {boolean} |
*/ |