-
Notifications
You must be signed in to change notification settings - Fork 17
/
BuiltinBookmarking.js
89 lines (81 loc) · 3.44 KB
/
BuiltinBookmarking.js
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
'use strict';
/*
* =================================================================================================
* FIREFOX BUILT-IN BOOKMARKING
* =================================================================================================
*/
class BuiltinBookmarking {
constructor(options) {
this._options = options;
}
_createMovingPropertiesForBookmark() {
const bookmarkTreeNode = {};
if (this._options.isBuiltinFolderSet()) {
if (this._options.isBuiltinFolderLastUsed()) {
bookmarkTreeNode.parentId = this._options.getLastUsedFolder();
} else {
bookmarkTreeNode.parentId = this._options.getBuiltinFolder();
}
}
if (this._options.addBuiltinBookmarksOnTop()) bookmarkTreeNode.index = 0;
return bookmarkTreeNode;
}
_createMovingPropertiesForAllTabsFolder() {
const bookmarkTreeNode = {};
if (this._options.isAllTabsFolderSet()) {
if (this._options.isAllTabsFolderLastUsed()) {
bookmarkTreeNode.parentId = this._options.getLastUsedFolder();
} else {
bookmarkTreeNode.parentId = this._options.getAllTabsFolder();
}
}
if (this._options.addAllTabsBookmarksOnTop()) bookmarkTreeNode.index = 0;
return bookmarkTreeNode;
}
_nodeIsValidForMoving(bookmarkTreeNode) {
let isValid = false;
if (Object.keys(bookmarkTreeNode).length !== 0 && bookmarkTreeNode.constructor === Object) {
if (
Object.prototype.hasOwnProperty.call(bookmarkTreeNode, 'parentId') &&
typeof bookmarkTreeNode.parentId !== 'undefined'
) {
isValid = true;
}
}
return isValid;
}
async _renameBookmark(bookmarkInfo) {
await browser.bookmarks.update(bookmarkInfo.id, {
title: bookmarkInfo.title.replace(DBF_INTERAL_INDICATOR, ''),
});
}
async _moveBookmarkToDefinedLocation(bookmarkInfo) {
let bookmarkTreeNode;
if (Utils.bookmarkIsWebPage(bookmarkInfo)) {
if (await Utils.bookmarkIsPartOfAllTabsFolder(bookmarkInfo)) return;
bookmarkTreeNode = this._createMovingPropertiesForBookmark();
} else if (Utils.bookmarkIsFolder(bookmarkInfo)) {
if (!Utils.bookmarkIsAllTabsSystemCreatedFolder(bookmarkInfo)) return;
bookmarkTreeNode = this._createMovingPropertiesForAllTabsFolder();
}
if (!Object.prototype.hasOwnProperty.call(bookmarkTreeNode, 'parentId')) {
bookmarkTreeNode.parentId = bookmarkInfo.parentId;
}
if (this._nodeIsValidForMoving(bookmarkTreeNode)) {
browser.bookmarks.move(bookmarkInfo.id, bookmarkTreeNode);
await this._options.updateLastUsedFolder(bookmarkTreeNode.parentId);
}
}
async move(id, bookmarkInfo) {
if (Utils.bookmarkIsAddonInternal(bookmarkInfo)) {
await this._renameBookmark(bookmarkInfo);
} else {
if (Utils.bookmarkIsSeparator(bookmarkInfo)) return;
if (Utils.bookmarkIsBlankWebPage(bookmarkInfo)) return;
if (Utils.bookmarkIsRegularFolder(bookmarkInfo)) return;
const bookmarkIsCurrentPage = await Utils.bookmarkIsCurrentPage(bookmarkInfo);
if (!bookmarkIsCurrentPage) return;
await this._moveBookmarkToDefinedLocation(bookmarkInfo);
}
}
}