1 // Copyright 2013 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.
6 * @fileoverview Kiosk apps menu implementation.
9 cr.define('login', function() {
12 var Menu = cr.ui.Menu;
13 var MenuButton = cr.ui.MenuButton;
16 * Creates apps menu button.
18 * @extends {cr.ui.MenuButton}
20 var AppsMenuButton = cr.ui.define('button');
22 AppsMenuButton.prototype = {
23 __proto__: MenuButton.prototype,
26 * Flag of whether to rebuild the menu.
33 * Array to hold apps info.
42 this.needsRebuild_ = true;
46 decorate: function() {
47 MenuButton.prototype.decorate.call(this);
49 cr.ui.decorate(this.menu, Menu);
50 document.body.appendChild(this.menu);
52 this.anchorType = cr.ui.AnchorType.ABOVE;
53 chrome.send('initializeKioskApps');
57 showMenu: function(shouldSetFocus) {
58 if (this.needsRebuild_) {
59 this.menu.textContent = '';
60 this.data_.forEach(this.addItem_, this);
61 this.needsRebuild_ = false;
64 MenuButton.prototype.showMenu.apply(this, arguments);
68 * Invoked when apps menu becomes visible.
71 window.setTimeout(function() {
72 if (!$('apps-header-bar-item').hidden)
73 chrome.send('checkKioskAppLaunchError');
77 findAndRunAppForTesting: function(id, opt_diagnostic_mode) {
79 for (var i = 0; i < this.menu.menuItems.length; i++) {
80 var menuNode = this.menu.menuItems[i];
81 if (menuNode.appId == id) {
82 var activationEvent = cr.doc.createEvent('Event');
83 activationEvent.initEvent('activate', true, true);
85 if (opt_diagnostic_mode) {
86 var fakeCtrlEnterEvent = cr.doc.createEvent('KeyboardEvent');
87 fakeCtrlEnterEvent.initKeyboardEvent('keypress', true, true, null,
89 true, false, false, false);
90 activationEvent.originalEvent = fakeCtrlEnterEvent;
93 menuNode.dispatchEvent(activationEvent);
100 * Launch the app. If |diagnosticMode| is true, ask user to confirm.
101 * @param {Object} app App data.
102 * @param {boolean} diagnosticMode Whether to run the app in diagnostic
105 launchApp_: function(app, diagnosticMode) {
106 if (!diagnosticMode) {
107 chrome.send('launchKioskApp', [app.id, false]);
111 if (!this.confirmDiagnosticMode_) {
112 this.confirmDiagnosticMode_ =
113 new cr.ui.dialogs.ConfirmDialog(document.body);
114 this.confirmDiagnosticMode_.setOkLabel(
115 loadTimeData.getString('confirmKioskAppDiagnosticModeYes'));
116 this.confirmDiagnosticMode_.setCancelLabel(
117 loadTimeData.getString('confirmKioskAppDiagnosticModeNo'));
120 this.confirmDiagnosticMode_.show(
121 loadTimeData.getStringF('confirmKioskAppDiagnosticModeFormat',
124 chrome.send('launchKioskApp', [app.id, true]);
129 * Adds an app to the menu.
130 * @param {Object} app An app info object.
133 addItem_: function(app) {
134 var menuItem = this.menu.addMenuItem(app);
135 menuItem.classList.add('apps-menu-item');
136 menuItem.appId = app.id;
137 menuItem.addEventListener('activate', function(e) {
138 var diagnosticMode = e.originalEvent && e.originalEvent.ctrlKey;
139 this.launchApp_(app, diagnosticMode);
145 * Sets apps to be displayed in the apps menu.
146 * @param {!Array.<!Object>} apps An array of app info objects.
148 AppsMenuButton.setApps = function(apps) {
149 $('show-apps-button').data = apps;
150 $('login-header-bar').hasApps = apps.length > 0;
151 chrome.send('kioskAppsLoaded');
155 * Shows the given error message.
156 * @param {!string} message Error message to show.
158 AppsMenuButton.showError = function(message) {
159 /** @const */ var BUBBLE_OFFSET = 25;
160 /** @const */ var BUBBLE_PADDING = 12;
161 $('bubble').showTextForElement($('show-apps-button'),
163 cr.ui.Bubble.Attachment.TOP,
170 * Runs app with a given id from the list of loaded apps.
171 * @param {!string} id of an app to run.
172 * @param {boolean=} opt_diagnostic_mode Whether to run the app in diagnostic
173 * mode. Default is false.
175 AppsMenuButton.runAppForTesting = function(id, opt_diagnostic_mode) {
176 $('show-apps-button').findAndRunAppForTesting(id, opt_diagnostic_mode);
180 AppsMenuButton: AppsMenuButton