Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @fileoverview The menu that shows tabs from sessions on other devices. | 6 * @fileoverview The menu that shows tabs from sessions on other devices. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 cr.define('ntp', function() { | 9 cr.define('ntp', function() { |
| 10 'use strict'; | 10 'use strict'; |
| 11 | 11 |
| 12 var localStrings = new LocalStrings(); | 12 var localStrings = new LocalStrings(); |
| 13 var Menu = cr.ui.Menu; | 13 var Menu = cr.ui.Menu; |
| 14 var MenuItem = cr.ui.MenuItem; | 14 var MenuItem = cr.ui.MenuItem; |
| 15 var MenuButton = cr.ui.MenuButton; | 15 var MenuButton = cr.ui.MenuButton; |
| 16 var OtherSessionsMenuButton = cr.ui.define('button'); | 16 var OtherSessionsMenuButton = cr.ui.define('button'); |
| 17 | 17 |
| 18 // Histogram buckets for UMA tracking of menu usage. | 18 // Histogram buckets for UMA tracking of menu usage. |
| 19 var HISTOGRAM_EVENT = { | 19 var HISTOGRAM_EVENT = { |
| 20 INITIALIZED: 0, | 20 INITIALIZED: 0, |
| 21 SHOW_MENU: 1, | 21 SHOW_MENU: 1, |
| 22 LINK_CLICKED: 2, | 22 LINK_CLICKED: 2, |
| 23 LINK_RIGHT_CLICKED: 3 | 23 LINK_RIGHT_CLICKED: 3 |
| 24 }; | 24 }; |
| 25 var HISTOGRAM_EVENT_LIMIT = HISTOGRAM_EVENT.LINK_RIGHT_CLICKED + 1; | 25 var HISTOGRAM_EVENT_LIMIT = HISTOGRAM_EVENT.LINK_RIGHT_CLICKED + 1; |
| 26 | 26 |
|
Dan Beam
2012/03/26 20:42:39
can you document when/where this will be changed
Patrick Dubroy
2012/03/26 21:41:47
Done.
| |
| 27 var isUserSignedIn = false; | |
| 28 | |
| 27 OtherSessionsMenuButton.prototype = { | 29 OtherSessionsMenuButton.prototype = { |
| 28 __proto__: MenuButton.prototype, | 30 __proto__: MenuButton.prototype, |
| 29 | 31 |
| 30 decorate: function() { | 32 decorate: function() { |
| 31 MenuButton.prototype.decorate.call(this); | 33 MenuButton.prototype.decorate.call(this); |
| 32 this.menu = new Menu; | 34 this.menu = new Menu; |
| 33 cr.ui.decorate(this.menu, Menu); | 35 cr.ui.decorate(this.menu, Menu); |
| 34 this.menu.classList.add('footer-menu'); | 36 this.menu.classList.add('footer-menu'); |
| 35 this.menu.addEventListener('contextmenu', | 37 this.menu.addEventListener('contextmenu', |
| 36 this.onContextMenu_.bind(this), true); | 38 this.onContextMenu_.bind(this), true); |
| 37 document.body.appendChild(this.menu); | 39 document.body.appendChild(this.menu); |
| 38 | 40 |
| 41 // Put the promo content (hidden by default) into the menu. | |
| 42 var messageContent = $('other-sessions-promo-message'); | |
| 43 this.menu.appendChild(messageContent); | |
|
Dan Beam
2012/03/26 20:42:39
nit: inline this
this.menu.appendChild($('other
Patrick Dubroy
2012/03/26 21:41:47
Done.
| |
| 44 | |
| 39 this.sessions_ = []; | 45 this.sessions_ = []; |
| 40 this.anchorType = cr.ui.AnchorType.ABOVE; | 46 this.anchorType = cr.ui.AnchorType.ABOVE; |
| 41 this.invertLeftRight = true; | 47 this.invertLeftRight = true; |
| 42 | 48 |
| 43 chrome.send('getForeignSessions'); | |
| 44 this.recordUmaEvent_(HISTOGRAM_EVENT.INITIALIZED); | 49 this.recordUmaEvent_(HISTOGRAM_EVENT.INITIALIZED); |
| 45 }, | 50 }, |
| 46 | 51 |
| 47 /** | 52 /** |
| 53 * Initialize this element. | |
| 54 * @param {boolean} signedIn Is the current user signed in? | |
| 55 */ | |
| 56 initialize: function(signedIn) { | |
| 57 this.updateSignInState(signedIn); | |
| 58 }, | |
| 59 | |
| 60 /** | |
| 48 * Record an event in the UMA histogram. | 61 * Record an event in the UMA histogram. |
| 49 * @param {Number} eventId The id of the event to be recorded. | 62 * @param {Number} eventId The id of the event to be recorded. |
| 50 */ | 63 */ |
| 51 recordUmaEvent_: function(eventId) { | 64 recordUmaEvent_: function(eventId) { |
| 52 chrome.send('metricsHandler:recordInHistogram', | 65 chrome.send('metricsHandler:recordInHistogram', |
| 53 ['NewTabPage.OtherSessionsMenu', eventId, HISTOGRAM_EVENT_LIMIT]); | 66 ['NewTabPage.OtherSessionsMenu', eventId, HISTOGRAM_EVENT_LIMIT]); |
| 54 }, | 67 }, |
| 55 | 68 |
| 56 /** | 69 /** |
| 57 * Handle a context menu event for an object in the menu's DOM subtree. | 70 * Handle a context menu event for an object in the menu's DOM subtree. |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 69 * @override | 82 * @override |
| 70 */ | 83 */ |
| 71 showMenu: function() { | 84 showMenu: function() { |
| 72 if (this.sessions_.length == 0) | 85 if (this.sessions_.length == 0) |
| 73 chrome.send('getForeignSessions'); | 86 chrome.send('getForeignSessions'); |
| 74 this.recordUmaEvent_(HISTOGRAM_EVENT.SHOW_MENU); | 87 this.recordUmaEvent_(HISTOGRAM_EVENT.SHOW_MENU); |
| 75 MenuButton.prototype.showMenu.call(this); | 88 MenuButton.prototype.showMenu.call(this); |
| 76 }, | 89 }, |
| 77 | 90 |
| 78 /** | 91 /** |
| 92 * Reset the menu to the default state. | |
|
Dan Beam
2012/03/26 20:42:39
@private
Patrick Dubroy
2012/03/26 21:41:47
Done.
| |
| 93 */ | |
| 94 resetMenu_: function() { | |
| 95 var promoContent = $('other-sessions-promo-message'); | |
| 96 this.menu.innerHTML = ''; | |
| 97 this.menu.appendChild(promoContent); | |
| 98 promoContent.hidden = true; | |
| 99 }, | |
| 100 | |
| 101 /** | |
| 79 * Create a custom click handler for a link, so that clicking on a link | 102 * Create a custom click handler for a link, so that clicking on a link |
| 80 * restores the session (including back stack) rather than just opening | 103 * restores the session (including back stack) rather than just opening |
| 81 * the URL. | 104 * the URL. |
| 82 */ | 105 */ |
| 83 makeClickHandler_: function(sessionTag, windowId, tabId) { | 106 makeClickHandler_: function(sessionTag, windowId, tabId) { |
| 84 var self = this; | 107 var self = this; |
| 85 return function(e) { | 108 return function(e) { |
| 86 self.recordUmaEvent_(HISTOGRAM_EVENT.LINK_CLICKED); | 109 self.recordUmaEvent_(HISTOGRAM_EVENT.LINK_CLICKED); |
| 87 chrome.send('openForeignSession', [sessionTag, windowId, tabId, | 110 chrome.send('openForeignSession', [sessionTag, windowId, tabId, |
| 88 e.button, e.altKey, e.ctrlKey, e.metaKey, e.shiftKey]); | 111 e.button, e.altKey, e.ctrlKey, e.metaKey, e.shiftKey]); |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 115 a.style.backgroundImage = 'url(chrome://favicon/' + tab.url + ')'; | 138 a.style.backgroundImage = 'url(chrome://favicon/' + tab.url + ')'; |
| 116 var clickHandler = this.makeClickHandler_( | 139 var clickHandler = this.makeClickHandler_( |
| 117 session.tag, String(window.sessionId), String(tab.sessionId)); | 140 session.tag, String(window.sessionId), String(tab.sessionId)); |
| 118 a.addEventListener('click', clickHandler); | 141 a.addEventListener('click', clickHandler); |
| 119 section.appendChild(a); | 142 section.appendChild(a); |
| 120 } | 143 } |
| 121 } | 144 } |
| 122 }, | 145 }, |
| 123 | 146 |
| 124 /** | 147 /** |
| 125 * Create the UI for the promo and place it inside the menu. | 148 * Show or hide the promo message shown in place of the session list. |
| 126 * The promo is shown instead of foreign session data when tab sync is | 149 * The promo is shown when the user is signed in, but there is no foreign |
| 127 * not enabled for a profile. | 150 * session data available. |
| 151 * @param {boolean} visible Whether the message should be shown. | |
|
Dan Beam
2012/03/26 20:42:39
@private
Patrick Dubroy
2012/03/26 21:41:47
Done.
| |
| 128 */ | 152 */ |
| 129 showPromo_: function() { | 153 setPromoVisible_: function(visible) { |
| 130 var message = localStrings.getString('otherSessionsEmpty'); | 154 this.resetMenu_(); |
| 131 this.menu.appendChild(this.ownerDocument.createTextNode(message)); | 155 $('other-sessions-promo-message').hidden = !visible; |
| 132 }, | 156 }, |
| 133 | 157 |
| 134 /** | 158 /** |
| 135 * Sets the menu model data. | 159 * Sets the menu model data. |
| 136 * @param {Array} sessionList Array of objects describing the sessions | 160 * @param {Array} sessionList Array of objects describing the sessions |
| 137 * from other devices. | 161 * from other devices. |
| 138 */ | 162 */ |
| 139 set sessions(sessionList) { | 163 set sessions(sessionList) { |
| 140 // Clear the current contents of the menu. | 164 this.sessions_ = sessionList; |
| 141 this.menu.innerHTML = ''; | 165 this.setPromoVisible_(sessionList.length == 0); |
| 166 if (sessionList.length > 0) { | |
| 167 // Rebuild the menu with the new data. | |
| 168 for (var i = 0; i < sessionList.length; i++) { | |
| 169 this.addSession_(sessionList[i]); | |
| 170 } | |
| 171 } | |
| 172 // Make the menu button visible. | |
| 173 this.classList.remove('invisible'); | |
| 174 }, | |
| 142 | 175 |
| 143 // Rebuild the menu with the new data. | 176 /** |
| 144 for (var i = 0; i < sessionList.length; i++) { | 177 * Called from the new tab page when the user's signed in state changes. |
| 145 this.addSession_(sessionList[i]); | 178 * @param {boolean} signedIn Is the user currently signed in? |
| 146 } | 179 */ |
| 180 updateSignInState: function(signedIn) { | |
|
Dan Beam
2012/03/26 20:42:39
if this is called nowhere else outside of this fun
Patrick Dubroy
2012/03/26 21:41:47
It's called from the ntp.js when the sign-in state
| |
| 181 isUserSignedIn = signedIn; | |
| 147 | 182 |
| 148 if (sessionList.length == 0) | 183 if (signedIn) |
| 184 chrome.send('getForeignSessions'); | |
| 185 else | |
| 149 this.classList.add('invisible'); | 186 this.classList.add('invisible'); |
| 150 else | |
| 151 this.classList.remove('invisible'); | |
| 152 | |
| 153 this.sessions_ = sessionList; | |
| 154 }, | 187 }, |
| 155 }; | 188 }; |
| 156 | 189 |
| 157 return { | 190 return { |
| 158 OtherSessionsMenuButton: OtherSessionsMenuButton, | 191 OtherSessionsMenuButton: OtherSessionsMenuButton, |
| 159 }; | 192 }; |
| 160 }); | 193 }); |
| OLD | NEW |