-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
168 lines (130 loc) · 4.07 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
const form = document.getElementById('form')
const modalWindow = document.querySelector('.js-modal')
const modalBody = document.querySelector('.js-modal-body')
const qrCodeContainer = document.getElementById('qr-code-container')
const qrCodeButton = document.querySelector('.js-modal-qr')
// TODO:
// - [ ] disabled state for buttons
// - [x] exit fullscreen on esc
// - [x] test fullscreen on different devices
// - [ ] fix for iOS fullscreen
// MODAL ------------------------------------------------------
const modalHandler = () => {
if (!modalWindow) return
closeModal(modalWindow)
buttonSwitch(modalWindow)
fullscreen()
resize(modalBody)
}
const openModal = (modalWindow, value) => {
const modalBody = modalWindow.querySelector('.js-modal-body')
modalBody.textContent = value
modalWindow.classList.add('is-visible')
return modalBody.innerHTML
}
const closeModal = (modalWindow) => {
const btnCloseModal = document.querySelector(".js-modal-close")
btnCloseModal.addEventListener('click', (e) => {
e.preventDefault()
modalWindow.classList.remove('is-visible')
})
document.addEventListener('keydown', (event) => {
if (event.key === 'Escape') {
modalWindow.classList.remove('is-visible')
qrCodeContainer.innerHTML = ""
qrCodeButton.classList.remove("is-active")
}
})
}
const buttonSwitch = (modalWindow) => {
const switchButton = document.querySelectorAll('.js-modal-button-switch')
switchButton.forEach(button => {
button?.addEventListener('click', (e) => {
button.classList.toggle('is-active')
if (e.currentTarget.classList.contains("js-modal-qr")) {
if (!qrCodeContainer) return
if (qrCodeContainer.innerHTML === "") {
new QRCode(qrCodeContainer, e.currentTarget.dataset.value)
} else {
qrCodeContainer.innerHTML = ""
}
}
if (e.currentTarget.classList.contains("js-modal-lights")) {
modalWindow.classList.toggle('lights-on')
}
})
})
}
const toggleFullScreen = () => {
// Thanks https://web.dev/native-hardware-fullscreen/
const doc = window.document;
const docEl = doc.documentElement;
const requestFullScreen =
docEl.requestFullscreen ||
docEl.mozRequestFullScreen ||
docEl.webkitRequestFullScreen ||
docEl.msRequestFullscreen;
const cancelFullScreen =
doc.exitFullscreen ||
doc.mozCancelFullScreen ||
doc.webkitExitFullscreen ||
doc.msExitFullscreen;
if (
!doc.fullscreenElement &&
!doc.mozFullScreenElement &&
!doc.webkitFullscreenElement &&
!doc.msFullscreenElement
) {
requestFullScreen.call(docEl);
} else {
cancelFullScreen.call(doc);
}
}
const fullscreen = () => {
const fullscreenButton = document.querySelector('.js-modal-fullscreen')
fullscreenButton.addEventListener('click', () => {
fullscreenButton.classList.toggle('is-active')
toggleFullScreen()
})
}
const resize = (modalBody) => {
const plus = document.querySelector(".js-modal-plus")
const minus = document.querySelector(".js-modal-minus")
// -- TODO: DRY ...
plus.addEventListener("click", () => {
let fontSize = Number(modalBody.dataset.fontSize)
if (fontSize >= 150) return
fontSize = fontSize + 10
modalBody.dataset.fontSize = fontSize
modalBody.setAttribute("style", `font-size: ${fontSize}%`)
})
minus.addEventListener("click", () => {
let fontSize = Number(modalBody.dataset.fontSize)
if (fontSize <= 50) return
fontSize = fontSize - 10
modalBody.dataset.fontSize = fontSize
modalBody.setAttribute("style", `font-size: ${fontSize}%`)
})
}
// FORM -------------------------------------------------------
const clearTextarea = (textarea) => {
textarea.value = ""
}
const onLoadFocus = (textarea) => {
textarea.focus()
}
if (form) {
const textarea = document.getElementById('text')
const btnClear = document.querySelector('.js-button-clear')
const btnScreen = document.querySelector('.js-button-screen')
btnScreen.addEventListener('click', () => {
if (!textarea.value) return
qrCodeButton.setAttribute("data-value", textarea.value)
openModal(modalWindow, textarea.value)
})
btnClear.addEventListener('click', () => {
clearTextarea(textarea)
})
onLoadFocus(textarea)
modalHandler()
}