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.
5 cr.define('options', function() {
6 /** @const */ var SettingsDialog = options.SettingsDialog;
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.
14 * @param {string} name See Page constructor.
15 * @param {string} title See Page constructor.
16 * @param {string} pageDivName See Page constructor.
17 * @param {HTMLButtonElement} okButton The confirmation button element.
18 * @param {HTMLButtonElement} cancelButton The cancellation button element.
19 * @param {string} pref The pref that requires confirmation.
20 * @param {string} metric User metrics identifier.
21 * @param {string=} opt_confirmedPref A pref used to remember whether the
22 * user has confirmed the dialog before. This ensures that the user is
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|.
25 * @extends {options.SettingsDialog}
27 function ConfirmDialog(name, title, pageDivName, okButton, cancelButton, pref,
28 metric, opt_confirmedPref) {
29 SettingsDialog.call(this, name, title, pageDivName, okButton, cancelButton);
38 this.confirmedPref_ = opt_confirmedPref;
41 this.confirmed_ = false;
44 ConfirmDialog.prototype = {
45 // Set up the prototype chain
46 __proto__: SettingsDialog.prototype,
49 * Handle changes to |pref|. Only uncommitted changes are relevant as these
50 * originate from user and need to be explicitly committed to take effect.
51 * Pop up the dialog or commit the change, depending on whether confirmation
53 * @param {Event} event Change event.
56 onPrefChanged_: function(event) {
57 if (!event.value.uncommitted)
60 if (event.value.value && !this.confirmed_)
61 PageManager.showPageByName(this.name, false);
63 Preferences.getInstance().commitPref(this.pref, this.metric);
67 * Handle changes to |confirmedPref_| by caching them.
68 * @param {Event} event Change event.
71 onConfirmedChanged_: function(event) {
72 this.confirmed_ = event.value.value;
76 initializePage: function() {
77 SettingsDialog.prototype.initializePage.call(this);
79 this.okButton.onclick = this.handleConfirm.bind(this);
80 this.cancelButton.onclick = this.handleCancel.bind(this);
81 Preferences.getInstance().addEventListener(
82 this.pref, this.onPrefChanged_.bind(this));
83 if (this.confirmedPref_) {
84 Preferences.getInstance().addEventListener(
85 this.confirmedPref_, this.onConfirmedChanged_.bind(this));
90 * Handle the confirm button by committing the |pref| change. If
91 * |confirmedPref_| has been specified, also remember that the dialog has
92 * been confirmed to avoid bringing it up in the future.
95 handleConfirm: function() {
96 SettingsDialog.prototype.handleConfirm.call(this);
98 Preferences.getInstance().commitPref(this.pref, this.metric);
99 if (this.confirmedPref_)
100 Preferences.setBooleanPref(this.confirmedPref_, true, true);
104 * Handle the cancel button by rolling back the |pref| change without it
105 * ever taking effect.
108 handleCancel: function() {
109 SettingsDialog.prototype.handleCancel.call(this);
110 Preferences.getInstance().rollbackPref(this.pref);
114 * When a user navigates away from a confirm dialog, treat as a cancel.
118 willHidePage: function() {
120 Preferences.getInstance().rollbackPref(this.pref);
125 ConfirmDialog: ConfirmDialog