-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
110 lines (88 loc) · 3.8 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
/*================ Doshi Naman ================*/
/*==================== CLOCK ====================*/
const hour = document.getElementById('clock-hour'),
minutes = document.getElementById('clock-minutes'),
seconds = document.getElementById('clock-seconds')
const clock = () =>{
let date = new Date()
let hh = date.getHours() * 30,
mm = date.getMinutes() * 6,
ss = date.getSeconds() * 6
// We add a rotation to the elements
hour.style.transform = `rotateZ(${hh + mm / 12}deg)`
minutes.style.transform = `rotateZ(${mm}deg)`
seconds.style.transform = `rotateZ(${ss}deg)`
}
setInterval(clock, 1000) // 1000 = 1s
/*==================== CLOCK & DATE TEXT ====================*/
const textHour = document.getElementById('text-hour'),
textMinutes = document.getElementById('text-minutes'),
textAmPm = document.getElementById('text-ampm'),
// dateWeek = document.getElementById('date-day-week'),
dateDay = document.getElementById('date-day'),
dateMonth = document.getElementById('date-month'),
dateYear = document.getElementById('date-year')
const clockText = () =>{
let date = new Date()
let hh = date.getHours(),
ampm,
mm = date.getMinutes(),
day = date.getDate(),
// dayweek = date.getDay(),
month = date.getMonth(),
year = date.getFullYear()
// We change the hours from 24 to 12 hours and establish whether it is AM or PM
if(hh >= 12){
hh = hh - 12
ampm = 'PM'
}else{
ampm = 'AM'
}
// We detect when it's 0 AM and transform to 12 AM
if(hh == 0){hh = 12}
// Show a zero before hours
if(hh < 10){hh = `0${hh}`}
// Show time
textHour.innerHTML = `${hh}:`
// Show a zero before the minutes
if(mm < 10){mm = `0${mm}`}
// Show minutes
textMinutes.innerHTML = mm
// Show am or pm
textAmPm.innerHTML = ampm
// If you want to show the name of the day of the week
// let week = ['Sun', 'Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat']
// We get the months of the year and show it
let months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
// We show the day, the month and the year
dateDay.innerHTML = day
// dateWeek.innerHTML = `${week[dayweek]}`
dateMonth.innerHTML = `${months[month]},`
dateYear.innerHTML = year
}
setInterval(clockText, 1000) // 1000 = 1s
/*==================== DARK/LIGHT THEME ====================*/
const themeButton = document.getElementById('theme-button')
const darkTheme = 'dark-theme'
const iconTheme = 'bxs-sun'
// Previously selected topic (if user selected)
const selectedTheme = localStorage.getItem('selected-theme')
const selectedIcon = localStorage.getItem('selected-icon')
// We obtain the current theme that the interface has by validating the dark-theme class
const getCurrentTheme = () => document.body.classList.contains(darkTheme) ? 'dark' : 'light'
const getCurrentIcon = () => themeButton.classList.contains(iconTheme) ? 'bxs-moon' : 'bxs-sun'
// We validate if the user previously chose a topic
if (selectedTheme) {
// If the validation is fulfilled, we ask what the issue was to know if we activated or deactivated the dark
document.body.classList[selectedTheme === 'dark' ? 'add' : 'remove'](darkTheme)
themeButton.classList[selectedIcon === 'bxs-moon' ? 'add' : 'remove'](iconTheme)
}
// Activate / deactivate the theme manually with the button
themeButton.addEventListener('click', () => {
// Add or remove the dark / icon theme
document.body.classList.toggle(darkTheme)
themeButton.classList.toggle(iconTheme)
// We save the theme and the current icon that the user chose
localStorage.setItem('selected-theme', getCurrentTheme())
localStorage.setItem('selected-icon', getCurrentIcon())
})