2017-09-05 12:35:52 +00:00
|
|
|
/**
|
|
|
|
|
* --------------------------------------------------------------------------
|
2020-09-29 15:33:00 +00:00
|
|
|
* Bootstrap (v5.0.0-alpha2): dom/manipulator.js
|
2020-06-16 18:41:47 +00:00
|
|
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
|
2017-09-05 12:35:52 +00:00
|
|
|
* --------------------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
|
2018-06-09 19:11:05 +00:00
|
|
|
function normalizeData(val) {
|
|
|
|
|
if (val === 'true') {
|
|
|
|
|
return true
|
2019-02-26 11:13:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (val === 'false') {
|
2018-06-09 19:11:05 +00:00
|
|
|
return false
|
2019-02-26 11:13:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (val === Number(val).toString()) {
|
2018-06-09 19:11:05 +00:00
|
|
|
return Number(val)
|
2019-02-26 11:13:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (val === '' || val === 'null') {
|
2018-06-09 19:11:05 +00:00
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return val
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function normalizeDataKey(key) {
|
2019-10-31 05:58:09 +00:00
|
|
|
return key.replace(/[A-Z]/g, chr => `-${chr.toLowerCase()}`)
|
2018-06-09 19:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
2017-09-05 12:35:52 +00:00
|
|
|
const Manipulator = {
|
2017-09-15 14:07:24 +00:00
|
|
|
setDataAttribute(element, key, value) {
|
2018-06-09 19:11:05 +00:00
|
|
|
element.setAttribute(`data-${normalizeDataKey(key)}`, value)
|
2017-09-15 14:07:24 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
removeDataAttribute(element, key) {
|
2018-06-09 19:11:05 +00:00
|
|
|
element.removeAttribute(`data-${normalizeDataKey(key)}`)
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getDataAttributes(element) {
|
2019-02-13 19:25:08 +00:00
|
|
|
if (!element) {
|
2018-06-09 19:11:05 +00:00
|
|
|
return {}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-13 19:25:08 +00:00
|
|
|
const attributes = {
|
|
|
|
|
...element.dataset
|
2017-09-15 14:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
2019-02-26 11:20:34 +00:00
|
|
|
Object.keys(attributes).forEach(key => {
|
2019-02-26 11:13:01 +00:00
|
|
|
attributes[key] = normalizeData(attributes[key])
|
|
|
|
|
})
|
2018-06-09 19:11:05 +00:00
|
|
|
|
|
|
|
|
return attributes
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getDataAttribute(element, key) {
|
2019-02-26 11:13:01 +00:00
|
|
|
return normalizeData(element.getAttribute(`data-${normalizeDataKey(key)}`))
|
2017-09-26 07:09:40 +00:00
|
|
|
},
|
|
|
|
|
|
2017-09-25 07:09:01 +00:00
|
|
|
offset(element) {
|
|
|
|
|
const rect = element.getBoundingClientRect()
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
top: rect.top + document.body.scrollTop,
|
|
|
|
|
left: rect.left + document.body.scrollLeft
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
position(element) {
|
|
|
|
|
return {
|
|
|
|
|
top: element.offsetTop,
|
|
|
|
|
left: element.offsetLeft
|
|
|
|
|
}
|
2017-09-05 12:35:52 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default Manipulator
|