-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
154 lines (125 loc) · 4.21 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
import './node_modules/cistercian-numerals/dist/cistercian-numerals.js'
const storageKey = 'cistsaver_settings'
function setClocks(now, timeClock, dateClock) {
timeClock.value = now.getHours() * 100 + now.getMinutes()
dateClock.value = (now.getMonth() + 1) * 100 + now.getDate()
}
function onFormInput({ target }) {
const getValue = t => {
switch (t.getAttribute('type')) {
case 'checkbox':
return t.checked
case 'number':
case 'range':
return Number(t.value)
default:
return t.value
}
}
const dispatch = () => {
target.dispatchEvent(new CustomEvent('setting-changed', {
detail: { name: target.name, value: getValue(target), fromDom: true},
bubbles: true
}))
}
setTimeout(dispatch, 0)
persistSettings(target.name, getValue(target))
}
function persistSettings(key, value) {
const newSettings = Object.assign(
JSON.parse(localStorage.getItem(storageKey) || '{}'),
{ [key]: value },
)
localStorage.setItem(storageKey, JSON.stringify(newSettings))
}
function readSettings(form) {
const settings = JSON.parse(localStorage.getItem(storageKey) || '{}')
for(let [name, value] of Object.entries(settings)) {
form.dispatchEvent(new CustomEvent('setting-changed', {
detail: { name, value, fromDom: false},
bubbles: true
}))
}
}
function setFormValue(event) {
const detail = event.detail
const form = event.target
if (detail.fromDom) {
return
}
const field = form[detail.name]
if (!field) {
console.error(`Cannot set value for ${detail.name} on form`)
return
}
}
function applySetting({ target, detail }) {
const value = detail.value
switch (detail.name) {
case 'fg-brightness':
document.body.style.setProperty('--fg-brightness', `${value}%`)
break;
case 'bg-brightness':
document.body.style.setProperty('--bg-brightness',`${value}%`)
break;
case 'nosleep':
if (!('wakeLock' in navigator)) {
console.error('wakeLock not supported')
} else if (value) {
navigator.wakeLock.request('screen')
.then(lock => { document.body._wakeLock = lock })
.catch(err => { console.error(err) })
} else if (document.body._lock) {
const lock = document.body._lock
lock.release()
.then(() => { document.body._wakeLock = null })
.catch(err => { console.error(err) })
}
break;
}
}
function toggleFullScreen(goFullScreen) {
if (goFullScreen) {
document.documentElement.requestFullscreen()
} else if (document.fullscreenElement) {
document.exitFullscreen();
}
}
function toggleFooter() {
document.querySelector('footer').classList.toggle('hidden')
}
export default function main() {
const settingsForm = document.getElementById('settings')
const timeClock = document.createElement('cistercian-number')
const dateClock = document.createElement('cistercian-number')
const yearClock = document.createElement('cistercian-number')
const now = new Date()
setClocks(now, timeClock, dateClock)
yearClock.value = now.getFullYear()
setTimeout(() => {
setClocks(new Date(), timeClock, dateClock)
setInterval(
() => setClocks(new Date(), timeClock, dateClock),
60000
)
}, 1000 * (60 - now.getSeconds()))
document.querySelector('.time').append(timeClock)
document.querySelector('.date').append(dateClock)
document.querySelector('.date').append(yearClock)
settingsForm.addEventListener('input', onFormInput)
settingsForm.addEventListener('setting-changed', setFormValue)
document.body.addEventListener('setting-changed', applySetting)
timeClock.addEventListener('dblclick', () => { toggleFullScreen(true) })
timeClock.addEventListener('click', () => { toggleFullScreen(false) })
dateClock.addEventListener('click', toggleFooter)
yearClock.addEventListener('click', toggleFooter)
document.querySelector('.close').addEventListener('click', toggleFooter)
readSettings(settingsForm)
if (!('wakeLock' in navigator)) {
const checkbox = document.getElementById('nosleep')
const label = document.querySelector('[for="nosleep"]')
checkbox.checked = false
checkbox.disabled = true
label.innerText += '. (No browser support)'
}
}