| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 /** @const */ var SettingsDialog = options.SettingsDialog; | 6 /** @const */ var SettingsDialog = options.SettingsDialog; |
| 7 | 7 |
| 8 /** | 8 /** |
| 9 * A dialog that will pop up when the user attempts to set the value of the | 9 * A dialog that will pop up when the user attempts to set the value of the |
| 10 * Boolean |pref| to |true|, asking for confirmation. If the user clicks OK, | 10 * Boolean |pref| to |true|, asking for confirmation. If the user clicks OK, |
| 11 * the new value is committed to Chrome. If the user clicks Cancel or leaves | 11 * the new value is committed to Chrome. If the user clicks Cancel or leaves |
| 12 * the settings page, the new value is discarded. | 12 * the settings page, the new value is discarded. |
| 13 * @constructor | 13 * @constructor |
| 14 * @param {string} name See Page constructor. | 14 * @param {string} name See Page constructor. |
| 15 * @param {string} title See Page constructor. | 15 * @param {string} title See Page constructor. |
| 16 * @param {string} pageDivName See Page constructor. | 16 * @param {string} pageDivName See Page constructor. |
| 17 * @param {HTMLInputElement} okButton The confirmation button element. | 17 * @param {HTMLInputElement} okButton The confirmation button element. |
| 18 * @param {HTMLInputElement} cancelButton The cancellation button element. | 18 * @param {HTMLInputElement} cancelButton The cancellation button element. |
| 19 * @param {string} pref The pref that requires confirmation. | 19 * @param {string} pref The pref that requires confirmation. |
| 20 * @param {string} metric User metrics identifier. | 20 * @param {string} metric User metrics identifier. |
| 21 * @param {string} confirmed_pref A pref used to remember whether the user has | 21 * @param {string=} opt_confirmedPref A pref used to remember whether the |
| 22 * confirmed the dialog before. This ensures that the user is presented | 22 * user has confirmed the dialog before. This ensures that the user is |
| 23 * with the dialog only once. If left |undefined| or |null|, the dialog | 23 * presented with the dialog only once. If left |undefined|, the dialog |
| 24 * will pop up every time the user attempts to set |pref| to |true|. | 24 * will pop up every time the user attempts to set |pref| to |true|. |
| 25 * @extends {options.SettingsDialog} | 25 * @extends {options.SettingsDialog} |
| 26 */ | 26 */ |
| 27 function ConfirmDialog(name, title, pageDivName, okButton, cancelButton, pref, | 27 function ConfirmDialog(name, title, pageDivName, okButton, cancelButton, pref, |
| 28 metric, confirmed_pref) { | 28 metric, opt_confirmedPref) { |
| 29 SettingsDialog.call(this, name, title, pageDivName, okButton, cancelButton); | 29 SettingsDialog.call(this, name, title, pageDivName, okButton, cancelButton); |
| 30 |
| 31 /** @protected */ |
| 30 this.pref = pref; | 32 this.pref = pref; |
| 33 |
| 34 /** @protected */ |
| 31 this.metric = metric; | 35 this.metric = metric; |
| 32 this.confirmed_pref = confirmed_pref; | 36 |
| 37 /** @private */ |
| 38 this.confirmedPref_ = opt_confirmedPref; |
| 39 |
| 40 /** @private */ |
| 33 this.confirmed_ = false; | 41 this.confirmed_ = false; |
| 34 } | 42 } |
| 35 | 43 |
| 36 ConfirmDialog.prototype = { | 44 ConfirmDialog.prototype = { |
| 37 // Set up the prototype chain | 45 // Set up the prototype chain |
| 38 __proto__: SettingsDialog.prototype, | 46 __proto__: SettingsDialog.prototype, |
| 39 | 47 |
| 40 /** | 48 /** |
| 41 * Handle changes to |pref|. Only uncommitted changes are relevant as these | 49 * Handle changes to |pref|. Only uncommitted changes are relevant as these |
| 42 * originate from user and need to be explicitly committed to take effect. | 50 * originate from user and need to be explicitly committed to take effect. |
| 43 * Pop up the dialog or commit the change, depending on whether confirmation | 51 * Pop up the dialog or commit the change, depending on whether confirmation |
| 44 * is needed. | 52 * is needed. |
| 45 * @param {Event} event Change event. | 53 * @param {Event} event Change event. |
| 46 * @private | 54 * @private |
| 47 */ | 55 */ |
| 48 onPrefChanged_: function(event) { | 56 onPrefChanged_: function(event) { |
| 49 if (!event.value.uncommitted) | 57 if (!event.value.uncommitted) |
| 50 return; | 58 return; |
| 51 | 59 |
| 52 if (event.value.value && !this.confirmed_) | 60 if (event.value.value && !this.confirmed_) |
| 53 PageManager.showPageByName(this.name, false); | 61 PageManager.showPageByName(this.name, false); |
| 54 else | 62 else |
| 55 Preferences.getInstance().commitPref(this.pref, this.metric); | 63 Preferences.getInstance().commitPref(this.pref, this.metric); |
| 56 }, | 64 }, |
| 57 | 65 |
| 58 /** | 66 /** |
| 59 * Handle changes to |confirmed_pref| by caching them. | 67 * Handle changes to |confirmedPref_| by caching them. |
| 60 * @param {Event} event Change event. | 68 * @param {Event} event Change event. |
| 61 * @private | 69 * @private |
| 62 */ | 70 */ |
| 63 onConfirmedChanged_: function(event) { | 71 onConfirmedChanged_: function(event) { |
| 64 this.confirmed_ = event.value.value; | 72 this.confirmed_ = event.value.value; |
| 65 }, | 73 }, |
| 66 | 74 |
| 67 /** @override */ | 75 /** @override */ |
| 68 initializePage: function() { | 76 initializePage: function() { |
| 69 SettingsDialog.prototype.initializePage.call(this); | 77 SettingsDialog.prototype.initializePage.call(this); |
| 70 | 78 |
| 71 this.okButton.onclick = this.handleConfirm.bind(this); | 79 this.okButton.onclick = this.handleConfirm.bind(this); |
| 72 this.cancelButton.onclick = this.handleCancel.bind(this); | 80 this.cancelButton.onclick = this.handleCancel.bind(this); |
| 73 Preferences.getInstance().addEventListener( | 81 Preferences.getInstance().addEventListener( |
| 74 this.pref, this.onPrefChanged_.bind(this)); | 82 this.pref, this.onPrefChanged_.bind(this)); |
| 75 if (this.confirmed_pref) { | 83 if (this.confirmedPref_) { |
| 76 Preferences.getInstance().addEventListener( | 84 Preferences.getInstance().addEventListener( |
| 77 this.confirmed_pref, this.onConfirmedChanged_.bind(this)); | 85 this.confirmedPref_, this.onConfirmedChanged_.bind(this)); |
| 78 } | 86 } |
| 79 }, | 87 }, |
| 80 | 88 |
| 81 /** | 89 /** |
| 82 * Handle the confirm button by committing the |pref| change. If | 90 * Handle the confirm button by committing the |pref| change. If |
| 83 * |confirmed_pref| has been specified, also remember that the dialog has | 91 * |confirmedPref_| has been specified, also remember that the dialog has |
| 84 * been confirmed to avoid bringing it up in the future. | 92 * been confirmed to avoid bringing it up in the future. |
| 85 * @override | 93 * @override |
| 86 */ | 94 */ |
| 87 handleConfirm: function() { | 95 handleConfirm: function() { |
| 88 SettingsDialog.prototype.handleConfirm.call(this); | 96 SettingsDialog.prototype.handleConfirm.call(this); |
| 89 | 97 |
| 90 Preferences.getInstance().commitPref(this.pref, this.metric); | 98 Preferences.getInstance().commitPref(this.pref, this.metric); |
| 91 if (this.confirmed_pref) | 99 if (this.confirmedPref_) |
| 92 Preferences.setBooleanPref(this.confirmed_pref, true, true); | 100 Preferences.setBooleanPref(this.confirmedPref_, true, true); |
| 93 }, | 101 }, |
| 94 | 102 |
| 95 /** | 103 /** |
| 96 * Handle the cancel button by rolling back the |pref| change without it | 104 * Handle the cancel button by rolling back the |pref| change without it |
| 97 * ever taking effect. | 105 * ever taking effect. |
| 98 * @override | 106 * @override |
| 99 */ | 107 */ |
| 100 handleCancel: function() { | 108 handleCancel: function() { |
| 101 SettingsDialog.prototype.handleCancel.call(this); | 109 SettingsDialog.prototype.handleCancel.call(this); |
| 102 Preferences.getInstance().rollbackPref(this.pref); | 110 Preferences.getInstance().rollbackPref(this.pref); |
| 103 }, | 111 }, |
| 104 | 112 |
| 105 /** | 113 /** |
| 106 * When a user navigates away from a confirm dialog, treat as a cancel. | 114 * When a user navigates away from a confirm dialog, treat as a cancel. |
| 107 * @protected | 115 * @protected |
| 108 * @override | 116 * @override |
| 109 */ | 117 */ |
| 110 willHidePage: function() { | 118 willHidePage: function() { |
| 111 if (this.visible) | 119 if (this.visible) |
| 112 Preferences.getInstance().rollbackPref(this.pref); | 120 Preferences.getInstance().rollbackPref(this.pref); |
| 113 }, | 121 }, |
| 114 }; | 122 }; |
| 115 | 123 |
| 116 return { | 124 return { |
| 117 ConfirmDialog: ConfirmDialog | 125 ConfirmDialog: ConfirmDialog |
| 118 }; | 126 }; |
| 119 }); | 127 }); |
| OLD | NEW |