forked from ReDEnergy/SessionSync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession-container.js
More file actions
440 lines (354 loc) · 12.8 KB
/
Copy pathsession-container.js
File metadata and controls
440 lines (354 loc) · 12.8 KB
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
define(function(require, exports) {
'use strict';
// *****************************************************************************
// Custom Modules
// App
const { AppConfig } = require('../config');
const { SessionBookmark, SessionTab, SessionWindow, HistoryWindow, HistoryTab } = require('./session-bookmark');
const { SessionBookmarkEvents } = require('./session-bookmark-events');
const { SessionSyncModel } = require('./session-sync-model');
// Utils
const { HTMLCreator } = require('../utils/dom');
const { WindowEvents, GlobalEvents } = require('../utils/global-events');
// Components
const { BookmarkManager } = require('../session/bookmarks');
const { SessionManager } = require('../session/management');
const { SessionHistory } = require('./session-history');
// *****************************************************************************
// APIconst { WindowEvents, GlobalEvents } = require('../utils/global-events');
/*
* SessionContainer UI
* Panel for listing session information (Firefox Bookmark Folder) and children bookmarks
*/
function SessionContainer()
{
var DomElem = HTMLCreator();
// ------------------------------------------------------------------------
// Init UI
var container = DomElem('div', {class: 'session-container'});
container.style.fontSize = AppConfig.get('style.scale.bookmarks') + 'px';
var bookmarks = DomElem('div', {class: 'bookmarks'});
container.appendChild(bookmarks);
// ------------------------------------------------------------------------
// User events
function isValidSessionID(ID)
{
return (SessionSyncModel.bookmarks[ID] !== undefined &&
SessionSyncModel.bookmarks[ID].type === 'folder' &&
SessionSyncModel.bookmarks[ID].parentId == AppConfig.get('storageID'));
}
function restoreHistoryWindow(sessionInfo)
{
var list = [];
sessionInfo.windows.forEach(historyWindow => {
list.push(historyWindow.tabs);
});
SessionManager.loadBookmarksNewWindow(list);
}
WindowEvents.on(document, 'HistorySessionSave', this.saveHistorySession.bind(this));
WindowEvents.on(document, 'HistorySessionRestore', function (index) {
SessionHistory.getHistorySession(index, function (sessionInfo) {
restoreHistoryWindow(sessionInfo);
});
});
WindowEvents.on(document, 'HistorySessionRestoreWindow', function (windowID) {
if (this.SyncModel.state.session === 'history') {
if (this.selectedHistorySession.active == true)
{
var activeSessionWindows = Object.values(this.selectedHistorySession.windows);
if (activeSessionWindows[windowID].active) {
SessionManager.activateWindow(activeSessionWindows[windowID].id);
}
else {
SessionManager.loadBookmarksNewWindow([activeSessionWindows[windowID].tabs]);
}
}
else {
SessionManager.loadBookmarksNewWindow([this.selectedHistorySession.windows[windowID].tabs]);
}
}
}.bind(this));
WindowEvents.on(document, 'MenuRestoreClick', function() {
if (isValidSessionID(this.activeSessionID)) {
SessionManager.restoreSession(this.activeSessionID);
}
}.bind(this));
WindowEvents.on(document, 'MenuRestoreNewWindow', function() {
if (isValidSessionID(this.activeSessionID)) {
SessionManager.restoreNewWindow(this.activeSessionID);
}
// history session
if (this.SyncModel.state.session === 'history') {
restoreHistoryWindow(this.selectedHistorySession);
}
}.bind(this));
WindowEvents.on(document, 'ReplaceSessionAll', function() {
if (isValidSessionID(this.activeSessionID)) {
SessionManager.replaceSessionAll(this.activeSessionID);
}
}.bind(this));
WindowEvents.on(document, 'ReplaceSession', function() {
if (isValidSessionID(this.activeSessionID)) {
SessionManager.replaceSession(this.activeSessionID);
}
}.bind(this));
WindowEvents.on(document, 'MenuMergeSessions', function(e) {
if (isValidSessionID(this.activeSessionID)) {
WindowEvents.emit(document, 'ConfirmBox-Open', {
event: e,
message: 'Merge selected session and active window session?',
callback: function() {
SessionManager.mergeSessions(this.activeSessionID);
}.bind(this)
});
}
}.bind(this));
WindowEvents.on(document, 'MenuReplaceSession', function(e) {
if (isValidSessionID(this.activeSessionID)) {
WindowEvents.emit(document, 'ConfirmBox-Open', {
event: e,
message: 'Overwrite the selected session with active window session?',
callback: function() {
SessionManager.overwriteSession(this.activeSessionID);
}.bind(this)
});
}
}.bind(this));
WindowEvents.on(document, 'MenuAddCurrentTab', function() {
if (isValidSessionID(this.activeSessionID)) {
this.bookmarkCurrentTab();
}
}.bind(this));
WindowEvents.on(document, 'MenuSaveSession', function() {
// do not save if the session is already saved
if (isValidSessionID(this.activeSessionID)) {
return;
}
switch(this.SyncModel.state.session)
{
case 'current':
SessionManager.saveActiveSession();
break;
case 'history':
this.saveHistorySession();
break;
}
}.bind(this));
WindowEvents.on(document, 'MenuFixLazySession', function() {
if (isValidSessionID(this.activeSessionID)) {
SessionManager.fixLazySession(this.activeSessionID);
}
}.bind(this));
GlobalEvents.on('UpdateCurrentSession', function() {
if (this.SyncModel.state.session == 'current') {
this.showCurrentSession();
}
}.bind(this));
WindowEvents.on(document, 'EditSelectedSession', function() {
WindowEvents.emit(document, 'SessionContextMenu-EditSession', this.activeSessionID);
}.bind(this));
WindowEvents.on(document, 'ShowCurrentSession', this.showCurrentSession.bind(this));
WindowEvents.on(document, 'ShowHistorySession', this.showHistorySession.bind(this));
WindowEvents.on(document, 'ViewSession', this.previewSession.bind(this));
AppConfig.onChange('style.scale.bookmarks', function(size) {
container.style.fontSize = size + 'px';
});
AppConfig.onChange('session.save', function(obj) {
if (this.SyncModel.state.session == 'current') {
this.showCurrentSession();
}
}.bind(this));
WindowEvents.on(document, 'SessionDescriptionChanged', function(sessionID) {
if (this.activeSessionID == sessionID)
{
this.updateSessionInfo();
}
}.bind(this));
// Tracked events
SessionBookmarkEvents(bookmarks);
var updateTimeout = null;
function updateOnTabEvent()
{
if (SessionManager.tabTracking())
{
if (updateTimeout == null) {
updateTimeout = setTimeout(function () {
updateTimeout = null;
GlobalEvents.emit('UpdateCurrentSession');
}, 1000);
}
}
}
// Tab updates tracking
var trackedEvents = ['onUpdated', 'onCreated', 'onDetached', 'onMoved', 'onAttached', 'onRemoved'];
trackedEvents.forEach(function (eventType) {
browser.tabs[eventType].addListener(updateOnTabEvent);
}.bind(this));
// ------------------------------------------------------------------------
// Public data
this.DOMBookmarks = bookmarks;
this.DOMRoot = container;
this.SyncModel = SessionSyncModel.getModel(document);
}
SessionContainer.prototype.setUIState = function setUIState(state, headerTitle, sessionID)
{
WindowEvents.emit(document, 'SetUIState', {'session': state});
WindowEvents.emit(document, 'SetSessionDescription', headerTitle);
this.DOMBookmarks.textContent = '';
this.activeSessionID = sessionID;
this.selectedHistorySession = undefined;
};
SessionContainer.prototype.updateSessionInfo = function updateSessionInfo()
{
var session = SessionSyncModel.bookmarks[this.activeSessionID];
WindowEvents.emit(document, 'SetSessionDescription', session.title);
WindowEvents.emit(document, 'SetSessionDate', session.dateAdded);
};
SessionContainer.prototype.showCurrentSession = function showCurrentSession()
{
var DOMBookmarks = this.DOMBookmarks;
this.setUIState('current', 'Active session', 0);
SessionManager.getCurrentWindow(function(mozWindow) {
// Get non-private windows
if (mozWindow.incognito == true || AppConfig.get('session.save').allWindows == false)
{
mozWindow.tabs.forEach(function (tab) {
let sessionTab = new SessionTab(tab, 0, -1);
this.SyncModel.tabs[sessionTab.tab.id] = sessionTab;
DOMBookmarks.appendChild(sessionTab.DOMRoot);
}.bind(this));
this.restoreScrollTop('current');
}
else
{
// Get non-private windows
var tabIndex = 0;
var windowIndex = 0; // display index in UI - ignores private windows
SessionManager.getAllWindows(function(windows) {
for (let mozWindow of windows)
{
if (!mozWindow.incognito)
{
// Add window separator
var sessionWindow = new SessionWindow(mozWindow.id, windowIndex, tabIndex);
DOMBookmarks.appendChild(sessionWindow.DOMRoot);
tabIndex++;
var globalOffset = tabIndex;
// Add window tabs
for (let i in mozWindow.tabs)
{
let tab = mozWindow.tabs[i];
let sessionTab = new SessionTab(tab, globalOffset);
this.SyncModel.tabs[sessionTab.tab.id] = sessionTab;
DOMBookmarks.appendChild(sessionTab.DOMRoot);
tabIndex++;
}
windowIndex++;
}
}
this.restoreScrollTop('current');
}.bind(this));
}
}.bind(this));
};
SessionContainer.prototype.showHistorySession = function showHistorySession(sessionInfo)
{
var newSession = (this.selectedHistorySession != undefined) && (this.selectedHistorySession != sessionInfo);
this.setUIState('history', (new Date(sessionInfo.lastSave)).toLocaleString(), -1);
this.selectedHistorySession = sessionInfo;
var tabIndex = 0;
var windowIndex = 0;
for (let key in sessionInfo.windows)
{
// Add window separator
var sessionWindow = new HistoryWindow(sessionInfo.windows[key], windowIndex, tabIndex);
this.DOMBookmarks.appendChild(sessionWindow.DOMRoot);
tabIndex++;
var globalOffset = tabIndex;
// Add window tabs
var windowTabs = sessionInfo.windows[key].tabs;
for (var i in windowTabs)
{
var tab = windowTabs[i];
tab.index = i | 0;
var sessionTab = new HistoryTab(tab, globalOffset);
this.DOMBookmarks.appendChild(sessionTab.DOMRoot);
tabIndex++;
}
windowIndex++;
}
this.restoreScrollTop('history', newSession ? 0 : undefined);
};
SessionContainer.prototype.previewSession = function previewSession(sessionID)
{
var session = SessionSyncModel.bookmarks[sessionID];
this.setUIState('restore', '', sessionID);
BookmarkManager.getFolderBookmarks(session.id, function (marks) {
WindowEvents.emit(document, 'SetPromiseSession', { sessionID: session.id, update: false } );
var len = marks.length;
for (var i = 0; i < len; i++)
{
var bookmark = new SessionBookmark(marks[i]);
bookmark.setVirtualPosition(marks[i].index);
SessionSyncModel.bookmarks[marks[i].id] = marks[i];
this.SyncModel.setBookmark(marks[i].id, bookmark);
this.DOMBookmarks.appendChild(bookmark.DOMRoot);
}
this.updateSessionInfo();
this.restoreScrollTop('restore');
}.bind(this));
};
SessionContainer.prototype.restoreScrollTop = function restoreScrollTop(state, forcedValue)
{
var scrollTop = AppConfig.get('state.scrollTop.' + state);
this.DOMBookmarks.scrollTop = (forcedValue !== undefined) ? forcedValue : scrollTop;
};
SessionContainer.prototype.bookmarkCurrentTab = function bookmarkCurrentTab()
{
// Test if already saved in this session
SessionManager.getCurrentTab(function(activeTab) {
var sessionID = this.activeSessionID;
var bookmarks = SessionSyncModel.bookmarks;
for (var i in bookmarks)
{
if (bookmarks[i].parentId == sessionID && bookmarks[i].url == activeTab.url)
{
WindowEvents.emit(document, 'Notification', {
message: 'Already Saved',
});
var bookmarkElem = this.SyncModel.getBookmark(bookmarks[i].id);
bookmarkElem.highlight();
return;
}
}
// Add bookmark into the session
BookmarkManager.createBookmarkFromTab(activeTab, sessionID)
.then(function (mark) {
var bookmark = new SessionBookmark(mark);
SessionSyncModel.bookmarks[mark.id] = mark;
this.SyncModel.setBookmark(mark.id, bookmark);
this.DOMBookmarks.appendChild(bookmark.DOMRoot);
bookmark.setVirtualPosition(mark.index);
bookmark.highlight();
}.bind(this));
}.bind(this));
};
SessionContainer.prototype.saveHistorySession = function saveHistorySession(sessionIndex)
{
if (sessionIndex == undefined)
{
// When Save button from toolbar is pressed
SessionManager.saveHistorySession(this.selectedHistorySession);
}
else
{
// Save from context menu
SessionHistory.getHistorySession(sessionIndex, function (sessionInfo) {
SessionManager.saveHistorySession(sessionInfo);
});
}
};
// *****************************************************************************
// Public API
exports.SessionContainer = SessionContainer;
});