forked from MikeSandford/MMM-AnyDo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-AnyDo.js
234 lines (201 loc) · 6.64 KB
/
MMM-AnyDo.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
"use strict";
/* global Module */
/* Magic Mirror
* Module: MMM-AnyDo
* Adapted By: Mike Sandford
* Adapted By: Dave Richer <davericher@gmail.com>
* Inspired by MMM-Wunderlist Paul-Vincent Roll http://paulvincentroll.com
* MIT Licensed.
*/
Module.register("MMM-AnyDo", {
defaults: {
maximumEntries: 10,
order: "normal",
lists: ["inbox"],
interval: 60,
fade: true,
fadePoint: 0.25,
allBrightTitles: false,
relativeDates: true,
showDeadline: true,
showAssignee: true,
showBullets: false,
// left, right, inline_left, inline_right, none
iconPosition: "left",
spaced: false
},
// Override socket notification handler.
socketNotificationReceived: function(notification, payload) {
switch (notification) {
case 'TASKS':
this.tasks = payload
this.updateDom(3000);
break;
case 'STARTED':
this.sendSocketNotification("addLists", this.config.lists);
if (!this.config.showAssignee)
break;
this.started = true;
this.sendSocketNotification("getUsers");
break;
case 'USERS':
this.users = payload;
if (this.tasks && this.tasks.length > 0)
this.updateDom(3000);
break;
}
},
start: function() {
this.tasks = [];
this.sendSocketNotification("CONFIG", this.config);
this.sendSocketNotification("CONNECTED");
Log.info("Starting module: " + this.name);
},
getTodos: function() {
var tasks = [];
this.config.lists.forEach((listValue, listKey) => {
let list = this.tasks[listValue];
if (list && list.length)
list.forEach(todo => {
if (this.config.order === 'reversed') {
tasks.push(todo);
} else {
tasks.unshift(todo)
}
});
}
);
return tasks;
},
getScripts: function() {
return ['String.format.js'];
},
getStyles: function() {
return ['font-awesome.css', 'MMM-Wunderlist-Enhanced.css'];
},
html: {
table: '<tbody>{0}</tbody>',
titleRow: '<tr><th colspan="{0}"><header class="module-header"><i class="fa fa-list-ul fa-fw"></i> {1}</header></th></tr>',
row: '<tr class="{0}">{1}</tr>',
tdAssignee: '<td class="light">{0}</td>',
tdDeadline: '<td class="light">{0}</td>',
tdBullet: '<td>{0}</td>',
tdContent: '<td class="title {0}">{1}</td>',
star: '<i class="fa fa-star fa-fw" aria-hidden="true"></i>',
bullet_left: '<i class="fa fa-chevron-right fa-fw" aria-hidden="true"></i>',
bullet_right: '<i class="fa fa-chevron-left fa-fw" aria-hidden="true"></i>',
bullet_none: '<i class="fa-fw" aria-hidden="true"></i>',
assignee: '<div class="assignee">{0}</div>'
},
getTitleRow: function(title) {
var columncount = 1;
if (this.config.showAssignee) columncount += 1;
if (this.config.showDeadline) columncount += 1;
if (this.config.iconPosition != "inline") columncount += 1;
return this.html.titleRow.format(columncount, title);
},
getBullet: function(starred) {
if (starred)
return this.html.star;
if (!this.config.showBullets)
return this.html.bullet_none;
if (this.config.iconPosition == "right" || this.config.iconPosition == "inline_right")
return this.html.bullet_right;
return this.html.bullet_left;
},
getRow: function(todo) {
var self = this;
var useTitle = todo.title;
if (self.config.iconPosition == "inline_left") {
useTitle = self.getBullet(todo.starred) + useTitle;
} else if (self.config.iconPosition == "inline_right") {
useTitle += self.getBullet(todo.starred);
}
var tds = self.html.tdContent.format(todo.starred || self.config.allBrightTitles ? 'bright' : 'normal', useTitle);
if (self.config.iconPosition == "right" || self.config.iconPosition == "left") {
var bulletTd = self.html.tdBullet.format(self.getBullet(todo.starred));
if (self.config.iconPosition == "left") {
tds = bulletTd + tds;
} else {
tds = tds + bulletTd;
}
}
if (self.config.showAssignee) {
tds += self.html.tdAssignee.format(todo.assignee_id && self.users
? self.html.assignee.format(self.users[todo.assignee_id])
: '');
}
if (self.config.showDeadline) {
if (self.config.relativeDates && todo.due_date) {
var now = moment();
var todoDate = moment(todo.due_date);
var relativeDate = '';
if (todoDate.isSame(now, 'day')) {
relativeDate = this.capFirst(this.translate("TODAY"));
} else if (todoDate.isSame(moment().add(1, 'days'), 'day')) {
relativeDate = this.capFirst(this.translate("TOMORROW"));
} else {
relativeDate = this.capFirst(moment(todoDate, "x").fromNow());
}
tds += self.html.tdDeadline.format(relativeDate);
} else {
tds += self.html.tdDeadline.format(todo.due_date
? todo.due_date
: '');
}
}
return self.html.row.format("", tds);
},
capFirst: function (string) {
return string.charAt(0).toUpperCase() + string.slice(1);
},
getDom: function() {
if (this.config.showAssignee) {
this.sendSocketNotification("getUsers");
}
var self = this;
var wrapper = document.createElement("table");
wrapper.className = "normal small wunderlist";
if (self.config.spaced) {
wrapper.className += " spaced";
}
var todos = this.getTodos();
var rows = [];
var titleRows = [];
todos.forEach(function(todo, i) {
// Generate Title Rows
if (!titleRows.includes(todo.listFrom))
titleRows.push(todo.listFrom);
const titleRowValue = titleRows.findIndex(x => x === todo.listFrom);
// Generate Rows
if (!rows[titleRowValue])
rows[titleRowValue] = [];
rows[titleRowValue].push(self.getRow(todo));
// Create fade effect
if (self.config.fade && self.config.fadePoint < 1) {
if (self.config.fadePoint < 0) {
self.config.fadePoint = 0;
}
var startingPoint = todos.length * self.config.fadePoint;
if (i >= startingPoint) {
wrapper.style.opacity = 1 - (1 / todos.length - startingPoint * (i - startingPoint));
}
}
});
const generateRows = () => {
var results = [];
titleRows.forEach((key, value) => {
results.push(self.getTitleRow(key));
let count = 0;
rows[value].forEach((rowValue, rowKey) => {
if (count < this.config.maximumEntries)
results.push(rowValue);
count = count + 1;
});
});
return results.join('');
}
wrapper.innerHTML = this.html.table.format(generateRows())
return wrapper;
}
});