forked from goodchai0/day-planner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
282 lines (240 loc) · 11.4 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
$(function(){
const immutableToday = moment();
let today = moment();
function generateView(){
$("#dayPicked").text(today.format('LL'));
//CURRENT DATE HEADER
$("#currentDay").text(immutableToday.format('LL'));
// CREATE DATE STRING DD/MM/YYYY, FOR USE AS STORAGE KEY
let date = moment($('#dayPicked').text()).locale('fr').format('L');
// DYNAMICALLY CREATE TIMEBLOCKS
for(let i = 0; i < 5; i++){
$("#timeBlocks").append(
`<div class='col-md-12 pb-2 my-3 mx-3 timeBlock' data-block='${i}' style='background: linear-gradient(#d3c8c854, black); border-radius:18px'>
<form class='form-inline m-2 p-2' id='activity-form'>
<div class='hourdiv col-xs-12 col-md-2'>
<label for='inlineFormInputName2'>
<h3 class='hour mt-2 p-1'>Hour: 0${i}</h3>
</label>
</div>
<div class='col-xs-12 col-md-8'>
<input type='text' class='form-control' data-hour='${i}' style='width:100%;'placeholder='Eat, sleep, code' minlength='2' maxlength='50'></input>
</div>
<div class='col-xs-12 col-md-2 pt-1'>
<button type='button' class='btn btn-primary create' data-hour='${i}'>Create</button>
</div>
</form>
<div class='row p-1 todos' data-hour='${i}'></div>
</div>`
);
}
for(let i = 5; i < 10; i++){
$("#timeBlocks").append(
`<div class='col-md-12 pb-2 my-3 mx-3 timeBlock' data-block='${i}' style='background: linear-gradient(#c3b0a52b, #ff6c00); border-radius:18px'>
<form class='form-inline m-2 p-2' id='activity-form'>
<div class='hourdiv col-xs-12 col-md-2'>
<label for='inlineFormInputName2'>
<h3 class='hour mt-2 p-1'>Hour: 0${i}</h3>
</label>
</div>
<div class='col-xs-12 col-md-8'>
<input type='text' class='form-control' data-hour='${i}' style='width:100%;'placeholder='Eat, sleep, code' minlength='2' maxlength='50'></input>
</div>
<div class='col-xs-12 col-md-2 pt-1'>
<button type='button' class='btn btn-primary create' data-hour='${i}'>Create</button>
</div>
</form>
<div class='row p-1 todos' data-hour='${i}'></div>
</div>`
);
}
for(let i = 10; i < 18; i++){
$("#timeBlocks").append(
`<div class='col-md-12 pb-2 my-3 mx-3 timeBlock' data-block='${i}' style='background: linear-gradient(#c9cbeb4f, #4848e0); border-radius:18px'>
<form class='form-inline m-2 p-2' id='activity-form'>
<div class='hourdiv col-xs-12 col-md-2'>
<label for='inlineFormInputName2'>
<h3 class='hour mt-2 p-1'>Hour: ${i}</h3>
</label>
</div>
<div class='col-xs-12 col-md-8'>
<input type='text' class='form-control' data-hour='${i}' style='width:100%;'placeholder='Eat, sleep, code' minlength='2' maxlength='50'></input>
</div>
<div class='col-xs-12 col-md-2 pt-1'>
<button type='button' class='btn btn-primary create' data-hour='${i}'>Create</button>
</div>
</form>
<div class='row p-1 todos' data-hour='${i}'></div>
</div>`
);
}
for(let i = 18; i < 21; i++){
$("#timeBlocks").append(
`<div class='col-md-12 pb-2 my-3 mx-3 timeBlock' data-block='${i}' style='background: linear-gradient(#c3b0a52b, #ff6c00); border-radius:18px'>
<form class='form-inline m-2 p-2' id='activity-form'>
<div class='hourdiv col-xs-12 col-md-2'>
<label for='inlineFormInputName2'>
<h3 class='hour mt-2 p-1'>Hour: ${i}</h3>
</label>
</div>
<div class='col-xs-12 col-md-8'>
<input type='text' class='form-control' data-hour='${i}' style='width:100%;'placeholder='Eat, sleep, code' minlength='2' maxlength='50'></input>
</div>
<div class='col-xs-12 col-md-2 pt-1'>
<button type='button' class='btn btn-primary create' data-hour='${i}'>Create</button>
</div>
</form>
<div class='row p-1 todos' data-hour='${i}'></div>
</div>`
);
}
for(let i = 21; i < 24; i++){
$("#timeBlocks").append(
`<div class='col-md-12 pb-2 my-3 mx-3 timeBlock' data-block='${i}' style='background: linear-gradient(#d3c8c854, black); border-radius:18px'>
<form class='form-inline m-2 p-2' id='activity-form'>
<div class='hourdiv col-xs-12 col-md-2'>
<label for='inlineFormInputName2'>
<h3 class='hour mt-2 p-1'>Hour: ${i}</h3>
</label>
</div>
<div class='col-xs-12 col-md-8'>
<input type='text' class='form-control' data-hour='${i}' style='width:100%;'placeholder='Eat, sleep, code' minlength='2' maxlength='50'></input>
</div>
<div class='col-xs-12 col-md-2 pt-1'>
<button type='button' class='btn btn-primary create' data-hour='${i}'>Create</button>
</div>
</form>
<div class='row p-1 todos' data-hour='${i}'></div>
</div>`
);
}
if(localStorage.getItem(date)){
refreshItems()
}/*else{
colorBlocks()
}*/
}
function addActivity() {
let date = moment($('#dayPicked').text()).locale('fr').format('L');
let saved = localStorage.getItem(date) ? JSON.parse(localStorage.getItem(date)) : [];
let buttonVal = $(this).attr("data-hour");
let inputVal = $(`input[data-hour=${buttonVal}]`);
// NO EMPTY INPUTS
if(inputVal.val() == ''){
return;
}
let savedElement = saved.filter(hour => hour.time == buttonVal);
// FIRST SAVE GLOBAL || FIRST SAVE PER HOUR
if(!saved.length || !saved.includes(savedElement[0])){
saved.push({"time": inputVal.attr("data-hour"), "activity" : [inputVal.val()]});
displayItem(buttonVal, inputVal.val())
}else{
// IF THERE IS A PREVIOUS LOCAL SAVE AT THIS SPOT, ADD THE NEW ITEM TO IT
saved.forEach(hour => {
if(hour.time == buttonVal && hour.activity.length){
hour.activity.push(inputVal.val());
displayItem(buttonVal, inputVal.val())
}
})
}
localStorage.setItem(date, JSON.stringify(saved));
inputVal.val('')
}
// function colorBlocks(){
// let date = moment($('#dayPicked').text()).locale('fr').format('L');
// let currentHour = moment().hour();
// let colorCode = ' #ED2939';
// $('.timeBlock').each(function(){
// if(moment(date).isBefore(moment(immutableToday).locale('fr').format('L'))){
// }else if(moment(date).isAfter(moment(immutableToday).locale('fr').format('L'))){
// colorCode = '#3F704D';
// }else{
// if($(this).attr('data-block') == currentHour){
// colorCode = '#F4C430';
// }
// if($(this).attr('data-block') > currentHour){
// colorCode = '#3F704D';
// }
// }
// $(this).attr('style', `background-color:${colorCode};`)
// })
// }
// DYNAMICALLY CREATE TODO ITEMS & CORRESPONDING DELETE BUTTONS
function displayItem(btn, val, index){
$(`div[data-hour=${btn}]`).append(
`<div class='col-xs-12 col-md-2 mt-2'>
<div class='card'>
<div class='card-body'>
<p>${val}</p>
<button type='button' class='btn btn-danger delete' data-hour='${btn}' data-value='${val}' data-index='${index}'>Delete</button>
</div>
</div>
</div>`
);
$(`button[data-value='${val}']`).on("click", removeActivity);
}
function refreshItems(){
let date=moment($('#dayPicked').text()).locale('fr').format('L');
let saved=JSON.parse(localStorage.getItem(date));
$(".todos").html('')
if(saved){
saved.forEach(hour=>{
hour.activity.forEach((activity,index)=>{
displayItem(hour.time,activity, index)
})
})
}
colorBlocks()
}
function removeActivity(){
let date = moment($('#dayPicked').text()).locale('fr').format('L');
let saved = JSON.parse(localStorage.getItem(date));
let buttonHour = $(this).attr("data-hour");
let buttonIndex = $(this).attr("data-index");
saved.forEach((hour, i) => {
if(hour.time == buttonHour){
//search in arr for element with current index
let index = hour.activity.findIndex(el => el === buttonIndex);
hour.activity.splice(index, 1);
// IF LAST ACTIVITY IN ARR, REMOVE ARR SO IT PLAYS NICE IN addActivity FUNC
if(hour.activity.length === 0){
saved.splice(i, 1)
}
}
});
localStorage.setItem(date, JSON.stringify(saved));
refreshItems()
}
generateView()
$(".create").on("click", addActivity)
$('#activity-form').on('submit', (e) => {
e.preventDefault();
});
// GO BACK A DAY
$("#dayEarlier").on("click", () => {
today = today.subtract(1, 'day');
$('#dayPicked').text(today.format('LL'));
refreshItems()
})
// GO FORWARD A DAY
$("#dayLater").on("click", () => {
today = today.add(1, 'day');
$('#dayPicked').text(today.format('LL'));
refreshItems()
})
})
let darkMode = document.querySelector("#darkMode");
let b = document.querySelector("body");
let fm = document.querySelector("#dayPicked");
darkMode.addEventListener('click', () => {
if(darkMode.innerText == "Enable Dark Mode") {
darkMode.innerText = "Enable Light Mode";
b.style.backgroundColor = "#2F3032";
fm.style.backgroundColor = "yellow";
}
else {
darkMode.innerText = "Enable Dark Mode";
b.style.backgroundColor = "white";
fm.style.backgroundColor = "yellow";
}
});