Home | History | Annotate | Download | only in login
      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.
      4 
      5 /**
      6  * @fileoverview Kiosk apps menu implementation.
      7  */
      8 
      9 cr.define('login', function() {
     10   'use strict';
     11 
     12   var Menu = cr.ui.Menu;
     13   var MenuButton = cr.ui.MenuButton;
     14 
     15   /**
     16    * Creates apps menu button.
     17    * @constructor
     18    * @extends {cr.ui.MenuButton}
     19    */
     20   var AppsMenuButton = cr.ui.define('button');
     21 
     22   AppsMenuButton.prototype = {
     23     __proto__: MenuButton.prototype,
     24 
     25     /**
     26      * Flag of whether to rebuild the menu.
     27      * @type {boolean}
     28      * @private
     29      */
     30     needsRebuild_: true,
     31 
     32     /**
     33      * Array to hold apps info.
     34      * @type {Array}
     35      */
     36     data_: null,
     37     get data() {
     38       return this.data_;
     39     },
     40     set data(data) {
     41       this.data_ = data;
     42       this.needsRebuild_ = true;
     43     },
     44 
     45     /** @override */
     46     decorate: function() {
     47       MenuButton.prototype.decorate.call(this);
     48       this.menu = new Menu;
     49       cr.ui.decorate(this.menu, Menu);
     50       document.body.appendChild(this.menu);
     51 
     52       this.anchorType = cr.ui.AnchorType.ABOVE;
     53       chrome.send('initializeKioskApps');
     54     },
     55 
     56     /** @override */
     57     showMenu: function(shouldSetFocus) {
     58       if (this.needsRebuild_) {
     59         this.menu.textContent = '';
     60         this.data_.forEach(this.addItem_, this);
     61         this.needsRebuild_ = false;
     62       }
     63 
     64       MenuButton.prototype.showMenu.apply(this, arguments);
     65     },
     66 
     67     /**
     68      * Invoked when apps menu becomes visible.
     69      */
     70     didShow: function() {
     71       window.setTimeout(function() {
     72         if (!$('apps-header-bar-item').hidden)
     73           chrome.send('checkKioskAppLaunchError');
     74       }, 500);
     75     },
     76 
     77     findAndRunAppForTesting: function(id) {
     78       this.showMenu(true);
     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);
     84           menuNode.dispatchEvent(activationEvent);
     85           break;
     86         }
     87       }
     88     },
     89 
     90     /**
     91      * Adds an app to the menu.
     92      * @param {Object} app An app info object.
     93      * @private
     94      */
     95     addItem_: function(app) {
     96       var menuItem = this.menu.addMenuItem(app);
     97       menuItem.classList.add('apps-menu-item');
     98       menuItem.appId = app.id;
     99       menuItem.addEventListener('activate', function() {
    100         chrome.send('launchKioskApp', [app.id]);
    101       });
    102     }
    103   };
    104 
    105   /**
    106    * Sets apps to be displayed in the apps menu.
    107    * @param {!Array.<!Object>} apps An array of app info objects.
    108    */
    109   AppsMenuButton.setApps = function(apps) {
    110     $('show-apps-button').data = apps;
    111     $('login-header-bar').hasApps = apps.length > 0;
    112     chrome.send('kioskAppsLoaded');
    113   };
    114 
    115   /**
    116    * Shows the given error message.
    117    * @param {!string} message Error message to show.
    118    */
    119   AppsMenuButton.showError = function(message) {
    120     /** @const */ var BUBBLE_OFFSET = 25;
    121     /** @const */ var BUBBLE_PADDING = 12;
    122     $('bubble').showTextForElement($('show-apps-button'),
    123                                    message,
    124                                    cr.ui.Bubble.Attachment.TOP,
    125                                    BUBBLE_OFFSET,
    126                                    BUBBLE_PADDING);
    127   };
    128 
    129 
    130   /**
    131    * Runs app with a given id from the list of loaded apps.
    132    * @param {!string} id of an app to run.
    133    */
    134   AppsMenuButton.runAppForTesting = function(id) {
    135     $('show-apps-button').findAndRunAppForTesting(id);
    136   };
    137 
    138   return {
    139     AppsMenuButton: AppsMenuButton
    140   };
    141 });
    142