-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathscript.js
More file actions
189 lines (159 loc) · 4.7 KB
/
script.js
File metadata and controls
189 lines (159 loc) · 4.7 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
const now = new Date();
const hours = now.getHours();
const greeting= document.querySelector('.greeting');
const form = document.getElementById('form')
const input = document.getElementById('input')
const todosUL = document.getElementById('todos')
const todos = JSON.parse(localStorage.getItem('todos'))
if(hours=>6 && hours<12) {
greeting.textContent= "Good morning";
}
if(hours=>12 && hours<17) {
greeting.textContent= "Good afternoon";
}
if(hours=>17 && hours<21) {
greeting.textContent= "Good evening"
}
if(hours=>21 && hours<24) {
greeting.textContent= "Good night";
}
if(hours=>0 && hours<6) {
greeting.textContent= "Sleep well";
}
if(todos) {
todos.forEach(todo => addTodo(todo))
}
form.addEventListener('submit', (e) => {
e.preventDefault()
addTodo()
})
function addTodo(todo) {
let todoText = input.value
if(todo) {
todoText = todo.text
}
if(todoText) {
const todoEl = document.createElement('li')
if(todo && todo.completed) {
todoEl.classList.add('completed')
}
todoEl.innerText = todoText
todoEl.addEventListener('click', () => {
todoEl.classList.toggle('completed')
updateLS()
})
todoEl.addEventListener('contextmenu', (e) => {
e.preventDefault()
todoEl.remove()
updateLS()
})
todosUL.appendChild(todoEl)
input.value = ''
updateLS()
}
}
function updateLS() {
todosEl = document.querySelectorAll('li')
const todos = []
todosEl.forEach(todoEl => {
todos.push({
text: todoEl.innerText,
completed: todoEl.classList.contains('completed')
})
})
localStorage.setItem('todos', JSON.stringify(todos))
}
//Themes logic
const themes = ["lavender", "orange", "aqua", "lime"];
let index=themes.findIndex((theme) => theme == localStorage.getItem('themePreference'));
theme(index);
function theme(color) {
let header = document.querySelector("h3");
let inputBox = document.querySelector(".input");
let themeIcon = document.querySelector(".theme-icon");
if (color==0) {
localStorage.setItem('themePreference', 'lavender');
}
if (color==1) {
localStorage.setItem('themePreference', 'orange');
}
if (color==2) {
localStorage.setItem('themePreference', 'aqua');
}
if (color==3) {
localStorage.setItem('themePreference', 'lime');
}
let preference=localStorage.getItem('themePreference');
try {
let todos = document.querySelector(".todos");
todos.classList.remove(...themes);
todos.classList.add(preference);
}
catch(err) {
}
themeIcon.classList.remove(...themes);
header.classList.remove(...themes);
inputBox.classList.remove(...themes);
themeIcon.classList.add(preference);
header.classList.add(preference);
inputBox.classList.add(preference);
}
//Dark light mode toggle
let calIcon = document.querySelector("#cal");
let imgSrc= calIcon.src;
if(localStorage.getItem('preference')=='dark') {
lightDarkToggle(1);
}
function lightDarkToggle(flag) {
let lightOrDark = document.querySelector(".lightOrDark");
let element = document.body;
let inputBox = document.querySelector(".input");
if(flag==0) {
if(localStorage.getItem('preference')=='dark') {
localStorage.setItem('preference', 'light');
}
else {
localStorage.setItem('preference', 'dark')
}
}
try {
let todos = document.querySelector(".todos li");
todos.classList.toggle("dark-todos");
}
catch(err) {
}
if (calIcon.src == imgSrc) {
calIcon.src= "public/images/calendar-dark.png";
}
else {
calIcon.src="public/images/calendar.png";
}
inputBox.classList.toggle("dark-input");
element.classList.toggle("dark");
if (lightOrDark.textContent === "☀︎") {
lightOrDark.textContent = "☾";
lightOrDark.style.color = "white";
} else {
lightOrDark.textContent = "☀︎";
lightOrDark.style.color = "#444444";
}
}
const quotesContainer = document.getElementById('quotes-container');
const quoteElement = document.getElementById('quote');
function getQuote() {
fetch('https://api.quotable.io/random')
.then(response => response.json())
.then(data => {
if (data.content.length>100) {
getQuote();
}
else {
quoteElement.textContent = `"${data.content}" - ${data.author}`;
}
})
.catch(error => {
console.log(error);
});
}
getQuote();
setInterval(getQuote, 3600000);