forked from ReDEnergy/SessionSync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession-folder.js
More file actions
326 lines (268 loc) · 8.58 KB
/
Copy pathsession-folder.js
File metadata and controls
326 lines (268 loc) · 8.58 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
define(function(require, exports) {
'use strict';
// *****************************************************************************
// Custom Modules
// App
const { AppConfig } = require('../config');
const { SessionSyncModel } = require('./session-sync-model');
// Utils
const { HTMLCreator } = require('../utils/dom');
const { WindowEvents } = require('../utils/global-events');
const { AutoScroll } = require('../utils/auto-scroll');
const { DragContext } = require('../utils/drag-context');
// *****************************************************************************
// API
/*
* Session Bookmark
* Used for interacting with a Firefox bookmark item
*/
function SessionFolder(document, bookmark)
{
var DomElem = HTMLCreator(document);
var folder = DomElem('div', {class: 'folder', type: bookmark.type});
folder.setAttribute('sessionID', bookmark.id);
this.DOMRoot = folder;
this.updateFrom(bookmark);
// ------------------------------------------------------------------------
// Public data
this.visible = true;
this.document = document;
this.bookmarkID = bookmark.id;
}
SessionFolder.prototype.folderOffset = 2.7; // em;
SessionFolder.prototype.updateFrom = function updateFrom(bookmark)
{
this.bookmark = bookmark;
this.bookmark.title_lowercase = this.bookmark.title.toLowerCase();
this.setTitle(bookmark.title);
};
SessionFolder.prototype.isVisible = function isVisible()
{
return this.visible;
};
SessionFolder.prototype.setVisible = function setVisible()
{
this.visible = true;
this.DOMRoot.removeAttribute('filter');
};
SessionFolder.prototype.match = function match(expression)
{
var name = this.bookmark.title_lowercase;
if (name.indexOf(expression) != -1) {
this.visible = true;
this.DOMRoot.removeAttribute('filter');
return true;
}
this.visible = false;
this.DOMRoot.setAttribute('filter', '');
return false;
};
SessionFolder.prototype.restorePosition = function restorePosition()
{
this.DOMRoot.style.left = '0px';
this.DOMRoot.style.top = this.position * this.folderOffset + 'em';
};
SessionFolder.prototype.setVirtualPosition = function setVirtualPosition(position)
{
if (this.position == position)
return;
this.position = position;
this.DOMRoot.style.top = (position * this.folderOffset).toFixed(2) + 'em';
};
SessionFolder.prototype.setTitle = function setTitle(title)
{
this.DOMRoot.textContent = title;
};
SessionFolder.prototype.remove = function remove()
{
var box = this.DOMRoot;
if (box.parentElement)
box.parentElement.removeChild(box);
};
// ----------------------------------------------------------------------------
// Session Events - delegated
var SessionFolderEvents = function SessionFolderEvents(document, container, SessionList)
{
var startDragEvent = null;
var trackingTelemetry;
var offsetRect = {};
var bookmarkContext = null;
var SyncModel = SessionSyncModel.getModel(document);
function getAttribute(target, attribute) {
try {
return target.getAttribute(attribute);
}
catch (err) {
console.log(err);
return null;
}
}
function getBookmarkID(e) {
return getAttribute(e.target, 'sessionID');
}
// --------------------------------------------------------------
// Drag Events
var trackDragging = function _mouseMove(e)
{
var delta = Math.abs(startDragEvent.clientX - e.clientX) > 5 || Math.abs(startDragEvent.clientY - e.clientY) > 5;
if (delta) {
DragContext.setContext(bookmarkContext);
trackingTelemetry = new Date();
offsetRect = container.parentElement.parentElement.getBoundingClientRect();
offsetRect.relativeY = offsetRect.y - container.parentElement.offsetTop;
offsetRect.relativeX = offsetRect.x - container.parentElement.offsetLeft;
bookmarkContext.DOMRoot.style.top = e.clientY - offsetRect.relativeY + 'px';
bookmarkContext.DOMRoot.style.left = e.clientX - offsetRect.relativeX + 'px';
bookmarkContext.DOMRoot.setAttribute('dragging', '');
// Events
window.addEventListener('mousemove', mouseDragging);
window.removeEventListener('mousemove', trackDragging);
}
};
var cancelDragging = function cancelDragging(e)
{
AutoScroll.stop();
bookmarkContext.DOMRoot.setAttribute('dragging', 'drop');
bookmarkContext.DOMRoot.style.top = e.clientY - offsetRect.y + container.scrollTop + 'px';
bookmarkContext.DOMRoot.style.left = e.clientX - offsetRect.x + 'px';
setTimeout(function() {
bookmarkContext.DOMRoot.removeAttribute('dragging');
bookmarkContext.restorePosition();
}, 50);
window.removeEventListener('mousemove', mouseDragging);
DragContext.clearContext();
};
var mouseDragging = function mouseMove(e)
{
if (this != window)
return;
var deltaTime = new Date() - trackingTelemetry;
if (deltaTime < 16) {
return;
}
var tryScroll = e.clientX > offsetRect.left && e.clientX < offsetRect.right;
if (tryScroll)
{
if (e.clientY < offsetRect.top) {
AutoScroll.scroll(container, (e.clientY - offsetRect.top));
}
else if (e.clientY > offsetRect.bottom) {
AutoScroll.scroll(container, (e.clientY - offsetRect.bottom));
} else {
AutoScroll.stop();
}
} else {
AutoScroll.stop();
}
bookmarkContext.DOMRoot.style.top = e.clientY - offsetRect.relativeY + 'px';
bookmarkContext.DOMRoot.style.left = e.clientX - offsetRect.relativeX + 'px';
trackingTelemetry = new Date();
};
var mouseUp = function _mouseUp(e)
{
// Test if changing
if (e.target.className == 'folder')
{
var sessionID = getBookmarkID(e);
// Preview the content if a click event was registered (mouse-up on the same DOM node)
if (bookmarkContext.bookmarkID == sessionID)
{
if (!DragContext.hasContext()) {
SessionList.selectSyncSession(bookmarkContext, false);
}
}
else
{
if (AppConfig.get('session.sorting') === 'position-asc')
{
SessionSyncModel.moveBookmark(bookmarkContext.bookmarkID, sessionID);
}
else
{
WindowEvents.emit(document, 'Notification', {
message: 'Rearage function available only when items are sorted ascending by position',
timeout: 3000,
});
}
}
}
if (DragContext.hasContext())
{
cancelDragging(e);
}
else
{
window.removeEventListener('mousemove', trackDragging);
}
window.removeEventListener('mouseup', mouseUp);
};
var folderMouseDown = function _folderMouseDown(e)
{
if (DragContext.hasContext()) {
cancelDragging(e);
return;
}
var sessionID = getBookmarkID(e);
bookmarkContext = SyncModel.getBookmark(sessionID);
if (e.button == 0) {
startDragEvent = e;
window.addEventListener('mousemove', trackDragging);
window.addEventListener('mouseup', mouseUp);
return;
}
};
var folderContextMenu = function folderContextMenu(e)
{
WindowEvents.emit(document, 'SessionContextMenu-Open', {
context: bookmarkContext.bookmark.id,
event: e
});
};
var sessionDblClick = function sessionDblClick(e)
{
if (bookmarkContext)
{
var type = e.target.getAttribute('type');
switch (type)
{
case 'folder':
WindowEvents.emit(document, 'SessionContextMenu-EditSession', bookmarkContext.bookmarkID);
break;
case 'bookmark':
WindowEvents.emit(document, 'BookmarkCtxMenu-EditBookmark', bookmarkContext.bookmarkID);
break;
}
}
};
// ------------------------------------------------------------------------
// Init Code Events
container.addEventListener('dblclick', sessionDblClick);
container.addEventListener('mousedown', folderMouseDown);
container.addEventListener('contextmenu', folderContextMenu);
};
var SortMethods = {
'name-asc' : function sortByNameAscending(a, b) {
return a.bookmark.title_lowercase > b.bookmark.title_lowercase;
},
'name-desc' : function sortByNameDescending(a, b) {
return a.bookmark.title_lowercase < b.bookmark.title_lowercase;
},
'position-asc' : function sortByPositionAscending(a, b) {
return a.bookmark.index > b.bookmark.index;
},
'position-desc' : function sortByPositionDescending(a, b) {
return a.bookmark.index < b.bookmark.index;
},
'date-asc' : function sortByDateAscending(a, b) {
return a.bookmark.dateAdded > b.bookmark.dateAdded;
},
'date-desc' : function sortByDateDescending(a, b) {
return a.bookmark.dateAdded < b.bookmark.dateAdded;
}
};
// *****************************************************************************
// Public API
exports.SessionFolder = SessionFolder;
exports.SessionFolderSortBy = SortMethods;
exports.SessionFolderEvents = SessionFolderEvents;
});