-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
214 lines (181 loc) · 6.91 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
const quoteContainer = document.getElementById('quote-container')
const quoteContent = document.getElementById('quote-content')
const quoteText = document.getElementById('quote')
const authorText = document.getElementById('author')
const loader = document.getElementById('loader');
const clock = document.getElementById('clock-container');
const mainTaskInp = document.getElementById('main-task-input');
const doneMessageDiv = document.getElementById('doneMessage');
let mainTask = '';
function getTime() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
// add a zero in front of numbers<10
if (m < 10) {
m = "0" + m;
}
clock.innerText = h + ":" + m;
var t = setTimeout(getTime, 500);
}
//Get Quote from API
async function getQuote() {
const proxyURL = 'https://cors-anywhere.herokuapp.com/'
apiUrl = "http://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=json";
try {
loading();
const response = await fetch(proxyURL + apiUrl);
const data = await response.json();
console.log(data);
if (data.quoteAuthor === '') {
authorText.innerText = 'Unknown'
} else {
authorText.innerText = data.quoteAuthor;
}
quoteText.innerText = data.quoteText;
loadingComplete();
} catch (error) {
getQuote();
console.log('Oops no quote!' + error);
}
}
function tweetQuote() {
const quote = quoteText.innerText;
const author = authorText.innerText;
const twitterApiUrl = `https://twitter.com/intent/tweet?text=${quote} - ${author}`
window.open(twitterApiUrl, '_blank')
}
function loading() {
loader.hidden = false;
quoteContent.hidden = true;
}
function loadingComplete() {
if (!loader.hidden) {
quoteContent.hidden = false;
loader.hidden = true;
}
}
// Main Task
function getMainTask() {
mainTask = localStorage.getItem('mainTask');
if (mainTask && mainTask.length > 0) {
// console.log('here coz mainTask is set in local storage', mainTask);
showMainTask();
document.getElementById('mainTaskText').innerText = mainTask;
} else {
hideMainTask();
mainTaskInp.addEventListener("keyup", function (event) {
// Number 13 is the "Enter" key on the keyboard
if (event.keyCode === 13) {
// console.log(mainTaskInp.value);
mainTask = mainTaskInp.value;
localStorage.setItem('mainTask', mainTaskInp.value);
// Cancel the default action, if needed
event.preventDefault();
showMainTask();
document.getElementById('mainTaskText').innerText = mainTask;
}
});
}
}
function showMainTask() {
// console.log('task input block being hidden');
taskDiv = document.getElementById('task-show');
taskDiv.style.display = 'inline-block';
taskInp = document.getElementById('task-input');
taskInp.style.display = 'none';
}
function hideMainTask() {
// console.log('task show block being hidden');
document.getElementById('task-show').style.display = 'none';
document.getElementById('task-input').style.display = 'inline-block';
}
function taskDone() {
checked = document.getElementById('taskCheckbox').checked;
console.log(checked);
if (checked) {
document.getElementById('mainTaskText').style.textDecoration = 'line-through';
doneMessageDiv.classList.add('fade');
setTimeout(function () {
doneMessageDiv.classList.remove('fade')
}, 3000)
} else {
document.getElementById('mainTaskText').style.textDecoration = 'none';
doneMessageDiv.classList.remove('fade');
}
}
function removeTask() {
mainTask = '';
localStorage.setItem('mainTask', '');
mainTaskInp.value = '';
document.getElementById('taskCheckbox').checked = false;
document.getElementById('mainTaskText').style.textDecoration = 'none';
getMainTask();
}
function showTaskOptions() {
document.getElementById('removeTaskDiv').style.display = 'inline-block';
document.getElementById('taskCheckboxDiv').style.display = 'inline-block';
}
function hideTaskOptions() {
document.getElementById('removeTaskDiv').style.display = 'none';
document.getElementById('taskCheckboxDiv').style.display = 'none';
}
// To Do List
function toggleToDoCardDisplay() {
var card = document.getElementById("to-do-card");
var button = document.getElementById('to-do-button')
if (card.style.display === "none") {
card.style.display = "block";
button.style.opacity = '1'
} else {
card.style.display = "none";
button.style.opacity = '0.8'
}
}
function setToDo() {
// Add a "checked" symbol when clicking on a list item
var list = document.querySelector('ul');
list.addEventListener('click', function (ev) {
if (ev.target.tagName === 'LI') {
ev.target.classList.toggle('checked');
}
}, false);
let todoInp = document.getElementById('new-to-do-inp');
todoInp.addEventListener("keyup", function (event) {
// Number 13 is the "Enter" key on the keyboard
if (event.keyCode === 13) {
// console.log(mainTaskInp.value);
newToDo();
}
});
}
function newToDo() {
var close = document.getElementsByClassName("close");
var li = document.createElement("li");
var inputValue = document.getElementById("new-to-do-inp").value;
var t = document.createTextNode(inputValue);
li.appendChild(t);
if (inputValue === '') {
alert("You must write something!");
} else {
document.getElementById("to-do-list").appendChild(li);
}
document.getElementById("new-to-do-inp").value = "";
var span = document.createElement("SPAN");
var txt = document.createTextNode("\u00D7");
span.className = "close";
span.appendChild(txt);
li.appendChild(span);
for (i = 0; i < close.length; i++) {
close[i].onclick = function () {
var div = this.parentElement;
div.style.display = "none";
}
}
}
// On Load
getTime();
getQuote();
getMainTask();
setToDo();