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 54 if (event.value.value && !this.confirmed_) 55 OptionsPage.showPageByName(this.name, false); 56 else 57 Preferences.getInstance().commitPref(this.pref, this.metric); 58 }, 59 60 /** 61 * Handle changes to |confirmed_pref| by caching them. 62 * @param {Event} event Change event. 63 * @private 64 */ 65 onConfirmedChanged_: function(event) { 66 this.confirmed_ = event.value.value; 67 }, 68 69 /** @override */ 70 initializePage: function() { 71 this.okButton.onclick = this.handleConfirm.bind(this); 72 this.cancelButton.onclick = this.handleCancel.bind(this); 73 Preferences.getInstance().addEventListener( 74 this.pref, this.onPrefChanged_.bind(this)); 75 if (this.confirmed_pref) { 76 Preferences.getInstance().addEventListener( 77 this.confirmed_pref, this.onConfirmedChanged_.bind(this)); 78 } 79 }, 80 81 /** 82 * Handle the confirm button by committing the |pref| change. If 83 * |confirmed_pref| has been specified, also remember that the dialog has 84 * been confirmed to avoid bringing it up in the future. 85 */ 86 handleConfirm: function() { 87 OptionsPage.closeOverlay(); 88 89 Preferences.getInstance().commitPref(this.pref, this.metric); 90 if (this.confirmed_pref) 91 Preferences.setBooleanPref(this.confirmed_pref, true, true); 92 }, 93 94 /** 95 * Handle the cancel button by rolling back the |pref| change without it 96 * ever taking effect. 97 */ 98 handleCancel: function() { 99 OptionsPage.closeOverlay(); 100 101 Preferences.getInstance().rollbackPref(this.pref); 102 }, 103 104 /** 105 * When a user navigates away from a confirm dialog, treat as a cancel. 106 * @protected 107 * @override 108 */ 109 willHidePage: function() { 110 if (this.visible) 111 Preferences.getInstance().rollbackPref(this.pref); 112 }, 113 }; 114 115 return { 116 ConfirmDialog: ConfirmDialog 117 }; 118 }); 119