1
1
let maxVertical = 0 ;
2
2
let minVertical = 0 ;
3
-
4
3
/**
5
4
* Focus the next focusable element based on the actualHorizontal and actualVertical indexes
6
5
*/
7
6
const carouselHorizontalMovement = ( ) => {
8
7
const carouselCard = document ?. querySelectorAll ( '.carousel-container' ) [ window . actualVertical ] ?. querySelectorAll ( '.focusable-element' ) [
9
8
window . actualHorizontal
10
9
] ;
11
-
12
10
checkBlockedCarousels ( ) ;
13
11
carouselCard . focus ( ) ;
14
12
} ;
15
-
16
13
/**
17
14
* Check the next focusable element on a grid carousel
18
15
*
@@ -26,7 +23,6 @@ const carouselGridMovement = (movement = 'down | up | left | right') => {
26
23
?. querySelectorAll ( '.carousel-container' )
27
24
[ window . actualVertical ] ?. querySelectorAll ( '.carousel-container-row' ) ;
28
25
const actualRow = carouselGridRows [ window . actualGridRow ] . childElementCount ;
29
-
30
26
if ( movement === 'down' || movement === 'up' ) {
31
27
try {
32
28
// Getting quanity of childs on previous or next row
@@ -42,15 +38,13 @@ const carouselGridMovement = (movement = 'down | up | left | right') => {
42
38
window . actualGridRow -= 1 ;
43
39
}
44
40
}
45
-
46
41
if ( actualRow - 1 === rowToFocus || window . actualGridCell === 0 ) {
47
42
// Focusing on same position on next row
48
43
carouselGridRows [ window . actualGridRow ] . querySelectorAll ( '.focusable-element' ) [ window . actualGridCell ] . focus ( ) ;
49
44
} else {
50
45
try {
51
46
// Calculating neareast aligned child
52
47
const actualChildrenPos = Math . round ( ( rowToFocus * window . actualGridCell + 1 ) / actualRow ) ;
53
-
54
48
carouselGridRows [ window . actualGridRow ] . querySelectorAll ( '.focusable-element' ) [ actualChildrenPos ] . focus ( ) ;
55
49
window . actualGridCell = actualChildrenPos ;
56
50
} catch ( e2 ) {
@@ -83,7 +77,6 @@ const carouselGridMovement = (movement = 'down | up | left | right') => {
83
77
} catch ( e ) {
84
78
try {
85
79
let rowToFocus ;
86
-
87
80
// Try getting previous or next row
88
81
// Getting quanity of childs on previous or next row
89
82
if ( movement === 'right' ) {
@@ -113,7 +106,6 @@ const carouselGridMovement = (movement = 'down | up | left | right') => {
113
106
}
114
107
}
115
108
} ;
116
-
117
109
/**
118
110
* If the user goes down or right, sum +1 to actualVertical index
119
111
* If the user goes up or left, substract -1 to actualVertical index
@@ -125,7 +117,6 @@ const carouselGridMovement = (movement = 'down | up | left | right') => {
125
117
*/
126
118
const focusNextCarousel = ( movement = 'down | up | left | right' ) => {
127
119
const listOfCarousels = document ?. querySelectorAll ( '.carousel-container' ) ;
128
-
129
120
if ( movement === 'down' || movement === 'right' ) {
130
121
try {
131
122
if ( ( maxVertical !== 0 && window . actualVertical < maxVertical ) || ( maxVertical === 0 && minVertical === 0 ) ) {
@@ -156,7 +147,6 @@ const focusNextCarousel = (movement = 'down | up | left | right') => {
156
147
}
157
148
}
158
149
} ;
159
-
160
150
/**
161
151
* Check if the currently focused carousel is a grid or a normal carousel (vertical or horizontal)
162
152
*
@@ -168,11 +158,9 @@ const checkTypeOfCarousel = (listOfCarousels) => {
168
158
// Reset variables for grid carousel
169
159
window . isInGridCarousel = true ;
170
160
window . isInNormalCarousel = false ;
171
-
172
161
window . actualHorizontal = 0 ;
173
162
window . actualGridRow = 0 ;
174
163
window . actualGridCell = 0 ;
175
-
176
164
// Focus the element based on row and cell
177
165
listOfCarousels [ window . actualVertical ]
178
166
. querySelectorAll ( '.carousel-container-row' )
@@ -183,48 +171,40 @@ const checkTypeOfCarousel = (listOfCarousels) => {
183
171
// Reset variables for normal carousel
184
172
window . isInNormalCarousel = true ;
185
173
window . isInGridCarousel = false ;
186
-
187
174
window . actualGridRow = 0 ;
188
175
window . actualGridCell = 0 ;
189
176
window . actualHorizontal = 0 ;
190
177
// Focus the current carousel
191
178
carouselHorizontalMovement ( ) ;
192
179
}
193
180
} ;
194
-
195
181
/**
196
182
* Vertical center the next carousel
197
183
* If the element height is less than the body or the current vertical position is 0, mantain the scroll on 0
198
184
* Else; do the vertical center
199
185
*/
200
186
const carouselVerticalCenter = ( ) => {
201
187
const carousel = document ?. querySelectorAll ( '.carousel-container' ) ;
202
-
203
188
let y =
204
189
carousel [ window . actualVertical ] . offsetTop - window . innerHeight / 2 + carousel [ window . actualVertical ] . getBoundingClientRect ( ) . height / 2 ;
205
190
y = Math . round ( y ) ;
206
-
207
191
carousel [ window . actualVertical ] . focus ( ) ;
208
-
209
192
if ( y < 100 || window . actualVertical === 0 || ( window . isInGridCarousel && window . actualGridCell === 0 ) ) {
210
193
document . getElementsByTagName ( 'body' ) [ 0 ] . scrollTop = 0 ;
211
194
} else {
212
195
document . getElementsByTagName ( 'body' ) [ 0 ] . scrollTop = y ;
213
196
}
214
197
} ;
215
-
216
198
/**
217
199
* Vertical center the next row on grid carousels
218
200
* If the element height is less than the body, mantain the scroll on 0
219
201
* Else; do the vertical center
220
202
*/
221
203
const carouselVerticalCenterGrid = ( ) => {
222
204
const carousel = document ?. querySelectorAll ( '.carousel-container' ) [ window . actualVertical ] ?. querySelectorAll ( '.carousel-container-row' ) ;
223
-
224
205
let y =
225
206
carousel [ window . actualGridRow ] . offsetTop - window . innerHeight / 2 + carousel [ window . actualGridRow ] . getBoundingClientRect ( ) . height / 2 ;
226
207
y = Math . round ( y ) ;
227
-
228
208
if (
229
209
document ?. querySelectorAll ( '.carousel-container' ) [ window . actualVertical ] . clientHeight <
230
210
document . getElementsByTagName ( 'body' ) [ 0 ] . clientHeight
@@ -234,21 +214,17 @@ const carouselVerticalCenterGrid = () => {
234
214
document . getElementsByTagName ( 'body' ) [ 0 ] . scrollTop = y ;
235
215
}
236
216
} ;
237
-
238
217
/**
239
218
* Click the focused element and focus the next element based on the window.actualVertical and window.actualHorizontal
240
219
*/
241
220
const carouselOK = ( ) => {
242
221
document . getElementsByTagName ( 'body' ) [ 0 ] . scrollTop = 0 ;
243
-
244
222
// Stop double propagation on button elements
245
223
if ( document . activeElement . tagName !== 'BUTTON' ) {
246
224
document . activeElement . click ( ) ;
247
225
}
248
-
249
226
focusElement ( ) ;
250
227
} ;
251
-
252
228
/**
253
229
* If there is not any focus, focus the element based on the window.actualVertical and window.actualHorizontal
254
230
*/
@@ -264,7 +240,6 @@ const focusElement = () => {
264
240
firstFocusableElementCounter += 1 ;
265
241
} , 200 ) ;
266
242
} ;
267
-
268
243
/**
269
244
* Close any closable carousel
270
245
*
@@ -277,7 +252,6 @@ const closeCarousel = () => {
277
252
}
278
253
return false ;
279
254
} ;
280
-
281
255
/*
282
256
* If it is a blocked container
283
257
* * Get all the quantity of blocked containers on the page
@@ -291,7 +265,11 @@ const closeCarousel = () => {
291
265
const checkBlockedCarousels = ( ) => {
292
266
const carousel = document ?. querySelectorAll ( '.carousel-container' ) ;
293
267
if ( carousel [ window . actualVertical ] ?. classList . contains ( 'blocked-container' ) && minVertical === 0 && maxVertical === 0 ) {
294
- minVertical = window . actualVertical - 1 ;
268
+ if ( window . actualVertical === 0 ) {
269
+ minVertical = 0 ;
270
+ } else {
271
+ minVertical = window . actualVertical - 1 ;
272
+ }
295
273
maxVertical = minVertical + document . querySelectorAll ( '.blocked-container' ) . length - 1 ;
296
274
} else if ( ! carousel [ window . actualVertical ] ?. classList . contains ( 'blocked-container' ) ) {
297
275
minVertical = 0 ;
0 commit comments