-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
87 lines (68 loc) · 2.07 KB
/
popup.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
let changeColor = document.getElementById('changeColor');
let addButton = document.getElementById('addButton');
let inputElement = document.getElementById('inputElement');
let showTask = document.getElementById('showTask');
var text, todos=[], complete=[];
let images;
chrome.storage.sync.get('todos', function(data) {
if (data.todos === null) {todos = ['New Tasks'];}
else{todos = data.todos;}
done = data.done;
refreshPage();
});
function refreshPage(){
text = "";
for (i = 0; i < todos.length; i++) {
appendToView(todos[i], i, false);
}
showTask.innerHTML = text;
refreshImages();
}
function refreshImages(){
deleteIcons = document.querySelectorAll('.markDelete');
doneIcons = document.querySelectorAll('.markDone');
deleteIcons.forEach(el => el.addEventListener('click', event => {
triggerDelete(event.target.alt);
}));
doneIcons.forEach(el => el.addEventListener('click', event => {
triggerDone(event.target.alt);
}));
}
// todos value set to todo array
addButton.onclick = function(element) {
let value = inputElement.value;
inputElement.value = "";
updateTasks(value);
}
function updateTasks(value){
todos.push(value);
chrome.storage.sync.set({todos:todos}, function() {
appendToView(value, todos.length-1);
refreshImages();
});
}
function appendToView(value, index, updateView=true){
text += "<div class='task'>";
text += "<p>" + value + "</p>";
text += "<div class='right'>";
text += "<img class='markDone' src='images/anydo.svg' alt='"+ index +"'>";
text += "<img class='markDelete' src='images/delete.svg' alt='"+ index +"'></div>";
text += "</div>";
if (updateView) {showTask.innerHTML = text;}
}
function triggerDelete(index) {
if (todos.length>0){
todos.splice(index, 1);
chrome.storage.sync.set({todos:todos}, function() {
refreshPage();
});
}
}
function triggerDone(index) {
const value = todos[index];
triggerDelete(index);
done.push(value);
chrome.storage.sync.set({done:done}, function() {
console.log("added value" + value + "to Done");
});
}