forked from hoominchu/sailboat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.js
340 lines (295 loc) · 11.9 KB
/
tasks.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
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
let saveBookmarks = true;
function Task(task_id, task_name, tabs, bookmarks, isOpen) {
this.id = task_id;
this.name = task_name;
this.tabs = tabs;
this.bookmarks = bookmarks;
this.history = [];
this.isOpen = isOpen;
this.activationTime = [];
this.deactivationTime = [];
this.likedPages = [];
this.archived = false;
}
function createAndActivateDefaultTask() {
var task = new Task(0, "Leisure", {}, {}, true); //The default task is active when created.
TASKS[task.id] = task;
chrome.windows.getCurrent(function (window) {
taskToWindow[0] = window.id; //Assigned to the current window.
// change bookmarks,tabs to task 0 bookmarks,tabs
chrome.storage.local.get("sailboatInitialised", function (response) { // first check if sailboat is initialised
if (!isEmpty(response)) {
// get all the tabs open currently
chrome.tabs.getAllInWindow(function (tabs) {
let tabIds = [];
for (let tabIdx = 0; tabIdx < tabs.length; tabIdx++) {
tabIds.push(tabs[tabIdx].id);
}
activateLeisure(tabIds);
});
function activateLeisure(tabIdsToBeRemoved) {
chrome.storage.local.get("TASKS", function (tasks) {
tasks = tasks["TASKS"];
//Mark task as active
const now = new Date();
tasks[0].activationTime.push(now.toString());
tasks[0].isOpen = true;
if (tasks[0].tabs.length > 0) { //task has more than 0 tabs.
for (let i = 0; i < tasks[0].tabs.length; i++) {
let url = tasks[0].tabs[i].url;
chrome.tabs.create({"url": url}, function (tab) {
if (i === 0) {
chrome.tabs.query({"url": "chrome://newtab/"}, function (tabs) {
chrome.tabs.remove(tabs[0].id);
});
}
});
}
}
TASKS = tasks;
CTASKID = 0;
updateStorage("TASKS", tasks);
updateStorage("CTASKID", 0);
saveBookmarks = false;
changeBookmarks(-1, 0);
chrome.tabs.remove(tabIdsToBeRemoved);
chrome.windows.getAll(function (windows) {
for (let i = 0; i < windows.length; i++) {
if (window.id !== windows[i].id) {
chrome.windows.remove(windows[i].id);
}
}
});
});
}
}
});
});
chrome.browserAction.setBadgeText({"text": "Leisure"}); //Badge set to Leisure
}
//filterTasks takes a dictionary of type {"archived": false}, and returns dict of type {0: "Default", 1: "Shopping"} that fit the filters
function filterTasks(filter) {
let tasksDict = {};
for (taskId in TASKS) {
if (taskId != "lastAssignedId") {
if (!isEmpty(filter)) {
for (propName in filter) {
if (TASKS[taskId][propName] == filter[propName]) {
tasksDict[taskId] = TASKS[taskId].name;
}
}
} else {
tasksDict[taskId] = TASKS[taskId].name;
}
}
}
return tasksDict;
}
function getLikedPages(task_id) {
const likedPages = [];
for (let i = 0; i < TASKS[task_id].history.length; i++) {
if (TASKS[task_id].history[i].isLiked) {
likedPages.push(TASKS[task_id].history[i]);
}
}
return likedPages;
}
function likePages(urls, task_id) {
for (let i = 0; i < urls.length; i++) {
TASKS[task_id].history[indexOfElementWithProperty(TASKS[task_id].history, "url", urls[i])].isLiked = !TASKS[task_id].history[indexOfElementWithProperty(TASKS[task_id].history, "url", urls[i])].isLiked;
}
updateStorage("TASKS", TASKS);
}
function deleteFromHistory(urls, task_id) {
for (let i = 0; i < urls.length; i++) {
TASKS[task_id].history.splice(indexOfElementWithProperty(TASKS[task_id].history, "url", urls[i]), 1);
}
updateStorage("TASKS", TASKS);
}
function createTask(taskName, tabs, createFromCurrentTabs, bookmarks) {
if (tabs === null) {
tabs = [];
}
if (createFromCurrentTabs) {
var newTask = new Task(TASKS["lastAssignedId"] + 1, taskName, tabs, {});
TASKS[TASKS["lastAssignedId"] + 1] = newTask;
TASKS["lastAssignedId"] = TASKS["lastAssignedId"] + 1;
updateStorage("TASKS", TASKS);
} else {
const emptyArray = [];
var newTask = new Task(TASKS["lastAssignedId"] + 1, taskName, emptyArray, {});
TASKS[TASKS["lastAssignedId"] + 1] = newTask;
TASKS["lastAssignedId"] = TASKS["lastAssignedId"] + 1;
updateStorage("TASKS", TASKS);
}
}
function addTabsToTask(taskId, tabs) {
TASKS[taskId].tabs = TASKS[taskId].tabs.concat(tabs);
updateStorage("TASKS", TASKS);
if (taskToWindow.hasOwnProperty(taskId)) {
//If there is a task that is not open but is in Task urls then open that
chrome.windows.get(taskToWindow[taskId], {"populate": true}, function (window) {
const tabs = window.tabs;
const openUrls = new Set();
for (let i = 0; i < tabs.length; i++) {
openUrls.add(tabs[i].url);
}
const taskUrls = new Set();
for (let j = 0; j < TASKS[taskId].tabs.length; j++) {
taskUrls.add(TASKS[taskId].tabs[j].url);
}
if (taskUrls.size > openUrls.size) {
taskUrls.forEach(function (url) {
if (!openUrls.has(url)) {
chrome.tabs.create({"windowId": window.id, "url": url, "selected": false});
}
});
}
});
}
}
function activateTaskInWindow(taskId) {
//Activating a task involves the following:
//1. Set the CTASKID to it's id.
//2. Mark its task object as active and add the current time to its activation time.
//3. Set the badge to current task.
//4. Switch/Create to the task's window.
//5. Add the task's bookmarks to the current bookmarks.
//6. Update storage for changes.
chrome.storage.local.get("TASKS", function (tasks) {
tasks = tasks["TASKS"];
if (taskId !== CTASKID) { //Do all this only if it is not already active.
try {
//Mark task as active.
let now = new Date();
tasks[taskId].activationTime.push(now.toString());
tasks[taskId].isOpen = true;
if (taskToWindow.hasOwnProperty(taskId)) {
// task is already open in some window, so just switch to that window.
chrome.windows.update(taskToWindow[taskId], {"focused": true});
} else {
// task is not open, so we create a new window with its tabs.
let urls = [];
if (tasks[taskId].tabs.length > 0) { // task has more than 0 tabs.
for (let i = 0; i < tasks[taskId].tabs.length; i++) {
urls.push(tasks[taskId].tabs[i].url);
}
} else {
// if no tabs in the task - open index page
urls.push("html/index.html");
}
chrome.windows.create({"url": urls}, function (window) { //create a window with these tabs
taskToWindow[taskId] = window.id; //assign the window id to the task
});
}
TASKS = tasks;
//Set the badge text as new task name.
chrome.browserAction.setBadgeText({"text": TASKS[taskId].name.slice(0, 4)});
updateStorage("TASKS", tasks); //Update chrome storage.
let lastTaskId;
chrome.storage.local.get("CTASKID", function (cTaskIdObject) {
if (cTaskIdObject["CTASKID"]) {
lastTaskId = cTaskIdObject["CTASKID"];
} else {
lastTaskId = 0;
}
// saveBookmarks = false;
changeBookmarks(lastTaskId, taskId);
updateStorage("CTASKID", taskId);
CTASKID = taskId; //Set the CTASKID as the id of the task/x
switchingTask = false;
});
} catch (err) {
console.log(err.message);
}
}
});
}
function saveBookmarksInStorage(taskId) {
chrome.bookmarks.getTree(function (bookmarks) {
TASKS[taskId].bookmarks = bookmarks;
updateStorage("TASKS", TASKS);
});
}
//This works only to save the task in the current window.
function saveTaskInWindow(taskId, f) {
// save the tabs in the TASK object
if (window) {
if (TASKS[taskId]) {
chrome.windows.getCurrent({"populate": true}, function (window) {
TASKS[taskId].tabs = window.tabs;
updateStorage("TASKS", TASKS);
if (f != null) {
f();
}
});
}
}
}
//Run this when a task is deactivated.
function deactivateTaskInWindow(taskId, f) {
if (CTASKID === taskId) {
//Mark task object as inactive and add the current time to its deactivation time.
TASKS[taskId].deactivationTime.push(new Date().toString());
updateStorage("TASKS", TASKS);
if (f != null) {
f();
}
}
}
function deleteTask(task_id) {
if (TASKS[task_id]) {
if (taskToWindow[task_id]) {
alert("This task is open. Please close it before deleting.");
} else {
const confirmation = confirm("Deleting a task will remove all the history and liked pages of the task. Are you sure you want to delete it?");
if (confirmation) {
delete TASKS[task_id];
if (taskToWindow[task_id]) {
chrome.windows.remove(taskToWindow[task_id]);
delete taskToWindow[task_id];
}
updateStorage("TASKS", TASKS);
}
}
}
}
function renameTask(task_id, newName) {
if (TASKS[task_id]) {
TASKS[task_id].name = newName;
updateStorage("TASKS", TASKS);
}
}
function addURLToTask(url, task_id) {
TASKS[task_id].tabs.push({"url": url});
if (taskToWindow[task_id]) {
chrome.tabs.create({"windowId": taskToWindow[task_id], "url": url, "selected": false});
}
updateStorage("TASKS", TASKS);
}
function archiveTask(task_id) {
if (task_id != CTASKID) {
TASKS[task_id].archived = !TASKS[task_id].archived;
updateStorage("TASKS", TASKS);
} else {
alert("Can't archive an open task. Please switch before archiving.")
}
}
function openLikedPages(task_id) {
const likedPages = getLikedPages(task_id);
const likedPagesUrls = [];
for (let i = 0; i < likedPages.length; i++) {
likedPagesUrls.push(likedPages[i].url);
}
openTabs(likedPagesUrls);
}
function closeTask(taskId) {
chrome.windows.remove(taskToWindow[taskId]);
TASKS[taskId].isOpen = false;
reloadSailboatTabs();
}
function downloadTasks() {
let dateObj = new Date();
let date = dateObj.toDateString();
downloadObjectAsJson(TASKS, "Sailboat Tasks from " + date);
}