Home | History | Annotate | Download | only in ntp4
      1 // Copyright (c) 2011 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 The recently closed menu: button, model data, and menu.
      7  */
      8 
      9 cr.define('ntp4', function() {
     10   'use strict';
     11 
     12   var localStrings = new LocalStrings();
     13 
     14   /**
     15    * Returns the text used for a recently closed window.
     16    * @param {number} numTabs Number of tabs in the window.
     17    * @return {string} The text to use.
     18    */
     19   function formatTabsText(numTabs) {
     20     if (numTabs == 1)
     21       return localStrings.getString('closedwindowsingle');
     22     return localStrings.getStringF('closedwindowmultiple', numTabs);
     23   }
     24 
     25   var Menu = cr.ui.Menu;
     26   var MenuItem = cr.ui.MenuItem;
     27   var MenuButton = cr.ui.MenuButton;
     28   var RecentMenuButton = cr.ui.define('button');
     29 
     30   RecentMenuButton.prototype = {
     31     __proto__: MenuButton.prototype,
     32 
     33     decorate: function() {
     34       MenuButton.prototype.decorate.call(this);
     35       this.menu = new Menu;
     36       cr.ui.decorate(this.menu, Menu);
     37       this.menu.classList.add('recent-menu');
     38       document.body.appendChild(this.menu);
     39 
     40       this.needsRebuild_ = true;
     41       this.hidden = true;
     42     },
     43 
     44     /**
     45      * Shows the menu, first rebuilding it if necessary.
     46      * TODO(estade): the right of the menu should align with the right of the
     47      * button.
     48      * @override
     49      */
     50     showMenu: function() {
     51       if (this.needsRebuild_) {
     52         this.menu.textContent = '';
     53         this.dataItems_.forEach(this.addItem_, this);
     54         this.needsRebuild_ = false;
     55       }
     56 
     57       MenuButton.prototype.showMenu.call(this);
     58     },
     59 
     60     /**
     61      * Sets the menu model data.
     62      * @param {Array} dataItems Array of objects that describe the apps.
     63      */
     64     set dataItems(dataItems) {
     65       this.dataItems_ = dataItems;
     66       this.needsRebuild_ = true;
     67       this.hidden = dataItems.length == 0;
     68     },
     69 
     70     /**
     71      * Adds an app to the menu.
     72      * @param {Object} data An object encapsulating all data about the app.
     73      * @private
     74      */
     75     addItem_: function(data) {
     76       var isWindow = data.type == 'window';
     77       var a = this.ownerDocument.createElement('a');
     78       a.className = 'recent-menu-item';
     79       if (isWindow) {
     80         a.href = '';
     81         a.classList.add('recent-window');
     82         a.textContent = formatTabsText(data.tabs.length);
     83       } else {
     84         a.href = data.url;
     85         a.style.backgroundImage = 'url(chrome://favicon/' + data.url + ')';
     86         a.textContent = data.title;
     87         // TODO(estade): add app ping url.
     88       }
     89 
     90       function onActivate(e) {
     91         // TODO(estade): don't convert to string.
     92         chrome.send('reopenTab', [String(data.sessionId)]);
     93         e.preventDefault();
     94       }
     95       a.addEventListener('activate', onActivate);
     96 
     97       this.menu.appendChild(a);
     98       cr.ui.decorate(a, MenuItem);
     99     },
    100   };
    101 
    102   return {
    103     RecentMenuButton: RecentMenuButton,
    104   };
    105 });
    106