Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / options / hotword_confirm_dialog.js
1 // Copyright 2014 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 ConfirmDialog = options.ConfirmDialog;
7   /** @const */ var SettingsDialog = options.SettingsDialog;
8   /** @const */ var PageManager = cr.ui.pageManager.PageManager;
9
10   /**
11    * A dialog that will pop up when the user attempts to set the value of the
12    * Boolean |pref| to |true|, asking for confirmation. It will first check for
13    * any errors and if any exist, not display the dialog but toggle the
14    * indicator. Like its superclass, if the user clicks OK, the new value is
15    * committed to Chrome. If the user clicks Cancel or leaves the settings page,
16    * the new value is discarded.
17    * @constructor
18    * @extends {options.ConfirmDialog}
19    */
20   function HotwordConfirmDialog() {
21     ConfirmDialog.call(this,
22         'hotwordConfim',  // name
23         loadTimeData.getString('hotwordConfirmOverlayTabTitle'),
24         'hotword-confirm-overlay',  // pageDivName
25         assertInstanceof($('hotword-confirm-ok'), HTMLButtonElement),
26         assertInstanceof($('hotword-confirm-cancel'), HTMLButtonElement),
27         $('hotword-search-enable')['pref'], // pref
28         $('hotword-search-enable')['metric']); // metric
29
30     this.indicator = $('hotword-search-setting-indicator');
31   }
32
33   HotwordConfirmDialog.prototype = {
34     // TODO(dbeam): this class should probably derive SettingsDialog again as it
35     // evily duplicates much of ConfirmDialog's functionality, calls methods
36     // on SettingsDialog.prototype, and shadows private method names.
37     __proto__: ConfirmDialog.prototype,
38
39     /**
40      * Handle changes to |pref|. Only uncommitted changes are relevant as these
41      * originate from user and need to be explicitly committed to take effect.
42      * Pop up the dialog if there are no errors from the indicator or commit the
43      * change, depending on whether confirmation is needed.
44      * @param {Event} event Change event.
45      * @private
46      */
47     onPrefChanged_: function(event) {
48       if (!event.value.uncommitted)
49         return;
50
51       if (event.value.value && !this.confirmed_) {
52         // If there is an error, show the indicator icon with more information.
53         if (this.indicator.errorText)
54           this.indicator.updateBasedOnError();
55
56         // Show confirmation dialog (regardless of whether there is an error).
57         PageManager.showPageByName(this.name, false);
58       } else {
59         Preferences.getInstance().commitPref(this.pref, this.metric);
60       }
61     },
62
63     /**
64      * Override the initializePage function so that an updated version of
65      * onPrefChanged_ can be used.
66      * @override
67      */
68     initializePage: function() {
69       SettingsDialog.prototype.initializePage.call(this);
70
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     }
76   };
77
78   return {
79     HotwordConfirmDialog: HotwordConfirmDialog
80   };
81 });