-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathui.js
More file actions
351 lines (298 loc) · 11.1 KB
/
Copy pathui.js
File metadata and controls
351 lines (298 loc) · 11.1 KB
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
const uiWrapper = document.querySelector('.ui-wrapper')
let constructorCb = null
let followElements = []
let followElementsPositions = []
let popup = null
let carousels = []
class Carousel {
constructor (container) {
this.container = container
this.track = container.querySelector('.carousel-track')
this.prevBtn = container.querySelector('.carousel-btn--prev')
this.nextBtn = container.querySelector('.carousel-btn--next')
this.indicators = container.querySelector('.carousel-indicators')
if (!this.track) return
this.items = Array.from(this.track.children)
this.pageCount = 0
this.init()
}
init () {
this.updatePagination()
if (this.prevBtn) {
this.prevBtn.addEventListener('click', (e) => {
e.preventDefault()
e.stopPropagation()
this.scrollStep('prev')
})
}
if (this.nextBtn) {
this.nextBtn.addEventListener('click', (e) => {
e.preventDefault()
e.stopPropagation()
this.scrollStep('next')
})
}
let scrollTid
this.track.addEventListener('scroll', () => {
clearTimeout(scrollTid)
scrollTid = setTimeout(() => this.updateActiveDot(), 40)
}, { passive: true })
window.addEventListener('resize', () => {
this.updatePagination()
this.updateActiveDot()
}, { passive: true })
}
updatePagination () {
if (!this.indicators) return
this.indicators.innerHTML = ''
const trackWidth = this.track.clientWidth
const scrollWidth = this.track.scrollWidth
const maxScroll = scrollWidth - trackWidth
if (maxScroll <= 10) {
this.indicators.style.display = 'none'
if (this.prevBtn) this.prevBtn.style.display = 'none'
if (this.nextBtn) this.nextBtn.style.display = 'none'
this.pageCount = 0
return
}
this.indicators.style.display = 'flex'
if (this.prevBtn) this.prevBtn.style.display = 'flex'
if (this.nextBtn) this.nextBtn.style.display = 'flex'
// Number of dots: for short lists use item count, for long lists use proportional steps
const rawPages = Math.round(scrollWidth / (trackWidth * 0.75))
this.pageCount = Math.min(this.items.length, Math.max(2, Math.min(8, rawPages)))
for (let i = 0; i < this.pageCount; i++) {
const dot = document.createElement('button')
dot.className = `carousel-dot ${i === 0 ? 'carousel-dot--active' : ''}`
dot.setAttribute('type', 'button')
dot.setAttribute('aria-label', `Slide ${i + 1} of ${this.pageCount}`)
dot.addEventListener('click', (e) => {
e.preventDefault()
e.stopPropagation()
const targetScroll = maxScroll * (i / (this.pageCount - 1))
this.track.scrollTo({ left: targetScroll, behavior: 'smooth' })
})
this.indicators.appendChild(dot)
}
}
updateActiveDot () {
if (!this.indicators || !this.pageCount) return
const dots = this.indicators.querySelectorAll('.carousel-dot')
if (!dots.length) return
const trackWidth = this.track.clientWidth
const maxScroll = this.track.scrollWidth - trackWidth
if (maxScroll <= 0) return
const currentScroll = this.track.scrollLeft
const ratio = Math.max(0, Math.min(1, currentScroll / maxScroll))
const activeIndex = Math.min(dots.length - 1, Math.round(ratio * (dots.length - 1)))
dots.forEach((dot, idx) => {
dot.classList.toggle('carousel-dot--active', idx === activeIndex)
})
}
scrollStep (direction) {
const scrollAmount = Math.max(160, this.track.clientWidth * 0.75)
const delta = direction === 'next' ? scrollAmount : -scrollAmount
this.track.scrollBy({ left: delta, behavior: 'smooth' })
}
reset () {
if (this.track) {
this.track.scrollLeft = 0
this.updateActiveDot()
}
}
}
export default class UI {
constructor (callback) {
constructorCb = callback
followElements = uiWrapper.querySelectorAll('[data-follow]')
this.getFollowElementsPosition()
const events = [
{ selector: '.burger', cb: this.toggleMenu.bind(this) },
{ selector: '.header-logo-link', cb: this.onPagingClick.bind(this) },
{ selector: '.fixed-content-header__contact', cb: this.onPagingClick.bind(this) },
{ selector: '.fixed-content-paging', cb: this.onPagingClick.bind(this) },
{ selector: '.menu-list', cb: this.onMenuPagingClick.bind(this) },
{ selector: '#button_email_list', cb: this.showPopup.bind(this, 'video') }
]
events.forEach(event => {
const element = uiWrapper.querySelector(event.selector)
if (element) element.addEventListener('click', event.cb)
})
// Keyboard activation for menu items and paging dots
const activateOnKey = cb => e => {
if (e.key !== 'Enter' && e.key !== ' ' && e.key !== 'Spacebar') return
if (!e.target.dataset || e.target.dataset.page === undefined) return
e.preventDefault()
cb(e)
}
const menuList = uiWrapper.querySelector('.menu-list')
if (menuList) menuList.addEventListener('keydown', activateOnKey(this.onMenuPagingClick.bind(this)))
const fixedPaging = uiWrapper.querySelector('.fixed-content-paging')
if (fixedPaging) fixedPaging.addEventListener('keydown', activateOnKey(this.onPagingClick.bind(this)))
// Escape key closes popup or menu
window.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
if (popup && popup.classList.contains('popup--active')) {
this.closePopup()
} else if (uiWrapper.classList.contains('menu-opened')) {
this.toggleMenu()
}
}
})
// Close popup on close button click
const popupCloseBtn = uiWrapper.querySelector('.popup__close-button')
if (popupCloseBtn) {
popupCloseBtn.addEventListener('click', (e) => {
e.stopPropagation()
this.closePopup()
})
}
// Window resize re-computes button follow coordinates
window.addEventListener('resize', () => {
this.getFollowElementsPosition()
}, { passive: true })
const footerYear = uiWrapper.querySelector('.footer-copy__date')
if (footerYear) footerYear.innerHTML = `© ${new Date().getFullYear()}`
// Initialize all responsive carousels
this.initCarousels()
}
initCarousels () {
carousels = []
const containers = uiWrapper.querySelectorAll('.carousel-container')
containers.forEach(container => {
carousels.push(new Carousel(container))
})
}
ui_moveEvent (e, Use2DTextOver3D) {
this.buttonMoveAnimation(e)
if (Use2DTextOver3D) {
this.mainLetters2DAnimation(e)
}
}
buttonMoveAnimation (e) {
const mouseLeft = e.clientX
const mouseTop = e.clientY
followElements.forEach((element, index) => {
const elementPositions = followElementsPositions[index]
if (!elementPositions) return
if (mouseLeft > elementPositions.left - 100 && mouseLeft < elementPositions.right + 100 &&
mouseTop > elementPositions.top - 100 && mouseTop < elementPositions.bottom + 100) {
const moveX = (elementPositions.left - mouseLeft) / 10
const moveY = (elementPositions.top - mouseTop) / 10
const isCenteredY = element.dataset.follow === 'centered_y'
element.style.transform = `translate3d(${-moveX}px, calc(${isCenteredY ? -50 : 0}% + ${-moveY}px), 0)`
element.style.transition = ''
} else {
element.style.transform = ''
element.style.transition = 'transform 500ms ease'
}
})
}
mainLetters2DAnimation (e) {
const letters = document.querySelector('.configuration__letters')
if (!letters) return
const xCenter = window.innerWidth / 2
const yCenter = window.innerHeight / 2
const LettersXPosition = xCenter - e.clientX
const LettersYPosition = yCenter - e.clientY
letters.style.transform = `rotateX(${-LettersXPosition / 50}deg) rotateY(${LettersYPosition / 50}deg) translateX(-50%)`
}
showPopup (popupType) {
popup = uiWrapper.querySelector(`[data-popup=${popupType}]`)
if (!popup) return
popup.classList.add('popup--active')
popup.addEventListener('click', this.handleBackdropClick.bind(this))
constructorCb().blockSceneScrolling(true)
}
handleBackdropClick (e) {
if (e.target === popup) {
this.closePopup()
}
}
closePopup () {
if (!popup) return
popup.classList.remove('popup--active')
popup.removeEventListener('click', this.handleBackdropClick.bind(this))
constructorCb().blockSceneScrolling(false)
}
toggleMenu () {
const isOpen = uiWrapper.classList.toggle('menu-opened')
const burger = uiWrapper.querySelector('.burger')
if (burger) {
burger.classList.toggle('burger--active', isOpen)
burger.setAttribute('aria-expanded', isOpen ? 'true' : 'false')
}
}
ui_moveScene (direction) {
this.checkContentVisibility(direction)
}
onMenuPagingClick (e) {
const target = e.target.closest('[data-page]')
if (!target) return
this.toggleMenu()
this.onPagingClick({ target })
}
onPagingClick (e) {
const target = e.target.closest('[data-page]')
if (!target) return
const datasetPage = +target.dataset.page
if (datasetPage >= 0) {
constructorCb().onPagingClick(datasetPage)
}
}
getFollowElementsPosition () {
followElementsPositions = []
followElements.forEach(element => followElementsPositions.push(element.getBoundingClientRect()))
}
checkContentVisibility (direction) {
const currentPage = constructorCb().getCurrentPage()
const contentSections = uiWrapper.querySelectorAll('.content-section[data-page], .paging__page[data-page]')
// Update active highlight on menu items
const menuItems = uiWrapper.querySelectorAll('.menu-list__item')
menuItems.forEach(item => {
const page = +item.dataset.page
item.classList.toggle('menu-list__item--active', page === currentPage)
})
const animateSection = section => {
const sectionPage = +section.dataset.page
const isActive = sectionPage === currentPage
if (direction === 'down') {
section.classList.add('section--hidden')
section.classList.remove('section--hidden-reverse')
} else {
section.classList.add('section--hidden-reverse')
section.classList.remove('section--hidden')
}
if (isActive) {
section.style.display = 'flex'
section.scrollTop = 0
const updateTrack = () => {
const track = section.querySelector('.carousel-track')
if (track) {
track.scrollLeft = 0
const c = carousels.find(entry => entry.track === track)
if (c) {
c.updatePagination()
c.updateActiveDot()
}
}
}
const removeClass = () => {
section.classList.remove('section--hidden', 'section--hidden-reverse')
updateTrack()
}
window.requestAnimationFrame(() => {
window.requestAnimationFrame(removeClass)
})
setTimeout(updateTrack, 300)
}
}
contentSections.forEach(section => {
section.style.opacity = '0'
setTimeout(() => {
section.removeAttribute('style')
animateSection(section)
}, 250)
})
}
}