-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
252 lines (206 loc) · 6.34 KB
/
main.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
"use strict"
// code imported from https://github.com/rustydcoder/todo-list-js/blob/master/index.js
// made a few tweaks
class Todo {
constructor(input, container) {
this.input = document.querySelector(input)
this.container = document.querySelector(container)
this._data = (localStorage.getItem('todoList')) ? JSON.parse(localStorage.getItem('todoList')) : {
todo: [],
state: true
}
}
addToList() {
const val = this.capitalizeVal(this.input.value);
const regex = (/([^\s])/);
if (regex.test(val) && !this._data.todo.includes(val)) {
this.insertToDom(val)
this._data.todo.push(val)
this.updateSavedData()
}
this.input.value = '';
this.input.focus()
}
capitalizeVal(val) {
return val.charAt(0).toUpperCase() + val.slice(1).toLowerCase()
}
insertToDom(val) {
const list = document.createElement('div')
const span = document.createElement('span')
span.innerText = val.trim()
list.classList.add('list__item')
const div = document.createElement('div')
div.classList.add('btn__group')
const removeBtn = document.createElement('button')
removeBtn.innerHTML = '<i class="fa fa-times" aria-hidden="true"></i>'
removeBtn.classList.add('remove-list__item')
removeBtn.addEventListener('click', (event) => this.removeFromList(event))
const checkbox = document.createElement('input')
checkbox.classList.add('complete-list__item')
checkbox.type = 'checkbox'
checkbox.value = val.trim()
checkbox.addEventListener('click', (event) => this.completeItem(event))
list.appendChild(span)
div.appendChild(checkbox)
div.appendChild(removeBtn)
list.appendChild(div)
this.container.insertBefore(list, this.container.childNodes[0])
}
emptyState() {
this.container.insertAdjacentHTML('afterend', `
<h2 class="empty__state" style="display: none">
Nothing to do?
</h2>
`)
}
updateSavedData() {
this._data.state = this._data.todo.length > 0 ? false : true
localStorage.setItem('todoList', JSON.stringify(this._data))
}
removeFromList(event) {
const item = event.target.parentNode.parentNode.parentNode;
const parent = item.parentNode
const id = (/done/).test(item.className)
const value = item.firstElementChild.innerText
if (!id) {
this._data.todo.splice(this._data.todo.indexOf(value), 1)
}
this.updateSavedData()
parent.removeChild(item)
}
completeItem(event) {
const item = event.target.parentNode.parentNode;
const id = (/done/).test(item.className)
const value = event.target.value
if (event.target.checked) {
item.classList.add('done')
}
else {
item.classList.remove('done')
}
if (id) {
this._data.todo.push(value)
} else {
this._data.todo.splice(this._data.todo.indexOf(value), 1)
}
this.updateSavedData()
}
renderTodoList() {
this.emptyState()
this._data.todo.forEach(val => {
this.insertToDom(val)
})
}
init() {
this.renderTodoList()
this.input.addEventListener('keypress', (e) => {
if (e.keyCode == 13 || e.which == 13) {
this.addToList()
}
})
}
}
const todo = new Todo('#focus', '#list')
todo.init()
// Variable declaration
const time = document.getElementById("time"),
greeting = document.getElementById("greeting"),
name = document.getElementById("name"),
focus = document.getElementById("focus"),
img = document.querySelector('.bg-img')
// Event Listeners
name.addEventListener("keypress", setName);
name.addEventListener("blur", setName);
// Handles Clock
function showTime() {
let today = new Date(),
hour = today.getHours(),
min = today.getMinutes(),
sec = today.getSeconds();
hour = hour % 12 || 12; // 12 hour format
const amPm = today.getHours() < 12 ? "AM" : "PM";
time.innerHTML = `
<span>${hour}:</span>
<span>${addZero(min)}</span>
<span class="sec">${addZero(sec)} ${amPm}</span>`;
setTimeout(showTime, 1000);
}
async function fetchImage(query) {
const url = "https://api.unsplash.com/photos/random?query="
+ query +
"&count=8&client_id=yR6wwzEL63OWRgnba7liHfBDspTNj0sRDDrJMgmICgQ"
const options = {
method: 'GET',
redirect: 'follow'
};
const data = await fetch(url, options);
const response = await data.json()
// returns a filtered array of images with city and country
// and the conditions
return response.filter(({
location: { city, country },
height,
width
}) => city && country && width > 2000 && height > 2000);
}
async function render() {
let today = new Date(),
hour = today.getHours();
if (hour < 12) {
greeting.innerHTML = "Good Morning";
} else if (hour < 18) {
greeting.innerHTML = "Good Afternoon";
} else {
greeting.innerHTML = "Good Evening";
}
const day_night = hour < 18 ? 'nature' : 'night-forest'
const unsplash = await fetchImage(day_night).catch(error => console.log('error', error));
const imgObj = format(unsplash[0])
img.onload = function () { console.log('Image Loaded') }
img.src = imgObj.raw
document.querySelector('.name').innerHTML = imgObj.name
document.querySelector('.country').innerHTML = imgObj.name === imgObj.city ? imgObj.country : `${imgObj.city}, ${imgObj.country}`
setTimeout(() => {
document.querySelector('.bg-alt').style.display = imgObj.raw ? 'none' : 'block'
}, 9000);
}
function setName(event) {
if (event.type === "keypress") {
if (event.which == 13 || event.keyCode == 13) {
localStorage.setItem("name", event.target.value.trim());
// Makes the width of the input === to the number of characters
// Hence the hardcoded 26
this.style.width = ((this.value.length + 1) * 26) + 'px';
this.disabled = true
name.blur();
}
} else {
localStorage.setItem("name", event.target.value.trim());
this.disabled = true
}
}
function getName() {
if (
localStorage.getItem("name") === null ||
localStorage.getItem("name") == ""
) {
name.disabled = false;
} else {
name.value = localStorage.getItem("name");
name.disabled = true;
}
}
function addZero(num) {
return (parseInt(num, 10) < 10 ? "0" : " ") + num;
}
function format({ urls: { raw }, location: { name, city, country } }) {
return {
raw,
city,
country,
name: name.split(',').slice(0, 1).toString()
}
}
showTime();
getName();
render()