Commit 193a75a3 authored by dbeam's avatar dbeam Committed by Commit bot

options: fix "Import" button for idle options pages.

R=estade@chromium.org
BUG=275265
TEST=open chrome://settings/importData in 2 windows, uncheck all checkboxes => "Import" disabled both places

Review URL: https://codereview.chromium.org/660623002

Cr-Commit-Position: refs/heads/master@{#300152}
parent b4e712c2
...@@ -40,8 +40,10 @@ cr.define('options', function() { ...@@ -40,8 +40,10 @@ cr.define('options', function() {
var checkboxes = var checkboxes =
document.querySelectorAll('#import-checkboxes input[type=checkbox]'); document.querySelectorAll('#import-checkboxes input[type=checkbox]');
for (var i = 0; i < checkboxes.length; i++) { for (var i = 0; i < checkboxes.length; i++) {
checkboxes[i].onchange = function() { checkboxes[i].customPrefChangeHandler = function(e) {
options.PrefCheckbox.prototype.defaultPrefChangeHandler.call(this, e);
self.validateCommitButton_(); self.validateCommitButton_();
return true;
}; };
} }
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
// found in the LICENSE file. // found in the LICENSE file.
cr.define('options', function() { cr.define('options', function() {
var Preferences = options.Preferences; var Preferences = options.Preferences;
/** /**
...@@ -16,14 +15,16 @@ cr.define('options', function() { ...@@ -16,14 +15,16 @@ cr.define('options', function() {
* @param {boolean} disabled Whether the element should be disabled or enabled * @param {boolean} disabled Whether the element should be disabled or enabled
* for the given |reason|. * for the given |reason|.
*/ */
function updateDisabledState_(el, reason, disabled) { function updateDisabledState(el, reason, disabled) {
if (!el.disabledReasons) if (!el.disabledReasons)
el.disabledReasons = {}; el.disabledReasons = {};
if (el.disabled && (Object.keys(el.disabledReasons).length == 0)) { if (el.disabled && (Object.keys(el.disabledReasons).length == 0)) {
// The element has been previously disabled without a reason, so we add // The element has been previously disabled without a reason, so we add
// one to keep it disabled. // one to keep it disabled.
el.disabledReasons.other = true; el.disabledReasons.other = true;
} }
if (!el.disabled) { if (!el.disabled) {
// If the element is not disabled, there should be no reason, except for // If the element is not disabled, there should be no reason, except for
// 'other'. // 'other'.
...@@ -31,11 +32,12 @@ cr.define('options', function() { ...@@ -31,11 +32,12 @@ cr.define('options', function() {
if (Object.keys(el.disabledReasons).length > 0) if (Object.keys(el.disabledReasons).length > 0)
console.error('Element is not disabled but should be'); console.error('Element is not disabled but should be');
} }
if (disabled) {
if (disabled)
el.disabledReasons[reason] = true; el.disabledReasons[reason] = true;
} else { else
delete el.disabledReasons[reason]; delete el.disabledReasons[reason];
}
el.disabled = Object.keys(el.disabledReasons).length > 0; el.disabled = Object.keys(el.disabledReasons).length > 0;
} }
...@@ -60,14 +62,14 @@ cr.define('options', function() { ...@@ -60,14 +62,14 @@ cr.define('options', function() {
var self = this; var self = this;
// Listen for user events. // Listen for user events.
this.addEventListener('change', this.handleChange_.bind(this)); this.addEventListener('change', this.handleChange.bind(this));
// Listen for pref changes. // Listen for pref changes.
Preferences.getInstance().addEventListener(this.pref, function(event) { Preferences.getInstance().addEventListener(this.pref, function(event) {
if (event.value.uncommitted && !self.dialogPref) if (event.value.uncommitted && !self.dialogPref)
return; return;
self.updateStateFromPref_(event); self.updateStateFromPref(event);
updateDisabledState_(self, 'notUserModifiable', event.value.disabled); updateDisabledState(self, 'notUserModifiable', event.value.disabled);
self.controlledBy = event.value.controlledBy; self.controlledBy = event.value.controlledBy;
}); });
}, },
...@@ -77,29 +79,29 @@ cr.define('options', function() { ...@@ -77,29 +79,29 @@ cr.define('options', function() {
* change handler does not suppress it, a default handler is invoked that * change handler does not suppress it, a default handler is invoked that
* updates the associated pref. * updates the associated pref.
* @param {Event} event Change event. * @param {Event} event Change event.
* @private * @protected
*/ */
handleChange_: function(event) { handleChange: function(event) {
if (!this.customChangeHandler(event)) if (!this.customChangeHandler(event))
this.updatePrefFromState_(); this.updatePrefFromState();
}, },
/** /**
* Handles changes to the pref. If a custom change handler does not suppress * Handles changes to the pref. If a custom change handler does not suppress
* it, a default handler is invoked that update the input element's state. * it, a default handler is invoked that updates the input element's state.
* @param {Event} event Pref change event. * @param {Event} event Pref change event.
* @private * @protected
*/ */
updateStateFromPref_: function(event) { updateStateFromPref: function(event) {
if (!this.customPrefChangeHandler(event)) if (!this.customPrefChangeHandler(event))
this.value = event.value.value; this.value = event.value.value;
}, },
/** /**
* See |updateDisabledState_| above. * See |updateDisabledState| above.
*/ */
setDisabled: function(reason, disabled) { setDisabled: function(reason, disabled) {
updateDisabledState_(this, reason, disabled); updateDisabledState(this, reason, disabled);
}, },
/** /**
...@@ -180,28 +182,30 @@ cr.define('options', function() { ...@@ -180,28 +182,30 @@ cr.define('options', function() {
// Consider a checked dialog checkbox as a 'suggestion' which is committed // Consider a checked dialog checkbox as a 'suggestion' which is committed
// once the user confirms the dialog. // once the user confirms the dialog.
if (this.dialogPref && this.checked) if (this.dialogPref && this.checked)
this.updatePrefFromState_(); this.updatePrefFromState();
}, },
/** /**
* Update the associated pref when when the user makes changes to the * Update the associated pref when when the user makes changes to the
* checkbox state. * checkbox state.
* @private * @override
*/ */
updatePrefFromState_: function() { updatePrefFromState: function() {
var value = this.inverted_pref ? !this.checked : this.checked; var value = this.inverted_pref ? !this.checked : this.checked;
Preferences.setBooleanPref(this.pref, value, Preferences.setBooleanPref(this.pref, value,
!this.dialogPref, this.metric); !this.dialogPref, this.metric);
}, },
/** @override */
updateStateFromPref: function(event) {
if (!this.customPrefChangeHandler(event))
this.defaultPrefChangeHandler(event);
},
/** /**
* Update the checkbox state when the associated pref changes. * @param {Event} event A pref change event.
* @param {Event} event Pref change event.
* @private
*/ */
updateStateFromPref_: function(event) { defaultPrefChangeHandler: function(event) {
if (this.customPrefChangeHandler(event))
return;
var value = Boolean(event.value.value); var value = Boolean(event.value.value);
this.checked = this.inverted_pref ? !value : value; this.checked = this.inverted_pref ? !value : value;
}, },
...@@ -231,10 +235,10 @@ cr.define('options', function() { ...@@ -231,10 +235,10 @@ cr.define('options', function() {
}, },
/** /**
* Update the associated pref when when the user inputs a number. * Update the associated pref when the user inputs a number.
* @private * @override
*/ */
updatePrefFromState_: function() { updatePrefFromState: function() {
if (this.validity.valid) { if (this.validity.valid) {
Preferences.setIntegerPref(this.pref, this.value, Preferences.setIntegerPref(this.pref, this.value,
!this.dialogPref, this.metric); !this.dialogPref, this.metric);
...@@ -262,9 +266,9 @@ cr.define('options', function() { ...@@ -262,9 +266,9 @@ cr.define('options', function() {
/** /**
* Update the associated pref when when the user selects the radio button. * Update the associated pref when when the user selects the radio button.
* @private * @override
*/ */
updatePrefFromState_: function() { updatePrefFromState: function() {
if (this.value == 'true' || this.value == 'false') { if (this.value == 'true' || this.value == 'false') {
Preferences.setBooleanPref(this.pref, Preferences.setBooleanPref(this.pref,
this.value == String(this.checked), this.value == String(this.checked),
...@@ -275,12 +279,8 @@ cr.define('options', function() { ...@@ -275,12 +279,8 @@ cr.define('options', function() {
} }
}, },
/** /** @override */
* Update the radio button state when the associated pref changes. updateStateFromPref: function(event) {
* @param {Event} event Pref change event.
* @private
*/
updateStateFromPref_: function(event) {
if (!this.customPrefChangeHandler(event)) if (!this.customPrefChangeHandler(event))
this.checked = this.value == String(event.value.value); this.checked = this.value == String(event.value.value);
}, },
...@@ -324,9 +324,9 @@ cr.define('options', function() { ...@@ -324,9 +324,9 @@ cr.define('options', function() {
/** /**
* Update the associated pref when when the user releases the slider. * Update the associated pref when when the user releases the slider.
* @private * @override
*/ */
updatePrefFromState_: function() { updatePrefFromState: function() {
Preferences.setIntegerPref( Preferences.setIntegerPref(
this.pref, this.pref,
this.mapPositionToPref(parseInt(this.value, 10)), this.mapPositionToPref(parseInt(this.value, 10)),
...@@ -334,12 +334,10 @@ cr.define('options', function() { ...@@ -334,12 +334,10 @@ cr.define('options', function() {
this.metric); this.metric);
}, },
/** /** @override */
* Ignore changes to the slider position made by the user while the slider handleChange: function() {
* has not been released. // Ignore changes to the slider position made by the user while the slider
* @private // has not been released.
*/
handleChange_: function() {
}, },
/** /**
...@@ -351,17 +349,16 @@ cr.define('options', function() { ...@@ -351,17 +349,16 @@ cr.define('options', function() {
*/ */
handleRelease_: function(event) { handleRelease_: function(event) {
if (!this.customChangeHandler(event)) if (!this.customChangeHandler(event))
this.updatePrefFromState_(); this.updatePrefFromState();
}, },
/** /**
* Handles changes to the pref associated with the slider. If a custom * Handles changes to the pref associated with the slider. If a custom
* change handler does not suppress it, a default handler is invoked that * change handler does not suppress it, a default handler is invoked that
* updates the slider position. * updates the slider position.
* @param {Event} event Pref change event. * @override.
* @private
*/ */
updateStateFromPref_: function(event) { updateStateFromPref: function(event) {
if (this.customPrefChangeHandler(event)) if (this.customPrefChangeHandler(event))
return; return;
var value = event.value.value; var value = event.value.value;
...@@ -390,9 +387,9 @@ cr.define('options', function() { ...@@ -390,9 +387,9 @@ cr.define('options', function() {
/** /**
* Update the associated pref when when the user selects an item. * Update the associated pref when when the user selects an item.
* @private * @override
*/ */
updatePrefFromState_: function() { updatePrefFromState: function() {
var value = this.options[this.selectedIndex].value; var value = this.options[this.selectedIndex].value;
switch (this.dataType) { switch (this.dataType) {
case 'number': case 'number':
...@@ -417,12 +414,8 @@ cr.define('options', function() { ...@@ -417,12 +414,8 @@ cr.define('options', function() {
} }
}, },
/** /** @override */
* Update the selected item when the associated pref changes. updateStateFromPref: function(event) {
* @param {Event} event Pref change event.
* @private
*/
updateStateFromPref_: function(event) {
if (this.customPrefChangeHandler(event)) if (this.customPrefChangeHandler(event))
return; return;
...@@ -477,9 +470,9 @@ cr.define('options', function() { ...@@ -477,9 +470,9 @@ cr.define('options', function() {
/** /**
* Update the associated pref when when the user inputs text. * Update the associated pref when when the user inputs text.
* @private * @override
*/ */
updatePrefFromState_: function(event) { updatePrefFromState: function(event) {
switch (this.dataType) { switch (this.dataType) {
case 'number': case 'number':
Preferences.setIntegerPref(this.pref, this.value, Preferences.setIntegerPref(this.pref, this.value,
...@@ -554,17 +547,17 @@ cr.define('options', function() { ...@@ -554,17 +547,17 @@ cr.define('options', function() {
// be disabled when the underlying Boolean preference is set to false by a // be disabled when the underlying Boolean preference is set to false by a
// policy or extension. // policy or extension.
Preferences.getInstance().addEventListener(this.pref, function(event) { Preferences.getInstance().addEventListener(this.pref, function(event) {
updateDisabledState_(self, 'notUserModifiable', updateDisabledState(self, 'notUserModifiable',
event.value.disabled && !event.value.value); event.value.disabled && !event.value.value);
self.controlledBy = event.value.controlledBy; self.controlledBy = event.value.controlledBy;
}); });
}, },
/** /**
* See |updateDisabledState_| above. * See |updateDisabledState| above.
*/ */
setDisabled: function(reason, disabled) { setDisabled: function(reason, disabled) {
updateDisabledState_(this, reason, disabled); updateDisabledState(this, reason, disabled);
}, },
}; };
...@@ -591,5 +584,4 @@ cr.define('options', function() { ...@@ -591,5 +584,4 @@ cr.define('options', function() {
PrefPortNumber: PrefPortNumber, PrefPortNumber: PrefPortNumber,
PrefButton: PrefButton PrefButton: PrefButton
}; };
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment