forked from themox/MMM-GoogleTasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-GoogleTasks.js
408 lines (335 loc) · 14.8 KB
/
MMM-GoogleTasks.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
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
Module.register("MMM-GoogleTasks", {
// Default module config.
defaults: {
listID: "", // List ID (see authenticate.js)
listName: "", // provide a unique pathing suffix to token files
maxResults: 10,
dateFormat: "MMM Do", // Format to display dates (moment.js formats)
updateInterval: 5, // Time between content updates (minutes)
animationSpeed: 2, // Speed of the update animation (seconds)
width: 320,
tableClass: "small", // Name of the classes issued from main.css
sortOrder: "ascending", // [ascending, descending]
sortBy: "due", // [due, updated, default, title]
groupSubTasks: true, // [true, false]
taskIcon: "bx:square-rounded", // something from the iconify icon set
},
// Define required scripts
getScripts: function () {
return ["moment.js"];
},
// Define required scripts.
getStyles: function () {
return ["MMM-GoogleTasks.css"];
},
// Define start sequence
start: function() {
Log.info("Starting module: " + this.name);
this.tasks;
this.loaded = false;
this.ASCENDING = 1;
this.DESCENDING = -1;
this.sortOrderVal = 1;
this.hasError = false;
this.errorMessage = "";
if (this.config.sortOrder == "ascending") {
this.sortOrderVal = this.ASCENDING;
} else if (this.config.sortOrder == "descending") {
this.sortOrderVal = this.DESCENDING;
}
if (!this.config.listID) {
Log.log("config listID required");
} else {
this.sendSocketNotification("MODULE_READY", this.config);
}
},
socketNotificationReceived: function(notification, payload) {
var self = this;
if (notification === "SERVICE_READY") {
self.sendSocketNotification("REQUEST_UPDATE", self.config);
// Create repeating call to node_helper get list
setInterval(function() {
self.sendSocketNotification("REQUEST_UPDATE", self.config);
}, this.config.updateInterval * 60 * 1000);
// Check if payload id matches module id
} else if (notification === "UPDATE_DATA" && payload.id === self.config.listID) {
// Handle new data
self.loaded = true;
if (payload.items) {
self.tasks = payload.items;
self.updateDom(self.config.animationSpeed * 1000);
} else {
self.tasks = null;
Log.info("No tasks found.");
self.updateDom(self.config.animationSpeed * 1000);
}
} else if (notification === "TASKS_API_ERROR") {
self.hasError = true;
//self.errorMessage = payload;
self.errorMessage = "Error loading GoogleTasks; see logs. <br>";
if (payload) {
if (payload.code) {
self.errorMessage += "Code: " + payload.code + " ";
}
if (payload.message) {
self.errorMessage += payload.message + "<br>";
}
if (payload.details) {
for (const detail of payload.details) {
self.errorMessage += "* " + detail + "<br>";
}
}
this.sendSocketNotification("MODULE_READY", this.config); //try restarting it including auth
}
self.updateDom(self.config.animationSpeed * 1000);
}
},
getDom: function() {
var wrapper = document.createElement('div');
wrapper.style = "width:" + this.config.width + "px;";
wrapper.className = "container ";
wrapper.className += this.config.tableClass;
var numTasks = 0;
var taskList = this.tasks;
if (taskList) {
numTasks = Object.keys(taskList).length;
}
if (this.hasError){
// May not need to recover from error; most errors will require restart
// @TODO: try it like this for a while and see how it works.
wrapper.innerHTML = this.errorMessage;
wrapper.className = this.config.tableClass + " dimmed";
wrapper.className = this.config.tableClass + " error";
return wrapper;
}
if (!taskList) {
wrapper.innerHTML = (this.loaded) ? "EMPTY" : "LOADING";
wrapper.className = this.config.tableClass + " dimmed";
return wrapper;
}
// sort the list according to the chosen sort order
if (this.config.sortOrder.toLowerCase() == "ascending") {
switch(this.config.sortBy) {
case "title":
taskList = taskList.sort(this.compare_title_ascending);
break;
case "due":
taskList = taskList.sort(this.compare_dueDate_ascending);
break;
case "updated":
taskList = taskList.sort(this.compare_updated_ascending);
break;
default:
// do nothing, use default sort order from Google
// This applies as well if the argument is a typo
break;
}
} else if (this.config.sortOrder.toLowerCase() == "descending") {
switch(this.config.sortBy) {
case "title":
taskList = taskList.sort(this.compare_title_descending);
break;
case "due":
taskList = taskList.sort(this.compare_dueDate_descending);
break;
case "updated":
taskList = taskList.sort(this.compare_updated_descending);
break;
default:
// do nothing, use default sort order from Google
// This applies as well if the argument is a typo
break;
}
}
var titleWrapper, dateWrapper, noteWrapper;
// build a dictionary to track parent/child relationships if subtasks
parents = {};
// this loop is just to identify which tasks have subtasks, and build a dictionary of those tasks
// where the key is the id of the parent, and the value is an array of indices into the master task list
// of the child tasks
for (i = 0; i < taskList.length; i++) {
if (taskList[i].parent) {
if (parents[taskList[i].parent]) {
// If this task already has a child, simply add the next one.
parents[taskList[i].parent].push(i);
} else {
// If this is the first child, need to initialize the array.
parents[taskList[i].parent] = Array();
parents[taskList[i].parent].push(i);
}
}
}
icon1 = "<span class=\"iconify\" data-icon=\"";
icon2 = "\"></span>";
task_icon = this.config.taskIcon; //"bx:square-rounded";
// next loop displays the tasks.
task_count = 0;
groupSubTasks = this.config.groupSubTasks;
for (i = 0; i < taskList.length; i++) {
// if groupsubtasks is true, and it doesn't have parents, display outerloop
// if groupsubtasks is false, regardless, display outerloop
// otherwise, don't display outer loop, means we're grouping by subtask and this one has parents (is child)
// ignore items that have parents; they'll get displayed in the subloop underneath their parents.
if (!groupSubTasks || (!taskList[i].parent && groupSubTasks)) {
task_count++;
// Display the top level tasks
item = taskList[i];
titleWrapper = document.createElement('div');
titleWrapper.className = "item title";
titleWrapper.innerHTML = icon1 + task_icon + icon2 + item.title;
if (groupSubTasks && item.parent) {
titleWrapper.className = "item child";
}
if (item.notes) {
noteWrapper = document.createElement('div');
noteWrapper.className = "item notes light";
noteWrapper.innerHTML = item.notes.replace(/\n/g , "<br>");
titleWrapper.appendChild(noteWrapper);
}
dateWrapper = document.createElement('div');
dateWrapper.className = "item date light";
if (item.due) {
var date = moment(item.due);
dateWrapper.innerHTML = date.utc().format(this.config.dateFormat);
}
// Create borders between parent items
if (numTasks < this.tasks.length-1 && !this.tasks[numTasks+1].parent) {
titleWrapper.style.borderBottom = "1px solid #666";
dateWrapper.style.borderBottom = "1px solid #666";
}
wrapper.appendChild(titleWrapper);
wrapper.appendChild(dateWrapper);
// Display subtasks under this task; they will be internally also sorted according to the overall sort order
// This mechanism works because there can be only one level of subtasks within Google Tasks
// If there were multiple levels, would probably want recursion
// This inner loop only gets displayed if we're grouping by subtasks
if (parents[taskList[i].id] && groupSubTasks) {
for (j = 0; j < parents[taskList[i].id].length; j++ ) {
// We're going to use this a couple times; abstract it a little for simplification and readability
index = parents[taskList[i].id][j];
item = taskList[index];
titleWrapper = document.createElement('div');
titleWrapper.className = "item title";
titleWrapper.innerHTML = icon1 + task_icon + icon2 + item.title;
titleWrapper.className = "item child";
if (item.notes) {
noteWrapper = document.createElement('div');
noteWrapper.className = "item notes light";
noteWrapper.innerHTML = item.notes.replace(/\n/g , "<br>");
titleWrapper.appendChild(noteWrapper);
}
dateWrapper = document.createElement('div');
dateWrapper.className = "item date light";
if (item.due) {
var date = moment(item.due);
dateWrapper.innerHTML = date.utc().format(this.config.dateFormat);
}
// Create borders between parent items
if (numTasks < this.tasks.length - 1 && !this.tasks[numTasks + 1].parent) {
titleWrapper.style.borderBottom = "1px solid #666";
dateWrapper.style.borderBottom = "1px solid #666";
}
wrapper.appendChild(titleWrapper);
wrapper.appendChild(dateWrapper);
}
}
}
}
return wrapper;
},
// Compare Google TasksList item based on due date
compare_dueDate_ascending: function(itemA, itemB) {
// Treat undefined dates like today
today = new Date();
dueA = itemA.due;
dueB = itemB.due;
if (!dueA) {
dueA = today;
} else {
dueA = new Date(dueA);
}
if (!dueB) {
dueB = today;
} else {
dueB = new Date(dueB);
}
return (isFinite(dueA = dueA.valueOf()) && isFinite(dueB = dueB.valueOf()) ? (dueA>dueB)-(dueA<dueB) : NaN); // * this.sortOrderVal;
},
compare_dueDate_descending: function(itemA, itemB) {
// Treat undefined dates like today
today = new Date();
dueA = itemA.due;
dueB = itemB.due;
if (!dueA) {
dueA = today;
} else {
dueA = new Date(dueA);
}
if (!dueB) {
dueB = today;
} else {
dueB = new Date(dueB);
}
return (isFinite(dueA = dueA.valueOf()) && isFinite(dueB = dueB.valueOf()) ? (dueA>dueB)-(dueA<dueB) : NaN) * -1;
},
// Compare Google TaskLIst item based on updated date
compare_updated_ascending: function(itemA, itemB) {
// Treat undefined dates like today
today = new Date();
updateA = itemA.updated;
updateB = itemB.updated;
if (!updateA) {
updateA = today;
} else {
updateA = new Date(updateA);
}
if (!updateB) {
updateB = today;
} else {
updateB = new Date(updateB);
}
return (isFinite(updateA = updateA.valueOf()) && isFinite(updateB = updateB.valueOf()) ? (updateA>updateB)-(updateA<updateB) : NaN);
},
// Compare Google TaskLIst item based on updated date
compare_updated_descending: function(itemA, itemB) {
// Treat undefined dates like today
today = new Date();
updateA = itemA.updated;
updateB = itemB.updated;
if (!updateA) {
updateA = today;
} else {
updateA = new Date(updateA);
}
if (!updateB) {
updateB = today;
} else {
updateB = new Date(updateB);
}
return (isFinite(updateA = updateA.valueOf()) && isFinite(updateB = updateB.valueOf()) ? (updateA>updateB)-(updateA<updateB) : NaN) * -1;
},
// Compare Google TaskLIst item based on alphabetical title
compare_title_ascending: function(itemA, itemB) {
updateA = itemA.title;
updateB = itemB.title;
if (!updateA) {
updateA = "";
}
if (!updateB) {
updateB = "";
}
return updateA.toLowerCase().localCompare(updateB.toLowerCase());
},
// Compare Google TaskLIst item based on alphabetical title
compare_title_descending: function(itemA, itemB) {
updateA = itemA.title;
updateB = itemB.title;
if (!updateA) {
updateA = "";
}
if (!updateB) {
updateB = "";
}
return updateA.toLowerCase().localCompare(updateB.toLowerCase()) * -1;
},
});