header_bar.js revision 5821806d5e7f356e8fa4b058a389a808ea183019
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/**
6 * @fileoverview Login UI header bar implementation.
7 */
8
9cr.define('login', function() {
10  // Network state constants.
11  /** @const */ var NET_STATE = {
12    OFFLINE: 0,
13    ONLINE: 1,
14    PORTAL: 2
15  };
16
17  /**
18   * Creates a header bar element.
19   * @constructor
20   * @extends {HTMLDivElement}
21   */
22  var HeaderBar = cr.ui.define('div');
23
24  HeaderBar.prototype = {
25    __proto__: HTMLDivElement.prototype,
26
27    // Whether guest button should be shown when header bar is in normal mode.
28    showGuest_: false,
29
30    /** @inheritDoc */
31    decorate: function() {
32      $('shutdown-header-bar-item').addEventListener('click',
33          this.handleShutdownClick_);
34      $('shutdown-button').addEventListener('click',
35          this.handleShutdownClick_);
36      $('add-user-button').addEventListener('click', function(e) {
37        chrome.send('loginRequestNetworkState',
38                    ['login.HeaderBar.handleAddUser',
39                     'check']);
40      });
41      $('cancel-add-user-button').addEventListener('click',
42          this.handleCancelAddUserClick_);
43      $('guest-user-header-bar-item').addEventListener('click',
44          this.handleGuestClick_);
45      $('guest-user-button').addEventListener('click',
46          this.handleGuestClick_);
47      $('sign-out-user-button').addEventListener('click',
48          this.handleSignoutClick_);
49    },
50
51    /**
52     * Tab index value for all button elements.
53     * @type {number}
54     */
55    set buttonsTabIndex(tabIndex) {
56      var buttons = this.getElementsByTagName('button');
57      for (var i = 0, button; button = buttons[i]; ++i) {
58        button.tabIndex = tabIndex;
59      }
60    },
61
62    /**
63     * Disables the header bar and all of its elements.
64     * @type {boolean}
65     */
66    set disabled(value) {
67      var buttons = this.getElementsByTagName('button');
68      for (var i = 0, button; button = buttons[i]; ++i) {
69        button.disabled = value;
70      }
71    },
72
73    /**
74     * Cancel add user button click handler.
75     * @private
76     */
77    handleCancelAddUserClick_: function(e) {
78      $('login-header-bar').signinUIActive = false;
79      $('pod-row').loadLastWallpaper();
80      Oobe.showScreen({id: SCREEN_ACCOUNT_PICKER});
81      Oobe.resetSigninUI(true);
82    },
83
84    /**
85     * Guest button click handler.
86     * @private
87     */
88    handleGuestClick_: function(e) {
89      Oobe.disableSigninUI();
90      chrome.send('launchIncognito');
91      e.stopPropagation();
92    },
93
94    /**
95     * Sign out button click handler.
96     * @private
97     */
98    handleSignoutClick_: function(e) {
99      this.disabled = true;
100      chrome.send('signOutUser');
101      e.stopPropagation();
102    },
103
104    /**
105     * Shutdown button click handler.
106     * @private
107     */
108    handleShutdownClick_: function(e) {
109      chrome.send('shutdownSystem');
110      e.stopPropagation();
111    },
112
113    /**
114     * If true then "Browse as Guest" button is shown.
115     * @type {boolean}
116     */
117    set showGuestButton(value) {
118      this.showGuest_ = value;
119      this.updateUI_();
120    },
121
122    /**
123     * If true then sign in UI is active and header controls
124     * should change accordingly.
125     * @type {boolean}
126     */
127    set signinUIActive(value) {
128      this.signinUIActive_ = value;
129      this.updateUI_();
130    },
131
132    /**
133     * Whether the Cancel button is enabled during Gaia sign-in.
134     * @type {boolean}
135     */
136    set allowCancel(value) {
137      this.allowCancel_ = value;
138      this.updateUI_();
139    },
140
141    /**
142     * Updates visibility state of action buttons.
143     * @private
144     */
145    updateUI_: function() {
146      $('add-user-header-bar-item').hidden = false;
147      $('add-user-button').hidden = this.signinUIActive_;
148      $('cancel-add-user-button').hidden =
149          !this.signinUIActive_ || !this.allowCancel_;
150      $('guest-user-header-bar-item').hidden =
151          this.signinUIActive_ || !this.showGuest_;
152    },
153
154    /**
155     * Animates Header bar to hide from the screen.
156     * @param {function()} callback will be called once animation is finished.
157     */
158    animateOut: function(callback) {
159      var launcher = this;
160      launcher.addEventListener(
161          'webkitTransitionEnd', function f(e) {
162            launcher.removeEventListener('webkitTransitionEnd', f);
163            callback();
164          });
165      this.classList.remove('login-header-bar-animate-slow');
166      this.classList.add('login-header-bar-animate-fast');
167      this.classList.add('login-header-bar-hidden');
168    },
169
170    /**
171     * Animates Header bar to slowly appear on the screen.
172     */
173    animateIn: function() {
174      this.classList.remove('login-header-bar-animate-fast');
175      this.classList.add('login-header-bar-animate-slow');
176      this.classList.remove('login-header-bar-hidden');
177    },
178  };
179
180  /**
181   * Continues add user button click handling after network state has
182   * been recieved.
183   * @param {number} state Current state of the network (see NET_STATE).
184   * @param {string} network Name of the network.
185   * @param {string} reason Reason the callback was called.
186   * @param {number} last Last active network type.
187   */
188  HeaderBar.handleAddUser = function(state, network, reason, last) {
189    if (state != NET_STATE.OFFLINE) {
190      Oobe.showSigninUI();
191    } else {
192      /** @const */ var BUBBLE_OFFSET = 8;
193      /** @const */ var BUBBLE_PADDING = 5;
194      $('bubble').showTextForElement(
195          $('add-user-button'),
196          localStrings.getString('addUserErrorMessage'),
197          cr.ui.Bubble.Attachment.TOP, BUBBLE_OFFSET, BUBBLE_PADDING);
198      chrome.send('loginAddNetworkStateObserver',
199                  ['login.HeaderBar.bubbleWatchdog']);
200    }
201  };
202
203  /**
204   * Observes network state, and close the bubble when network becomes online.
205   * @param {number} state Current state of the network (see NET_STATE).
206   * @param {string} network Name of the network.
207   * @param {string} reason Reason the callback was called.
208   * @param {number} last Last active network type.
209   */
210  HeaderBar.bubbleWatchdog = function(state, network, reason, last) {
211    if (state != NET_STATE.OFFLINE) {
212      $('bubble').hideForElement($('add-user-button'));
213      chrome.send('loginRemoveNetworkStateObserver',
214                  ['login.HeaderBar.bubbleWatchdog']);
215    }
216  };
217
218  return {
219    HeaderBar: HeaderBar
220  };
221});
222