Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 cr.define('options', function() { | |
| 6 /** @const */ var OptionsPage = options.OptionsPage; | |
| 7 | |
| 8 /** | |
| 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, | |
| 11 * the new value is committed to Chrome. If the user clicks Cancel or leaves | |
| 12 * the settings page, the new value is discarded. | |
| 13 * @constructor | |
| 14 * @param {string} name See OptionsPage constructor. | |
| 15 * @param {string} title See OptionsPage constructor. | |
| 16 * @param {string} pageDivName See OptionsPage constructor. | |
| 17 * @param {HTMLInputElement} okButton The confirmation button element. | |
| 18 * @param {HTMLInputElement} cancelButton The cancellation button element. | |
| 19 * @param {string} pref The pref that requires confirmation. | |
| 20 * @param {string} metric User metrics identifier. | |
| 21 * @param {string} confirmed_pref A pref used to remember whether the user has | |
| 22 * confirmed the dialog before. This ensures that the user is presented | |
| 23 * with the dialog only once. If left |undefined| or |null|, the dialog | |
| 24 * will pop up every time the user attempts to set |pref| to |true|. | |
| 25 * @extends {OptionsPage} | |
| 26 */ | |
| 27 function ConfirmDialog(name, title, pageDivName, okButton, cancelButton, pref, | |
| 28 metric, confirmed_pref) { | |
| 29 OptionsPage.call(this, name, title, pageDivName); | |
| 30 this.okButton = okButton; | |
| 31 this.cancelButton = cancelButton; | |
| 32 this.pref = pref; | |
| 33 this.metric = metric; | |
| 34 this.confirmed_pref = confirmed_pref; | |
| 35 this.confirmed_ = false; | |
| 36 } | |
| 37 | |
| 38 ConfirmDialog.prototype = { | |
| 39 // Set up the prototype chain | |
| 40 __proto__: OptionsPage.prototype, | |
| 41 | |
| 42 /** | |
| 43 * Handle changes to |pref|. Only uncommitted changes are relevant as these | |
| 44 * originate from user and need to be explicitly committed to take effect. | |
| 45 * Pop up the dialog or commit the change, depending on whether confirmation | |
| 46 * is needed. | |
| 47 * @param {Event} event Change event. | |
| 48 * @private | |
| 49 */ | |
| 50 onPrefChanged_: function(event) { | |
| 51 if (!event.value.uncommitted) | |
| 52 return; | |
| 53 if (event.value.value && !this.confirmed_) | |
|
James Hawkins
2012/10/24 00:54:17
Optional nit: For readability, I'd insert a blank
bartfab (slow)
2012/10/24 13:51:46
Done.
| |
| 54 OptionsPage.showPageByName(this.name, false); | |
| 55 else | |
| 56 Preferences.getInstance().commitPref(this.pref, this.metric); | |
| 57 }, | |
| 58 | |
| 59 /** | |
| 60 * Handle changes to |confirmed_pref| by caching them. | |
| 61 * @param {Event} event Change event. | |
| 62 * @private | |
| 63 */ | |
| 64 onConfirmedChanged_: function(event) { | |
| 65 this.confirmed_ = event.value.value; | |
| 66 }, | |
| 67 | |
| 68 /** | |
| 69 * @inheritDoc | |
| 70 */ | |
| 71 initializePage: function() { | |
| 72 this.okButton.onclick = this.handleConfirm.bind(this); | |
| 73 this.cancelButton.onclick = this.handleCancel.bind(this); | |
| 74 Preferences.getInstance().addEventListener( | |
| 75 this.pref, this.onPrefChanged_.bind(this)); | |
| 76 if (this.confirmed_pref) { | |
| 77 Preferences.getInstance().addEventListener( | |
| 78 this.confirmed_pref, this.onConfirmedChanged_.bind(this)); | |
| 79 } | |
| 80 }, | |
| 81 | |
| 82 /** | |
| 83 * Handle the confirm button by committing the |pref| change. If | |
| 84 * |confirmed_pref| has been specified, also remember that the dialog has | |
| 85 * been confirmed to avoid bringing it up in the future. | |
| 86 */ | |
| 87 handleConfirm: function() { | |
| 88 OptionsPage.closeOverlay(); | |
| 89 | |
| 90 Preferences.getInstance().commitPref(this.pref, this.metric); | |
| 91 if (this.confirmed_pref) | |
| 92 Preferences.setBooleanPref(this.confirmed_pref, true, true); | |
| 93 }, | |
| 94 | |
| 95 /** | |
| 96 * Handle the cancel button by rolling back the |pref| change without it | |
| 97 * ever taking effect. | |
| 98 */ | |
| 99 handleCancel: function() { | |
| 100 OptionsPage.closeOverlay(); | |
| 101 | |
| 102 Preferences.getInstance().rollbackPref(this.pref); | |
| 103 }, | |
| 104 }; | |
| 105 | |
| 106 return { | |
| 107 ConfirmDialog: ConfirmDialog | |
| 108 }; | |
| 109 }); | |
| OLD | NEW |