-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTasklist.js
More file actions
207 lines (194 loc) · 6.06 KB
/
Copy pathTasklist.js
File metadata and controls
207 lines (194 loc) · 6.06 KB
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
/**
* FILE NAME: Tasklist.js
* WRITTEN BY: Alina Zheng
* DATE: 5/1/20
* PURPOSE: CS204 Taskmin
*/
class TaskList {
/**
* Constructor for objects of class TaskList
* @param {String} key name of localStorage key to save tasklist under
*/
constructor(key) {
this.arr = [];
this.key = key;
}
/**
* Pushes task object onto array
* @param {Task} task the task object to push
*/
push(task) {
task.setId(this.arr.length);
this.arr.push(task);
}
/**
* Creates new task object from task description object,
* pushes it onto internal array, adds it to the DOM
* @param description task description object
*/
addTask(description) {
var task = new Task(description);
this.push(task);
task.addToDom();
}
/**
* Takes array of task description objects, invokes addTask for each of them
* @param descriptions array of task description objects
*/
addTasks(descriptions) {
for (var i = 0; i < descriptions.length; i++) {
this.addTask(descriptions[i]);
}
}
/**
* Searches array for a task of the given id, returns the task
* @param id the given id
* @return the task, or null if the task is not in the array
*/
findTask(id) {
var task = null;
for (var i = 0; i < this.arr.length; i++) {
if (this.arr[i].id == id) {
task = this.arr[i];
}
}
return task;
}
/**
* Finds the index of a task given its id,
* deletes task from the array
*/
deleteTask(id) {
var task = this.findTask(id);
if (task == null) {
return -1;
}
var index = this.arr.findIndex(function(tsk) {return tsk == task});
this.arr.splice(index, 1);
return task;
}
/**
* Creates an array of description dictionaries from an array of task objects
* JSON.stringifies the array
* Saves it to local storage under the key that was passed to the TaskList constructor
*/
save() {
var array = [];
for (var i = 0; i < this.arr.length; i++) {
var dict = {};
dict.text = this.arr[i].text;
dict.priority = this.arr[i].priority;
dict.duedate = this.arr[i].duedate;
dict.tag = this.arr[i].tag;
dict.id = this.arr[i].id;
dict.done = this.arr[i].done;
array.push(dict);
}
var str = JSON.stringify(array);
localStorage.setItem(this.key, str);
}
/**
* Reads a string from local storage
* Parses it into an array using JSON.parse
* Iterates over the array, adds each task description to the internal list
* (Since result of load() replaces current tasks, the internal array
* should be emptied first, and the DOM element showing the tasks should be emptied + replaced)
*/
load() {
for (var i = 0; i < this.arr.length; i++) {
// delete from internal array
this.deleteTask(i);
// remove DOM element from page
this.arr[i].delete();
}
var str = localStorage.getItem(this.key);
var array = JSON.parse(str);
this.addTasks(array);
}
/**
* Sorts the internal array by the tag
* Re-displays the task list by emptying the DOM element
* that displays the tasks and creates new DOM elements for each Task
*/
sortByTag() {
// empties the DOM element
for (var i = 0; i < this.arr.length; i++) {
this.arr[i].delete();
}
// sorts the array of Task objects by tag
// put the personal tasks before the work tasks
this.arr.sort(function(a,b) {
return (a.tag).localeCompare(b.tag);
});
// creates new DOM elements for each task
for (var i = 0; i < this.arr.length; i++) {
var task = new Task(this.arr[i]);
this.arr[i].addToDom();
}
}
/**
* Like sortByTag(), except sorts by ID
*/
sortById() {
// empties the DOM element
for (var i = 0; i < this.arr.length; i++) {
this.arr[i].delete();
}
// sorts the array of Task objects by ID
this.arr.sort(function(a, b) {
return a.id - b.id;
});
console.log(this.arr);
// creates new DOM elements for each task
for (var i = 0; i < this.arr.length; i++) {
var task = new Task(this.arr[i]);
this.arr[i].addToDom();
}
}
/**
* Like sortByTag(), except sorts Tasks by the due date
*/
sortByDueDate() {
// empties the DOM element
for (var i = 0; i < this.arr.length; i++) {
this.arr[i].delete();
}
// sorts the array of Task objects by their due date{
this.arr.sort(function(a, b) {
return a.duedate - b.duedate;
});
console.log(this.arr);
// creates new DOM elements for each task
for (var i = 0; i < this.arr.length; i++) {
var task = new Task(this.arr[i]);
this.arr[i].addToDom();
}
}
/**
* Like sortByTag(), except sorts Tasks by the priority
*/
sortByPriority() {
// empties the DOM element
for (var i = 0; i < this.arr.length; i++) {
this.arr[i].delete();
}
// sorts the array of Task objects by priority
this.arr.sort(function(a, b) {
if (a.priority == b.priority) {
return 0;
} else if ((a.priority == "high") || (a.priority == "medium" && b.priority == "low")) {
return -1;
} else {
return 1;
}
});
console.log(this.arr);
// creates new DOM elements for each task
for (var i = 0; i < this.arr.length; i++) {
var task = new Task(this.arr[i]);
this.arr[i].addToDom();
}
}
}
// global variable - instance of TaskList
var theTaskList = new TaskList("Alina");