todos.mjs 29.6 KB
Newer Older
1
"use strict";
ransome1's avatar
ransome1 committed
2
import "../../node_modules/jstodotxt/jsTodoExtensions.js";
3
import { getActiveFile, userData, appData, handleError, translations, setUserData, startBuilding, getConfirmation, resetModal } from "../render.js";
4
import { _paq } from "./matomo.mjs";
5
6
7
8
import { categories } from "./filters.mjs";
import { generateRecurrence } from "./recurrences.mjs";
import { convertDate, isToday, isTomorrow, isPast } from "./date.mjs";
import { show } from "./form.mjs";
9
import { SugarDueExtension, RecExtension, ThresholdExtension } from "./todotxtExtensions.mjs";
10

11
const modalForm = document.getElementById("modalForm");
12
const todoContext = document.getElementById("todoContext");
13
const todoContextDelete = document.getElementById("todoContextDelete");
14
15
16
const todoContextEdit = document.getElementById("todoContextEdit");
const todoContextUseAsTemplate = document.getElementById("todoContextUseAsTemplate");
const todoTableWrapper = document.getElementById("todoTableWrapper");
17
18
19
20

todoContextUseAsTemplate.innerHTML = translations.useAsTemplate;
todoContextEdit.innerHTML = translations.edit;
todoContextDelete.innerHTML = translations.delete;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// ########################################################################################################################
// CONFIGURE MARKDOWN PARSER
// ########################################################################################################################
marked.setOptions({
  pedantic: false,
  gfm: true,
  breaks: false,
  sanitize: false,
  sanitizer: false,
  smartLists: false,
  smartypants: false,
  xhtml: false,
  baseUrl: "https://"
});
const renderer = {
  link(href, title, text) {
    // truncate the url
    if(text.length > 40) text = text.slice(0, 40) + " [...] ";
39
    return `${text} <a href="${href}" target="_blank"><i class="fas fa-external-link-alt"></i></a>`;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  }
};
marked.use({ renderer });
// ########################################################################################################################
// PREPARE TABLE
// ########################################################################################################################
const tableContainerContent = document.createDocumentFragment();
const todoTableBodyRowTemplate = document.createElement("div");
const todoTableBodyCellCheckboxTemplate  = document.createElement("div");
const todoTableBodyCellTextTemplate = document.createElement("a");
const tableContainerCategoriesTemplate = document.createElement("span");
const todoTableBodyCellPriorityTemplate = document.createElement("div");
const todoTableBodyCellDueDateTemplate = document.createElement("span");
const todoTableBodyCellRecurrenceTemplate = document.createElement("span");
ransome1's avatar
ransome1 committed
54
const todoTableBodyCellArchiveTemplate = document.createElement("span");
ransome1's avatar
ransome1 committed
55
const todoTableBodyCellHiddenTemplate = document.createElement("span");
56
const item = { previous: "" }
57
58
59
let
  items,
  clusterCounter,
60
  clusterSize = Math.ceil(window.innerHeight/30), // 35 being the pixel height of one todo in compact mode
61
  clusterThreshold = 0,
ransome1's avatar
ransome1 committed
62
  //stopBuilding = false,
63
  visibleRows = 0;
64

