-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
337 lines (295 loc) · 11.5 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import { comments, comments_en } from "./utils/comment.js"
import {
isChinese,
getCurrentLanguage,
generateModalItem,
addModal,
macDownloadBody
} from "./utils/index.js"
void (function () {
const $ = sel => document.querySelector(sel)
const $$ = sel => Array.from(document.querySelectorAll(sel))
Node.prototype.on = Node.prototype.addEventListener
function initComment() {
const { speed, currentComment, desktopContainerCls, mobileContainerCls } = $CONFIG.comment
const commentDesktopContainers = $$(desktopContainerCls)
const commentMobileContainer = $(mobileContainerCls)
const span = Math.ceil(currentComment.length / commentDesktopContainers.length)
const fragment = currentComment.map(i => {
const comment = document.createElement("div")
comment.innerHTML = `
<div>
<img src="${i.src}" alt="${i.name}" width=30 style="vertical-align: middle">
<span style="vertical-align: middle">${i.name}</span>
</div>
<p class="subtitle">${i.content}</p>
`
return comment
})
commentDesktopContainers.forEach((container, idx) =>
container.append(
...fragment.slice(idx * span, span * (idx + 1)).map(i => i.cloneNode(true)),
// the purpose of this copy is to make the animation keyframe to set `translateY(-50%)` at `to`。
...fragment.slice(idx * span, span * (idx + 1)).map(i => i.cloneNode(true)),
),
)
commentMobileContainer.append(
...fragment.map(i => i.cloneNode(true)),
...fragment.map(i => i.cloneNode(true)),
)
// make the same speed for each comment list
commentDesktopContainers.forEach(container => {
const animation = initAnimationSpeed(container)
container.classList.add(animation)
})
function initAnimationSpeed(el, s = speed) {
const { offsetHeight } = el
const uid = Math.random().toString(36).substring(2)
const style = document.createElement("style")
style.innerHTML = `
@keyframes scroll${uid} {
from {
transform: translateY(0)
}
to {
transform: translateY(-50%)
}
}
.scroll-${uid}{
animation: ${Math.floor(
((offsetHeight / 2) * 1000) / s,
)}ms scroll${uid} linear infinite normal
}`
document.getElementsByTagName("head")[0].appendChild(style)
return `scroll-${uid}`
}
}
function initTabSwitch() {
const { intervals } = $CONFIG.tab
const timer = []
for (let i = 0; i < 3; i++) {
const tabs = $$(`.carousel-${i + 1} .items .item`).filter(
i => !i.classList.contains("text-disabled"),
)
const imgs = $$(`.carousel-${i + 1} .images img`)
function updateTab(activeTab) {
tabs.forEach((tab, i) => {
tab.classList.toggle("active", tab === activeTab)
imgs[i].style.opacity = tab === activeTab ? 1 : 0
imgs[i].style.transform = `translateX(-${i * 100}%)`
})
}
tabs.forEach(tab => tab.on("click", updateTab.bind(null, tab)))
timer.push(
setInterval(() => {
const activeTab = tabs.find(tab => tab.classList.contains("active"))
const index = tabs.indexOf(activeTab)
const nextIndex = (index + 1) % tabs.length
updateTab(tabs[nextIndex])
}, intervals[i]),
)
}
window.addEventListener(
"beforeunload",
timer.forEach(t => clearInterval.bind(null, t)),
)
}
function setLanguageSwitch() {
const {
modalId,
options,
langKey,
langHref,
desktopTargetId,
mobileTargetId,
bodyCls,
queryBodyCls
} = $CONFIG.lang
const selectLangCallback = lang => {
localStorage.setItem(langKey, lang)
window.location.href = langHref[lang]
}
// desktop
const select = $(desktopTargetId)
select.innerHTML = ""
const langGroup = options.map(({ key, name }) => {
const option = document.createElement("option")
option.value = key
option.textContent = name
return option
})
select.append(...langGroup)
select.value = getCurrentLanguage()
select.on("change", e => selectLangCallback(e.target.value))
// mobile
$(mobileTargetId).on("click", e => {
langCallbackMobile()
e.stopPropagation()
})
function langCallbackMobile() {
if (!$(modalId)) {
addModal({ id: modalId.substring(1), bodyCls })
const langModalBody = $(queryBodyCls)
langModalBody.innerHTML = ""
const langGroup = options.map(({ key, name }) => {
const p = generateModalItem({ value: name }, e =>
selectLangCallback(e.target.value),
)
p.value = key
p.classList.toggle("active", key === getCurrentLanguage())
return p
})
langModalBody.append(...langGroup)
} else {
document.body.removeChild($(modalId))
}
}
}
function initFolding() {
const { id, options, target, bodyCls, queryBodyCls } = $CONFIG.folding
$(target).on("click", e => {
foldingCallback()
e.stopPropagation()
})
function foldingCallback() {
if (!$(id)) {
addModal({ id: id.substring(1), bodyCls })
const foldingModalBody = $(queryBodyCls)
foldingModalBody.innerHTML = ""
foldingModalBody.append(
...Object.keys(options).map(key =>
generateModalItem({ value: options[key].value }, options[key].callback),
),
)
} else {
document.body.removeChild($(id))
}
}
}
function setCoverAnimation() {
const { cls, maxRotation } = $CONFIG.cover
let guard = false
const cover = $(cls)
cover.on("mousemove", e => {
guard = true
requestAnimationFrame(() => {
if (!guard) {
return
}
const { offsetWidth, offsetHeight } = cover
const { left, top } = cover.getBoundingClientRect()
const { clientX: mouseX, clientY: mouseY } = e
const centerX = left + offsetWidth / 2
const centerY = top + offsetHeight / 2
const rotateX = maxRotation * ((centerX - mouseX) / (offsetWidth / 2))
const rotateY = -maxRotation * ((centerY - mouseY) / (offsetHeight / 2))
// Actually, the animation is not matched with design.
// The design describes gravity dynamics, where all four corners are pressed down, which is not the case here.
// @TODO: This needs to be optimized if there is time
// if ((rotateX > 0 && rotateY < 0 || rotateX < 0 && rotateY > 0) && Math.abs(rotateX) > 0.1 && Math.abs(rotateY) > 0.1) {
// rotateX = -rotateX
// rotateY = -rotateY
// }
cover.style.transform = `perspective(700px) rotateX(${rotateX}deg) rotateY(${rotateY}deg)`
cover.style.transition = "transform .1s ease-in-out"
})
})
cover.on("mouseleave", () => {
guard = false
cover.style.transform = "none"
cover.style.transition = "transform .7s ease-in-out"
})
}
function initMacDownloadModal() {
const { target, closeCls, id, bodyCls, queryBodyCls } = $CONFIG.download
$(target).on("click", e => {
foldingCallback()
e.stopPropagation()
})
function foldingCallback() {
if (!$(id)) {
addModal({ id: id.substring(1), bodyCls }, "desktop")
const foldingModalBody = $(queryBodyCls)
foldingModalBody.innerHTML = macDownloadBody()
$(closeCls)?.on("click", foldingCallback)
} else {
document.body.removeChild($(id))
}
}
}
function comingSoon() {
$$(".download-list a").forEach(a => a.on("click", (e) => {
if (!a.href) {
alert("Coming soon...")
e.stopPropagation()
}
}))
}
function main() {
const CONFIG = {
folding: {
id: "#folding-modal",
target: "#folding-mobile",
bodyCls: "folding",
queryBodyCls: ".modal-wrap-mobile.folding",
options: {
feedback: {
value: isChinese() ? "问题反馈" : "Feedback",
callback: () =>
window.open("https://github.com/netless-io/flat/issues", "_blank"),
}
}
},
lang: {
desktopTargetId: "#lang",
mobileTargetId: "#lang-mobile",
langKey: "flat:language",
langHref: {
"zh-CN": "/zh/",
"en": "/",
},
options: [
{ key: "en", name: "English" },
{ key: "zh-CN", name: "简体中文" },
],
modalId: "#lang-modal",
bodyCls: "lang",
queryBodyCls: ".modal-wrap-mobile.lang",
},
tab: {
intervals: [4000, 5000, 6000]
},
download: {
id: "#download-modal",
target: "#mac-download",
bodyCls: "download",
queryBodyCls: ".modal-wrap.download",
closeCls: ".modal-close-btn",
},
cover: {
cls: ".cover",
maxRotation: 1 // adjust this value to change the rotation effect
},
comment: {
speed: 35,
currentComment: isChinese() ? comments : comments_en,
desktopContainerCls: ".scroll .desktop",
mobileContainerCls: ".scroll .mobile",
}
}
window.$CONFIG = CONFIG
// mobile will be coming soon
// comingSoon();
initComment() // set comments
initTabSwitch() // set carousel item event
initMacDownloadModal() // set mac download modal
initFolding() // set folding
// setLanguageSwitch() // set language
setCoverAnimation() // set header cover press effect
// set header shadow on scroll
window.addEventListener("scroll", () =>
$("header").classList.toggle("with-shadow", window.scrollY > 0),
)
}
document.addEventListener("DOMContentLoaded", main)
})()