forked from jgauth/MMM-GoogleTasks
-
Notifications
You must be signed in to change notification settings - Fork 13
/
MMM-GoogleTasks.js
167 lines (144 loc) · 5.3 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
Module.register("MMM-GoogleTasks", {
// Default module config.
defaults: {
listID: "", // List ID (see authenticate.js)
maxResults: 10,
showCompleted: false, //set showCompleted and showHidden true
ordering: "myorder", // Order by due date, title, updated timestamp or by 'my order'
dateFormat: "MMM Do", // Format to display dates (moment.js formats)
updateInterval: 10000, // Time between content updates (millisconds)
animationSpeed: 2000, // Speed of the update animation (milliseconds)
tableClass: "small" // Name of the classes issued from main.css
// Pointless for a mirror, not currently implemented
/*
dueMax: "2040-07-11T18:30:00.000Z", // RFC 3339 timestamp
dueMin: "1970-07-11T18:30:00.000Z", // RFC 3339 timestamp
completedMax: "2040-07-11T18:30:00.000Z", //only if showCompleted true (RFC 3339 timestamp)
completedMin: "1970-07-11T18:30:00.000Z", //only if showCompleted true (RFC 3339 timestamp)
*/
},
// Define required scripts
getScripts: function () {
return ["moment.js"];
},
// Define required scripts.
getStyles: function () {
return ["font-awesome.css", "MMM-GoogleTasks.css"];
},
// Define start sequence
start: function () {
Log.info("Starting module: " + this.name);
this.tasks;
this.loaded = false;
if (!this.config.listID) {
Log.log("config listID required");
} else {
this.sendSocketNotification("MODULE_READY", {});
}
// API requies completed config settings if showCompleted
if (!this.config.showCompleted) {
// delete this.config.completedMin;
// delete this.config.completedMax;
} else {
this.config.showHidden = true;
}
},
socketNotificationReceived: function (notification, payload) {
if (notification === "SERVICE_READY") {
this.sendSocketNotification("REQUEST_UPDATE", this.config);
// Create repeating call to node_helper get list
setInterval(function () {
this.sendSocketNotification("REQUEST_UPDATE", this.config);
}, this.config.updateInterval);
// Check if payload id matches module id
} else if (
notification === "UPDATE_DATA" &&
payload.id === this.config.listID
) {
// Handle new data
this.loaded = true;
if (payload.items) {
this.tasks = payload.items;
this.updateDom(this.config.animationSpeed);
} else {
this.tasks = null;
Log.info("No tasks found.");
this.updateDom(this.config.animationSpeed);
}
}
},
getDom: function () {
let wrapper = document.createElement("div");
wrapper.className = "container ";
wrapper.className += this.config.tableClass;
if (!this.tasks) {
wrapper.innerHTML = this.loaded ? "EMPTY" : "LOADING";
wrapper.className = this.config.tableClass + " dimmed";
return wrapper;
}
// Sort attributes like they are shown in the Tasks app
switch (this.config.ordering) {
case "myorder":
let temp = [];
this.tasks
.filter((task) => task.parent === undefined) // Filter tasks to only parent tasks
.sort((a, b) => (a.position > b.position ? 1 : -1)) // Sort parent tasks by position
.forEach((task) => {
// Map over parents to create reordered list of tasks
temp.push(task);
// Loop through all tasks to find and sort subtasks for each parent
let subList = [];
this.tasks.map((subtask) => {
if (subtask.parent === task.id) {
subList.push(subtask);
}
});
subList.sort((a, b) => (a.position > b.position ? 1 : -1));
temp.push(...subList);
});
this.tasks = temp;
break;
case "due":
case "title":
case "updated":
this.tasks = this.tasks.sort((a, b) =>
a[this.config.ordering] > b[this.config.ordering] ? 1 : -1
);
break;
}
let titleWrapper, dateWrapper, noteWrapper;
this.tasks.forEach((item, index) => {
titleWrapper = document.createElement("div");
titleWrapper.className = "item title";
titleWrapper.innerHTML =
'<i class="fa fa-circle-thin" ></i>' + item.title;
// If item is completed change icon to checkmark
if (item.status === "completed") {
titleWrapper.innerHTML = '<i class="fa fa-check" ></i>' + item.title;
}
if (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) {
let date = moment(item.due);
dateWrapper.innerHTML = date.utc().format(this.config.dateFormat);
}
// Create borders between parent items
if (index < this.tasks.length - 1 && !this.tasks[index + 1].parent) {
titleWrapper.style.borderBottom = "1px solid #666";
dateWrapper.style.borderBottom = "1px solid #666";
}
wrapper.appendChild(titleWrapper);
wrapper.appendChild(dateWrapper);
});
return wrapper;
}
});