65
todoTableWrapper.addEventListener("scroll", function(event) {
ransome1's avatar
ransome1 committed
66
67
  if(visibleRows>=items.filtered.length) return false;
  if(Math.floor(event.target.scrollHeight - event.target.scrollTop) <= event.target.clientHeight) {
ransome1's avatar
ransome1 committed
68
    //stopBuilding = false;
69
    startBuilding(true);
70
71
  }
});
72
73
74
todoContext.addEventListener("keyup", function(event) {
  if(event.key==="Escape") this.classList.remove("is-active");
});
75

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
function showResultStats() {
  try {
    // we show some information on filters if any are set
    if(items.filtered.length!=items.objects.length) {
      resultStats.classList.add("is-active");
      resultStats.firstElementChild.innerHTML = translations.visibleTodos + "&nbsp;<strong>" + items.filtered.length + " </strong>&nbsp;" + translations.of + "&nbsp;<strong>" + items.objects.length + "</strong>";
      return Promise.resolve("Info: Result box is shown");
    } else {
      resultStats.classList.remove("is-active");
      return Promise.resolve("Info: Result box is hidden");
    }
  } catch(error) {
    error.functionName = showResultStats.name;
    return Promise.reject(error);
  }
}
92
93
function configureTodoTableTemplate(append) {
  try {
ransome1's avatar
ransome1 committed
94
    // setting up for the first cluster
95
    if(!append) {
ransome1's avatar
ransome1 committed
96
      todoTable.innerHTML = "";
97
98
      visibleRows = 0;
      clusterThreshold = 0;
ransome1's avatar
ransome1 committed
99
      //stopBuilding = false;
100
    }
101
102
103
    todoTableBodyRowTemplate.setAttribute("class", "todo");
    todoTableBodyCellCheckboxTemplate.setAttribute("class", "cell checkbox");
    todoTableBodyCellTextTemplate.setAttribute("class", "cell text");
104
105
106
    todoTableBodyCellTextTemplate.setAttribute("tabindex", 0);
    todoTableBodyCellTextTemplate.setAttribute("href", "#");
    tableContainerCategoriesTemplate.setAttribute("class", "categories");
107
108
    todoTableBodyCellDueDateTemplate.setAttribute("class", "cell itemDueDate");
    todoTableBodyCellRecurrenceTemplate.setAttribute("class", "cell recurrence");
109
110
111
112
113
114
    return Promise.resolve("Success: Table templates set up");
  } catch(error) {
    error.functionName = configureTodoTableTemplate.name;
    return Promise.reject(error);
  }
}
115
116
function generateItems(content) {
  try {
117
    items = { objects: TodoTxt.parse(content, [ new DueExtension(), new HiddenExtension(), new RecExtension(), new ThresholdExtension() ]) }
118
119
120
121
    // items.objects = items.objects.filter(function(item) {
    //   if(!item.text && !item.h) return false;
    //   return true;
    // });
122
123
124
125
126
127
128
129
130
131
    items.complete = items.objects.filter(function(item) { return item.complete === true });
    items.incomplete = items.objects.filter(function(item) { return item.complete === false });
    items.objects = items.objects.filter(function(item) { return item.toString() != "" });
    return Promise.resolve(items);
  } catch(error) {
    error.functionName = generateItems.name;
    return Promise.reject(error);
  }
}
function generateGroups(items) {
ransome1's avatar
ransome1 committed
132
  const sortBy = userData.sortBy[0];
133
134
135
136
  // build object according to sorting method
  items = items.reduce((object, a) => {
    if(userData.sortCompletedLast && a.complete) {
      object["completed"] = [...object["completed"] || [], a];
ransome1's avatar
ransome1 committed
137
    } else if(sortBy==="dueString" && !a.due) {
138
139
      object["noDueDate"] = [...object["noDueDate"] || [], a];
    } else {
ransome1's avatar
ransome1 committed
140
      object[a[sortBy]] = [...object[a[sortBy]] || [], a];
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
    }
    return object;
  }, {});
  // object is converted to a sorted array
  items = Object.entries(items).sort(function(a,b) {
    // when a is null sort it after b
    if(a[0]==="null") return 1;
    // when b is null sort it after a
    if(b[0]==="null") return -1;
    // sort alphabetically
    if(a < b) return -1;
  });
  //
  if(userData.sortCompletedLast) {
    items.sort(function(a,b) {
      // when a is null sort it after b
      if(a[0]==="completed") return 1;
      // when b is null sort it after a
      if(b[0]==="completed") return -1;
      return 0;
    });
  }
163
164
165
166
  // sort the items within the groups
  items.forEach((group) => {
    group[1] = sortTodoData(group[1]);
  });
167
168
  return Promise.resolve(items)
}
ransome1's avatar
ransome1 committed
169
async function generateTable(groups, append, loadAll) {
ransome1's avatar
ransome1 committed
170
  try {
171
172
    // configure stats
    showResultStats();
ransome1's avatar
ransome1 committed
173
174
175
    // prepare the templates for the table
    await configureTodoTableTemplate(append);
    // reset cluster count for this run
176
    clusterCounter = 0;
177
    for (let group in groups) {
ransome1's avatar
ransome1 committed
178
179
180
181
      // if(stopBuilding) {
      //   stopBuilding = false;
      //   break;
      // }
182
      // create a divider row
183
      let dividerRow;
184
185
      // completed todos
      if(userData.sortCompletedLast && groups[group][0]==="completed") {
ransome1's avatar
ransome1 committed
186
        dividerRow = document.createRange().createContextualFragment("<div id=\"" + userData.sortBy[0] + groups[group][0] + "\" class=\"group " + userData.sortBy[0] + " " + groups[group][0] + "\"><div class=\"cell\"></div></div>")
187
      // for priority, context and project
ransome1's avatar
ransome1 committed
188
      } else if(groups[group][0]!="null" && userData.sortBy[0]!="dueString") {
ransome1's avatar
ransome1 committed
189
        dividerRow = document.createRange().createContextualFragment("<div id=\"" + userData.sortBy[0] + groups[group][0] + "\" class=\"group " + userData.sortBy[0] + " " + groups[group][0] + "\"><div class=\"cell\"><button tabindex=\"-1\" class=\"" + groups[group][0] + "\">" + groups[group][0].replace(/,/g, ', ') + "</button></div></div>")
190
      // if sorting is by due date
ransome1's avatar
ransome1 committed
191
      } else if(userData.sortBy[0]==="dueString" && groups[group][1][0].due) {
192
        if(isToday(groups[group][1][0].due)) {
ransome1's avatar
ransome1 committed
193
          dividerRow= document.createRange().createContextualFragment("<div id=\"" + userData.sortBy[0] + groups[group][0] + "\" class=\"group due\"><div class=\"cell isToday\">" + translations.today + "</button></div></div>")
194
        } else if(isTomorrow(groups[group][1][0].due)) {
ransome1's avatar
ransome1 committed
195
          dividerRow = document.createRange().createContextualFragment("<div id=\"" + userData.sortBy[0] + groups[group][0] + "\" class=\"group due\"><div class=\"cell isTomorrow\">" + translations.tomorrow + "</button></div></div>")
196
        } else if(isPast(groups[group][1][0].due)) {
ransome1's avatar
ransome1 committed
197
          dividerRow = document.createRange().createContextualFragment("<div id=\"" + userData.sortBy[0] + groups[group][0] + "\" class=\"group due\"><div class=\"cell isPast\">" + groups[group][0] + "</button></div></div>")
198
        } else {
ransome1's avatar
ransome1 committed
199
          dividerRow = document.createRange().createContextualFragment("<div id=\"" + userData.sortBy[0] + groups[group][0] + "\" class=\"group due\"><div class=\"cell\">" + groups[group][0] + "</div></div>")
200
201
        }
      // create an empty divider row
202
      } else {
203
        dividerRow = document.createRange().createContextualFragment("<div class=\"group\"></div>")
204
      }
205
      // add divider row only if it doesn't exist yet
ransome1's avatar
ransome1 committed
206
      if(!append && !document.getElementById(userData.sortBy[0] + groups[group][0]) && dividerRow) tableContainerContent.appendChild(dividerRow);
207
      for (let item in groups[group][1]) {
208
        let todo = groups[group][1][item];
ransome1's avatar
ransome1 committed
209
210
        //TODO: Explain this, maybe refactor
        if(!loadAll && clusterCounter<clusterThreshold) {
ransome1's avatar
ransome1 committed
211
212
          clusterCounter++;
          continue;
ransome1's avatar
ransome1 committed
213
        } else if(!loadAll && (visibleRows===clusterSize+clusterThreshold) || visibleRows===items.filtered.length ) {
ransome1's avatar
ransome1 committed
214
          clusterThreshold = visibleRows;
ransome1's avatar
ransome1 committed
215
          //stopBuilding = true;
ransome1's avatar
ransome1 committed
216
217
          break;
        }
218
        // if this todo is not a recurring one the rec value will be set to null
219
        if(!todo.rec) todo.rec = null;
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
        // incompleted todos with due date
        if (todo.due && !todo.complete) {
          // create notification
          if(isToday(todo.due)) {
            generateNotification(todo, 0).then(response => {
              console.log(response);
            }).catch(error => {
              handleError(error);
            });
          } else if(isTomorrow(todo.due)) {
            generateNotification(todo, 1).then(response => {
              console.log(response);
            }).catch(error => {
              handleError(error);
            });
          }
        }
        tableContainerContent.appendChild(generateTableRow(todo));
      }
    }
ransome1's avatar
ransome1 committed
240
    todoTable.appendChild(tableContainerContent);
ransome1's avatar
ransome1 committed
241
242
243
244
245
    return Promise.resolve("Success: Todo table generated");
  } catch(error) {
    error.functionName = archiveTodos.name;
    return Promise.reject(error);
  }
246
247
248
}
function generateTableRow(todo) {
  try {
249
    clusterCounter++;
250
251
252
253
254
255
256
257
258
    visibleRows++;
    // create nodes from templates
    let todoTableBodyRow = todoTableBodyRowTemplate.cloneNode(true);
    let todoTableBodyCellCheckbox = todoTableBodyCellCheckboxTemplate.cloneNode(true);
    let todoTableBodyCellText = todoTableBodyCellTextTemplate.cloneNode(true);
    let tableContainerCategories = tableContainerCategoriesTemplate.cloneNode(true);
    let todoTableBodyCellPriority = todoTableBodyCellPriorityTemplate.cloneNode(true);
    let todoTableBodyCellDueDate = todoTableBodyCellDueDateTemplate.cloneNode(true);
    let todoTableBodyCellRecurrence = todoTableBodyCellRecurrenceTemplate.cloneNode(true);
ransome1's avatar
ransome1 committed
259
    let todoTableBodyCellArchive = todoTableBodyCellArchiveTemplate.cloneNode(true);
ransome1's avatar
ransome1 committed
260
    let todoTableBodyCellHidden = todoTableBodyCellHiddenTemplate.cloneNode(true);
261
262
263
264
265
266
267
    // if new item was saved, row is being marked
    if(todo.toString()==item.previous) {
      todoTableBodyRow.setAttribute("id", "previousItem");
      item.previous = null;
    }
    // start with the individual config of the items
    if(todo.complete==true) {
268
      todoTableBodyRow.setAttribute("class", "todo completed");
269
270
271
    }
    todoTableBodyRow.setAttribute("data-item", todo.toString());
    // add the priority marker or a white spacer
ransome1's avatar
ransome1 committed
272
    if(todo.priority && userData.sortBy[0]==="priority") {
273
      todoTableBodyCellPriority.setAttribute("class", "cell priority " + todo.priority);
274
      todoTableBodyRow.appendChild(todoTableBodyCellPriority);
ransome1's avatar
CI AUR    
ransome1 committed
275
    }
276
277
278
279
280
281
282
283
284
285
286
    // add the checkbox
    if(todo.complete==true) {
      todoTableBodyCellCheckbox.setAttribute("title", translations.inProgress);
      todoTableBodyCellCheckbox.innerHTML = "<a href=\"#\"><i class=\"fas fa-check-circle\"></i></a>";
    } else {
      todoTableBodyCellCheckbox.setAttribute("title", translations.done);
      todoTableBodyCellCheckbox.innerHTML = "<a href=\"#\"><i class=\"far fa-circle\"></i></a>";
    }
    // add a listener on the checkbox to call the completeItem function
    todoTableBodyCellCheckbox.onclick = function() {
      // passing the data-item attribute of the parent tag to complete function
287
      setTodoComplete(this.parentElement.getAttribute("data-item")).then(response => {
288
289
290
291
292
         console.log(response);
      }).catch(error => {
        handleError(error);
      });
      // trigger matomo event
293
      if(userData.matomoEvents) _paq.push(["trackEvent", "Todo-Table", "Click on Checkbox"]);
294
295
    }
    todoTableBodyRow.appendChild(todoTableBodyCellCheckbox);
ransome1's avatar
ransome1 committed
296
297
298
    // add archiving icon
    if(todo.complete) {
      todoTableBodyCellArchive.setAttribute("class", "cell archive");
ransome1's avatar
ransome1 committed
299
      todoTableBodyCellArchive.innerHTML = "<a href=\"#\"><i class=\"fas fa-archive\"></i></a>";
ransome1's avatar
ransome1 committed
300
301
302
303
304
305
306
307
      todoTableBodyCellArchive.onclick = function() {
        getConfirmation(archiveTodos, translations.archivingPrompt);
        // trigger matomo event
        if(userData.matomoEvents) _paq.push(["trackEvent", "Todo-Table", "Click on Archive button"]);
      }
      // append the due date to the text item
      todoTableBodyRow.appendChild(todoTableBodyCellArchive);
    }
ransome1's avatar
ransome1 committed
308
309
310
311
312
313
314
315
    // add hidden icon
    if(todo.h) {
      todoTableBodyRow.setAttribute("class", "todo is-greyed-out");
      todoTableBodyCellHidden.setAttribute("class", "cell");
      todoTableBodyCellHidden.innerHTML = "<i class=\"far fa-eye-slash\"></i>";
      // append the due date to the text item
      todoTableBodyRow.appendChild(todoTableBodyCellHidden);
    }
316
317
    // creates cell for the text
    if(todo.text) {
ransome1's avatar
ransome1 committed
318
      if(todo.priority && userData.sortBy[0]!="priority") todoTableBodyCellText.innerHTML = "<span class=\"priority\"><button class=\"" + todo.priority + "\">" + todo.priority + "</button></span>";
319
      // parse text string through markdown parser
320
      todoTableBodyCellText.innerHTML +=  "<span class=\"text\">" + marked.parseInline(todo.text) + "</span>";
321
      // replace line feed character with a space
322
323
324
325
      todoTableBodyCellText.innerHTML = todoTableBodyCellText.innerHTML.replaceAll(String.fromCharCode(16)," ");
      // add a spacer to divide text (and link) and categories
      todoTableBodyCellText.innerHTML += " ";
    }
326
    // click on the text
327
328
329
    todoTableBodyCellText.onclick = function() {
      // if the clicked item is not the external link icon, show(true) will be called
      if(!event.target.classList.contains('fa-external-link-alt')) {
330
        show(this.parentElement.getAttribute("data-item"));
331
        // trigger matomo event
332
        if(userData.matomoEvents) _paq.push(["trackEvent", "Todo-Table", "Click on Todo item"]);
333
334
      }
    }
335
336
337
    // cell for the categories
    categories.forEach(category => {
      if(todo[category] && category!="priority") {
ransome1's avatar
ransome1 committed
338
        todo[category].forEach(element => {
339
340
          let todoTableBodyCellCategory = document.createElement("span");
          todoTableBodyCellCategory.setAttribute("class", "tag " + category);
ransome1's avatar
ransome1 committed
341
          todoTableBodyCellCategory.innerHTML = element;
342
343
344
345
346
347
          tableContainerCategories.appendChild(todoTableBodyCellCategory);
        });
      }
    });
    // only add the categories to text cell if it has child nodes
    if(tableContainerCategories.hasChildNodes()) todoTableBodyCellText.appendChild(tableContainerCategories);
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
    // check for and add a given due date
    if(todo.due) {
      var tag = convertDate(todo.due);
      if(isToday(todo.due)) {
        todoTableBodyCellDueDate.classList.add("isToday");
        tag = translations.today;
      } else if(isTomorrow(todo.due)) {
        todoTableBodyCellDueDate.classList.add("isTomorrow");
        tag = translations.tomorrow;
      } else if(isPast(todo.due)) {
        todoTableBodyCellDueDate.classList.add("isPast");
      }
      todoTableBodyCellDueDate.innerHTML = `
        <i class="far fa-clock"></i>
        <div class="tags has-addons">
          <span class="tag">` + translations.due + `</span><span class="tag is-dark">` + tag + `</span>
        </div>
ransome1's avatar
ransome1 committed
365
        <i class="fas fa-sort-down"></i>`;
366
367
368
369
370
371
372
373
374
375
376
      // append the due date to the text item
      todoTableBodyCellText.appendChild(todoTableBodyCellDueDate);
    }
    // add recurrence icon
    if(todo.rec) {
      todoTableBodyCellRecurrence.innerHTML = "<i class=\"fas fa-redo\"></i>";
      // append the due date to the text item
      todoTableBodyCellText.appendChild(todoTableBodyCellRecurrence);
    }
    // add the text cell to the row
    todoTableBodyRow.appendChild(todoTableBodyCellText);
377
    todoTableBodyRow.addEventListener("contextmenu", event => {
ransome1's avatar
ransome1 committed
378
      //todoContextUseAsTemplate.focus();
379
380
381
382
383
      todoContext.style.left = event.x + "px";
      todoContext.style.top = event.y + "px";
      todoContext.classList.toggle("is-active");
      todoContext.setAttribute("data-item", todo.toString())
      // click on use as template option
ransome1's avatar
ransome1 committed
384
385
      todoContextUseAsTemplate.onclick = function() {
        show(todoContext.getAttribute('data-item'), true);
386
387
388
389
390
        todoContext.classList.toggle("is-active");
        todoContext.removeAttribute("data-item");
        // trigger matomo event
        if(userData.matomoEvents) _paq.push(["trackEvent", "Todo-Table-Context", "Click on Use as template"]);
      }
ransome1's avatar
ransome1 committed
391
392
      todoContextEdit.onclick = function() {
        show(todoContext.getAttribute('data-item'));
393
394
395
396
397
398
        todoContext.classList.toggle("is-active");
        todoContext.removeAttribute("data-item");
        // trigger matomo event
        if(userData.matomoEvents) _paq.push(["trackEvent", "Todo-Table-Context", "Click on Edit"]);
      }
      // click on delete
ransome1's avatar
ransome1 committed
399
      todoContextDelete.onclick = function() {
400
        // passing the data-item attribute of the parent tag to complete function
ransome1's avatar
ransome1 committed
401
        setTodoDelete(todoContext.getAttribute('data-item')).then(response => {
402
403
404
405
406
          console.log(response);
          todoContext.classList.toggle("is-active");
          todoContext.removeAttribute("data-item");
        }).catch(error => {
          handleError(error);
407
408
        });
        // trigger matomo event
409
        if(userData.matomoEvents) _paq.push(["trackEvent", "Todo-Table-Context", "Click on Delete"]);
410
      }
411
    });
412
413
414
415
416
417
418
419
420
    // return the fully built row
    return todoTableBodyRow;
  } catch(error) {
    error.functionName = generateTableRow.name;
    return Promise.reject(error);
  }
}
function sortTodoData(group) {
  try {
ransome1's avatar
ransome1 committed
421
    for(let i = 1; i < userData.sortBy.length; i++) {
ransome1's avatar
ransome1 committed
422
423
      group.sort(function(a, b) {
        // only continue if the two items have the same filters from the previous iteration
ransome1's avatar
ransome1 committed
424
425
        if(i>1 && JSON.stringify(a[userData.sortBy[i-2]]) !== JSON.stringify(b[userData.sortBy[i-2]]) ) return;
        if(i>1 &&  JSON.stringify(a[userData.sortBy[i-1]]) !== JSON.stringify(b[userData.sortBy[i-1]]) ) return;
ransome1's avatar
ransome1 committed
426
        let
ransome1's avatar
ransome1 committed
427
428
          item1 = a[userData.sortBy[i]],
          item2 = b[userData.sortBy[i]];
ransome1's avatar
ransome1 committed
429
430
431
432
433
434
435
436
437
438
439
        // when item1 is empty or bigger than item2 it will be sorted after item2
        if(!item1 && item2 || item1 > item2) {
          return 1;
        // when item2 is empty or bigger than item1, item 1 will be sorted before item2
        } else if(item1 && !item2 || item1 < item2) {
          return -1;
        }
        // no change to sorting
        return;
      });
    }
440
441
442
443
444
445
446
447
448
    return group;
  } catch(error) {
    error.functionName = sortTodoData.name;
    return Promise.reject(error);
  }
}
function setTodoComplete(todo) {
  try {
    // first convert the string to a todo.txt object
449
    todo = new TodoTxtItem(todo, [ new DueExtension(), new HiddenExtension(), new RecExtension(), new ThresholdExtension() ]);
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
    // get index of todo
    const index = items.objects.map(function(item) {return item.toString(); }).indexOf(todo.toString());
    // mark item as in progress
    if(todo.complete) {
      // if item was already completed we set complete to false and the date to null
      todo.complete = false;
      todo.completed = null;
      // delete old item from array and add the new one at it's position
      items.objects.splice(index, 1, todo);
    // Mark item as complete
    } else if(!todo.complete) {
      if(todo.due) {
        const date = convertDate(todo.due);
        // if set to complete it will be removed from persisted notifcations
        if(userData.dismissedNotifications) {
          // the one set for today
          userData.dismissedNotifications = userData.dismissedNotifications.filter(e => e !== generateHash(date + todo.text)+0);
          // the one set for tomorrow
          userData.dismissedNotifications = userData.dismissedNotifications.filter(e => e !== generateHash(date + todo.text)+1);
          setUserData("dismissedNotifications", userData.dismissedNotifications);
        }
      }
      todo.complete = true;
      todo.completed = new Date();
      // delete old todo from array and add the new one at it's position
      items.objects.splice(index, 1, todo);
      // if recurrence is set start generating the recurring todo
      if(todo.rec) generateRecurrence(todo)
    }
    //write the data to the file
480
481
    window.api.send("writeToFile", [items.objects.join("\n").toString() + "\n"]);
    return Promise.resolve("Success: Changes written to file: " + getActiveFile());
482
483
484
485
486
487
488
489
  } catch(error) {
    error.functionName = setTodoComplete.name;
    return Promise.reject(error);
  }
}
function setTodoDelete(todo) {
  try {
    // in case edit form is open, text has changed and complete button is pressed, we do not fall back to the initial value of todo but instead choose input value
ransome1's avatar
ransome1 committed
490
    if(document.getElementById("modalFormInput").value) todo = document.getElementById("modalFormInput").value;
491
    // first convert the string to a todo.txt object
492
    todo = new TodoTxtItem(todo, [ new DueExtension(), new HiddenExtension(), new RecExtension(), new ThresholdExtension() ]);
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
    // get index of todo
    const index = items.objects.map(function(item) {return item.toString(); }).indexOf(todo.toString());
    // Delete item
    if(todo.due) {
      var date = convertDate(todo.due);
      // if deleted it will be removed from persisted notifcations
      if(userData.dismissedNotifications) {
        // the one set for today
        userData.dismissedNotifications = userData.dismissedNotifications.filter(e => e !== generateHash(date + todo.text)+0);
        // the one set for tomorrow
        userData.dismissedNotifications = userData.dismissedNotifications.filter(e => e !== generateHash(date + todo.text)+1);
        setUserData("dismissedNotifications", userData.dismissedNotifications);
      }
    }
    items.objects.splice(index, 1);
    //write the data to the file
509
510
    window.api.send("writeToFile", [items.objects.join("\n").toString() + "\n"]);
    return Promise.resolve("Success: Changes written to file: " + getActiveFile());
511
512
513
514
515
  } catch(error) {
    error.functionName = setTodoDelete.name;
    return Promise.reject(error);
  }
}
ransome1's avatar
ransome1 committed
516
517
function addTodo(todo) {
  try {
518
    todo = new TodoTxtItem(todo, [ new SugarDueExtension(), new HiddenExtension(), new RecExtension(), new ThresholdExtension() ]);
ransome1's avatar
ransome1 committed
519
    // abort if there is no text
520
    if(!todo.text && !todo.h) return Promise.resolve("Info: Text is missing, no todo is written");
ransome1's avatar
ransome1 committed
521
522
523
524
525
526
527
528
529
    // we add the current date to the start date attribute of the todo.txt object
    todo.date = new Date();
    // get index of todo
    const index = items.objects.map(function(item) { return item.toString(); }).indexOf(todo.toString());
    if(index===-1) {
      // we build the array
      items.objects.push(todo);
      //write the data to the file
      // a newline character is added to prevent other todo.txt apps to append new todos to the last line
530
531
      window.api.send("writeToFile", [items.objects.join("\n").toString() + "\n"]);
      return Promise.resolve("Success: New todo added to file: " + getActiveFile());
ransome1's avatar
ransome1 committed
532
533
534
535
536
537
538
    } else {
      return Promise.resolve("Info: Todo already in file, nothing will be written");
    }
  } catch (error) {
    return Promise.reject(error);
  }
}
539
async function archiveTodos() {
540
  try {
541
542
    const index = userData.files.findIndex(file => file[0]===1);
    const file = userData.files[index][1];
543
544
545
    // cancel operation if there are no completed todos
    if(items.complete.length===0) return Promise.resolve("Info: No completed todos found, nothing will be archived")
    // if user archives within done.txt file, operating is canceled
546
    if(file.includes("_done.")) return Promise.resolve("Info: Current file seems to be a done.txt file, won't archive")
547
    // define path to done.txt
548
549
    let doneFile = function() {
      if(appData.os==="windows") {
550
        return file.replace(file.split("\\").pop(), file.substr(0, file.lastIndexOf(".")).split("\\").pop() + "_done.txt");
551
      } else {
552
        return file.replace(file.split("/").pop(), file.substr(0, file.lastIndexOf(".")).split("/").pop() + "_done.txt");
553
      }
554
    }
555
    const getContentFromDoneFile = new Promise(function(resolve) {
556
557
558
559
      window.api.send("getContent", doneFile());
      return window.api.receive("getContent", (content) => {
        resolve(content);
      });
560
    });
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
    let contentFromDoneFile = await getContentFromDoneFile;
    let contentForDoneFile = items.complete;
    if(contentFromDoneFile) {
      // create array from done file
      contentFromDoneFile = contentFromDoneFile.split("\n");
      //combine the two arrays
      contentForDoneFile  = contentFromDoneFile.concat(items.complete.toString().split(","));
      // use Set function to remove the duplicates: https://www.javascripttutorial.net/array/javascript-remove-duplicates-from-array/
      contentForDoneFile= [...new Set(contentForDoneFile)];
      // remove empty entries
      contentForDoneFile = contentForDoneFile.filter(function(element) {
        return element;
      });
    }
    //write completed items to done file
    window.api.send("writeToFile", [contentForDoneFile.join("\n").toString() + "\n", doneFile()]);
    // write incompleted items to todo file
578
    window.api.send("writeToFile", [items.incomplete.join("\n").toString() + "\n", file]);
579
580
581
    // send notifcation on success
    generateNotification(null, null, translations.archivingCompletedTitle, translations.archivingCompletedBody + doneFile());

582
    return Promise.resolve("Success: Completed todos appended to: " + doneFile())
583
584
585
586
587
  } catch(error) {
    error.functionName = archiveTodos.name;
    return Promise.reject(error);
  }
}
588
function generateNotification(todo, offset, customTitle, customBody) {
589
590
  try {
    // abort if user didn't permit notifications within sleek
591
    if(!userData.notifications) return Promise.resolve("Info: Notification surpressed (turned off in sleek's settings)");
592
    // check for necessary permissions
593
    return navigator.permissions.query({name: "notifications"}).then(function(result) {
594
595
      // abort if user didn't permit notifications
      if(result.state!="granted") return Promise.resolve("Info: Notification surpressed (not permitted by OS)");
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
      let notification;
      if(todo) {
        // add the offset so a notification shown today with "due tomorrow", will be shown again tomorrow but with "due today"
        const hash = generateHash(todo.toString()) + offset;
        let title;
        switch (offset) {
          case 0:
            title = translations.dueToday;
            break;
          case 1:
            title = translations.dueTomorrow;
            break;
        }
        // if notification already has been triggered once it will be discarded
        if(userData.dismissedNotifications.includes(hash)) return Promise.resolve("Info: Notification skipped (has already been sent)");
        // set options for notifcation
        notification = {
          title: title,
          body: todo.text,
          string: todo.toString(),
          timeoutType: "never",
          silent: false,
          actions: [{
            type: "button",
            text: "Show Button"
          }]
        }
        // once shown, it will be persisted as hash to it won't be shown a second time
        userData.dismissedNotifications.push(hash);
        setUserData("dismissedNotifications", userData.dismissedNotifications);
      } else {
        notification = {
          title: customTitle,
          body: customBody,
          timeoutType: "default",
          silent: true
        }
633
634
      }
      // send notification object to main process for execution
635
      window.api.send("showNotification", notification);
636
      // trigger matomo event
637
      if(userData.matomoEvents) _paq.push(["trackEvent", "Notification", "Shown"]);
638
639
640
641
642
643
644
      return Promise.resolve("Info: Notification successfully sent");
    });
  } catch(error) {
    error.functionName = generateNotification.name;
    return Promise.reject(error);
  }
}
645
646
function generateHash(string) {
  return string.split('').reduce((prevHash, currVal) =>
647
648
649
    (((prevHash << 5) - prevHash) + currVal.charCodeAt(0))|0, 0);
}

ransome1's avatar
ransome1 committed
650
export { generateItems, generateGroups, generateTable, items, item, visibleRows, setTodoComplete, archiveTodos, addTodo };