-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
107 lines (94 loc) · 2.59 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
// DOM Elements
const time = document.getElementById('time'),
greeting = document.getElementById('greeting'),
name = document.getElementById('name'),
focus = document.getElementById('focus');
// Options
const showAmPm = true;
// Show Time
function showTime() {
let today = new Date(),
hour = today.getHours(),
min = today.getMinutes();
// Output Time
time.innerHTML = `${hour}<span>:</span>${addZero(min)}`;
setTimeout(showTime, 1000);
}
// Add Zeros
function addZero(n) {
return (parseInt(n, 10) < 10 ? '0' : '') + n;
}
// Set Background and Greeting
function setBgGreet() {
let today = new Date(),
hour = today.getHours();
if (hour < 6) {
// night
document.body.style.backgroundImage = "url('images/night.png')";
document.body.style.backgroundSize="100% 100%";
greeting.textContent = 'Good night, ';
} else if (hour < 13) {
// Morning
document.body.style.backgroundImage = "url('https://i.ibb.co/7vDLJFb/morning.jpg')";
greeting.textContent = 'Good Morning, ';
} else if (hour < 18) {
// Afternoon
document.body.style.backgroundImage = "url('images/chima.jpeg')";
document.body.style.backgroundSize="100% 100%";
greeting.textContent = 'Good Afternoon, ';
} else {
// Evening
document.body.style.backgroundImage = "url('images/night.png')";
document.body.style.backgroundSize="100% 100%";
greeting.textContent = 'Good Evening, ';
}
}
// Get Name
function getName() {
if (localStorage.getItem('name') === null) {
name.textContent = '[Enter Name]';
} else {
name.textContent = localStorage.getItem('name');
}
}
// Set Name
function setName(e) {
if (e.type === 'keypress') {
// Make sure enter is pressed
if (e.which == 13 || e.keyCode == 13) {
localStorage.setItem('name', e.target.innerText);
name.blur();
}
} else {
localStorage.setItem('name', e.target.innerText);
}
}
// Get Focus
function getFocus() {
if (localStorage.getItem('focus') === null) {
focus.textContent = '[Enter Focus]';
} else {
focus.textContent = localStorage.getItem('focus');
}
}
// Set Focus
function setFocus(e) {
if (e.type === 'keypress') {
// Make sure enter is pressed
if (e.which == 13 || e.keyCode == 13) {
localStorage.setItem('focus', e.target.innerText);
focus.blur();
}
} else {
localStorage.setItem('focus', e.target.innerText);
}
}
name.addEventListener('keypress', setName);
name.addEventListener('blur', setName);
focus.addEventListener('keypress', setFocus);
focus.addEventListener('blur', setFocus);
// Run
showTime();
setBgGreet();
getName();
getFocus();