-
Notifications
You must be signed in to change notification settings - Fork 8
/
init.js
81 lines (70 loc) · 1.91 KB
/
init.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
import Alpine from 'alpinejs'
function getAlpineAttributes (el) {
const alpineAttributes = []
for (let i = 0; i < el.attributes.length; ++i) {
const a = el.attributes[i]
if (a.name.startsWith('x-')) {
alpineAttributes.push(a)
}
}
return alpineAttributes
}
function wrapInTemplate (el) {
const template = document.createElement('template')
const attributes = getAlpineAttributes(el)
attributes.forEach(a => {
template.setAttribute(a.name, a.value)
el.removeAttribute(a.name)
})
el.parentNode.insertBefore(template, el)
template.content.appendChild(el)
}
function replaceDotAttributes (el) {
const attributes = getAlpineAttributes(el)
attributes.forEach(a => {
const m = a.name.match(/^(x-[^:]+)(:.+)$/)
if (m) {
let newA = null
if (['x-bind', 'x-on'].includes(m[1])) {
let prefix = m[1]
let suffix = m[2].substring(1)
if (prefix === 'x-on' && suffix.startsWith('update:')) {
prefix += ':update'
suffix = suffix.substring(7)
}
if (suffix.includes(':')) {
newA = prefix + ':' + suffix.replace(/:/g, '.')
}
} else {
newA = m[1] + m[2].replace(/:/g, '.')
}
if (newA) {
el.setAttribute(newA, a.value)
el.removeAttribute(a.name)
}
}
})
}
function removeUnnecessaryAttributeValues (el) {
const attributes = getAlpineAttributes(el)
attributes.forEach(a => {
if (a.name.match(/^x-transition.*(?!(enter|leave))/)) {
el.setAttribute(a.name, '')
}
})
}
function init () {
document.querySelectorAll('[x-data],[x-data] *').forEach((el) => {
replaceDotAttributes(el)
removeUnnecessaryAttributeValues(el)
})
document.querySelectorAll('[x-data] [x-for], [x-data] [x-if]').forEach(wrapInTemplate)
}
window.Alpine = Alpine
if (!window.Webflow) {
window.Webflow = []
}
window.Webflow.push(() => {
init()
Alpine.start()
})