1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | import DOMPurify from 'dompurify'
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | export const Xt = {}
|
14 | Xt.DOMPurify = DOMPurify
|
15 |
|
16 | if (typeof window !== 'undefined') {
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | if (window.XtSetGlobal) {
|
22 | global[typeof window.XtSetGlobal === 'string' ? window.XtSetGlobal : 'Xt'] = Xt
|
23 | }
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | Xt._running = {}
|
30 | Xt._currents = {}
|
31 | Xt.options = {}
|
32 | Xt._mountArr = []
|
33 | Xt._unmountArr = []
|
34 | Xt.resizeSkip = () => matchMedia('(hover: none), (pointer: coarse)').matches
|
35 | Xt.resizeDelay = 200
|
36 | Xt.medialoadedDelay = false
|
37 | Xt.durationTimescale = 1
|
38 | Xt.autoTimescale = 1
|
39 | Xt.scrolltoHashforce = null
|
40 | Xt.formScrollWindowFactor = 0.2
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | |
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | Xt.ready = ({ func, state = 'interactive complete', raf = false } = {}) => {
|
54 | const states = [...state.split(' ')]
|
55 | if (states.includes(document.readyState)) {
|
56 | if (raf) {
|
57 |
|
58 | requestAnimationFrame(() => {
|
59 | func()
|
60 | })
|
61 | } else {
|
62 | func()
|
63 | }
|
64 | } else {
|
65 | const interactive = () => {
|
66 | if (states.includes(document.readyState)) {
|
67 | func()
|
68 |
|
69 | document.removeEventListener('readystatechange', interactive)
|
70 | }
|
71 | }
|
72 | document.addEventListener('readystatechange', interactive)
|
73 | }
|
74 | }
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | |
81 |
|
82 |
|
83 | Xt._mutationObserver = new MutationObserver(mutationsList => {
|
84 | for (const mutation of mutationsList) {
|
85 | if (mutation.type === 'childList') {
|
86 |
|
87 | for (const added of mutation.addedNodes) {
|
88 | if (added.nodeType === 1) {
|
89 | Xt._mountCheck({ added })
|
90 | }
|
91 | }
|
92 |
|
93 | for (const removed of mutation.removedNodes) {
|
94 | if (removed.nodeType === 1) {
|
95 | Xt._unmountCheck({ removed })
|
96 | }
|
97 | }
|
98 | }
|
99 | }
|
100 | })
|
101 |
|
102 | Xt.ready({
|
103 | func: () => {
|
104 | Xt._mutationObserver.disconnect()
|
105 | Xt._mutationObserver.observe(document.documentElement, {
|
106 | characterData: false,
|
107 | attributes: false,
|
108 | childList: true,
|
109 | subtree: true,
|
110 | })
|
111 | },
|
112 | })
|
113 |
|
114 | |
115 |
|
116 |
|
117 | Xt.refresh = () => {
|
118 | Xt._mountCheck()
|
119 | }
|
120 |
|
121 | |
122 |
|
123 |
|
124 |
|
125 | Xt.mount = obj => {
|
126 | Xt._mountArr.push(obj)
|
127 | Xt.ready({
|
128 | raf: obj.raf,
|
129 | func: () => {
|
130 | Xt._mountCheck({ obj })
|
131 | },
|
132 | })
|
133 | }
|
134 |
|
135 | |
136 |
|
137 |
|
138 |
|
139 | Xt.unmount = obj => {
|
140 | Xt._unmountArr.push(obj)
|
141 | }
|
142 |
|
143 | |
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | Xt._mountCheck = ({ added = document.documentElement, obj } = {}) => {
|
150 |
|
151 |
|
152 | if (!added.closest('html')) {
|
153 | return
|
154 | }
|
155 | const arr = obj ? [obj] : Xt._mountArr
|
156 | for (const obj of arr) {
|
157 |
|
158 | const refs = []
|
159 | if (added.matches(obj.matches)) {
|
160 | refs.push(added)
|
161 | }
|
162 | for (const ref of added.querySelectorAll(obj.matches)) {
|
163 | refs.push(ref)
|
164 | }
|
165 |
|
166 | if (refs.length) {
|
167 | for (const [index, ref] of refs.entries()) {
|
168 |
|
169 | if (obj.root && !obj.root.contains(ref)) {
|
170 | continue
|
171 | }
|
172 |
|
173 | const ignoreStr = obj.ignoreMount ?? '.xt-ignore'
|
174 | if (ignoreStr && ref.closest(ignoreStr)) {
|
175 | continue
|
176 | }
|
177 |
|
178 |
|
179 | obj.done = obj.done ? obj.done : []
|
180 | if (obj.done.includes(ref)) {
|
181 | return
|
182 | }
|
183 | obj.done.push(ref)
|
184 |
|
185 | const call = obj.mount({ ref, obj, index })
|
186 |
|
187 | if (call) {
|
188 | Xt.unmount({
|
189 | ref,
|
190 | root: obj.root,
|
191 | ignoreUnmount: obj.ignoreUnmount,
|
192 | unmount: call,
|
193 | unmountRemove: function () {
|
194 |
|
195 | obj.done = obj.done.filter(x => x !== ref)
|
196 |
|
197 | Xt._unmountArr = Xt._unmountArr.filter(x => {
|
198 | return x !== this
|
199 | })
|
200 | },
|
201 | })
|
202 | }
|
203 | }
|
204 | }
|
205 | }
|
206 | }
|
207 |
|
208 | |
209 |
|
210 |
|
211 |
|
212 |
|
213 | Xt._unmountCheck = ({ removed = document.documentElement } = {}) => {
|
214 |
|
215 |
|
216 | if (removed.closest('html')) {
|
217 | return
|
218 | }
|
219 | for (const obj of Xt._unmountArr) {
|
220 |
|
221 | if (removed === obj.ref || removed.contains(obj.ref)) {
|
222 |
|
223 | if (obj.root && !obj.root.contains(obj.ref)) {
|
224 | continue
|
225 | }
|
226 |
|
227 | const ignoreStr = obj.ignoreUnmount ?? '.xt-ignore'
|
228 | if (ignoreStr && obj.ref.closest(ignoreStr)) {
|
229 | continue
|
230 | }
|
231 |
|
232 | obj.unmount({ obj })
|
233 | obj.unmountRemove()
|
234 | }
|
235 | }
|
236 | }
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 | |
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 | Xt._set = ({ name, el, self } = {}) => {
|
250 | Xt.dataStorage.set(el, name, self)
|
251 | }
|
252 |
|
253 | |
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 | Xt.get = ({ name, el } = {}) => {
|
261 | return Xt.dataStorage.get(el, name)
|
262 | }
|
263 |
|
264 | |
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 | Xt._remove = ({ name, el } = {}) => {
|
272 | return Xt.dataStorage.remove(el, name)
|
273 | }
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 | |
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 | Xt._initMatches = ({ self, optionsInitial } = {}) => {
|
286 | const options = self.options
|
287 |
|
288 | if (self.initial === undefined) {
|
289 |
|
290 | if (self.matches) {
|
291 | Xt._removeMatches({ self, optionsInitial })
|
292 | }
|
293 |
|
294 | if (options.matches) {
|
295 | self.matches = []
|
296 | const mqs = Object.entries(options.matches)
|
297 | if (mqs.length) {
|
298 | for (const [key, value] of mqs) {
|
299 |
|
300 | const mql = matchMedia(key)
|
301 | self.matches.push({ mql, value })
|
302 | Xt._eventMatches({ self, mql, value, skipReinit: true, optionsInitial })
|
303 | if (mql.addEventListener) {
|
304 | mql.addEventListener('change', Xt._eventMatches.bind(null, { self, mql, value, optionsInitial }))
|
305 | } else {
|
306 | mql.addListener(Xt._eventMatches.bind(null, { self, mql, value, optionsInitial }))
|
307 | }
|
308 | }
|
309 | }
|
310 | }
|
311 | }
|
312 | }
|
313 |
|
314 | |
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 | Xt._eventMatches = ({ self, mql, value, skipReinit = false, optionsInitial } = {}) => {
|
323 |
|
324 | if (!self.container.closest('html')) {
|
325 | return
|
326 | }
|
327 |
|
328 | if (mql.matches) {
|
329 |
|
330 | self.options = Xt.merge([self.options, value])
|
331 | } else {
|
332 |
|
333 | self.options = Xt.mergeReset({ start: self.options, reset: optionsInitial, check: value })
|
334 | }
|
335 |
|
336 | if (!skipReinit) {
|
337 |
|
338 | Xt.frame({
|
339 | el: self.container,
|
340 | ns: `${self.ns}MatchFrame`,
|
341 | func: () => {
|
342 | Xt._eventReinit({ self })
|
343 | },
|
344 | })
|
345 | }
|
346 | }
|
347 |
|
348 | |
349 |
|
350 |
|
351 |
|
352 |
|
353 | Xt._removeMatches = ({ self, optionsInitial } = {}) => {
|
354 |
|
355 | if (self.matches?.length) {
|
356 | for (const obj of self.matches) {
|
357 |
|
358 | const mql = obj.mql
|
359 | const value = obj.value
|
360 | if (mql.removeEventListener) {
|
361 | mql.removeEventListener('change', Xt._eventMatches.bind(null, { self, mql, value, optionsInitial }))
|
362 | } else {
|
363 | mql.removeListener(Xt._eventMatches.bind(null, { self, mql, value, optionsInitial }))
|
364 | }
|
365 | }
|
366 | }
|
367 | }
|
368 |
|
369 | |
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 | Xt._eventReinit = ({ self } = {}, e) => {
|
376 |
|
377 | if (!e?.detail?.container || e?.detail?.container.contains(self.container)) {
|
378 |
|
379 | self.reinit()
|
380 | }
|
381 | }
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 | Xt.dataStorage = {
|
389 | |
390 |
|
391 |
|
392 | _storage: new Map(),
|
393 |
|
394 | |
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 |
|
401 | set: (el, key, obj) => {
|
402 |
|
403 | if (!Xt.dataStorage._storage.has(el)) {
|
404 | Xt.dataStorage._storage.set(el, new Map())
|
405 | }
|
406 |
|
407 | const getEl = Xt.dataStorage._storage.get(el)
|
408 | getEl.set(key, obj)
|
409 |
|
410 | return getEl.get(key)
|
411 | },
|
412 |
|
413 | |
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 | put: (el, key, obj) => {
|
421 |
|
422 | if (!Xt.dataStorage._storage.has(el)) {
|
423 | Xt.dataStorage._storage.set(el, new Map())
|
424 | }
|
425 |
|
426 | const getEl = Xt.dataStorage._storage.get(el)
|
427 | const getKey = getEl.get(key)
|
428 | if (getKey) {
|
429 | return getKey
|
430 | }
|
431 |
|
432 | getEl.set(key, obj)
|
433 |
|
434 | return getEl.get(key)
|
435 | },
|
436 |
|
437 | |
438 |
|
439 |
|
440 |
|
441 |
|
442 |
|
443 | get: (el, key) => {
|
444 | const getEl = Xt.dataStorage._storage.get(el)
|
445 |
|
446 | if (!getEl) {
|
447 | return null
|
448 | }
|
449 |
|
450 | return getEl.get(key)
|
451 | },
|
452 |
|
453 | |
454 |
|
455 |
|
456 |
|
457 |
|
458 | getAll: el => {
|
459 | const getEl = Xt.dataStorage._storage.get(el)
|
460 |
|
461 | if (!getEl) {
|
462 | return null
|
463 | }
|
464 |
|
465 | return getEl
|
466 | },
|
467 |
|
468 | |
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 | has: (el, key) => {
|
475 |
|
476 | return Xt.dataStorage._storage.get(el).has(key)
|
477 | },
|
478 |
|
479 | |
480 |
|
481 |
|
482 |
|
483 |
|
484 |
|
485 | remove: (el, key) => {
|
486 | const getEl = Xt.dataStorage._storage.get(el)
|
487 |
|
488 | if (!getEl) {
|
489 | return null
|
490 | }
|
491 |
|
492 | const ret = getEl.delete(key)
|
493 |
|
494 | if (getEl.size === false) {
|
495 | Xt.dataStorage._storage.delete(el)
|
496 | }
|
497 |
|
498 | return ret
|
499 | },
|
500 | }
|
501 |
|
502 |
|
503 |
|
504 |
|
505 |
|
506 |
|
507 | Xt._classBody = {
|
508 | |
509 |
|
510 |
|
511 | currents: [],
|
512 |
|
513 | |
514 |
|
515 |
|
516 |
|
517 | add: obj => {
|
518 | Xt._classBody.currents.push(obj)
|
519 | },
|
520 |
|
521 | |
522 |
|
523 |
|
524 |
|
525 | remove: obj => {
|
526 | Xt._classBody.currents = Xt._classBody.currents.filter(x => x.c !== obj.c || x.ns !== obj.ns)
|
527 | },
|
528 |
|
529 | |
530 |
|
531 |
|
532 |
|
533 |
|
534 | get: obj => {
|
535 | return Xt._classBody.currents.filter(x => x.c === obj.c)
|
536 | },
|
537 | }
|
538 |
|
539 |
|
540 |
|
541 |
|
542 |
|
543 | |
544 |
|
545 |
|
546 |
|
547 |
|
548 |
|
549 |
|
550 | Xt._friction = ({ el, obj, transform = true } = {}) => {
|
551 | Xt.frame({
|
552 | el,
|
553 | ns: `xtFrictionFrame`,
|
554 | })
|
555 | Xt.frame({
|
556 | el,
|
557 | ns: `xtFrictionInitFrame`,
|
558 | func: () => {
|
559 |
|
560 | if (Xt.visible({ el })) {
|
561 | let xCurrent
|
562 | let yCurrent
|
563 | if (transform) {
|
564 | const translate = Xt.getTranslate({ el })
|
565 | xCurrent = translate[0]
|
566 | yCurrent = translate[1]
|
567 | } else {
|
568 | const rect = el.getBoundingClientRect()
|
569 | xCurrent = rect.left
|
570 | yCurrent = rect.top
|
571 | }
|
572 | let xDist = obj.x - xCurrent
|
573 | let yDist = obj.y - yCurrent
|
574 |
|
575 | const fncFriction = obj.friction
|
576 |
|
577 | if (fncFriction && Xt.dataStorage.get(el, 'xtFrictionX') && Xt.durationTimescale !== 1000) {
|
578 |
|
579 | xCurrent += fncFriction({ delta: Math.abs(xDist) }) * Math.sign(xDist)
|
580 | yCurrent += fncFriction({ delta: Math.abs(yDist) }) * Math.sign(yDist)
|
581 | if (transform) {
|
582 | el.style.transform = `translateX(${xCurrent}px) translateY(${yCurrent}px)`
|
583 | } else {
|
584 | el.style.left = `${xCurrent}px`
|
585 | el.style.top = `${yCurrent}px`
|
586 | }
|
587 | } else {
|
588 |
|
589 | xCurrent = obj.x
|
590 | yCurrent = obj.y
|
591 |
|
592 | if (transform) {
|
593 | el.style.transform = `translateX(${xCurrent}px) translateY(${yCurrent}px)`
|
594 | } else {
|
595 | el.style.top = `${yCurrent}px`
|
596 | el.style.left = `${xCurrent}px`
|
597 | }
|
598 | }
|
599 |
|
600 | Xt.dataStorage.set(el, 'xtFrictionX', xCurrent)
|
601 | Xt.dataStorage.set(el, 'xtFrictionY', yCurrent)
|
602 |
|
603 | if (fncFriction && Xt.durationTimescale !== 1000) {
|
604 | const frictionLimit = obj.frictionLimit ? obj.frictionLimit : 1.5
|
605 | xDist = obj.x - xCurrent
|
606 | yDist = obj.y - yCurrent
|
607 | Xt.frame({
|
608 | el,
|
609 | ns: `xtFrictionFrame`,
|
610 | func: () => {
|
611 | if (Math.abs(xDist) >= frictionLimit || Math.abs(yDist) >= frictionLimit) {
|
612 |
|
613 | Xt._friction({ el, obj, transform })
|
614 | } else {
|
615 |
|
616 | Xt.dataStorage.remove(el, 'xtFrictionX')
|
617 | Xt.dataStorage.remove(el, 'xtFrictionY')
|
618 | }
|
619 | },
|
620 | })
|
621 | }
|
622 | }
|
623 | },
|
624 | })
|
625 | }
|
626 |
|
627 | |
628 |
|
629 |
|
630 |
|
631 |
|
632 |
|
633 | Xt.getTranslate = ({ el } = {}) => {
|
634 | const transArr = []
|
635 | const style = getComputedStyle(el)
|
636 | const transform = style.transform
|
637 | let mat = transform.match(/^matrix3d\((.+)\)$/)
|
638 | if (mat) {
|
639 | transArr.push(parseFloat(mat[1].split(', ')[13]))
|
640 | } else {
|
641 | mat = transform.match(/^matrix\((.+)\)$/)
|
642 | mat ? transArr.push(parseFloat(mat[1].split(', ')[4])) : transArr.push(0)
|
643 | mat ? transArr.push(parseFloat(mat[1].split(', ')[5])) : transArr.push(0)
|
644 | }
|
645 | return transArr
|
646 | }
|
647 |
|
648 | |
649 |
|
650 |
|
651 |
|
652 |
|
653 |
|
654 |
|
655 | Xt.contains = ({ els, tr } = {}) => {
|
656 | if (els instanceof HTMLElement) {
|
657 | return els.contains(tr)
|
658 | }
|
659 | for (const el of els) {
|
660 | if (el.contains(tr)) {
|
661 | return true
|
662 | }
|
663 | }
|
664 | return false
|
665 | }
|
666 |
|
667 | |
668 |
|
669 |
|
670 |
|
671 | Xt.uniqueId = () => {
|
672 | Xt.uid = Xt.uid !== undefined ? Xt.uid : 0
|
673 | return `xt-${Xt.uid++}`
|
674 | }
|
675 |
|
676 | |
677 |
|
678 |
|
679 |
|
680 |
|
681 | Xt.merge = arr => {
|
682 | const final = {}
|
683 | for (const obj of arr) {
|
684 | if (obj) {
|
685 | for (const [key, value] of Object.entries(obj)) {
|
686 | if (Array.isArray(value)) {
|
687 |
|
688 | final[key] = final[key] ? final[key] : []
|
689 | final[key].push(...value)
|
690 | } else if (
|
691 | value !== null &&
|
692 | typeof value === 'object' &&
|
693 | !value.nodeName &&
|
694 | value !== window
|
695 | ) {
|
696 |
|
697 | final[key] = Xt.merge([final[key], value])
|
698 | } else {
|
699 | final[key] = value
|
700 | }
|
701 | }
|
702 | }
|
703 | }
|
704 | return final
|
705 | }
|
706 |
|
707 | |
708 |
|
709 |
|
710 |
|
711 |
|
712 |
|
713 |
|
714 |
|
715 | Xt.mergeReset = ({ start, reset, check } = {}) => {
|
716 | const final = start
|
717 | for (const [key, value] of Object.entries(check)) {
|
718 | if (
|
719 | value !== null &&
|
720 | typeof value === 'object' &&
|
721 | !Array.isArray(value) &&
|
722 | !value.nodeName &&
|
723 | value !== window
|
724 | ) {
|
725 | final[key] = Xt.mergeReset({ start: start[key], reset: reset[key], check: check[key] })
|
726 | } else {
|
727 | if (start[key] === check[key]) {
|
728 | final[key] = reset[key]
|
729 | }
|
730 | }
|
731 | }
|
732 | return final
|
733 | }
|
734 |
|
735 | |
736 |
|
737 |
|
738 |
|
739 |
|
740 |
|
741 | Xt.sanitize = str => {
|
742 | return DOMPurify.sanitize(str)
|
743 | }
|
744 |
|
745 | |
746 |
|
747 |
|
748 |
|
749 |
|
750 |
|
751 |
|
752 | Xt.node = ({ sanitize = true, str }) => {
|
753 | const template = document.createElement('template')
|
754 | template.innerHTML = sanitize ? Xt.sanitize(str.trim()) : str.trim()
|
755 | return template.content.firstChild
|
756 | }
|
757 |
|
758 | |
759 |
|
760 |
|
761 |
|
762 |
|
763 |
|
764 | Xt.nodes = ({ sanitize = true, str }) => {
|
765 | const template = document.createElement('template')
|
766 | template.innerHTML = sanitize ? Xt.sanitize(str.trim()) : str.trim()
|
767 | return template.content.childNodes
|
768 | }
|
769 |
|
770 | |
771 |
|
772 |
|
773 |
|
774 |
|
775 |
|
776 |
|
777 |
|
778 | Xt.script = ({ url, callback, defer = true, async = true } = {}) => {
|
779 | if (!document.querySelector(`script[src="${url}"]`)) {
|
780 | const asyncfix = async
|
781 | const script = document.createElement('script')
|
782 | if (callback) {
|
783 | script.onload = callback
|
784 | }
|
785 | script.type = 'text/javascript'
|
786 | script.src = url
|
787 | script.defer = defer
|
788 | script.async = asyncfix
|
789 | document.body.append(script)
|
790 | }
|
791 | }
|
792 |
|
793 | |
794 |
|
795 |
|
796 |
|
797 |
|
798 |
|
799 |
|
800 | Xt.frame = ({ el, ns = '', func } = {}) => {
|
801 | cancelAnimationFrame(Xt.dataStorage.get(el, `${ns}Frame`))
|
802 | if (func) {
|
803 |
|
804 | Xt.dataStorage.set(
|
805 | el,
|
806 | `${ns}Frame`,
|
807 | requestAnimationFrame(() => {
|
808 | func()
|
809 | })
|
810 | )
|
811 | }
|
812 | }
|
813 |
|
814 | |
815 |
|
816 |
|
817 |
|
818 |
|
819 |
|
820 |
|
821 | Xt.frameDouble = ({ el, ns = '', func } = {}) => {
|
822 | cancelAnimationFrame(Xt.dataStorage.get(el, `${ns}FrameDouble`))
|
823 | if (func) {
|
824 |
|
825 | Xt.dataStorage.set(
|
826 | el,
|
827 | `${ns}FrameDouble`,
|
828 | requestAnimationFrame(() => {
|
829 | Xt.dataStorage.set(
|
830 | el,
|
831 | `${ns}FrameDouble`,
|
832 | requestAnimationFrame(() => {
|
833 | func()
|
834 | })
|
835 | )
|
836 | })
|
837 | )
|
838 | }
|
839 | }
|
840 |
|
841 | |
842 |
|
843 |
|
844 |
|
845 |
|
846 |
|
847 |
|
848 |
|
849 |
|
850 |
|
851 | Xt.on = ({ el, ns = '', duration, raf = true, initial = false, callback } = {}) => {
|
852 | Xt.animTimeout({ el, ns })
|
853 | el.classList.add('on')
|
854 | el.classList.remove('out')
|
855 | const func = () => {
|
856 | el.classList.add('in')
|
857 | el.classList.remove('done')
|
858 | Xt.animTimeout({
|
859 | el,
|
860 | ns,
|
861 | duration,
|
862 | actionCurrent: 'In',
|
863 | func: () => {
|
864 | el.classList.add('done')
|
865 | if (callback) {
|
866 | callback()
|
867 | }
|
868 | },
|
869 | })
|
870 | }
|
871 | if (raf) {
|
872 |
|
873 | Xt.frameDouble({ el, ns, func })
|
874 | } else {
|
875 |
|
876 | Xt.frameDouble({ el, ns })
|
877 | func()
|
878 | }
|
879 |
|
880 | if (initial) {
|
881 | el.classList.add('initial')
|
882 | }
|
883 | Xt.frameDouble({
|
884 | el,
|
885 | ns: `${ns}Initial`,
|
886 | func: () => {
|
887 | if (initial) {
|
888 | el.classList.remove('initial')
|
889 | }
|
890 | },
|
891 | })
|
892 | }
|
893 |
|
894 | |
895 |
|
896 |
|
897 |
|
898 |
|
899 |
|
900 |
|
901 |
|
902 |
|
903 |
|
904 | Xt.off = ({ el, ns = '', duration, raf = true, initial = false, callback } = {}) => {
|
905 | Xt.animTimeout({ el, ns })
|
906 |
|
907 | el.classList.remove('on')
|
908 | const func = () => {
|
909 | el.classList.remove('in', 'done')
|
910 | el.classList.add('out')
|
911 | Xt.animTimeout({
|
912 | el,
|
913 | ns,
|
914 | duration,
|
915 | actionCurrent: 'Out',
|
916 | func: () => {
|
917 | el.classList.remove('out')
|
918 | if (callback) {
|
919 | callback()
|
920 | }
|
921 | },
|
922 | })
|
923 | }
|
924 | if (raf) {
|
925 |
|
926 | Xt.frameDouble({ el, ns, func })
|
927 | } else {
|
928 |
|
929 | Xt.frameDouble({ el, ns })
|
930 | func()
|
931 | }
|
932 |
|
933 | if (initial) {
|
934 | el.classList.add('initial')
|
935 | }
|
936 | Xt.frameDouble({
|
937 | el,
|
938 | ns: `${ns}Initial`,
|
939 | func: () => {
|
940 | if (initial) {
|
941 | el.classList.remove('initial')
|
942 | }
|
943 | },
|
944 | })
|
945 | }
|
946 |
|
947 | |
948 |
|
949 |
|
950 |
|
951 |
|
952 |
|
953 |
|
954 |
|
955 |
|
956 | Xt.animTimeout = ({ el, ns = '', duration, actionCurrent, func } = {}) => {
|
957 | clearTimeout(Xt.dataStorage.get(el, `${ns}AnimTimeout`))
|
958 | if (func) {
|
959 | duration = Xt.animTime({ el, duration, actionCurrent }) ?? 0
|
960 | if (!duration) {
|
961 | func()
|
962 | } else {
|
963 | Xt.dataStorage.set(el, `${ns}AnimTimeout`, setTimeout(func, duration))
|
964 | }
|
965 | }
|
966 | }
|
967 |
|
968 | |
969 |
|
970 |
|
971 |
|
972 |
|
973 |
|
974 |
|
975 | Xt.animTime = ({ el, duration, actionCurrent } = {}) => {
|
976 | const custom =
|
977 | (actionCurrent && el.getAttribute(`data-xt-duration-${actionCurrent}`)) || el.getAttribute('data-xt-duration')
|
978 | if (custom) {
|
979 |
|
980 | return isNaN(parseFloat(custom)) ? custom : parseFloat(custom) / Xt.durationTimescale
|
981 | } else if (typeof duration === 'function') {
|
982 | return duration
|
983 | } else if (duration || duration === 0) {
|
984 | return duration / Xt.durationTimescale
|
985 | }
|
986 | }
|
987 |
|
988 | |
989 |
|
990 |
|
991 |
|
992 |
|
993 |
|
994 |
|
995 | Xt.delayTime = ({ el, duration, actionCurrent } = {}) => {
|
996 | const custom =
|
997 | (actionCurrent && el.getAttribute(`data-xt-delay-${actionCurrent}`)) || el.getAttribute('data-xt-delay')
|
998 | if (custom) {
|
999 |
|
1000 | return isNaN(parseFloat(custom)) ? custom : parseFloat(custom) / Xt.durationTimescale
|
1001 | } else if (typeof duration === 'function') {
|
1002 | return duration
|
1003 | } else if (duration || duration === 0) {
|
1004 | return duration / Xt.durationTimescale
|
1005 | }
|
1006 | }
|
1007 |
|
1008 | |
1009 |
|
1010 |
|
1011 |
|
1012 |
|
1013 |
|
1014 |
|
1015 | Xt.queryAll = ({ els, query } = {}) => {
|
1016 |
|
1017 | if (!query || els.length === 0) {
|
1018 | return []
|
1019 | }
|
1020 | if (!els.length) {
|
1021 |
|
1022 | return Array.from(els.querySelectorAll(query))
|
1023 | } else {
|
1024 |
|
1025 | const arr = []
|
1026 | for (const el of els) {
|
1027 | arr.push(...el.querySelectorAll(query))
|
1028 | }
|
1029 | return arr
|
1030 | }
|
1031 | }
|
1032 |
|
1033 | |
1034 |
|
1035 |
|
1036 |
|
1037 |
|
1038 |
|
1039 | Xt.visible = ({ el } = {}) => {
|
1040 | return !!(el.offsetWidth || el.offsetHeight || el.getClientRects().length)
|
1041 | }
|
1042 |
|
1043 | |
1044 |
|
1045 |
|
1046 | Xt._setScrollbarWidth = () => {
|
1047 | if (Xt.scrollbarWidth === undefined) {
|
1048 | const scrollbarWidthHandler = Xt.dataStorage.put(window, 'resize/scrollbar', Xt._setScrollbarWidth)
|
1049 | removeEventListener('resize', scrollbarWidthHandler)
|
1050 | addEventListener('resize', scrollbarWidthHandler)
|
1051 | }
|
1052 |
|
1053 | const outer = document.createElement('div')
|
1054 | outer.style.visibility = 'hidden'
|
1055 | outer.style.width = '100%'
|
1056 | outer.style.msOverflowStyle = 'scrollbar'
|
1057 | outer.classList.add('xt-ignore', 'xt-overflow-main')
|
1058 | document.body.append(outer)
|
1059 |
|
1060 | outer.style.overflow = 'scroll'
|
1061 |
|
1062 | const inner = document.createElement('div')
|
1063 | inner.style.width = '100%'
|
1064 | inner.classList.add('xt-ignore')
|
1065 | outer.append(inner)
|
1066 |
|
1067 | const widthNoScroll = outer.offsetWidth
|
1068 | const widthWithScroll = inner.offsetWidth
|
1069 | Xt.scrollbarWidth = widthNoScroll - widthWithScroll
|
1070 | document.documentElement.style.setProperty('--scrollbar-width', `${Xt.scrollbarWidth}px`)
|
1071 |
|
1072 | outer.remove()
|
1073 | }
|
1074 |
|
1075 | Xt.ready({
|
1076 | func: () => {
|
1077 | Xt._setScrollbarWidth()
|
1078 | },
|
1079 | })
|
1080 |
|
1081 | |
1082 |
|
1083 |
|
1084 |
|
1085 | addEventListener('resize', e => {
|
1086 |
|
1087 |
|
1088 | const w = window.innerWidth + window.outerWidth
|
1089 | const h = window.innerHeight + window.outerHeight
|
1090 | if (
|
1091 | !e?.detail?.force &&
|
1092 | Xt.dataStorage.get(window, 'xtEventDelayWidth') === w &&
|
1093 | (Xt.resizeSkip() || Xt.dataStorage.get(window, 'xtEventDelayHeight') === h)
|
1094 | ) {
|
1095 |
|
1096 | return
|
1097 | }
|
1098 | Xt.dataStorage.set(
|
1099 | window,
|
1100 | `eventDelaySaveTimeout`,
|
1101 | setTimeout(() => {
|
1102 | Xt.dataStorage.set(window, 'xtEventDelayWidth', w)
|
1103 | Xt.dataStorage.set(window, 'xtEventDelayHeight', h)
|
1104 | dispatchEvent(new CustomEvent('resize.xt', { detail: e?.detail }))
|
1105 | }, Xt.resizeDelay)
|
1106 | )
|
1107 | })
|
1108 |
|
1109 | Xt.dataStorage.set(window, 'xtEventDelayWidth', window.innerWidth + window.outerWidth)
|
1110 | Xt.dataStorage.set(window, 'xtEventDelayHeight', window.innerHeight + window.outerHeight)
|
1111 |
|
1112 | |
1113 |
|
1114 |
|
1115 | Xt._innerHeightSet = () => {
|
1116 | Xt.innerHeight = window.innerHeight
|
1117 | document.documentElement.style.setProperty('--vh', `${Xt.innerHeight * 0.01}px`)
|
1118 | }
|
1119 |
|
1120 | addEventListener('resize.xt', () => {
|
1121 | Xt._innerHeightSet()
|
1122 | })
|
1123 |
|
1124 | Xt.ready({
|
1125 | func: () => {
|
1126 | Xt._innerHeightSet()
|
1127 | },
|
1128 | })
|
1129 |
|
1130 |
|
1131 |
|
1132 |
|
1133 |
|
1134 | |
1135 |
|
1136 |
|
1137 |
|
1138 |
|
1139 | Xt.scrolltriggerRerfreshFix = ({ ScrollTrigger } = {}) => {
|
1140 |
|
1141 | ScrollTrigger.config({
|
1142 | autoRefreshEvents: 'visibilitychange,DOMContentLoaded,load',
|
1143 | })
|
1144 |
|
1145 | const resize = () => {
|
1146 | ScrollTrigger.refresh()
|
1147 | }
|
1148 | removeEventListener('resize.xt', resize)
|
1149 | addEventListener('resize.xt', resize)
|
1150 | }
|
1151 |
|
1152 |
|
1153 | }
|