1 | /**
|
---|
2 | * --------------------------------------------------------------------------
|
---|
3 | * Bootstrap (v5.1.3): dom/manipulator.js
|
---|
4 | * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
|
---|
5 | * --------------------------------------------------------------------------
|
---|
6 | */
|
---|
7 |
|
---|
8 | function normalizeData(val) {
|
---|
9 | if (val === 'true') {
|
---|
10 | return true
|
---|
11 | }
|
---|
12 |
|
---|
13 | if (val === 'false') {
|
---|
14 | return false
|
---|
15 | }
|
---|
16 |
|
---|
17 | if (val === Number(val).toString()) {
|
---|
18 | return Number(val)
|
---|
19 | }
|
---|
20 |
|
---|
21 | if (val === '' || val === 'null') {
|
---|
22 | return null
|
---|
23 | }
|
---|
24 |
|
---|
25 | return val
|
---|
26 | }
|
---|
27 |
|
---|
28 | function normalizeDataKey(key) {
|
---|
29 | return key.replace(/[A-Z]/g, chr => `-${chr.toLowerCase()}`)
|
---|
30 | }
|
---|
31 |
|
---|
32 | const Manipulator = {
|
---|
33 | setDataAttribute(element, key, value) {
|
---|
34 | element.setAttribute(`data-bs-${normalizeDataKey(key)}`, value)
|
---|
35 | },
|
---|
36 |
|
---|
37 | removeDataAttribute(element, key) {
|
---|
38 | element.removeAttribute(`data-bs-${normalizeDataKey(key)}`)
|
---|
39 | },
|
---|
40 |
|
---|
41 | getDataAttributes(element) {
|
---|
42 | if (!element) {
|
---|
43 | return {}
|
---|
44 | }
|
---|
45 |
|
---|
46 | const attributes = {}
|
---|
47 |
|
---|
48 | Object.keys(element.dataset)
|
---|
49 | .filter(key => key.startsWith('bs'))
|
---|
50 | .forEach(key => {
|
---|
51 | let pureKey = key.replace(/^bs/, '')
|
---|
52 | pureKey = pureKey.charAt(0).toLowerCase() + pureKey.slice(1, pureKey.length)
|
---|
53 | attributes[pureKey] = normalizeData(element.dataset[key])
|
---|
54 | })
|
---|
55 |
|
---|
56 | return attributes
|
---|
57 | },
|
---|
58 |
|
---|
59 | getDataAttribute(element, key) {
|
---|
60 | return normalizeData(element.getAttribute(`data-bs-${normalizeDataKey(key)}`))
|
---|
61 | },
|
---|
62 |
|
---|
63 | offset(element) {
|
---|
64 | const rect = element.getBoundingClientRect()
|
---|
65 |
|
---|
66 | return {
|
---|
67 | top: rect.top + window.pageYOffset,
|
---|
68 | left: rect.left + window.pageXOffset
|
---|
69 | }
|
---|
70 | },
|
---|
71 |
|
---|
72 | position(element) {
|
---|
73 | return {
|
---|
74 | top: element.offsetTop,
|
---|
75 | left: element.offsetLeft
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | export default Manipulator
|
---|