source: imaps-frontend/node_modules/bootstrap/js/src/dom/manipulator.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.6 KB
Line 
1/**
2 * --------------------------------------------------------------------------
3 * Bootstrap dom/manipulator.js
4 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
5 * --------------------------------------------------------------------------
6 */
7
8function normalizeData(value) {
9 if (value === 'true') {
10 return true
11 }
12
13 if (value === 'false') {
14 return false
15 }
16
17 if (value === Number(value).toString()) {
18 return Number(value)
19 }
20
21 if (value === '' || value === 'null') {
22 return null
23 }
24
25 if (typeof value !== 'string') {
26 return value
27 }
28
29 try {
30 return JSON.parse(decodeURIComponent(value))
31 } catch {
32 return value
33 }
34}
35
36function normalizeDataKey(key) {
37 return key.replace(/[A-Z]/g, chr => `-${chr.toLowerCase()}`)
38}
39
40const Manipulator = {
41 setDataAttribute(element, key, value) {
42 element.setAttribute(`data-bs-${normalizeDataKey(key)}`, value)
43 },
44
45 removeDataAttribute(element, key) {
46 element.removeAttribute(`data-bs-${normalizeDataKey(key)}`)
47 },
48
49 getDataAttributes(element) {
50 if (!element) {
51 return {}
52 }
53
54 const attributes = {}
55 const bsKeys = Object.keys(element.dataset).filter(key => key.startsWith('bs') && !key.startsWith('bsConfig'))
56
57 for (const key of bsKeys) {
58 let pureKey = key.replace(/^bs/, '')
59 pureKey = pureKey.charAt(0).toLowerCase() + pureKey.slice(1, pureKey.length)
60 attributes[pureKey] = normalizeData(element.dataset[key])
61 }
62
63 return attributes
64 },
65
66 getDataAttribute(element, key) {
67 return normalizeData(element.getAttribute(`data-bs-${normalizeDataKey(key)}`))
68 }
69}
70
71export default Manipulator
Note: See TracBrowser for help on using the repository browser.