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 |
| 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 messageContent.parentNode.removeChild(messageContent); | |
| 44 this.menu.appendChild(messageContent); | |
|
Dan Beam
2012/03/26 17:08:34
I think appendChild() will implicitly remove the n
Patrick Dubroy
2012/03/26 19:36:46
Done.
| |
| 45 | |
| 39 this.sessions_ = []; | 46 this.sessions_ = []; |
| 40 this.anchorType = cr.ui.AnchorType.ABOVE; | 47 this.anchorType = cr.ui.AnchorType.ABOVE; |
| 41 this.invertLeftRight = true; | 48 this.invertLeftRight = true; |
| 42 | 49 |
| 43 chrome.send('getForeignSessions'); | |
| 44 this.recordUmaEvent_(HISTOGRAM_EVENT.INITIALIZED); | 50 this.recordUmaEvent_(HISTOGRAM_EVENT.INITIALIZED); |
| 51 this.classList.remove('invisible'); | |
| 45 }, | 52 }, |
| 46 | 53 |
| 47 /** | 54 /** |
| 55 * Initialize the menu. | |
|
Dan Beam
2012/03/26 17:08:34
may want to be more specific of which menu or just
Patrick Dubroy
2012/03/26 19:36:46
Done.
| |
| 56 * @param {boolean} signedIn Is the current user signed in? | |
| 57 * @param {function} signInCallback A function to open the sign-in UI. | |
| 58 */ | |
| 59 initialize: function(signedIn, signInCallback) { | |
| 60 var promo = $('other-sessions-promo-message'); | |
|
Dan Beam
2012/03/26 17:08:34
nit: why not document.querySelector('#other-sessio
Patrick Dubroy
2012/03/26 19:36:46
This code is gone.
| |
| 61 promo.querySelector('.link-span').addEventListener('click', | |
| 62 signInCallback); | |
| 63 | |
| 64 // Always show the promo, because getForeignSessions can be a no-op. | |
| 65 this.showPromo_(); | |
| 66 if (signedIn) | |
| 67 chrome.send('getForeignSessions'); | |
|
Dan Beam
2012/03/26 17:08:34
replace this code with:
this.updateSignInStat
Patrick Dubroy
2012/03/26 19:36:46
Done.
| |
| 68 }, | |
| 69 | |
| 70 /** | |
| 48 * Record an event in the UMA histogram. | 71 * Record an event in the UMA histogram. |
| 49 * @param {Number} eventId The id of the event to be recorded. | 72 * @param {Number} eventId The id of the event to be recorded. |
| 50 */ | 73 */ |
| 51 recordUmaEvent_: function(eventId) { | 74 recordUmaEvent_: function(eventId) { |
| 52 chrome.send('metricsHandler:recordInHistogram', | 75 chrome.send('metricsHandler:recordInHistogram', |
| 53 ['NewTabPage.OtherSessionsMenu', eventId, HISTOGRAM_EVENT_LIMIT]); | 76 ['NewTabPage.OtherSessionsMenu', eventId, HISTOGRAM_EVENT_LIMIT]); |
| 54 }, | 77 }, |
| 55 | 78 |
| 56 /** | 79 /** |
| 57 * Handle a context menu event for an object in the menu's DOM subtree. | 80 * Handle a context menu event for an object in the menu's DOM subtree. |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 69 * @override | 92 * @override |
| 70 */ | 93 */ |
| 71 showMenu: function() { | 94 showMenu: function() { |
| 72 if (this.sessions_.length == 0) | 95 if (this.sessions_.length == 0) |
| 73 chrome.send('getForeignSessions'); | 96 chrome.send('getForeignSessions'); |
| 74 this.recordUmaEvent_(HISTOGRAM_EVENT.SHOW_MENU); | 97 this.recordUmaEvent_(HISTOGRAM_EVENT.SHOW_MENU); |
| 75 MenuButton.prototype.showMenu.call(this); | 98 MenuButton.prototype.showMenu.call(this); |
| 76 }, | 99 }, |
| 77 | 100 |
| 78 /** | 101 /** |
| 102 * Reset the menu to the default state. | |
| 103 */ | |
| 104 resetMenu_: function() { | |
| 105 var promoContent = $('other-sessions-promo-message'); | |
| 106 this.menu.innerHTML = ''; | |
| 107 this.menu.appendChild(promoContent); | |
| 108 promoContent.hidden = true; | |
| 109 }, | |
| 110 | |
| 111 /** | |
| 79 * Create a custom click handler for a link, so that clicking on a link | 112 * 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 | 113 * restores the session (including back stack) rather than just opening |
| 81 * the URL. | 114 * the URL. |
| 82 */ | 115 */ |
| 83 makeClickHandler_: function(sessionTag, windowId, tabId) { | 116 makeClickHandler_: function(sessionTag, windowId, tabId) { |
| 84 var self = this; | 117 var self = this; |
| 85 return function(e) { | 118 return function(e) { |
| 86 self.recordUmaEvent_(HISTOGRAM_EVENT.LINK_CLICKED); | 119 self.recordUmaEvent_(HISTOGRAM_EVENT.LINK_CLICKED); |
| 87 chrome.send('openForeignSession', [sessionTag, windowId, tabId, | 120 chrome.send('openForeignSession', [sessionTag, windowId, tabId, |
| 88 e.button, e.altKey, e.ctrlKey, e.metaKey, e.shiftKey]); | 121 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 + ')'; | 148 a.style.backgroundImage = 'url(chrome://favicon/' + tab.url + ')'; |
| 116 var clickHandler = this.makeClickHandler_( | 149 var clickHandler = this.makeClickHandler_( |
| 117 session.tag, String(window.sessionId), String(tab.sessionId)); | 150 session.tag, String(window.sessionId), String(tab.sessionId)); |
| 118 a.addEventListener('click', clickHandler); | 151 a.addEventListener('click', clickHandler); |
| 119 section.appendChild(a); | 152 section.appendChild(a); |
| 120 } | 153 } |
| 121 } | 154 } |
| 122 }, | 155 }, |
| 123 | 156 |
| 124 /** | 157 /** |
| 125 * Create the UI for the promo and place it inside the menu. | 158 * Show a promo message inside the menu. |
| 126 * The promo is shown instead of foreign session data when tab sync is | 159 * The promo is shown when there is no foreign session data available. |
| 127 * not enabled for a profile. | |
| 128 */ | 160 */ |
| 129 showPromo_: function() { | 161 showPromo_: function() { |
| 130 var message = localStrings.getString('otherSessionsEmpty'); | 162 this.resetMenu_(); |
| 131 this.menu.appendChild(this.ownerDocument.createTextNode(message)); | 163 $('other-sessions-promo-message').hidden = false; |
|
Dan Beam
2012/03/26 17:08:34
nit: you seem to do this a couple times (toggle th
Patrick Dubroy
2012/03/26 19:36:46
Done.
| |
| 132 }, | 164 }, |
| 133 | 165 |
| 134 /** | 166 /** |
| 135 * Sets the menu model data. | 167 * Sets the menu model data. |
| 136 * @param {Array} sessionList Array of objects describing the sessions | 168 * @param {Array} sessionList Array of objects describing the sessions |
| 137 * from other devices. | 169 * from other devices. |
|
Dan Beam
2012/03/26 17:08:34
nit: +4\s before from
Patrick Dubroy
2012/03/26 19:36:46
Done.
| |
| 138 */ | 170 */ |
| 139 set sessions(sessionList) { | 171 set sessions(sessionList) { |
| 140 // Clear the current contents of the menu. | 172 this.sessions_ = sessionList; |
| 141 this.menu.innerHTML = ''; | 173 if (sessionList.length == 0) { |
| 174 this.showPromo_(); | |
| 175 } else { | |
| 176 this.resetMenu_(); | |
| 142 | 177 |
| 143 // Rebuild the menu with the new data. | 178 // Rebuild the menu with the new data. |
| 144 for (var i = 0; i < sessionList.length; i++) { | 179 for (var i = 0; i < sessionList.length; i++) { |
| 145 this.addSession_(sessionList[i]); | 180 this.addSession_(sessionList[i]); |
| 181 } | |
| 182 $('other-sessions-promo-message').hidden = true; | |
|
Dan Beam
2012/03/26 17:08:34
this .hidden = true; seems redundant now that it's
Patrick Dubroy
2012/03/26 19:36:46
Done.
| |
| 146 } | 183 } |
| 184 }, | |
| 147 | 185 |
| 148 if (sessionList.length == 0) | 186 /** |
| 149 this.classList.add('invisible'); | 187 * Called from the new tab page when the user's signed in state changes. |
| 150 else | 188 * @param {boolean} signedIn Is the user currently signed in? |
| 151 this.classList.remove('invisible'); | 189 */ |
| 152 | 190 updateSignInState: function(signedIn) { |
| 153 this.sessions_ = sessionList; | 191 // Always show the promo, because getForeignSessions can be a no-op. |
| 192 this.showPromo_(); | |
| 193 if (signedIn) | |
| 194 chrome.send('getForeignSessions'); | |
| 154 }, | 195 }, |
| 155 }; | 196 }; |
| 156 | 197 |
| 157 return { | 198 return { |
| 158 OtherSessionsMenuButton: OtherSessionsMenuButton, | 199 OtherSessionsMenuButton: OtherSessionsMenuButton, |
| 159 }; | 200 }; |
| 160 }); | 201 }); |
| OLD | NEW |