0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-04 16:24:22 +01:00
Bootstrap/js/dist/dom/manipulator.js

90 lines
2.5 KiB
JavaScript
Raw Normal View History

2019-03-01 17:31:34 +01:00
/*!
2021-03-23 17:26:54 +01:00
* Bootstrap manipulator.js v5.0.0-beta3 (https://getbootstrap.com/)
* Copyright 2011-2021 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
2020-06-16 20:50:01 +02:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
2019-03-01 17:31:34 +01:00
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
2020-09-14 17:12:06 +02:00
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Manipulator = factory());
2019-11-08 09:11:23 +01:00
}(this, (function () { 'use strict';
2019-03-01 17:31:34 +01:00
/**
* --------------------------------------------------------------------------
2021-03-23 17:26:54 +01:00
* Bootstrap (v5.0.0-beta3): dom/manipulator.js
2020-06-16 20:50:01 +02:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
2019-03-01 17:31:34 +01:00
* --------------------------------------------------------------------------
*/
function normalizeData(val) {
if (val === 'true') {
return true;
}
if (val === 'false') {
return false;
}
if (val === Number(val).toString()) {
return Number(val);
}
if (val === '' || val === 'null') {
return null;
}
return val;
}
function normalizeDataKey(key) {
2021-03-23 17:26:54 +01:00
return key.replace(/[A-Z]/g, chr => `-${chr.toLowerCase()}`);
2019-03-01 17:31:34 +01:00
}
2021-03-23 17:26:54 +01:00
const Manipulator = {
setDataAttribute(element, key, value) {
element.setAttribute(`data-bs-${normalizeDataKey(key)}`, value);
2019-03-01 17:31:34 +01:00
},
2021-03-23 17:26:54 +01:00
removeDataAttribute(element, key) {
element.removeAttribute(`data-bs-${normalizeDataKey(key)}`);
2019-03-01 17:31:34 +01:00
},
2021-03-23 17:26:54 +01:00
getDataAttributes(element) {
2019-03-01 17:31:34 +01:00
if (!element) {
return {};
}
2021-03-23 17:26:54 +01:00
const attributes = {};
Object.keys(element.dataset).filter(key => key.startsWith('bs')).forEach(key => {
let pureKey = key.replace(/^bs/, '');
2020-11-23 14:17:16 +01:00
pureKey = pureKey.charAt(0).toLowerCase() + pureKey.slice(1, pureKey.length);
attributes[pureKey] = normalizeData(element.dataset[key]);
2019-03-01 17:31:34 +01:00
});
return attributes;
},
2021-03-23 17:26:54 +01:00
getDataAttribute(element, key) {
return normalizeData(element.getAttribute(`data-bs-${normalizeDataKey(key)}`));
2019-03-01 17:31:34 +01:00
},
2021-03-23 17:26:54 +01:00
offset(element) {
const rect = element.getBoundingClientRect();
2019-03-01 17:31:34 +01:00
return {
top: rect.top + document.body.scrollTop,
left: rect.left + document.body.scrollLeft
};
},
2021-03-23 17:26:54 +01:00
position(element) {
2019-03-01 17:31:34 +01:00
return {
top: element.offsetTop,
left: element.offsetLeft
};
}
2021-03-23 17:26:54 +01:00
2019-03-01 17:31:34 +01:00
};
return Manipulator;
2019-11-08 09:11:23 +01:00
})));
2019-03-01 17:31:34 +01:00
//# sourceMappingURL=manipulator.js.map