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