webpack-bootstrap-ui-kit/src/js/main/funcs.js

36 lines
841 B
JavaScript
Raw Normal View History

2022-05-03 20:50:57 +02:00
const funcs = {}
2021-08-09 18:04:09 +02:00
/*!
* Get all of an element's parent elements up the DOM tree
* (c) 2019 Chris Ferdinandi, MIT License, https://gomakethings.com
* @param {Node} elem The element
* @param {String} selector Selector to match against [optional]
* @return {Array} The parent elements
*/
funcs.getParents = (elem, selector) => {
// Setup parents array
2022-05-03 20:50:57 +02:00
const parents = []
let el = elem
2021-08-09 18:04:09 +02:00
// Get matching parent elements
while (el && el !== document) {
// If using a selector, add matching parents to array
// Otherwise, add all parents
if (selector) {
if (el.matches(selector)) {
2022-05-03 20:50:57 +02:00
parents.push(el)
2021-08-09 18:04:09 +02:00
}
} else {
2022-05-03 20:50:57 +02:00
parents.push(el)
2021-08-09 18:04:09 +02:00
}
// Jump to the next parent node
2022-05-03 20:50:57 +02:00
el = el.parentNode
2021-08-09 18:04:09 +02:00
}
2022-05-03 20:50:57 +02:00
return parents
}
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
module.exports = funcs
module.exports.default = funcs