1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview The menu that shows tabs from sessions on other devices.
*/
cr.define('ntp', function() {
'use strict';
/** @const */ var ContextMenuButton = cr.ui.ContextMenuButton;
/** @const */ var Menu = cr.ui.Menu;
/** @const */ var MenuItem = cr.ui.MenuItem;
/** @const */ var MenuButton = cr.ui.MenuButton;
/** @const */ var OtherSessionsMenuButton = cr.ui.define('button');
// Histogram buckets for UMA tracking of menu usage.
/** @const */ var HISTOGRAM_EVENT = {
INITIALIZED: 0,
SHOW_MENU: 1,
LINK_CLICKED: 2,
LINK_RIGHT_CLICKED: 3,
SESSION_NAME_RIGHT_CLICKED: 4,
SHOW_SESSION_MENU: 5,
COLLAPSE_SESSION: 6,
EXPAND_SESSION: 7,
OPEN_ALL: 8
};
/** @const */ var HISTOGRAM_EVENT_LIMIT =
HISTOGRAM_EVENT.OPEN_ALL + 1;
/**
* Record an event in the UMA histogram.
* @param {number} eventId The id of the event to be recorded.
* @private
*/
function recordUmaEvent_(eventId) {
chrome.send('metricsHandler:recordInHistogram',
['NewTabPage.OtherSessionsMenu', eventId, HISTOGRAM_EVENT_LIMIT]);
}
OtherSessionsMenuButton.prototype = {
__proto__: MenuButton.prototype,
decorate: function() {
MenuButton.prototype.decorate.call(this);
this.menu = new Menu;
cr.ui.decorate(this.menu, Menu);
this.menu.menuItemSelector = '[role=menuitem]';
this.menu.classList.add('footer-menu');
this.menu.addEventListener('contextmenu',
this.onContextMenu_.bind(this), true);
document.body.appendChild(this.menu);
// Create the context menu that appears when the user right clicks
// on a device name.
this.deviceContextMenu_ = DeviceContextMenuController.getInstance().menu;
document.body.appendChild(this.deviceContextMenu_);
this.promoMessage_ = $('other-sessions-promo-template').cloneNode(true);
this.promoMessage_.removeAttribute('id'); // Prevent a duplicate id.
this.sessions_ = [];
this.anchorType = cr.ui.AnchorType.ABOVE;
this.invertLeftRight = true;
// Initialize the images for the drop-down buttons that appear beside the
// session names.
MenuButton.createDropDownArrows();
recordUmaEvent_(HISTOGRAM_EVENT.INITIALIZED);
},
/**
* Initialize this element.
* @param {boolean} signedIn Is the current user signed in?
*/
initialize: function(signedIn) {
this.updateSignInState(signedIn);
},
/**
* Handle a context menu event for an object in the menu's DOM subtree.
*/
onContextMenu_: function(e) {
// Only record the action if it occurred in one of the menu items or
// on one of the session headings.
if (findAncestorByClass(e.target, 'footer-menu-item')) {
recordUmaEvent_(HISTOGRAM_EVENT.LINK_RIGHT_CLICKED);
} else {
var heading = findAncestorByClass(e.target, 'session-heading');
if (heading) {
recordUmaEvent_(HISTOGRAM_EVENT.SESSION_NAME_RIGHT_CLICKED);
// Let the context menu know which session it was invoked on,
// since they all share the same instance of the menu.
DeviceContextMenuController.getInstance().setSession(
heading.sessionData_);
}
}
},
/**
* Hides the menu.
* @override
*/
hideMenu: function() {
// Don't hide if the device context menu is currently showing.
if (this.deviceContextMenu_.hidden)
MenuButton.prototype.hideMenu.call(this);
},
/**
* Shows the menu, first rebuilding it if necessary.
* TODO(estade): the right of the menu should align with the right of the
* button.
* @override
*/
showMenu: function(shouldSetFocus) {
if (this.sessions_.length == 0)
chrome.send('getForeignSessions');
recordUmaEvent_(HISTOGRAM_EVENT.SHOW_MENU);
MenuButton.prototype.showMenu.apply(this, arguments);
// Work around https://bugs.webkit.org/show_bug.cgi?id=85884.
this.menu.scrollTop = 0;
},
/**
* Reset the menu contents to the default state.
* @private
*/
resetMenuContents_: function() {
this.menu.innerHTML = '';
this.menu.appendChild(this.promoMessage_);
},
/**
* Create a custom click handler for a link, so that clicking on a link
* restores the session (including back stack) rather than just opening
* the URL.
*/
makeClickHandler_: function(sessionTag, windowId, tabId) {
var self = this;
return function(e) {
recordUmaEvent_(HISTOGRAM_EVENT.LINK_CLICKED);
chrome.send('openForeignSession', [sessionTag, windowId, tabId,
e.button, e.altKey, e.ctrlKey, e.metaKey, e.shiftKey]);
e.preventDefault();
};
},
/**
* Add the UI for a foreign session to the menu.
* @param {Object} session Object describing the foreign session.
*/
addSession_: function(session) {
var doc = this.ownerDocument;
var section = doc.createElement('section');
this.menu.appendChild(section);
var heading = doc.createElement('h3');
heading.className = 'session-heading';
heading.textContent = session.name;
heading.sessionData_ = session;
section.appendChild(heading);
var dropDownButton = new ContextMenuButton;
dropDownButton.classList.add('drop-down');
// Keep track of the drop down that triggered the menu, so we know
// which element to apply the command to.
function handleDropDownFocus(e) {
DeviceContextMenuController.getInstance().setSession(session);
}
dropDownButton.addEventListener('mousedown', handleDropDownFocus);
dropDownButton.addEventListener('focus', handleDropDownFocus);
heading.appendChild(dropDownButton);
var timeSpan = doc.createElement('span');
timeSpan.className = 'details';
timeSpan.textContent = session.modifiedTime;
heading.appendChild(timeSpan);
cr.ui.contextMenuHandler.setContextMenu(heading,
this.deviceContextMenu_);
if (!session.collapsed)
section.appendChild(this.createSessionContents_(session));
},
/**
* Create the DOM tree representing the tabs and windows in a session.
* @param {Object} session The session model object.
* @return {Element} A single div containing the list of tabs & windows.
* @private
*/
createSessionContents_: function(session) {
var doc = this.ownerDocument;
var contents = doc.createElement('div');
for (var i = 0; i < session.windows.length; i++) {
var window = session.windows[i];
// Show a separator between multiple windows in the same session.
if (i > 0)
contents.appendChild(doc.createElement('hr'));
for (var j = 0; j < window.tabs.length; j++) {
var tab = window.tabs[j];
var a = doc.createElement('a');
a.className = 'footer-menu-item';
a.textContent = tab.title;
a.href = tab.url;
a.style.backgroundImage = getFaviconImageSet(tab.url);
var clickHandler = this.makeClickHandler_(
session.tag, String(window.sessionId), String(tab.sessionId));
a.addEventListener('click', clickHandler);
contents.appendChild(a);
cr.ui.decorate(a, MenuItem);
}
}
return contents;
},
/**
* Sets the menu model data. An empty list means that either there are no
* foreign sessions, or tab sync is disabled for this profile.
* |isTabSyncEnabled| makes it possible to distinguish between the cases.
*
* @param {Array} sessionList Array of objects describing the sessions
* from other devices.
* @param {boolean} isTabSyncEnabled Is tab sync enabled for this profile?
*/
setForeignSessions: function(sessionList, isTabSyncEnabled) {
this.sessions_ = sessionList;
this.resetMenuContents_();
if (sessionList.length > 0) {
// Rebuild the menu with the new data.
for (var i = 0; i < sessionList.length; i++) {
this.addSession_(sessionList[i]);
}
}
// The menu button is shown iff tab sync is enabled.
this.hidden = !isTabSyncEnabled;
},
/**
* Called when this element is initialized, and from the new tab page when
* the user's signed in state changes,
* @param {boolean} signedIn Is the user currently signed in?
*/
updateSignInState: function(signedIn) {
if (signedIn)
chrome.send('getForeignSessions');
else
this.hidden = true;
},
};
/**
* Controller for the context menu for device names in the list of sessions.
* This class is designed to be used as a singleton.
*
* @constructor
*/
function DeviceContextMenuController() {
this.__proto__ = DeviceContextMenuController.prototype;
this.initialize();
}
cr.addSingletonGetter(DeviceContextMenuController);
DeviceContextMenuController.prototype = {
initialize: function() {
var menu = new cr.ui.Menu;
cr.ui.decorate(menu, cr.ui.Menu);
menu.classList.add('device-context-menu');
menu.classList.add('footer-menu-context-menu');
this.menu = menu;
this.collapseItem_ = this.appendMenuItem_('collapseSessionMenuItemText');
this.collapseItem_.addEventListener('activate',
this.onCollapseOrExpand_.bind(this));
this.expandItem_ = this.appendMenuItem_('expandSessionMenuItemText');
this.expandItem_.addEventListener('activate',
this.onCollapseOrExpand_.bind(this));
this.openAllItem_ = this.appendMenuItem_('restoreSessionMenuItemText');
this.openAllItem_.addEventListener('activate',
this.onOpenAll_.bind(this));
},
/**
* Appends a menu item to |this.menu|.
* @param {string} textId The ID for the localized string that acts as
* the item's label.
*/
appendMenuItem_: function(textId) {
var button = cr.doc.createElement('button');
this.menu.appendChild(button);
cr.ui.decorate(button, cr.ui.MenuItem);
button.textContent = loadTimeData.getString(textId);
return button;
},
/**
* Handler for the 'Collapse' and 'Expand' menu items.
* @param {Event} e The activation event.
* @private
*/
onCollapseOrExpand_: function(e) {
this.session_.collapsed = !this.session_.collapsed;
this.updateMenuItems_();
chrome.send('setForeignSessionCollapsed',
[this.session_.tag, this.session_.collapsed]);
chrome.send('getForeignSessions'); // Refresh the list.
var eventId = this.session_.collapsed ?
HISTOGRAM_EVENT.COLLAPSE_SESSION : HISTOGRAM_EVENT.EXPAND_SESSION;
recordUmaEvent_(eventId);
},
/**
* Handler for the 'Open all' menu item.
* @param {Event} e The activation event.
* @private
*/
onOpenAll_: function(e) {
chrome.send('openForeignSession', [this.session_.tag]);
recordUmaEvent_(HISTOGRAM_EVENT.OPEN_ALL);
},
/**
* Set the session data for the session the context menu was invoked on.
* This should never be called when the menu is visible.
* @param {Object} session The model object for the session.
*/
setSession: function(session) {
this.session_ = session;
this.updateMenuItems_();
},
/**
* Set the visibility of the Expand/Collapse menu items based on the state
* of the session that this menu is currently associated with.
* @private
*/
updateMenuItems_: function() {
this.collapseItem_.hidden = this.session_.collapsed;
this.expandItem_.hidden = !this.session_.collapsed;
}
};
return {
OtherSessionsMenuButton: OtherSessionsMenuButton,
};
});
|