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 | |
| 29 // Function to show the sync sign-in UI. | |
| 30 var showSignInUI; | |
|
Dan Beam
2012/03/23 21:12:47
s/UI/Ui/ (and if it's like this anywhere else, fix
Patrick Dubroy
2012/03/26 15:16:46
Hmmm. The style guide says, "Prefer to capitalize
Dan Beam
2012/03/26 17:08:33
Then we need to eventually update those to *Ui* if
Patrick Dubroy
2012/03/26 19:36:46
I'm pretty sure "prefer" != "must". There is also
| |
| 31 | |
| 27 OtherSessionsMenuButton.prototype = { | 32 OtherSessionsMenuButton.prototype = { |
| 28 __proto__: MenuButton.prototype, | 33 __proto__: MenuButton.prototype, |
| 29 | 34 |
| 30 decorate: function() { | 35 decorate: function() { |
| 31 MenuButton.prototype.decorate.call(this); | 36 MenuButton.prototype.decorate.call(this); |
| 32 this.menu = new Menu; | 37 this.menu = new Menu; |
| 33 cr.ui.decorate(this.menu, Menu); | 38 cr.ui.decorate(this.menu, Menu); |
| 34 this.menu.classList.add('footer-menu'); | 39 this.menu.classList.add('footer-menu'); |
| 35 this.menu.addEventListener('contextmenu', | 40 this.menu.addEventListener('contextmenu', |
| 36 this.onContextMenu_.bind(this), true); | 41 this.onContextMenu_.bind(this), true); |
| 37 document.body.appendChild(this.menu); | 42 document.body.appendChild(this.menu); |
| 38 | 43 |
| 39 this.sessions_ = []; | 44 this.sessions_ = []; |
| 40 this.anchorType = cr.ui.AnchorType.ABOVE; | 45 this.anchorType = cr.ui.AnchorType.ABOVE; |
| 41 this.invertLeftRight = true; | 46 this.invertLeftRight = true; |
| 42 | 47 |
| 43 chrome.send('getForeignSessions'); | |
| 44 this.recordUmaEvent_(HISTOGRAM_EVENT.INITIALIZED); | 48 this.recordUmaEvent_(HISTOGRAM_EVENT.INITIALIZED); |
| 49 this.classList.remove('invisible'); | |
| 45 }, | 50 }, |
| 46 | 51 |
| 47 /** | 52 /** |
| 53 * Initialize the menu. | |
| 54 * @param {Boolean} signedIn Is the current user signed in? | |
|
Dan Beam
2012/03/23 21:12:47
s/Boolean/boolean/
Patrick Dubroy
2012/03/26 15:16:46
Done.
| |
| 55 * @param {function} signInCallback A function to open the sign-in UI. | |
| 56 */ | |
| 57 initialize: function(signedIn, signInCallback) { | |
| 58 isUserSignedIn = signedIn; | |
| 59 if (signInCallback) | |
| 60 showSignInUI = signInCallback; | |
| 61 | |
| 62 // Always show the promo, because getForeignSessions can be a no-op. | |
| 63 this.showPromo_(); | |
| 64 if (signedIn) | |
| 65 chrome.send('getForeignSessions'); | |
| 66 }, | |
| 67 | |
| 68 /** | |
| 48 * Record an event in the UMA histogram. | 69 * Record an event in the UMA histogram. |
| 49 * @param {Number} eventId The id of the event to be recorded. | 70 * @param {Number} eventId The id of the event to be recorded. |
| 50 */ | 71 */ |
| 51 recordUmaEvent_: function(eventId) { | 72 recordUmaEvent_: function(eventId) { |
| 52 chrome.send('metricsHandler:recordInHistogram', | 73 chrome.send('metricsHandler:recordInHistogram', |
| 53 ['NewTabPage.OtherSessionsMenu', eventId, HISTOGRAM_EVENT_LIMIT]); | 74 ['NewTabPage.OtherSessionsMenu', eventId, HISTOGRAM_EVENT_LIMIT]); |
| 54 }, | 75 }, |
| 55 | 76 |
| 56 /** | 77 /** |
| 57 * Handle a context menu event for an object in the menu's DOM subtree. | 78 * Handle a context menu event for an object in the menu's DOM subtree. |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 116 var clickHandler = this.makeClickHandler_( | 137 var clickHandler = this.makeClickHandler_( |
| 117 session.tag, String(window.sessionId), String(tab.sessionId)); | 138 session.tag, String(window.sessionId), String(tab.sessionId)); |
| 118 a.addEventListener('click', clickHandler); | 139 a.addEventListener('click', clickHandler); |
| 119 section.appendChild(a); | 140 section.appendChild(a); |
| 120 } | 141 } |
| 121 } | 142 } |
| 122 }, | 143 }, |
| 123 | 144 |
| 124 /** | 145 /** |
| 125 * Create the UI for the promo and place it inside the menu. | 146 * Create the UI for the promo and place it inside the menu. |
| 126 * The promo is shown instead of foreign session data when tab sync is | 147 * The promo is shown when there is no foreign session data available. |
| 127 * not enabled for a profile. | |
| 128 */ | 148 */ |
| 129 showPromo_: function() { | 149 showPromo_: function() { |
| 130 var message = localStrings.getString('otherSessionsEmpty'); | 150 // Clear the current contents of the menu. |
| 131 this.menu.appendChild(this.ownerDocument.createTextNode(message)); | 151 this.menu.innerHTML = ''; |
| 152 | |
| 153 var emptyMessage = localStrings.getString('otherSessionsEmpty'); | |
| 154 var doc = this.ownerDocument; | |
| 155 this.menu.appendChild(doc.createTextNode(emptyMessage)); | |
|
Dan Beam
2012/03/23 21:12:47
eh, just put this in the markup and hide/unhide, w
Patrick Dubroy
2012/03/26 15:16:46
I put it into the markup in the template section.
| |
| 156 this.menu.appendChild(doc.createElement('p')); | |
| 157 var link = doc.createElement('span'); | |
| 158 link.classList.add('link-span'); | |
| 159 link.textContent = localStrings.getString('otherSessionsSignInText'); | |
| 160 link.addEventListener('click', function(e) { | |
| 161 console.log(showSignInUI); | |
|
Dan Beam
2012/03/23 21:12:47
remove
Patrick Dubroy
2012/03/26 15:16:46
Done.
| |
| 162 showSignInUI(this); | |
|
Dan Beam
2012/03/23 21:12:47
use e.currentTarget, don't use |this| in JavaScrip
Patrick Dubroy
2012/03/26 15:16:46
Done. Thanks, that's a good thing to remember for
| |
| 163 e.preventDefault(); | |
| 164 }); | |
| 165 this.menu.appendChild(link); | |
| 132 }, | 166 }, |
| 133 | 167 |
| 134 /** | 168 /** |
| 135 * Sets the menu model data. | 169 * Sets the menu model data. |
| 136 * @param {Array} sessionList Array of objects describing the sessions | 170 * @param {Array} sessionList Array of objects describing the sessions |
| 137 * from other devices. | 171 * from other devices. |
| 138 */ | 172 */ |
| 139 set sessions(sessionList) { | 173 set sessions(sessionList) { |
| 140 // Clear the current contents of the menu. | 174 this.sessions_ = sessionList; |
| 141 this.menu.innerHTML = ''; | 175 if (sessionList.length == 0) { |
| 176 this.showPromo_(); | |
| 177 } else { | |
| 178 // Clear the current contents of the menu. | |
| 179 this.menu.innerHTML = ''; | |
|
Dan Beam
2012/03/23 21:12:47
can you move this to central function that's calle
Patrick Dubroy
2012/03/26 15:16:46
Done.
| |
| 142 | 180 |
| 143 // Rebuild the menu with the new data. | 181 // Rebuild the menu with the new data. |
| 144 for (var i = 0; i < sessionList.length; i++) { | 182 for (var i = 0; i < sessionList.length; i++) { |
| 145 this.addSession_(sessionList[i]); | 183 this.addSession_(sessionList[i]); |
| 184 } | |
| 146 } | 185 } |
| 186 }, | |
| 147 | 187 |
| 148 if (sessionList.length == 0) | 188 /** |
| 149 this.classList.add('invisible'); | 189 * Called from the new tab page when the user's signed in state changes. |
|
Dan Beam
2012/03/23 21:12:47
doc param
Patrick Dubroy
2012/03/26 15:16:46
Done.
| |
| 150 else | 190 */ |
| 151 this.classList.remove('invisible'); | 191 updateSignInState: function(signedIn) { |
| 152 | 192 this.initialize(signedIn); |
| 153 this.sessions_ = sessionList; | |
| 154 }, | 193 }, |
| 155 }; | 194 }; |
| 156 | 195 |
| 157 return { | 196 return { |
| 158 OtherSessionsMenuButton: OtherSessionsMenuButton, | 197 OtherSessionsMenuButton: OtherSessionsMenuButton, |
| 159 }; | 198 }; |
| 160 }); | 199 }); |
| OLD | NEW |