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'; |
(...skipping 18 matching lines...) Expand all Loading... | |
29 | 29 |
30 decorate: function() { | 30 decorate: function() { |
31 MenuButton.prototype.decorate.call(this); | 31 MenuButton.prototype.decorate.call(this); |
32 this.menu = new Menu; | 32 this.menu = new Menu; |
33 cr.ui.decorate(this.menu, Menu); | 33 cr.ui.decorate(this.menu, Menu); |
34 this.menu.classList.add('footer-menu'); | 34 this.menu.classList.add('footer-menu'); |
35 this.menu.addEventListener('contextmenu', | 35 this.menu.addEventListener('contextmenu', |
36 this.onContextMenu_.bind(this), true); | 36 this.onContextMenu_.bind(this), true); |
37 document.body.appendChild(this.menu); | 37 document.body.appendChild(this.menu); |
38 | 38 |
39 this.promoMessage_ = $('other-sessions-promo-message'); | |
Dan Beam
2012/03/27 01:21:06
this would be much more loosely coupled if you pas
Patrick Dubroy
2012/03/27 13:44:21
What is the advantage of that? Currently new_tab.j
| |
40 | |
41 this.resetMenu_(); | |
Dan Beam
2012/03/27 01:21:06
you're probably better off doing all this in initi
Patrick Dubroy
2012/03/27 13:44:21
It's not clear what you're talking about here. Wha
| |
42 | |
39 this.sessions_ = []; | 43 this.sessions_ = []; |
40 this.anchorType = cr.ui.AnchorType.ABOVE; | 44 this.anchorType = cr.ui.AnchorType.ABOVE; |
41 this.invertLeftRight = true; | 45 this.invertLeftRight = true; |
42 | 46 |
43 chrome.send('getForeignSessions'); | |
44 this.recordUmaEvent_(HISTOGRAM_EVENT.INITIALIZED); | 47 this.recordUmaEvent_(HISTOGRAM_EVENT.INITIALIZED); |
45 }, | 48 }, |
46 | 49 |
47 /** | 50 /** |
51 * Initialize this element. | |
52 * @param {boolean} signedIn Is the current user signed in? | |
53 */ | |
54 initialize: function(signedIn) { | |
55 this.updateSignInState(signedIn); | |
56 }, | |
57 | |
58 /** | |
48 * Record an event in the UMA histogram. | 59 * Record an event in the UMA histogram. |
49 * @param {Number} eventId The id of the event to be recorded. | 60 * @param {Number} eventId The id of the event to be recorded. |
50 */ | 61 */ |
51 recordUmaEvent_: function(eventId) { | 62 recordUmaEvent_: function(eventId) { |
52 chrome.send('metricsHandler:recordInHistogram', | 63 chrome.send('metricsHandler:recordInHistogram', |
53 ['NewTabPage.OtherSessionsMenu', eventId, HISTOGRAM_EVENT_LIMIT]); | 64 ['NewTabPage.OtherSessionsMenu', eventId, HISTOGRAM_EVENT_LIMIT]); |
54 }, | 65 }, |
55 | 66 |
56 /** | 67 /** |
57 * Handle a context menu event for an object in the menu's DOM subtree. | 68 * Handle a context menu event for an object in the menu's DOM subtree. |
(...skipping 11 matching lines...) Expand all Loading... | |
69 * @override | 80 * @override |
70 */ | 81 */ |
71 showMenu: function() { | 82 showMenu: function() { |
72 if (this.sessions_.length == 0) | 83 if (this.sessions_.length == 0) |
73 chrome.send('getForeignSessions'); | 84 chrome.send('getForeignSessions'); |
74 this.recordUmaEvent_(HISTOGRAM_EVENT.SHOW_MENU); | 85 this.recordUmaEvent_(HISTOGRAM_EVENT.SHOW_MENU); |
75 MenuButton.prototype.showMenu.call(this); | 86 MenuButton.prototype.showMenu.call(this); |
76 }, | 87 }, |
77 | 88 |
78 /** | 89 /** |
90 * Reset the menu to the default state. | |
91 * @private | |
92 */ | |
93 resetMenu_: function() { | |
94 this.menu.innerHTML = ''; | |
95 this.menu.appendChild(this.promoMessage_); | |
Dan Beam
2012/03/27 01:21:06
also, if you never need more than 1 and/or you mov
Patrick Dubroy
2012/03/27 13:44:21
What is the purpose of "also" in this sentence? Is
| |
96 this.promoMessage_.hidden = true; | |
Dan Beam
2012/03/27 01:21:06
this should probably be using setPromoVisible_() i
Patrick Dubroy
2012/03/27 13:44:21
I've removed this line altogether. It's not really
| |
97 }, | |
98 | |
99 /** | |
79 * Create a custom click handler for a link, so that clicking on a link | 100 * 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 | 101 * restores the session (including back stack) rather than just opening |
81 * the URL. | 102 * the URL. |
82 */ | 103 */ |
83 makeClickHandler_: function(sessionTag, windowId, tabId) { | 104 makeClickHandler_: function(sessionTag, windowId, tabId) { |
84 var self = this; | 105 var self = this; |
85 return function(e) { | 106 return function(e) { |
86 self.recordUmaEvent_(HISTOGRAM_EVENT.LINK_CLICKED); | 107 self.recordUmaEvent_(HISTOGRAM_EVENT.LINK_CLICKED); |
87 chrome.send('openForeignSession', [sessionTag, windowId, tabId, | 108 chrome.send('openForeignSession', [sessionTag, windowId, tabId, |
88 e.button, e.altKey, e.ctrlKey, e.metaKey, e.shiftKey]); | 109 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 + ')'; | 136 a.style.backgroundImage = 'url(chrome://favicon/' + tab.url + ')'; |
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 * 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 | 147 * The promo is shown when the user is signed in, but there is no foreign |
127 * not enabled for a profile. | 148 * session data available. |
149 * @param {boolean} visible Whether the message should be shown. | |
150 * @private | |
128 */ | 151 */ |
129 showPromo_: function() { | 152 setPromoVisible_: function(visible) { |
130 var message = localStrings.getString('otherSessionsEmpty'); | 153 this.resetMenu_(); |
Dan Beam
2012/03/27 01:21:06
should setting it |visible| remove the contents?
Patrick Dubroy
2012/03/27 13:44:21
No, probably not. Fixed.
| |
131 this.menu.appendChild(this.ownerDocument.createTextNode(message)); | 154 this.promoMessage_.hidden = !visible; |
132 }, | 155 }, |
133 | 156 |
134 /** | 157 /** |
135 * Sets the menu model data. | 158 * Sets the menu model data. An empty list means that either there are no |
159 * foreign sessions, or tab sync is disabled for this profile. | |
160 * |isTabSyncEnabled| makes it possible to distinguish between the cases. | |
161 * | |
136 * @param {Array} sessionList Array of objects describing the sessions | 162 * @param {Array} sessionList Array of objects describing the sessions |
137 * from other devices. | 163 * from other devices. |
164 * @param {boolean} isTabSyncEnabled Is tab sync enabled for this profile? | |
138 */ | 165 */ |
139 set sessions(sessionList) { | 166 setForeignSessions: function(sessionList, isTabSyncEnabled) { |
140 // Clear the current contents of the menu. | 167 this.sessions_ = sessionList; |
141 this.menu.innerHTML = ''; | 168 this.setPromoVisible_(sessionList.length == 0); |
Dan Beam
2012/03/27 01:21:06
shouldn't this be done after you rebuild the sessi
Patrick Dubroy
2012/03/27 13:44:21
In this version of the code, it needed to be done
| |
169 if (sessionList.length > 0) { | |
170 // Rebuild the menu with the new data. | |
171 for (var i = 0; i < sessionList.length; i++) { | |
172 this.addSession_(sessionList[i]); | |
173 } | |
174 } | |
175 // The menu button is shown iff tab sync is enabled. | |
176 if (isTabSyncEnabled) | |
177 this.classList.remove('invisible'); | |
178 else | |
179 this.classList.add('invisible'); | |
180 }, | |
142 | 181 |
143 // Rebuild the menu with the new data. | 182 /** |
144 for (var i = 0; i < sessionList.length; i++) { | 183 * Called when this element is initialized, and from the new tab page when |
145 this.addSession_(sessionList[i]); | 184 * the user's signed in state changes, |
146 } | 185 * @param {boolean} signedIn Is the user currently signed in? |
147 | 186 */ |
148 if (sessionList.length == 0) | 187 updateSignInState: function(signedIn) { |
188 if (signedIn) | |
189 chrome.send('getForeignSessions'); | |
190 else | |
149 this.classList.add('invisible'); | 191 this.classList.add('invisible'); |
150 else | |
151 this.classList.remove('invisible'); | |
152 | |
153 this.sessions_ = sessionList; | |
154 }, | 192 }, |
155 }; | 193 }; |
156 | 194 |
157 return { | 195 return { |
158 OtherSessionsMenuButton: OtherSessionsMenuButton, | 196 OtherSessionsMenuButton: OtherSessionsMenuButton, |
159 }; | 197 }; |
160 }); | 198 }); |
OLD | NEW |