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'); | |
40 | |
39 this.sessions_ = []; | 41 this.sessions_ = []; |
40 this.anchorType = cr.ui.AnchorType.ABOVE; | 42 this.anchorType = cr.ui.AnchorType.ABOVE; |
41 this.invertLeftRight = true; | 43 this.invertLeftRight = true; |
42 | 44 |
43 chrome.send('getForeignSessions'); | |
44 this.recordUmaEvent_(HISTOGRAM_EVENT.INITIALIZED); | 45 this.recordUmaEvent_(HISTOGRAM_EVENT.INITIALIZED); |
45 }, | 46 }, |
46 | 47 |
47 /** | 48 /** |
49 * Initialize this element. | |
50 * @param {boolean} signedIn Is the current user signed in? | |
51 */ | |
52 initialize: function(signedIn) { | |
53 this.updateSignInState(signedIn); | |
54 }, | |
55 | |
56 /** | |
48 * Record an event in the UMA histogram. | 57 * Record an event in the UMA histogram. |
49 * @param {Number} eventId The id of the event to be recorded. | 58 * @param {Number} eventId The id of the event to be recorded. |
50 */ | 59 */ |
51 recordUmaEvent_: function(eventId) { | 60 recordUmaEvent_: function(eventId) { |
52 chrome.send('metricsHandler:recordInHistogram', | 61 chrome.send('metricsHandler:recordInHistogram', |
53 ['NewTabPage.OtherSessionsMenu', eventId, HISTOGRAM_EVENT_LIMIT]); | 62 ['NewTabPage.OtherSessionsMenu', eventId, HISTOGRAM_EVENT_LIMIT]); |
54 }, | 63 }, |
55 | 64 |
56 /** | 65 /** |
57 * Handle a context menu event for an object in the menu's DOM subtree. | 66 * Handle a context menu event for an object in the menu's DOM subtree. |
(...skipping 11 matching lines...) Expand all Loading... | |
69 * @override | 78 * @override |
70 */ | 79 */ |
71 showMenu: function() { | 80 showMenu: function() { |
72 if (this.sessions_.length == 0) | 81 if (this.sessions_.length == 0) |
73 chrome.send('getForeignSessions'); | 82 chrome.send('getForeignSessions'); |
74 this.recordUmaEvent_(HISTOGRAM_EVENT.SHOW_MENU); | 83 this.recordUmaEvent_(HISTOGRAM_EVENT.SHOW_MENU); |
75 MenuButton.prototype.showMenu.call(this); | 84 MenuButton.prototype.showMenu.call(this); |
76 }, | 85 }, |
77 | 86 |
78 /** | 87 /** |
88 * Reset the menu contents to the default state. | |
89 * @private | |
90 */ | |
91 resetMenuContents_: function() { | |
Dan Beam
2012/03/27 18:09:21
thank you for renaming this, beat me to it
| |
92 this.menu.innerHTML = ''; | |
93 this.menu.appendChild(this.promoMessage_); | |
94 }, | |
95 | |
96 /** | |
79 * Create a custom click handler for a link, so that clicking on a link | 97 * 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 | 98 * restores the session (including back stack) rather than just opening |
81 * the URL. | 99 * the URL. |
82 */ | 100 */ |
83 makeClickHandler_: function(sessionTag, windowId, tabId) { | 101 makeClickHandler_: function(sessionTag, windowId, tabId) { |
84 var self = this; | 102 var self = this; |
85 return function(e) { | 103 return function(e) { |
86 self.recordUmaEvent_(HISTOGRAM_EVENT.LINK_CLICKED); | 104 self.recordUmaEvent_(HISTOGRAM_EVENT.LINK_CLICKED); |
87 chrome.send('openForeignSession', [sessionTag, windowId, tabId, | 105 chrome.send('openForeignSession', [sessionTag, windowId, tabId, |
88 e.button, e.altKey, e.ctrlKey, e.metaKey, e.shiftKey]); | 106 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 + ')'; | 133 a.style.backgroundImage = 'url(chrome://favicon/' + tab.url + ')'; |
116 var clickHandler = this.makeClickHandler_( | 134 var clickHandler = this.makeClickHandler_( |
117 session.tag, String(window.sessionId), String(tab.sessionId)); | 135 session.tag, String(window.sessionId), String(tab.sessionId)); |
118 a.addEventListener('click', clickHandler); | 136 a.addEventListener('click', clickHandler); |
119 section.appendChild(a); | 137 section.appendChild(a); |
120 } | 138 } |
121 } | 139 } |
122 }, | 140 }, |
123 | 141 |
124 /** | 142 /** |
125 * Create the UI for the promo and place it inside the menu. | 143 * 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 | 144 * The promo is shown when the user is signed in, but there is no foreign |
127 * not enabled for a profile. | 145 * session data available. |
146 * @param {boolean} visible Whether the message should be shown. | |
147 * @private | |
128 */ | 148 */ |
129 showPromo_: function() { | 149 setPromoVisible_: function(visible) { |
Evan Stade
2012/03/27 18:20:20
can you do this with css instead? something like:
Patrick Dubroy
2012/03/27 20:06:19
Nice idea. Doesn't quite work but your second sugg
| |
130 var message = localStrings.getString('otherSessionsEmpty'); | 150 this.promoMessage_.hidden = !visible; |
131 this.menu.appendChild(this.ownerDocument.createTextNode(message)); | |
132 }, | 151 }, |
133 | 152 |
134 /** | 153 /** |
135 * Sets the menu model data. | 154 * Sets the menu model data. An empty list means that either there are no |
155 * foreign sessions, or tab sync is disabled for this profile. | |
156 * |isTabSyncEnabled| makes it possible to distinguish between the cases. | |
157 * | |
136 * @param {Array} sessionList Array of objects describing the sessions | 158 * @param {Array} sessionList Array of objects describing the sessions |
137 * from other devices. | 159 * from other devices. |
160 * @param {boolean} isTabSyncEnabled Is tab sync enabled for this profile? | |
138 */ | 161 */ |
139 set sessions(sessionList) { | 162 setForeignSessions: function(sessionList, isTabSyncEnabled) { |
140 // Clear the current contents of the menu. | 163 this.sessions_ = sessionList; |
141 this.menu.innerHTML = ''; | 164 this.resetMenuContents_(); |
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 } | |
Evan Stade
2012/03/27 18:20:20
\n
Patrick Dubroy
2012/03/27 20:06:19
Done.
| |
172 // The menu button is shown iff tab sync is enabled. | |
173 if (isTabSyncEnabled) | |
174 this.classList.remove('invisible'); | |
175 else | |
176 this.classList.add('invisible'); | |
177 }, | |
142 | 178 |
143 // Rebuild the menu with the new data. | 179 /** |
144 for (var i = 0; i < sessionList.length; i++) { | 180 * Called when this element is initialized, and from the new tab page when |
145 this.addSession_(sessionList[i]); | 181 * the user's signed in state changes, |
146 } | 182 * @param {boolean} signedIn Is the user currently signed in? |
147 | 183 */ |
148 if (sessionList.length == 0) | 184 updateSignInState: function(signedIn) { |
185 if (signedIn) | |
186 chrome.send('getForeignSessions'); | |
187 else | |
149 this.classList.add('invisible'); | 188 this.classList.add('invisible'); |
150 else | |
151 this.classList.remove('invisible'); | |
152 | |
153 this.sessions_ = sessionList; | |
154 }, | 189 }, |
155 }; | 190 }; |
156 | 191 |
157 return { | 192 return { |
158 OtherSessionsMenuButton: OtherSessionsMenuButton, | 193 OtherSessionsMenuButton: OtherSessionsMenuButton, |
159 }; | 194 }; |
160 }); | 195 }); |
OLD | NEW |