2017-09-05 14:35:52 +02:00
|
|
|
/**
|
|
|
|
* --------------------------------------------------------------------------
|
2018-07-25 11:29:16 +02:00
|
|
|
* Bootstrap (v4.1.3): dom/manipulator.js
|
2017-09-05 14:35:52 +02:00
|
|
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
|
|
|
|
* --------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2018-06-09 21:11:05 +02:00
|
|
|
const regexDataKey = /[A-Z]/g
|
|
|
|
|
|
|
|
function normalizeData(val) {
|
|
|
|
if (val === 'true') {
|
|
|
|
return true
|
|
|
|
} else if (val === 'false') {
|
|
|
|
return false
|
|
|
|
} else if (val === 'null') {
|
|
|
|
return null
|
|
|
|
} else if (val === Number(val).toString()) {
|
|
|
|
return Number(val)
|
|
|
|
} else if (val === '') {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
function normalizeDataKey(key) {
|
|
|
|
return key.replace(regexDataKey, (chr) => chr.toLowerCase())
|
|
|
|
}
|
|
|
|
|
2017-09-05 14:35:52 +02:00
|
|
|
const Manipulator = {
|
2017-09-15 16:07:24 +02:00
|
|
|
setDataAttribute(element, key, value) {
|
2018-06-09 21:11:05 +02:00
|
|
|
element.setAttribute(`data-${normalizeDataKey(key)}`, value)
|
2017-09-15 16:07:24 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
removeDataAttribute(element, key) {
|
2018-06-09 21:11:05 +02:00
|
|
|
element.removeAttribute(`data-${normalizeDataKey(key)}`)
|
|
|
|
},
|
|
|
|
|
|
|
|
getDataAttributes(element) {
|
|
|
|
if (typeof element === 'undefined' || element === null) {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
|
|
|
|
let attributes
|
|
|
|
if (Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'dataset')) {
|
|
|
|
attributes = {
|
|
|
|
...element.dataset
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
attributes = {}
|
|
|
|
for (let i = 0; i < element.attributes.length; i++) {
|
|
|
|
const attribute = element.attributes[i]
|
|
|
|
|
|
|
|
if (attribute.nodeName.indexOf('data-') !== -1) {
|
|
|
|
// remove 'data-' part of the attribute name
|
|
|
|
const attributeName = attribute
|
|
|
|
.nodeName
|
|
|
|
.substring('data-'.length)
|
|
|
|
.replace(/-./g, (str) => str.charAt(1).toUpperCase())
|
|
|
|
|
|
|
|
attributes[attributeName] = attribute.nodeValue
|
|
|
|
}
|
|
|
|
}
|
2017-09-15 16:07:24 +02:00
|
|
|
}
|
|
|
|
|
2018-11-08 13:43:23 +01:00
|
|
|
Object.keys(attributes)
|
|
|
|
.forEach((key) => {
|
|
|
|
attributes[key] = normalizeData(attributes[key])
|
|
|
|
})
|
2018-06-09 21:11:05 +02:00
|
|
|
|
|
|
|
return attributes
|
|
|
|
},
|
|
|
|
|
|
|
|
getDataAttribute(element, key) {
|
|
|
|
return normalizeData(element
|
|
|
|
.getAttribute(`data-${normalizeDataKey(key)}`)
|
|
|
|
)
|
2017-09-26 09:09:40 +02:00
|
|
|
},
|
|
|
|
|
2017-09-25 09:09:01 +02: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-26 09:09:40 +02:00
|
|
|
toggleClass(element, className) {
|
|
|
|
if (typeof element === 'undefined' || element === null) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (element.classList.contains(className)) {
|
|
|
|
element.classList.remove(className)
|
|
|
|
} else {
|
|
|
|
element.classList.add(className)
|
|
|
|
}
|
2017-09-05 14:35:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Manipulator
|