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 OptionsPage = options.OptionsPage;
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 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}
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;
34 this.confirmed_pref = confirmed_pref;
35 this.confirmed_ = false;
38 ConfirmDialog.prototype = {
39 // Set up the prototype chain
40 __proto__: OptionsPage.prototype,
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
47 * @param {Event} event Change event.
50 onPrefChanged_: function(event) {
51 if (!event.value.uncommitted)
54 if (event.value.value && !this.confirmed_)
55 OptionsPage.showPageByName(this.name, false);
57 Preferences.getInstance().commitPref(this.pref, this.metric);
61 * Handle changes to |confirmed_pref| by caching them.
62 * @param {Event} event Change event.
65 onConfirmedChanged_: function(event) {
66 this.confirmed_ = event.value.value;
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));
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.
86 handleConfirm: function() {
87 OptionsPage.closeOverlay();
89 Preferences.getInstance().commitPref(this.pref, this.metric);
90 if (this.confirmed_pref)
91 Preferences.setBooleanPref(this.confirmed_pref, true, true);
95 * Handle the cancel button by rolling back the |pref| change without it
98 handleCancel: function() {
99 OptionsPage.closeOverlay();
101 Preferences.getInstance().rollbackPref(this.pref);
105 * When a user navigates away from a confirm dialog, treat as a cancel.
109 willHidePage: function() {
111 Preferences.getInstance().rollbackPref(this.pref);
116 ConfirmDialog: ConfirmDialog