-
Notifications
You must be signed in to change notification settings - Fork 2
/
bookmarksorter.js
109 lines (92 loc) · 3.57 KB
/
bookmarksorter.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (c) 2013 ICRL
// See the file license.txt for copying permission.
/******* BOOKMARK SORTER *******/
define(["jquery", "underscore", "jqueryhelpers", "storage", "alchemy", "config", "sharedbrowser", "lib/purl", ], function($, _, jhelpers, storage, alchemy, config, shared) {
return {
/**
* Sorts a single bookmark via chained promises
* Makes two folders - one for category, and one for concept - and moves the bookmark in the concept folder.
* @param {BookmarkTreeNode} bookmark The bookmark to sort. In Firefox, this will be adapted to be identical to a Chrome object.
* @config {boolean} isSorting Successful Local storage variable for in progress sorting
* @return {object} deferred The deferred object to resolve [JQuery whenSync].
*/
sortBookmarkEx : function (bookmark, archivesId, options) {
// Make a deferred
var dfd = $.Deferred(),
bookmark = bookmark,
options = options || {sortAction: true, maxLevels: config.defaultTaxonomyLevels, archivesFolder: config.archivesFolder},
sortAction = options.sortAction,
maxLevels = options.maxLevels || config.defaultTaxonomyLevels;
// ...sorting...
shared.createFoldersByTaxonomy(bookmark.url, archivesId, maxLevels).done(function() {
var folderIds = arguments[0],
lastFolderId = folderIds[folderIds.length - 1];
// Move the bookmark
if(sortAction) {
shared.moveBookmarkIfNotExists(bookmark, lastFolderId).done(function (id, parentId) {
dfd.resolve(id, parentId);
});
}
else {
// Otherwise, create a copy by default
shared.createBookmarkIfNotExists(lastFolderId, bookmark.title, bookmark.url).done(function(id, parentId) {
dfd.resolve(id, parentId);
});
}
}).fail(function() {
console.log("Failed to create folders by taxonomy.");
dfd.reject();
});
// Return a promise
return dfd.promise();
},
sortBookmarksEx: function (bookmarks, rootId, options) {
var me = this,
dfd = $.Deferred(),
rootId = _.isUndefined(rootId) ? config.rootBookmarksId : rootId,
archivesFolder = options.archivesFolder || config.archivesFolder,
cull = options.cull,
cullThreshold = options.cullThreshold,
bookmarks = _.isArray(bookmarks) ? bookmarks : new Array(bookmarks);
shared.createFolderIfNotExists(archivesFolder, rootId).done(function(archivesId) {
var count = 0;
var defFunctors = _.map(bookmarks, function (bookmark) {
return function () {
var deferred = arguments[0];
// Resolve deferred in the future.
me.sortBookmarkEx(bookmark, archivesId, options).done(function(id, parentId) {
deferred.resolve({id: id, parentId: parentId});
dfd.notify(count);
});
count++;
};
});
var asyncChain = jhelpers.jQueryWhenSync(me, defFunctors);
asyncChain.then(function() {
// Set a local storage variable to sorting in progress
storage.setIsOnManualSorting(true);
});
asyncChain.always(function() {
// Set a local storage variable to finished sorting
storage.setIsOnManualSorting(false);
});
asyncChain.done(function() {
if(cull) {
shared.cullTree(archivesId, cullThreshold).always(function () {
dfd.resolve(archivesId, arguments);
});
}
else {
dfd.resolve(archivesId, arguments);
}
});
asyncChain.fail(function(results) {
dfd.reject(results);
});
}).fail(function() {
dfd.reject();
});
return dfd.promise();
}
};
});