-
Notifications
You must be signed in to change notification settings - Fork 15
/
toggle.js
3037 lines (2917 loc) · 90.5 KB
/
toggle.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import RJSON from 'relaxed-json'
import { Xt } from 'xtend-library/src/xt.js'
/**
* Toggle
*/
class Toggle {
/**
* constructor
* @param {Node|HTMLElement|EventTarget|Window} object Base node
* @param {Object} optionsCustom User options
* @constructor
*/
constructor(object, optionsCustom = {}) {
const self = this
self.object = object
self.optionsCustom = optionsCustom
self.componentName = self.constructor.componentName
self.componentNamespace = self.componentName.replace(/^[^a-z]+|[ ,#_:.-]+/gi, '')
Xt.destroyAndInit(self)
}
//
// init
//
/**
* init
* @param {Node|HTMLElement|EventTarget|Window} object Base node
* @param {Object} optionsCustom User options
*/
init(object = false, optionsCustom = false) {
const self = this
self.object = object || self.object
self.optionsCustom = optionsCustom || self.optionsCustom
// vars
self.classes = []
self.classesIn = []
self.classesOut = []
self.classesInitial = []
self.classesInverse = []
self.elements = []
self.targets = []
self.currentIndex = null
self.direction = null
self.inverse = null
self.initialCurrents = []
self.detail = {}
self.disabled = false
self.detail.queueOn = []
self.detail.queueOff = []
self.detail.autopaused = false
self.destroyElements = [document, window, self.object]
// init
self.initVars()
self.initSetup()
self.initLogic()
}
/**
* init vars
*/
initVars() {
const self = this
// options
if (self.constructor.optionsDefaultSuper) {
self.optionsDefault = Xt.merge([self.constructor.optionsDefaultSuper, self.constructor.optionsDefault])
} else {
self.optionsDefault = self.constructor.optionsDefaultSuper
}
self.optionsDefault = Xt.merge([self.optionsDefault, Xt.optionsGlobal[self.componentName]])
self.options = Xt.merge([self.optionsDefault, self.optionsCustom ? self.optionsCustom : {}])
// classes
self.classes = self.options.class ? [...self.options.class.split(' ')] : []
self.classesIn = self.options.classIn ? [...self.options.classIn.split(' ')] : []
self.classesInDone = self.options.classInDone ? [...self.options.classInDone.split(' ')] : []
self.classesOut = self.options.classOut ? [...self.options.classOut.split(' ')] : []
self.classesInitial = self.options.classInitial ? [...self.options.classInitial.split(' ')] : []
self.classesInverse = self.options.classInverse ? [...self.options.classInverse.split(' ')] : []
}
/**
* init setup
*/
initSetup() {
const self = this
const options = self.options
// setup (based on xtend mode)
if (options.targets && options.targets.indexOf('#') !== -1) {
// xtend unique mode
self.mode = 'unique'
self.container = document.documentElement
options.max = Infinity
self.namespace = self.componentName + '-' + options.targets.toString() + '-' + self.classes.toString()
} else {
// xtend multiple mode
self.mode = 'multiple'
self.container = self.object
const uniqueId = Xt.dataStorage.get(self.container, 'xtUniqueId')
Xt.dataStorage.set(self.container, 'xtUniqueId', uniqueId || Xt.getuniqueId())
self.namespace = self.componentName + '-' + Xt.dataStorage.get(self.container, 'xtUniqueId')
}
// final namespace
self.namespace = self.namespace.replace(/^[^a-z]+|[ ,#_:.-]+/gi, '')
// currents array based on namespace (so shared between Xt objects)
self.setCurrents([])
}
/**
* init logic
* @param {Boolean} saveCurrents
*/
initLogic(saveCurrents = true) {
const self = this
// init
self.enable()
self.initScope()
self.initAria()
self.initStart(saveCurrents)
}
/**
* init elements, targets and currents
*/
initScope() {
const self = this
// elements
self.initScopeElements()
// targets
self.initScopeTargets()
}
/**
* init elements
*/
initScopeElements() {
const self = this
const options = self.options
// elements
if (options.elements) {
let arr = Array.from(Xt.arrSingle(self.container.querySelectorAll(options.elements)))
arr = arr.filter(x => !x.closest('.xt-ignore')) // filter out ignore
arr = arr.filter(x => !x.closest('[data-xt-nav]')) // filter out nav
self.elements = arr
self.destroyElements.push(...self.elements)
// reset namespace
Xt.dataStorage.remove(self.namespace, 'xtNamespaceDone')
}
// xtend unique mode
if (!self.elements.length) {
self.elements = Xt.arrSingle(self.object)
// set namespace for next frame
requestAnimationFrame(() => {
let arr = Xt.dataStorage.get(self.namespace, 'xtNamespace')
arr = arr.filter(x => !x.closest('.xt-ignore')) // filter out ignore
if (arr.length) {
// fix when using shadow dom doesn't query deep
self.elements = arr
self.destroyElements.push(...self.elements)
}
// reset namespace
Xt.dataStorage.remove(self.namespace, 'xtNamespaceDone')
})
}
// set self
for (const el of self.elements) {
Xt.set(self.componentName, el, self)
}
}
/**
* init targets
*/
initScopeTargets() {
const self = this
const options = self.options
// targets
if (options.targets) {
let arr = Array.from(self.container.querySelectorAll(options.targets))
// arr = arr.filter(x => !x.parentElement.closest(options.targets)) // filter out parent
arr = arr.filter(x => !x.closest('.xt-ignore')) // filter out ignore
self.targets = arr
self.destroyElements.push(...self.targets)
}
// set self
for (const tr of self.targets) {
Xt.set(self.componentName, tr, self)
}
}
/**
* init start
* @param {Boolean} saveCurrents
*/
initStart(saveCurrents = false) {
const self = this
const options = self.options
// status
self.eventStatusHandler()
// vars
let currents = 0
self.initial = true
self.wrap = false
self.currentIndex = null
// [disabled]
self.destroyDisabled()
// reset namespace
if (!Xt.dataStorage.get(self.namespace, 'xtNamespaceDone')) {
Xt.dataStorage.set(self.namespace, 'xtNamespace', [])
Xt.dataStorage.set(self.namespace, 'xtNamespaceDone', true)
}
// set namespace for next frame
for (const el of self.elements) {
const arr = Xt.dataStorage.get(self.namespace, 'xtNamespace')
arr.push(el)
Xt.dataStorage.set(self.namespace, 'xtNamespace', arr)
}
// automatic initial currents
const elements = self.getGroups()
if (elements.length) {
// check elements
for (const element of elements) {
// reset
const found = self.initReset(element, saveCurrents)
if (found && currents < options.max) {
// initial
currents++
// reactivate
requestAnimationFrame(() => {
// activation
self.eventOn(element, true)
})
}
}
// if currents < min
let todo = options.min - currents
if (todo > 0 && self.targets.length) {
let start = 0
// @FIX initial activation drag wrap
if ((!self.disabled || !self.initial) && self.wrapIndex) {
start = self.wrapIndex
todo += start
}
// initial
currents += todo
// activation
requestAnimationFrame(() => {
for (let i = start; i < todo; i++) {
self.eventOn(self.elements[i], true)
}
})
}
// initial
self.setCurrents([])
if (saveCurrents) {
requestAnimationFrame(() => {
self.initialCurrents = self.getCurrents().slice(0)
})
}
// no currents
if (currents === 0) {
// vars
self.initial = false
// @FIX autostart after self.initial or it gives error on reinitialization (demos fullscreen)
// auto
self.eventAutostart()
}
}
// init events
self.initEvents()
// listener dispatch
requestAnimationFrame(() => {
self.object.dispatchEvent(new CustomEvent('init.xt'))
})
}
/**
* init reset element activation
* @param {Node|HTMLElement|EventTarget|Window} el Element to check and reset
* @param {Boolean} saveCurrents
* @return {Boolean} if element was activated
*/
initReset(el, saveCurrents = false) {
const self = this
let found = false
// reset
const reset = elReset => {
let isActive = false
for (const c of self.classes) {
if (elReset.classList.contains(c)) {
isActive = true
break
}
}
if (isActive) {
if (saveCurrents) {
found = true
}
requestAnimationFrame(() => {
elReset.classList.remove(
...self.classes,
...self.classesIn,
...self.classesInDone,
...self.classesOut,
...self.classesInitial,
...self.classesInverse
)
})
}
return found
}
// elements
const group = el.getAttribute('data-xt-group')
if (group) {
const groupEls = Array.from(self.elements).filter(x => x.getAttribute('data-xt-group') === group)
for (const groupEl of groupEls) {
found = reset(groupEl)
if (!saveCurrents && self.initialCurrents.includes(groupEl)) {
found = true
}
}
} else {
found = reset(el)
if (!saveCurrents && self.initialCurrents.includes(el)) {
found = true
}
}
// targets
const targets = self.getTargets(el)
for (const tr of targets) {
found = reset(tr)
}
return found
}
/**
* init aria
*/
initAria() {
const self = this
const options = self.options
// role
self.initAriaRole()
// aria
if (options.aria) {
if (self.targets.length) {
// elements
for (const el of self.elements) {
const ariaEls = Xt.queryAll(el, options.ariaControls)
const ariaEl = ariaEls.length ? ariaEls[0] : el
// id
if (options.aria === true || options.aria.labelledby || options.aria.controls) {
const id = ariaEl.getAttribute('id')
if (!id) {
ariaEl.setAttribute('id', Xt.getuniqueId())
}
}
// selected
ariaEl.setAttribute('aria-selected', 'false')
}
// targets
for (const tr of self.targets) {
// expanded
const role = tr.getAttribute('role')
if (role === 'tabpanel' || role === 'listbox' || role === 'dialog') {
tr.setAttribute('aria-expanded', 'false')
}
// tabindex
if (options.aria === true || options.aria.tabindex) {
const focusables = tr.querySelectorAll(Xt.focusables)
for (const focusable of focusables) {
focusable.setAttribute('tabindex', '-1')
}
}
// id
if (options.aria === true || options.aria.labelledby || options.aria.controls) {
const id = tr.getAttribute('id')
if (!id) {
tr.setAttribute('id', Xt.getuniqueId())
}
}
// labelledby
if (options.aria === true || options.aria.labelledby) {
const els = self.getElements(tr)
let str = ' '
str += ''
for (const el of els) {
const ariaEls = Xt.queryAll(el, options.ariaControls)
const ariaEl = ariaEls.length ? ariaEls[0] : el
str += ' ' + ariaEl.getAttribute('id')
}
tr.setAttribute('aria-labelledby', str.trim())
}
}
// ariaControls
if (options.aria === true || options.aria.controls) {
for (const el of self.elements) {
const trs = self.getTargets(el)
const ariaEls = Xt.queryAll(el, options.ariaControls)
const ariaEl = ariaEls.length ? ariaEls[0] : el
// controls
let str = ' '
str += ''
for (const tr of trs) {
str += ' ' + tr.getAttribute('id')
}
ariaEl.setAttribute('aria-controls', str.trim())
}
}
}
}
}
/**
* init aria role
*/
initAriaRole() {
const self = this
const options = self.options
// aria
if (options.aria) {
if (self.targets.length) {
// role
if (self.mode === 'multiple') {
self.object.setAttribute('role', 'tablist')
if (options.max > 1) {
self.object.setAttribute('aria-multiselectable', 'true')
}
for (const el of self.elements) {
const ariaEls = Xt.queryAll(el, options.ariaControls)
const ariaEl = ariaEls.length ? ariaEls[0] : el
ariaEl.setAttribute('role', 'tab')
}
for (const tr of self.targets) {
tr.setAttribute('role', 'tabpanel')
}
}
}
}
}
/**
* init events
*/
initEvents() {
const self = this
const options = self.options
// status
const checkHandler = Xt.dataStorage.put(window, 'resize/check' + '/' + self.namespace, self.eventStatusHandler.bind(self).bind(self))
addEventListener('resize', checkHandler)
// elements
for (const el of self.elements) {
// event on
const onHandler = Xt.dataStorage.put(el, options.on + '/' + self.namespace, self.eventOnHandler.bind(self).bind(self, el))
if (options.on) {
const events = [...options.on.split(' ')]
for (const event of events) {
el.addEventListener(event, onHandler)
}
// @FIX prevents click links on click until clicked two times
const withlinkStartTouchHandler = Xt.dataStorage.put(
el,
'touchend/withlink' + '/' + self.namespace,
self.eventWithlinkStartHandler.bind(self).bind(self, el)
)
el.addEventListener('touchend', withlinkStartTouchHandler)
if (!events.includes('mouseenter') && !events.includes('mousehover')) {
const withlinkStartMouseHandler = Xt.dataStorage.put(
el,
'mouseup/withlink' + '/' + self.namespace,
self.eventWithlinkStartHandler.bind(self).bind(self, el)
)
el.addEventListener('mouseup', withlinkStartMouseHandler)
}
Xt.dataStorage.put(el, 'active/withlink' + '/' + self.namespace, self.hasCurrent(el))
}
const onHandlerCustom = Xt.dataStorage.put(el, 'on' + '/' + self.namespace, self.eventOnHandler.bind(self).bind(self, el))
el.addEventListener('on.trigger.xt', onHandlerCustom)
// event off
if (options.off) {
const offHandler = Xt.dataStorage.put(el, options.off + '/' + self.namespace, self.eventOffHandler.bind(self).bind(self, el))
const events = [...options.off.split(' ')]
for (const event of events) {
el.addEventListener(event, offHandler)
}
const offHandlerCustom = Xt.dataStorage.put(el, 'off' + '/' + self.namespace, self.eventOffHandler.bind(self).bind(self, el))
el.addEventListener('off.trigger.xt', offHandlerCustom)
} else {
const offHandlerCustom = Xt.dataStorage.put(el, 'off' + '/' + self.namespace, self.eventOnHandler.bind(self).bind(self, el))
el.addEventListener('off.trigger.xt', offHandlerCustom)
}
}
// targets
for (const tr of self.targets) {
// event
const onHandler = Xt.dataStorage.put(tr, options.on + '/' + self.namespace, self.eventOnHandler.bind(self).bind(self, tr))
tr.addEventListener('on.trigger.xt', onHandler)
if (options.off) {
const offHandlerCustom = Xt.dataStorage.put(tr, 'off' + '/' + self.namespace, self.eventOffHandler.bind(self).bind(self, tr))
tr.addEventListener('off.trigger.xt', offHandlerCustom)
} else {
const offHandlerCustom = Xt.dataStorage.put(tr, 'off' + '/' + self.namespace, self.eventOnHandler.bind(self).bind(self, tr))
tr.addEventListener('off.trigger.xt', offHandlerCustom)
}
}
// auto
if (options.auto && options.auto.time) {
// focus
const focusHandler = Xt.dataStorage.put(window, 'focus/auto' + '/' + self.namespace, self.eventAutoresume.bind(self))
addEventListener('focus', focusHandler)
// blur
const blurHandler = Xt.dataStorage.put(window, 'blur/auto' + '/' + self.namespace, self.eventAutopause.bind(self))
addEventListener('blur', blurHandler)
// event
const autostartHandler = Xt.dataStorage.put(self.object, 'autostart' + '/' + self.namespace, self.eventAutostart.bind(self))
self.object.addEventListener('autostart.trigger.xt', autostartHandler)
const autostopHandler = Xt.dataStorage.put(self.object, 'autostop' + '/' + self.namespace, self.eventAutostop.bind(self))
self.object.addEventListener('autostop.trigger.xt', autostopHandler)
const autopauseHandler = Xt.dataStorage.put(self.object, 'autopause' + '/' + self.namespace, self.eventAutopause.bind(self))
self.object.addEventListener('autopause.trigger.xt', autopauseHandler)
const autoresumeHandler = Xt.dataStorage.put(self.object, 'autoresume' + '/' + self.namespace, self.eventAutoresume.bind(self))
self.object.addEventListener('autoresume.trigger.xt', autoresumeHandler)
// autopause
if (options.auto.pause) {
const autopauseEls = self.object.querySelectorAll(options.auto.pause)
if (autopauseEls.length) {
self.destroyElements.push(...autopauseEls)
for (const el of autopauseEls) {
// pause
const autopauseOnHandler = Xt.dataStorage.put(el, 'mouseenter focus/auto' + '/' + self.namespace, self.eventAutopause.bind(self))
const eventsPause = ['mouseenter', 'focus']
for (const event of eventsPause) {
el.addEventListener(event, autopauseOnHandler)
}
// resume
const autoresumeOnHandler = Xt.dataStorage.put(el, 'mouseleave blur/auto' + '/' + self.namespace, self.eventAutoresume.bind(self))
const eventsResume = ['mouseleave', 'blur']
for (const event of eventsResume) {
el.addEventListener(event, autoresumeOnHandler)
}
}
}
}
}
// jump
if (options.jump) {
for (const jump of self.targets) {
const jumpHandler = Xt.dataStorage.put(jump, 'click/jump' + '/' + self.namespace, self.eventJumpHandler.bind(self).bind(self, jump))
jump.addEventListener('click', jumpHandler, true) // @FIX elements inside targets (slider pagination)
// jump
if (!self.disabled) {
jump.classList.add('xt-jump')
} else {
jump.classList.remove('xt-jump')
}
}
}
// navigation
if (options.navigation) {
self.navs = self.object.querySelectorAll(options.navigation)
if (self.navs.length) {
self.destroyElements.push(...self.navs)
for (const nav of self.navs) {
const navHandler = Xt.dataStorage.put(nav, 'click/nav' + '/' + self.namespace, self.eventNavHandler.bind(self).bind(self, nav))
nav.addEventListener('click', navHandler)
}
}
}
// keyboard
if (options.keyboard && options.keyboard.selector) {
const keyboards = options.keyboard.selector === 'object' ? Xt.arrSingle(self.object) : self.object.querySelectorAll(options.keyboard.selector)
self.destroyElements.push(...keyboards)
for (const keyboard of keyboards) {
keyboard.setAttribute('tabindex', '0')
// focus
const keyboardfocusHandler = Xt.dataStorage.put(
keyboard,
'focus/keyboard' + '/' + self.namespace,
self.eventKeyboardfocusHandler.bind(self).bind(self, keyboard)
)
keyboard.addEventListener('focus', keyboardfocusHandler)
// blur
const keyboardblurHandler = Xt.dataStorage.put(
keyboard,
'blur/keyboard' + '/' + self.namespace,
self.eventKeyboardblurHandler.bind(self).bind(self, keyboard)
)
keyboard.addEventListener('blur', keyboardblurHandler)
}
}
// autoclose
if (options.autoClose) {
const autocloseHandler = Xt.dataStorage.put(window, 'autoclose' + '/' + self.namespace, self.eventAutocloseHandler.bind(self))
addEventListener('autoclose.trigger.xt', autocloseHandler)
}
// mediaLoaded
if (options.mediaLoaded || options.mediaLoadedReinit) {
for (const el of self.elements.filter(x => !x.classList.contains('xt-ignore'))) {
const imgs = el.querySelectorAll('img')
self.destroyElements.push(...imgs)
for (const img of imgs) {
if (!Xt.dataStorage.get(img, self.componentNamespace + 'MedialoadedDone')) {
Xt.dataStorage.set(img, self.componentNamespace + 'MedialoadedDone', true)
if (!img.complete) {
const medialoadedHandler = Xt.dataStorage.put(
img,
'load/media' + '/' + self.namespace,
self.eventMedialoadedHandler.bind(self).bind(self, el, true)
)
img.addEventListener('load', medialoadedHandler)
} else {
self.eventMedialoadedHandler(el)
}
}
}
}
for (const tr of self.targets.filter(x => !x.classList.contains('xt-ignore'))) {
const imgs = tr.querySelectorAll('img')
self.destroyElements.push(...imgs)
for (const img of imgs) {
if (!Xt.dataStorage.get(img, self.componentNamespace + 'MedialoadedDone')) {
Xt.dataStorage.set(img, self.componentNamespace + 'MedialoadedDone', true)
if (!img.complete) {
const medialoadedHandler = Xt.dataStorage.put(
img,
'load/media' + '/' + self.namespace,
self.eventMedialoadedHandler.bind(self).bind(self, tr, true)
)
img.addEventListener('load', medialoadedHandler)
} else {
self.eventMedialoadedHandler(tr)
}
}
}
}
}
// wheel
if (options.wheel && options.wheel.selector) {
const wheel = (self.wheel =
options.wheel.selector === 'object'
? self.object
: options.wheel.selector === 'scrollingElement'
? document.scrollingElement
: self.object.querySelector(options.wheel.selector))
self.destroyElements.push(wheel)
const eWheel = 'onwheel' in wheel ? 'wheel' : wheel.onmousewheel !== undefined ? 'mousewheel' : 'DOMMouseScroll'
// wheel
const wheelHandler = Xt.dataStorage.put(wheel, eWheel + '/' + self.namespace, self.eventWheelHandler.bind(self))
wheel.addEventListener(eWheel, wheelHandler, { passive: false })
// stop
const wheelstopHandler = Xt.dataStorage.put(wheel, eWheel + '/stop' + '/' + self.namespace, self.eventWheelstop.bind(self))
wheel.addEventListener('wheelstop.trigger.xt', wheelstopHandler, { passive: false })
// block
if (options.wheel.block) {
const block = wheel.parentNode
const wheelblockHandler = Xt.dataStorage.put(block, eWheel + '/block' + '/' + self.namespace, self.eventWheelblockHandler.bind(self))
block.addEventListener(eWheel, wheelblockHandler, { passive: false })
}
}
}
//
// handler
//
/**
* element on handler
* @param {Node|HTMLElement|EventTarget|Window} element
* @param {Event} e
*/
eventOnHandler(element, e) {
const self = this
const options = self.options
// @FIX targets handler
const el = self.getElements(element)[0]
// event block
if (options.onBlock) {
const now = new Date().getTime()
const old = Xt.dataStorage.get(el, self.componentNamespace + 'EventBlock' + e.type) || 0
Xt.dataStorage.set(el, self.componentNamespace + 'EventBlock' + e.type, now)
if (now - old < options.onBlock) {
return
}
}
// handler
if (options.eventLimit) {
const eventLimit = self.container.querySelectorAll(options.eventLimit)
if (eventLimit.length) {
if (!Xt.contains(eventLimit, e.target) || e.target.closest('.event-force')) {
self.eventOn(el, false, e)
}
} else {
self.eventOn(el, false, e)
}
} else {
self.eventOn(el, false, e)
}
}
/**
* element off handler
* @param {Node|HTMLElement|EventTarget|Window} element
* @param {Event} e
*/
eventOffHandler(element, e) {
const self = this
const options = self.options
// @FIX targets handler
const el = self.getElements(element)[0]
// event block
if (options.offBlock) {
const now = new Date().getTime()
const old = Xt.dataStorage.get(el, self.componentNamespace + 'EventBlock' + e.type) || 0
Xt.dataStorage.set(el, self.componentNamespace + 'EventBlock' + e.type, now)
if (now - old < options.offBlock) {
return
}
}
// handler
if (options.eventLimit) {
const eventLimit = self.container.querySelectorAll(options.eventLimit)
if (eventLimit.length) {
if (!Xt.contains(eventLimit, e.target) || e.target.closest('.event-force')) {
self.eventOff(el, false, e)
}
} else {
self.eventOff(el, false, e)
}
} else {
self.eventOff(el, false, e)
}
}
/**
* init prevents click on touch until clicked two times
* @param {Node|HTMLElement|EventTarget|Window} el
* @param {Event} e
*/
eventWithlinkStartHandler(el, e) {
const self = this
// active
Xt.dataStorage.put(el, 'active/withlink' + '/' + self.namespace, self.hasCurrent(el))
// event link
const withlinkHandler = Xt.dataStorage.put(el, 'click/withlink' + '/' + self.namespace, self.eventWithlinkHandler.bind(self).bind(self, el))
el.addEventListener('click', withlinkHandler)
// event reset
const withlinkResetHandler = Xt.dataStorage.put(el, 'off/withlink' + '/' + self.namespace, self.eventWithlinkResetHandler.bind(self).bind(self, el))
el.addEventListener('off.xt', withlinkResetHandler)
}
/**
* remove prevents click on touch until clicked two times
* @param {Node|HTMLElement|EventTarget|Window} el
*/
eventWithlinkEndHandler(el) {
const self = this
// event link
const withlinkHandler = Xt.dataStorage.get(el, 'click/withlink' + '/' + self.namespace)
el.removeEventListener('click', withlinkHandler)
// event reset
const withlinkResetHandler = Xt.dataStorage.get(el, 'off/withlink' + '/' + self.namespace)
el.removeEventListener('off.xt', withlinkResetHandler)
}
/**
* prevents click on touch until clicked two times
* @param {Node|HTMLElement|EventTarget|Window} el
* @param {Event} e
*/
eventWithlinkHandler(el, e) {
const self = this
const active = Xt.dataStorage.get(el, 'active/withlink' + '/' + self.namespace)
if (!active && !Xt.dataStorage.get(el, self.componentNamespace + 'WithlinkDone')) {
Xt.dataStorage.set(el, self.componentNamespace + 'WithlinkDone', true)
// prevent default
e.preventDefault()
} else {
self.eventWithlinkEndHandler(el)
Xt.dataStorage.remove(el, self.componentNamespace + 'WithlinkDone')
Xt.dataStorage.remove(el, 'active/withlink' + '/' + self.namespace)
}
}
/**
* reset prevents click on touch until clicked two times
* @param {Node|HTMLElement|EventTarget|Window} el
* @param {Event} e
*/
eventWithlinkResetHandler(el, e) {
const self = this
self.eventWithlinkEndHandler(el)
Xt.dataStorage.remove(el, self.componentNamespace + 'WithlinkDone')
Xt.dataStorage.remove(el, 'active/withlink' + '/' + self.namespace)
}
/**
* jump handler
* @param {Node|HTMLElement|EventTarget|Window} tr
* @param {Event} e
*/
eventJumpHandler(tr, e) {
const self = this
// useCapture delegation
if (self.targets.includes(tr)) {
// handler
self.eventJump(tr, e)
}
}
/**
* nav handler
* @param {Node|HTMLElement|EventTarget|Window} nav
* @param {Event} e
*/
eventNavHandler(nav, e) {
const self = this
// handler
self.eventNav(nav, e)
}
/**
* keyboard focus handler
* @param {Node|HTMLElement|EventTarget|Window} el
* @param {Event} e
*/
eventKeyboardfocusHandler(el, e) {
const self = this
// handler
const keyboardHandler = Xt.dataStorage.put(document, 'keyup/keyboard' + '/' + self.namespace, self.eventKeyboardHandler.bind(self))
document.addEventListener('keyup', keyboardHandler)
}
/**
* keyboard blur handler
* @param {Node|HTMLElement|EventTarget|Window} el
* @param {Event} e
*/
eventKeyboardblurHandler(el, e) {
const self = this
// handler
const keyboardHandler = Xt.dataStorage.get(document, 'keyup/keyboard' + '/' + self.namespace)
document.removeEventListener('keyup', keyboardHandler)
}
/**
* keyboard handler
* @param {Event} e
*/
eventKeyboardHandler(e) {
const self = this
const options = self.options
// key
const code = e.keyCode ? e.keyCode : e.which
let prev
let next
if (options.keyboard.vertical) {
if (options.keyboard.inverse) {
prev = 40
next = 38
} else {
prev = 38
next = 40
}
} else {
if (options.keyboard.inverse) {
prev = 39
next = 37
} else {
prev = 37
next = 39
}
}
if (code === prev) {
self.goToPrev(1)
} else if (code === next) {
self.goToNext(1)
}
}
/**
* autoclose handler
* @param {Event} e
*/
eventAutocloseHandler(e) {
const self = this
// triggering e.detail.container
if (!e || !e.detail || !e.detail.container || e.detail.container.contains(self.object)) {
// handler
const currents = self.getCurrents()
for (const current of currents) {
self.eventOff(current, true)
}
}
}
/**
* medialoaded
* @param {Node|HTMLElement|EventTarget|Window} el
* @param {Boolean} deferred
*/
eventMedialoadedHandler(el, deferred = false) {
const self = this
const options = self.options
// mediaLoaded
if (options.mediaLoaded) {
el.classList.add('xt-medialoaded')
}
// mediaLoadedReinit
if (options.mediaLoadedReinit && deferred) {
clearTimeout(Xt.dataStorage.get(self.object, 'xt' + self.componentNamespace + 'MedialoadedInit' + 'Timeout'))
Xt.dataStorage.set(
self.object,
'xt' + self.componentNamespace + 'MedialoadedInit' + 'Timeout',
setTimeout(() => {
// debug
if (Xt.debug === true) {
console.debug('Xt.debug: mediaLoadedReinit', el)
}
// handler
self.restart()
}, Xt.medialoadedDelay)
)
}
// listener dispatch
el.dispatchEvent(new CustomEvent('medialoaded.xt', { detail: { deferred: deferred } }))
}
//
// event util
//
/**
* get groups (one element per group)
* @return {Array} array of elements
*/
getGroups() {
const self = this
// groups
const groups = []
for (const element of self.elements) {
// choose element by group
const group = element.getAttribute('data-xt-group')
if (group) {
const found = groups.filter(x => x.getAttribute('data-xt-group') === group)
if (!found.length) {
groups.push(element)
}
} else {
groups.push(element)
}
}
return groups
}
/**
* get elements from element or target
* @param {Node|HTMLElement|EventTarget|Window} el Element that triggered interaction
* @return {Array} The first element is the one on getGroups()
*/
getElements(el) {
const self = this
// getElements
if (!self.elements || !self.elements.length) {
return []
}
if (self.mode === 'unique' || !el) {
// choose all elements
return self.elements
} else if (self.mode === 'multiple') {
// choose element by group
let final
const group = el.getAttribute('data-xt-group')
const groupElements = Array.from(self.elements).filter(x => x.getAttribute('data-xt-group') === group)
const groupTargets = Array.from(self.targets).filter(x => x.getAttribute('data-xt-group') === group)
if (group) {
// all group targets if group
final = Xt.arrSingle(groupElements)
} else {
// not group targets by index if not group
if (Array.from(self.elements).includes(el)) {
// @FIX when argument is already element
final = Xt.arrSingle(el)
} else {
const index = groupTargets.findIndex(x => x === el)
final = Xt.arrSingle(groupElements[index])
}
}
return final
}
}
/**
* get targets from element or target
* @param {Node|HTMLElement|EventTarget|Window} el Element that triggered interaction
* @return {Array}
*/
getTargets(el) {
const self = this
// getTargets
if (!self.targets || !self.targets.length) {