-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
323 lines (290 loc) · 11 KB
/
script.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
const buttons = document.querySelectorAll("button");
const mainText = document.getElementById("text");
class State{
constructor(){
this._strategy = null;
this._name = null;
}
set currentState(state){
this._strategy = state;
this._name = state._name;
}
loadState(){
this._strategy.loadState();
}
inputState(){
this._strategy.inputState();
}
updateList(){
this._strategy.updateList();
}
addElem(text){
this._strategy.addElem(text);
}
get currentState(){
return this._name;
}
}
class Completed{
constructor(name){
this._name = name;
}
loadState(){
sessionStorage.setItem("state", JSON.stringify(this._name));
buttons[0].disabled = true;
buttons[1].disabled = false;
buttons[2].disabled = false;
}
updateList(option){
if(option == undefined){
option = 0;
}
const oldUl = document.querySelectorAll("form");
oldUl.forEach(elemUl =>{
elemUl.remove();
});
const form = document.createElement("form");
const mainText = document.createElement("p");
mainText.innerHTML = "Completed";
mainText.classList.add("completed-topPanel");
form.appendChild(mainText);
const arr = JSON.parse(localStorage.getItem(state._name)) || [];
if(arr.length){
const elementDIV = document.createElement("div");
elementDIV.classList.add("completed-body");
const elementSpan = document.createElement("span");
elementSpan.innerHTML = arr[option].date +":";
elementDIV.appendChild(elementSpan);
const elementUL = document.createElement("ul");
arr[option].obj.forEach(el =>{
const textLi = document.createElement("li");
textLi.innerHTML = el.text;
if(el.comp == true){
textLi.classList.add("change");
}
elementUL.appendChild(textLi);
});
elementDIV.appendChild(elementUL);
form.appendChild(elementDIV);
const bottomPanel = document.createElement("div");
bottomPanel.classList.add("completed-bottomPanel");
const size = arr.length;
for(let i=0; i<size; i++){
const radio = document.createElement("input");
radio.setAttribute("type", "radio");
radio.setAttribute("name", "radio");
radio.setAttribute("value", i);
if(i==option){
radio.checked = true;
}
bottomPanel.appendChild(radio);
}
bottomPanel.addEventListener("change", (e)=>{
console.log(e.target);
let target = (e.target).value;
this.updateList(target);
});
form.appendChild(bottomPanel);
}
document.body.appendChild(form);
}
}
class Current{
constructor(name){
this._name = name;
}
loadState(){
sessionStorage.setItem("state", JSON.stringify(this._name));
buttons[0].disabled = false;
buttons[1].disabled = true;
buttons[2].disabled = false;
}
updateList(){
const oldUl = document.querySelectorAll("form");
oldUl.forEach(elemUl =>{
elemUl.remove();
});
const form = document.createElement("form");
form.classList.add("current-form");
const formHeader = document.createElement("div");
formHeader.classList.add("current-header");
const input = document.createElement("input");
input.setAttribute("type", "text");
input.setAttribute("class", "input");
input.setAttribute("placeholder", "Enter your input...");
input.setAttribute("autocomplete", "off");
formHeader.appendChild(input);
form.addEventListener("submit", (e)=>{
this.addElem(input.value);
input.value = "";
this.updateList();
});
const saveButton = document.createElement("button");
saveButton.setAttribute("type", "button");
const imgButton = document.createElement("i");
saveButton.addEventListener("click", ()=>{
const press = confirm(`Are you sure you want to move your toDos from current to completed? \nIt will be impossible to take them back from completed`);
if (press == true){
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
let arr = JSON.parse(localStorage.getItem("completed")) || [];
let currentArray = JSON.parse(localStorage.getItem(state._name));
if(currentArray.length){
if(arr.length && arr[0].date == date){
currentArray.forEach(elem =>{
arr[0].obj.push(elem);
});
}else{
if(arr.length>=30){
arr.pop();
}
arr.push({
obj : currentArray,
date : date
});
}
arr.sort((a,b) => new Date(b.date) - new Date(a.date));
localStorage.setItem("completed", JSON.stringify(arr));
localStorage.removeItem(state._name);
this.updateList();
}else{
alert("You did not complete anything today!")
}
}
});
imgButton.classList.add("far");
imgButton.classList.add("fa-save");
saveButton.appendChild(imgButton);
formHeader.appendChild(saveButton);
form.appendChild(formHeader);
const formBody = document.createElement("ul");
formBody.classList.add("current-body");
const arr = JSON.parse(localStorage.getItem(state._name)) || [];
arr.forEach(element=>{
const liElem = document.createElement("li");
liElem.innerHTML = element.text;
if(element.comp == true){
liElem.classList.toggle("change");
}
liElem.addEventListener("click", () =>{
const press = confirm(`Do you want to delete: ${element.text}?`);
if (press == true){
const arr = JSON.parse(localStorage.getItem(state._name));
const index = arr.map(e => e.text).indexOf(element.text);
arr.splice(index, 1);
localStorage.setItem(state._name, JSON.stringify(arr));
liElem.remove();
}
});
liElem.addEventListener("contextmenu", (e) =>{
e.preventDefault();
liElem.classList.toggle("change");
const arr = JSON.parse(localStorage.getItem(state._name));
const index = arr.map(e => e.text).indexOf(element.text);
console.log(index);
if(liElem.classList.contains("change") == true){
arr[index].comp = true;
}else{
arr[index].comp = false;
}
localStorage.setItem(state._name, JSON.stringify(arr));
});
formBody.appendChild(liElem);
});
form.appendChild(formBody);
const spanA = document.createElement("span");
spanA.innerHTML = "Left mouse click to delete<br>";
const spanB = document.createElement("span");
spanB.innerHTML = "Right mouse click to complete";
form.appendChild(spanA);
form.appendChild(spanB);
document.body.appendChild(form);
}
addElem(text){
const arr = JSON.parse(localStorage.getItem(state._name)) || [];
arr.push({
text : text,
comp: false
});
localStorage.setItem(state._name, JSON.stringify(arr));
}
}
class Future{
constructor(name){
this._name = name;
}
loadState(){
sessionStorage.setItem("state", JSON.stringify(this._name));
buttons[0].disabled = false;
buttons[1].disabled = false;
buttons[2].disabled = true;
}
updateList(){
const oldUl = document.querySelectorAll("form");
oldUl.forEach(elemUl =>{
elemUl.remove();
});
const form = document.createElement("form");
const formHeader = document.createElement("div");
formHeader.classList.add("future-header");
const input = document.createElement("input");
input.setAttribute("type", "text");
input.setAttribute("class", "input");
input.setAttribute("placeholder", "Enter your input...");
input.setAttribute("autocomplete", "off");
formHeader.appendChild(input);
form.addEventListener("submit", (e)=>{
e.preventDefault();
this.addElem(input.value);
input.value = "";
this.updateList();
});
form.appendChild(formHeader);
const formBody = document.createElement("ul");
formBody.classList.add("future-body");
const arr = JSON.parse(localStorage.getItem(state._name)) || [];
arr.forEach(element=>{
const liElem = document.createElement("li");
liElem.innerHTML = element;
liElem.addEventListener("click", () =>{
const arr = JSON.parse(localStorage.getItem(state._name));
const index = arr.indexOf(element.innerHTML);
arr.splice(index, 1);
localStorage.setItem(state._name, JSON.stringify(arr));
liElem.remove();
});
formBody.appendChild(liElem);
});
form.appendChild(formBody);
document.body.appendChild(form);
}
addElem(text){
const arr = JSON.parse(localStorage.getItem(state._name)) || [];
arr.push(text);
localStorage.setItem(state._name, JSON.stringify(arr));
}
}
const state = new State();
const state1 = new Completed(buttons[0].innerHTML);
const state2 = new Current(buttons[1].innerHTML);
const state3 = new Future(buttons[2].innerHTML);
const text = JSON.parse(sessionStorage.getItem("state"));
if(!text){
select(state2);
}else{
clickButton(text);
}
function clickButton(btnName){
if(state1._name == btnName){
select(state1);
}else if(state2._name == btnName){
select(state2);
}else{
select(state3);
}
}
function select(chosed){
state.currentState = chosed;
state.loadState();
state.updateList();
}