-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeekly.js
More file actions
216 lines (175 loc) · 7.3 KB
/
Copy pathWeekly.js
File metadata and controls
216 lines (175 loc) · 7.3 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
208
209
210
211
212
213
214
215
216
const dayBoxes = ['.js-output1', '.js-output2', '.js-output3', '.js-output4', '.js-output5', '.js-output6', '.js-output7'];
let tasksArray = JSON.parse(localStorage.getItem('tasksArray')); //stores every task entered by user
//if task array is not in local storage, create empty array
if(!tasksArray) {
tasksArray = [];
}
console.log('Loading TaskArray: ' + JSON.stringify(tasksArray));
renderAll();
//lets you add task to memory and calls render function to update page
function addTodo(daySelect) {
//get input box and its value
const inputElement = document.querySelector('.js-input-name');
const name = inputElement.value;
if(name !== "") {
const newTask = {name, day: daySelect, isCompleted: false};
tasksArray.push(newTask); //add task to array
localStorage.setItem('tasksArray', JSON.stringify(tasksArray)); //save array to storage
//console.log(tasksArray.length);
currentIndexOfTask = tasksArray.length - 1;
//console.log(currentIndexOfTask);
inputElement.value = '';
//console.log(tasksArray);
renderTask(newTask, currentIndexOfTask);
}
else {
alert("Cannot add blank item!");
}
//printArray(tasksArray);
}
function clearDay(daySelect) {
//first empty day box html
document.querySelector(dayBoxes[daySelect]).innerHTML = '';
//store indecies of tasks with certain day value here
/*In tasksArray, all the tasks are stored.
The daySelect is an integer corresponding to the day the button
was pressed. Example: Sunday is day 0, Monday is day 1...
Now look at the task in the task array and see its "day"
attribute. If the day matches with the daySelect from remove button, then put that index in a new array.*/
let tasksToRemove = [];
for(let i = 0; i < tasksArray.length; i++) {
if(tasksArray[i].day === daySelect) {
tasksToRemove.push(i);
}
console.log(tasksToRemove);
}
//remove tasks at the indicies
if(tasksToRemove.length !== 0) {
for(let i = 0; i < tasksToRemove.length; i++){
console.log(`At index ${i}: ${JSON.stringify(tasksArray[tasksToRemove[i] - i])}`);
tasksArray.splice(tasksToRemove[i] - i,1); //tasktorem array value minus loop index because taskArray changing in size
}
localStorage.setItem('tasksArray', JSON.stringify(tasksArray));
console.log('Tasks removed!');
}
else {
console.log('No tasks removed');
}
renderAll();
}
//whenever a change is made in any array, update the box on the page
function renderTask(newTask, buttonID) {
const daySelect = newTask.day;
document.querySelector(dayBoxes[daySelect]).innerHTML += `<div style="margin: 10px 5px;"><button class="checkbox-button" id="${buttonID}" onclick="
completeTask(${buttonID})
"></button> ${newTask.name}</div>`;
console.log('New task added.');
}
//if clear all button is pressed all the tasks will be erased from page
function clearAll() {
tasksArray = []; //empty array
localStorage.setItem('tasksArray', JSON.stringify(tasksArray)); //save to local storage
//console.log(tasksArray);
for(let i = 0; i < dayBoxes.length; i++) {
document.querySelector(dayBoxes[i]).innerHTML = ''; //update html to blank
}
console.log('All tasks cleared.');
}
function renderAll() {
weekInfoText = document.querySelector('.js-change-info');
weekInfoText.innerHTML = localStorage.getItem('weekInfo');
if(!weekInfoText.innerHTML) {
weekInfoText.innerHTML = 'Use input box to enter date range here...';
}
if(tasksArray.length !== 0) {
for(let i = 0; i < dayBoxes.length; i++) {
document.querySelector(dayBoxes[i]).innerHTML = '';
}
for(let i = 0; i < tasksArray.length; i++) {
const taskName = tasksArray[i].name;
const daySelect = tasksArray[i].day;
const taskCompleted = tasksArray[i].isCompleted;
document.querySelector(dayBoxes[daySelect]).innerHTML += `<div style="margin: 10px 5px;"><button class="checkbox-button" id="${i}" onclick="completeTask(${i});"></button> ${taskName}</div>`;
if(taskCompleted) {
document.getElementById(i).style['background-color'] = "rgba(0,255,0,0.6)"; //rgb(0,180,255)
document.getElementById(i).innerHTML = "✓";
}
}
console.log('Existing tasks loaded.');
}
else {
for(let i = 0; i < dayBoxes.length; i++) {
document.querySelector(dayBoxes[i]).innerHTML = '';
}
console.log('No existing tasks found to load. Add tasks to get started.');
}
document.querySelector(".js-remove-a-task").innerHTML = `<button onclick="removeView();"
class="button-clear-tasks"
>Remove A Task</button>`;
}
function printArray(taskList) {
for(let i = 0; i < taskList.length; i++) {
console.log(`${i}: ${taskList[i].name}, Day: ${taskList[i].day}`);
}
}
let editButtonCounter = 0;
function changeInfo() {
inputElement = document.querySelector('.js-input-name');
textElement = document.querySelector('.js-change-info');
textElement.innerHTML = inputElement.value;
if(textElement.innerHTML === '') {
textElement.innerHTML = 'Use input box to enter date range here...';
}
localStorage.setItem('weekInfo', inputElement.value);
inputElement.value = '';
}
function completeTask(buttonID) {
//console.log(buttonID);
buttonElement = document.getElementById(buttonID);
//console.log(buttonElement.style['background-color']);
//currently background-color white applies to checkbox-button class, but not id - that is why this branch below initially sets up the background-color for each button by "id" when passed to function
//The first time you load the page, background-color is actually blank/white
if(!buttonElement.style['background-color']) {
buttonElement.style['background-color'] = 'white';
}
if(buttonElement.style['background-color'] == 'white') {
buttonElement.style['background-color'] = 'rgba(0,255,0,0.6)';
buttonElement.innerHTML = "✓";
tasksArray[buttonID].isCompleted = true;
}
else {
buttonElement.style['background-color'] = 'white';
buttonElement.innerHTML = ""; //•
tasksArray[buttonID].isCompleted = false;
}
localStorage.setItem('tasksArray', JSON.stringify(tasksArray));
}
function removeView() {
if(tasksArray.length !== 0) {
for(let i = 0; i < dayBoxes.length; i++) {
document.querySelector(dayBoxes[i]).innerHTML = '';
}
for(let i = 0; i < tasksArray.length; i++) {
const taskName = tasksArray[i].name;
const daySelect = tasksArray[i].day;
const taskCompleted = tasksArray[i].isCompleted;
document.querySelector(dayBoxes[daySelect]).innerHTML += `<div style="margin: 10px 5px;"><button class="checkbox-button" style="background-color: red;" id="${i}" onclick="removeTask(${i});">x</button> ${taskName}</div>`;
document.querySelector(".js-remove-a-task").innerHTML = '';
document.querySelector(".js-remove-a-task").innerHTML += `<button onclick="renderAll();"
class="button-clear-tasks"
>Cancel</button>`;
}
console.log('MODE ENTERED: Remove a task.');
}
else {
alert("No available tasks to remove, add a task first.");
}
}
function removeTask(taskIndex){
//remove tasks at the index in taskArray
console.log(`At index ${taskIndex}: ${JSON.stringify(tasksArray[taskIndex])}`);
tasksArray.splice(taskIndex,1);
localStorage.setItem('tasksArray', JSON.stringify(tasksArray));
console.log('Task removed!');
renderAll();
}