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
5cr.define('options', function() {
6  var Page = cr.ui.pageManager.Page;
7  var PageManager = cr.ui.pageManager.PageManager;
8
9  // UI state of the turn off overlay.
10  // @enum {string}
11  var UIState = {
12    UNKNOWN: 'unknown',
13    OFFLINE: 'offline',
14    IDLE: 'idle',
15    PENDING: 'pending',
16    SERVER_ERROR: 'server-error',
17  };
18
19  /**
20   * EasyUnlockTurnOffOverlay class
21   * Encapsulated handling of the Factory Reset confirmation overlay page.
22   * @class
23   */
24  function EasyUnlockTurnOffOverlay() {
25    Page.call(this, 'easyUnlockTurnOffOverlay',
26              loadTimeData.getString('easyUnlockTurnOffTitle'),
27              'easy-unlock-turn-off-overlay');
28  }
29
30  cr.addSingletonGetter(EasyUnlockTurnOffOverlay);
31
32  EasyUnlockTurnOffOverlay.prototype = {
33    // Inherit EasyUnlockTurnOffOverlay from Page.
34    __proto__: Page.prototype,
35
36    /** Current UI state */
37    uiState_: UIState.UNKNOWN,
38    get uiState() {
39      return this.uiState_;
40    },
41    set uiState(newUiState) {
42      if (newUiState == this.uiState_)
43        return;
44
45      this.uiState_ = newUiState;
46      switch (this.uiState_) {
47        case UIState.OFFLINE:
48          this.setUpOfflineUI_();
49          break;
50        case UIState.IDLE:
51          this.setUpTurnOffUI_(false);
52          break;
53        case UIState.PENDING:
54          this.setUpTurnOffUI_(true);
55          break;
56        case UIState.SERVER_ERROR:
57          this.setUpServerErrorUI_();
58          break;
59        default:
60          console.error('Unknow Easy unlock turn off UI state: ' +
61                        this.uiState_);
62          this.setUpTurnOffUI_(false);
63          break;
64      }
65    },
66
67    /** @override */
68    initializePage: function() {
69      Page.prototype.initializePage.call(this);
70
71      $('easy-unlock-turn-off-dismiss').onclick = function(event) {
72        EasyUnlockTurnOffOverlay.dismiss();
73      };
74      $('easy-unlock-turn-off-confirm').onclick = function(event) {
75        this.uiState = UIState.PENDING;
76        chrome.send('easyUnlockRequestTurnOff');
77      }.bind(this);
78    },
79
80    /** @override */
81    didShowPage: function() {
82      if (navigator.onLine) {
83        this.uiState = UIState.IDLE;
84        chrome.send('easyUnlockGetTurnOffFlowStatus');
85      } else {
86        this.uiState = UIState.OFFLINE;
87      }
88    },
89
90    /** @override */
91    didClosePage: function() {
92      chrome.send('easyUnlockTurnOffOverlayDismissed');
93    },
94
95    /**
96     * Returns the button strip element.
97     * @return {HTMLDivElement} The container div of action buttons.
98     */
99    get buttonStrip() {
100      return this.pageDiv.querySelector('.button-strip');
101    },
102
103    /**
104     * Set visibility of action buttons in button strip.
105     * @private
106     */
107    setActionButtonsVisible_: function(visible) {
108      var buttons = this.buttonStrip.querySelectorAll('button');
109      for (var i = 0; i < buttons.length; ++i) {
110        buttons[i].hidden = !visible;
111      }
112    },
113
114    /**
115     * Set visibility of spinner.
116     * @private
117     */
118    setSpinnerVisible_: function(visible) {
119      $('easy-unlock-turn-off-spinner').hidden = !visible;
120    },
121
122    /**
123     * Set up UI for showing offline message.
124     * @private
125     */
126    setUpOfflineUI_: function() {
127      $('easy-unlock-turn-off-title').textContent =
128          loadTimeData.getString('easyUnlockTurnOffOfflineTitle');
129      $('easy-unlock-turn-off-messagee').textContent =
130          loadTimeData.getString('easyUnlockTurnOffOfflineMessage');
131
132      this.setActionButtonsVisible_(false);
133      this.setSpinnerVisible_(false);
134    },
135
136    /**
137     * Set up UI for turning off Easy Unlock.
138     * @param {boolean} pending Whether there is a pending turn-off call.
139     * @private
140     */
141    setUpTurnOffUI_: function(pending) {
142      $('easy-unlock-turn-off-title').textContent =
143          loadTimeData.getString('easyUnlockTurnOffTitle');
144      $('easy-unlock-turn-off-messagee').textContent =
145          loadTimeData.getString('easyUnlockTurnOffDescription');
146      $('easy-unlock-turn-off-confirm').textContent =
147          loadTimeData.getString('easyUnlockTurnOffButton');
148
149      this.setActionButtonsVisible_(true);
150      this.setSpinnerVisible_(pending);
151      $('easy-unlock-turn-off-confirm').disabled = pending;
152      $('easy-unlock-turn-off-dismiss').hidden = false;
153    },
154
155    /**
156     * Set up UI for showing server error.
157     * @private
158     */
159    setUpServerErrorUI_: function() {
160      $('easy-unlock-turn-off-title').textContent =
161          loadTimeData.getString('easyUnlockTurnOffErrorTitle');
162      $('easy-unlock-turn-off-messagee').textContent =
163          loadTimeData.getString('easyUnlockTurnOffErrorMessage');
164      $('easy-unlock-turn-off-confirm').textContent =
165          loadTimeData.getString('easyUnlockTurnOffRetryButton');
166
167      this.setActionButtonsVisible_(true);
168      this.setSpinnerVisible_(false);
169      $('easy-unlock-turn-off-confirm').disabled = false;
170      $('easy-unlock-turn-off-dismiss').hidden = true;
171    },
172  };
173
174  /**
175   * Closes the Easy unlock turn off overlay.
176   */
177  EasyUnlockTurnOffOverlay.dismiss = function() {
178    PageManager.closeOverlay();
179  };
180
181  /**
182   * Update UI to reflect the turn off operation status.
183   * @param {string} newState The UIState string representing the new state.
184   */
185  EasyUnlockTurnOffOverlay.updateUIState = function(newState) {
186    EasyUnlockTurnOffOverlay.getInstance().uiState = newState;
187  };
188
189  // Export
190  return {
191    EasyUnlockTurnOffOverlay: EasyUnlockTurnOffOverlay
192  };
193});
194