summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/chromeos/login/apps_menu.js
blob: 37fa769ce121c91131e07d05012f9cfd42f79e08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview Kiosk apps menu implementation.
 */

cr.define('login', function() {
  'use strict';

  var Menu = cr.ui.Menu;
  var MenuButton = cr.ui.MenuButton;

  /**
   * Creates apps menu button.
   * @constructor
   * @extends {cr.ui.MenuButton}
   */
  var AppsMenuButton = cr.ui.define('button');

  AppsMenuButton.prototype = {
    __proto__: MenuButton.prototype,

    /**
     * Flag of whether to rebuild the menu.
     * @type {boolean}
     * @private
     */
    needsRebuild_: true,

    /**
     * Array to hold apps info.
     * @type {Array}
     */
    data_: null,
    get data() {
      return this.data_;
    },
    set data(data) {
      this.data_ = data;
      this.needsRebuild_ = true;
    },

    /** @override */
    decorate: function() {
      MenuButton.prototype.decorate.call(this);
      this.menu = new Menu;
      cr.ui.decorate(this.menu, Menu);
      document.body.appendChild(this.menu);

      this.anchorType = cr.ui.AnchorType.ABOVE;
      chrome.send('initializeKioskApps');
    },

    /** @override */
    showMenu: function(shouldSetFocus) {
      if (this.needsRebuild_) {
        this.menu.textContent = '';
        this.data_.forEach(this.addItem_, this);
        this.needsRebuild_ = false;
      }

      if (this.data.length > 0)
        MenuButton.prototype.showMenu.apply(this, arguments);
    },

    /**
     * Invoked when apps menu becomes visible.
     */
    didShow: function() {
      window.setTimeout(function() {
        if (!$('apps-header-bar-item').hidden)
          chrome.send('checkKioskAppLaunchError');
      }, 500);
    },

    findAndRunAppForTesting: function(id, opt_diagnostic_mode) {
      this.showMenu(true);
      for (var i = 0; i < this.menu.menuItems.length; i++) {
        var menuNode = this.menu.menuItems[i];
        if (menuNode.appId == id) {
          var activationEvent = cr.doc.createEvent('Event');
          activationEvent.initEvent('activate', true, true);

          if (opt_diagnostic_mode) {
            var fakeCtrlEnterEvent = cr.doc.createEvent('KeyboardEvent');
            fakeCtrlEnterEvent.initKeyboardEvent('keypress', true, true, null,
                                                 'Enter', 0,
                                                 true, false, false, false);
            activationEvent.originalEvent = fakeCtrlEnterEvent;
          }

          menuNode.dispatchEvent(activationEvent);
          break;
        }
      }
    },

    /**
     * Launch the app. If |diagnosticMode| is true, ask user to confirm.
     * @param {Object} app App data.
     * @param {boolean} diagnosticMode Whether to run the app in diagnostic
     *     mode.
     */
    launchApp_: function(app, diagnosticMode) {
      if (!diagnosticMode) {
        chrome.send('launchKioskApp', [app.id, false]);
        return;
      }

      if (!this.confirmDiagnosticMode_) {
        this.confirmDiagnosticMode_ =
            new cr.ui.dialogs.ConfirmDialog(document.body);
        this.confirmDiagnosticMode_.setOkLabel(
            loadTimeData.getString('confirmKioskAppDiagnosticModeYes'));
        this.confirmDiagnosticMode_.setCancelLabel(
            loadTimeData.getString('confirmKioskAppDiagnosticModeNo'));
      }

      this.confirmDiagnosticMode_.show(
          loadTimeData.getStringF('confirmKioskAppDiagnosticModeFormat',
                                  app.label),
          function() {
            chrome.send('launchKioskApp', [app.id, true]);
          });
    },

    /**
     * Adds an app to the menu.
     * @param {Object} app An app info object.
     * @private
     */
    addItem_: function(app) {
      var menuItem = this.menu.addMenuItem(app);
      menuItem.classList.add('apps-menu-item');
      menuItem.appId = app.id;
      menuItem.addEventListener('activate', function(e) {
        var diagnosticMode = e.originalEvent && e.originalEvent.ctrlKey;
        this.launchApp_(app, diagnosticMode);
      }.bind(this));
    }
  };

  /**
   * Sets apps to be displayed in the apps menu.
   * @param {!Array.<!Object>} apps An array of app info objects.
   */
  AppsMenuButton.setApps = function(apps) {
    $('show-apps-button').data = apps;
    $('login-header-bar').hasApps =
        apps.length > 0 || loadTimeData.getBoolean('kioskAppHasLaunchError');
    chrome.send('kioskAppsLoaded');
  };

  /**
   * Shows the given error message.
   * @param {!string} message Error message to show.
   */
  AppsMenuButton.showError = function(message) {
    /** @const */ var BUBBLE_OFFSET = 25;
    /** @const */ var BUBBLE_PADDING = 12;
    $('bubble').showTextForElement($('show-apps-button'),
                                   message,
                                   cr.ui.Bubble.Attachment.TOP,
                                   BUBBLE_OFFSET,
                                   BUBBLE_PADDING);
  };


  /**
   * Runs app with a given id from the list of loaded apps.
   * @param {!string} id of an app to run.
   * @param {boolean=} opt_diagnostic_mode Whether to run the app in diagnostic
   *     mode.  Default is false.
   */
  AppsMenuButton.runAppForTesting = function(id, opt_diagnostic_mode) {
    $('show-apps-button').findAndRunAppForTesting(id, opt_diagnostic_mode);
  };

  return {
    AppsMenuButton: AppsMenuButton
  };
});