-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (55 loc) · 2.31 KB
/
index.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
import Plugin from '@swup/plugin'
export class SwupCorePlugin extends Plugin {
isSwupPlugin = true
name = 'SwupCorePlugin'
requires = { swup: '>=4' }
mount() {
document.addEventListener('click', this.handleNoSwupClick.bind(this))
this.swup.options = {
...this.swup.options,
ignoreVisit: (url, { el }) => el?.closest('[data-no-swup], [data-naja]'),
animationSelector: '.view-transition-main'
}
this.swup.hooks.before('scroll:top', this.handleScrollTop.bind(this))
this.swup.hooks.on('content:replace', this.handleContentReplace.bind(this))
this.swup.hooks.on('cache:set', this.handleCacheSet.bind(this))
this.swup.hooks.before('page:load', this.handlePageLoad.bind(this))
}
unmount() {
document.removeEventListener('click', this.handleNoSwupClick)
}
handleScrollTop(visit, { options }) {
if (visit.from.url !== visit.to.url) (options.behavior = 'instant')
}
handleContentReplace(visit, { page }) {
const content = new DOMParser().parseFromString(page.html, 'text/html')
content.querySelectorAll('[data-swup-replace]').forEach((element) => {
const replaceElement = document.querySelector(`[data-swup-replace="${element.dataset.libReplaceTag}"]`)
const placement = element.closest('head') ? document.head : replaceElement.parentElement
replaceElement ? (replaceElement.outerHTML = element.outerHTML) : placement.insertAdjacentHTML('beforeend', element.outerHTML)
})
this.handleConversion()
}
/** @var {Array} dataLayer */
handleConversion() {
window.dataLayer && window.dataLayer.push({
event: 'page_view'
})
}
handleCacheSet(visit, { page }) {
this.swup.cache.update(page.url, { created: Date.now(), ttl: 1000 * 60 })
}
handlePageLoad() {
// noinspection JSCheckFunctionSignatures
this.swup.cache.prune((url, { created, ttl }) => Date.now() > created + ttl)
}
handleNoSwupClick({ target }) {
const noSwup = target.closest('[data-no-swup]')
if (!noSwup) return
if (noSwup.classList.contains('x-button')) {
noSwup.dataset.loading = ''
} else {
noSwup.style.cursor = 'wait'
}
}
}