-
Notifications
You must be signed in to change notification settings - Fork 4
/
extension.js
340 lines (272 loc) · 9.85 KB
/
extension.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
const St = imports.gi.St;
const GLib = imports.gi.GLib;
const GObject = imports.gi.GObject;
const Clutter = imports.gi.Clutter;
const Main = imports.ui.main;
const Mainloop = imports.mainloop;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const ExtensionUtils = imports.misc.extensionUtils;
const Util = imports.misc.util;
const Gettext = imports.gettext;
const _ = Gettext.gettext;
const Me = ExtensionUtils.getCurrentExtension();
const Convenience = Me.imports.convenience;
const Polyfill = Me.imports.polyfill;
const Todoist = Me.imports.todoist;
const Uuid = Me.imports.uuid;
let TodoistTaskMenuItem = GObject.registerClass(
class TodoistTaskMenuItem extends PopupMenu.PopupBaseMenuItem {
_init(task, projects, on_activation, params) {
super._init(params);
this.actor.add_style_class_name("check-box");
this.actor.add_style_class_name("todoist-indicator-container-task");
let box = new St.Bin();
let label = new St.Label({ text: task.content });
this.actor.add_child(box);
this.actor.add_child(label);
if (projects instanceof Array) {
projects.forEach((project) => {
let label = new St.Label({
text: project.name === "Inbox" ? _("Inbox") : project.name,
style_class: "todoist-indicator-container-task-project-label",
});
if (Todoist.ObjectColors.has(project.color))
label.set_style("color: " + Todoist.ObjectColors.get(project.color) + ";");
this.actor.add_child(label);
}, this);
}
let self = this;
this.connect("activate", function (actor, event, data) {
self.actor.add_style_pseudo_class("checked");
if(on_activation)
on_activation(task, () => self.destroy(), () => self.remove_style_pseudo_class("checked"));
});
}
}
);
const TodoistIndicator = GObject.registerClass(
class TodoistIndicator extends PanelMenu.Button {
_init() {
super._init(0.0, "Todoist Indicator");
// properties init
this._settings = Convenience.getSettings();
this._api = new Todoist.API(this._settings.get_string("api-token"));
this._tasks = [];
this._projects = [];
// label initialization
this.buttonText = new St.Label({
text: _("Loading..."),
y_align: Clutter.ActorAlign.CENTER
});
this.actor.add_actor(this.buttonText);
// layout setup
this._container = new St.BoxLayout({
vertical: true,
x_expand: true,
y_expand: true,
style_class: "todoist-indicator-container"
});
this.menu.box.add(this._container);
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
let refreshButton = new PopupMenu.PopupMenuItem(_("Refresh"));
// refreshButton.actor.set_x_align(Clutter.ActorAlign.CENTER);
refreshButton.connect("activate", this._refresh.bind(this));
this.menu.addMenuItem(refreshButton);
let openTodoistWebButton = new PopupMenu.PopupMenuItem(_("Open todoist.com"));
// openTodoistWebButton.actor.set_x_align(Clutter.ActorAlign.CENTER);
openTodoistWebButton.connect("activate", _openTodoistWeb);
this.menu.addMenuItem(openTodoistWebButton);
// start up
this._refresh();
this._refreshTimer = Mainloop.timeout_add_seconds(this._settings.get_int("refresh-interval"), this._refresh.bind(this));
}
_refresh() {
let apiCallback = function (data) {
if (data == undefined) {
this._renderError(_("Connection error"));
return;
}
this._parseProjects(data.projects);
this._parseTasks(data.items);
this._render();
};
this._api.sync(["items", "projects"], apiCallback.bind(this), function(msg) {
log("sync have failed")
let payload = JSON.parse(msg.response_body.data);
log("response: " + JSON.stringify(payload));
});
return true;
}
// classification helpers
_isDoneOrDeletedOrArchived (item){
return item.checked === true || item.is_deleted === true || item.in_history === true;
}
_isDeletedOrArchived(item) {
return item.is_deleted === true || item.is_archived === true;
}
_isNotDone(item) {
return item.checked === false;
}
_isDueDateToday(item) {
if (item.due === null) return false;
let dueDate = new Date(item.due.date);
dueDate.setHours(0,0,0,0);
let today_min = new Date(), today_max = new Date();
today_min.setHours(0,0,0,0);
today_max.setHours(23,59,59,999);
return (dueDate >= today_min) && (dueDate <= today_max);
}
_isDueDateInPast(item) {
if (item.due === null) return false;
let dueDate = new Date(item.due.date);
dueDate.setHours(0,0,0,0);
let today = new Date();
today.setHours(0,0,0,0);
return dueDate < today;
}
// tasks actions
_closeTask(task, on_success, on_failure) {
let uuid = Uuid.UuidV1()
let commands = [
{
uuid: uuid,
type: "item_close",
args: {
id: task.id
}
}
];
this._api.execute(commands, on_success, data => {
log("close task command failed with error " + data.sync_status[uuid]["error"]);
on_failure();
});
}
// functions doing actual tasks and projects list parsing
_parseTasks(tasks) {
let undoneTasks = tasks.filter(this._isNotDone);
// on init just push undone items
if (this._tasks.length == 0) {
this._tasks = undoneTasks;
return;
}
undoneTasks.forEach(function (item) {
// adds or updates undone items
let index = this._tasks.findIndex(openItem => openItem.id === item.id);
if (index === -1)
this._tasks.splice(this._tasks.length, 0, item);
else
this._tasks[index] = item;
}, this);
let doneTasks = tasks.filter(this._isDoneOrDeletedOrArchived);
doneTasks.forEach(function (item) {
// remove items that are definitely done
let index = this._tasks.findIndex(openItem => openItem.id === item.id);
if (index > -1)
this._tasks.splice(index, 1);
}, this);
}
_parseProjects(projects) {
let activeProjects = projects.filter(item => !this._isDeletedOrArchived(item));
// init, there is always inbox so
if (this._projects.length == 0) {
this._projects = activeProjects;
return;
}
projects.filter(function (item) {
let index = this._tasks.findIndex(existingItem => existingItem.id === item.id);
log("Item " + item.id + " found index " + index);
if (index > -1) {
if (this._isDeletedOrArchived(item))
this._projects.splice(index, 1);
else
this._projects[index] = item;
}
else
this._projects.push(item);
}, this);
}
// rendering functions
_getTextForTaskCount(count) {
switch (count) {
case 0: return _("no due tasks");
default: return Gettext.ngettext("one due task", "%d due tasks", count).format(count);
}
}
_renderTodoLists(pastDueItems, todayItems) {
this._container.destroy_all_children();
if (pastDueItems.length > 0) {
this._container.add(new St.Label({
text: _("PAST DUE"),
style_class: "todoist-indicator-container-section-label",
}));
let pastDueContainer = new St.BoxLayout({
vertical: true,
x_expand: true,
y_expand: true
});
pastDueItems.sort(function (a, b) {
return a.project_id - b.project_id;
}, this);
pastDueItems.forEach(function(item) {
let menuItem = new TodoistTaskMenuItem(item, this._projects.filter(project => project.id === item.project_id), this._closeTask.bind(this));
pastDueContainer.add(menuItem.actor);
}, this);
this._container.add(pastDueContainer);
}
if (todayItems.length > 0) {
this._container.add(new St.Label({
text: _("TODAY"),
style_class: "todoist-indicator-container-section-label"
}));
let todayContainer = new St.BoxLayout({
vertical: true,
x_expand: true,
y_expand: true
});
todayItems.sort(function (a, b) {
return a.day_order - b.day_order;
}, this);
todayItems.forEach(function(item) {
let menuItem = new TodoistTaskMenuItem(item, this._projects.filter( project => project.id === item.project_id), this._closeTask.bind(this));
todayContainer.add(menuItem.actor);
}, this);
this._container.add(todayContainer);
}
}
_render() {
let pastDueItems = this._tasks.filter(this._isDueDateInPast);
let todayItems = this._tasks.filter(this._isDueDateToday);
this.buttonText.set_text(this._getTextForTaskCount(pastDueItems.length));
this._renderTodoLists(pastDueItems, todayItems);
}
_renderError(errorMsg) {
this.menu.box.destroy_all_children();
this.buttonText.set_text(errorMsg);
}
stop() {
if (this._refreshTimer) {
Mainloop.source_remove(this._refreshTimer);
this._refreshTimer = undefined;
}
this.menu.box.destroy_all_children();
this._api.destroy();
}
}
);
function _openTodoistWeb() {
Util.spawn(['xdg-open', 'https://todoist.com/app#agenda%2Foverdue%2C%20today']);
}
let _extensionInstance;
function init() {
Gettext.textdomain("[email protected]");
Gettext.bindtextdomain("[email protected]", Me.dir.get_child("locale").get_path());
}
function enable() {
_extensionInstance = new TodoistIndicator;
Main.panel.addToStatusArea("todoist-indicator", _extensionInstance);
}
function disable() {
_extensionInstance.stop();
_extensionInstance.destroy();
}