primevue-mirror/components/lib/utils/DomHandler.js

863 lines
29 KiB
JavaScript
Raw Normal View History

import { $dt, $dtp } from 'primevue/themes';
2022-09-14 11:26:01 +00:00
export default {
2021-10-02 15:58:31 +00:00
innerWidth(el) {
if (el) {
let width = el.offsetWidth;
let style = getComputedStyle(el);
2018-12-08 21:35:53 +00:00
width += parseFloat(style.paddingLeft) + parseFloat(style.paddingRight);
2022-09-14 11:26:01 +00:00
return width;
}
2022-09-14 11:26:01 +00:00
return 0;
2021-10-02 15:58:31 +00:00
},
2018-12-08 21:35:53 +00:00
2021-10-02 15:58:31 +00:00
width(el) {
if (el) {
let width = el.offsetWidth;
let style = getComputedStyle(el);
2018-12-08 21:35:53 +00:00
width -= parseFloat(style.paddingLeft) + parseFloat(style.paddingRight);
2022-09-14 11:26:01 +00:00
return width;
}
2022-09-14 11:26:01 +00:00
return 0;
2021-10-02 15:58:31 +00:00
},
2018-12-08 21:35:53 +00:00
2021-10-02 15:58:31 +00:00
getWindowScrollTop() {
2018-12-08 21:35:53 +00:00
let doc = document.documentElement;
2022-09-14 11:26:01 +00:00
2018-12-08 21:35:53 +00:00
return (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);
2021-10-02 15:58:31 +00:00
},
2018-12-08 21:35:53 +00:00
2021-10-02 15:58:31 +00:00
getWindowScrollLeft() {
2018-12-08 21:35:53 +00:00
let doc = document.documentElement;
2022-09-14 11:26:01 +00:00
2018-12-08 21:35:53 +00:00
return (window.pageXOffset || doc.scrollLeft) - (doc.clientLeft || 0);
2021-10-02 15:58:31 +00:00
},
2018-12-08 21:35:53 +00:00
2021-10-02 15:58:31 +00:00
getOuterWidth(el, margin) {
2018-12-08 21:35:53 +00:00
if (el) {
let width = el.offsetWidth;
if (margin) {
let style = getComputedStyle(el);
2022-09-14 11:26:01 +00:00
2018-12-08 21:35:53 +00:00
width += parseFloat(style.marginLeft) + parseFloat(style.marginRight);
}
return width;
}
2022-09-14 11:26:01 +00:00
return 0;
2021-10-02 15:58:31 +00:00
},
2018-12-08 21:35:53 +00:00
2021-10-02 15:58:31 +00:00
getOuterHeight(el, margin) {
2018-12-08 21:35:53 +00:00
if (el) {
let height = el.offsetHeight;
if (margin) {
let style = getComputedStyle(el);
2022-09-14 11:26:01 +00:00
2018-12-08 21:35:53 +00:00
height += parseFloat(style.marginTop) + parseFloat(style.marginBottom);
}
2019-10-01 13:00:26 +00:00
2018-12-08 21:35:53 +00:00
return height;
}
2022-09-14 11:26:01 +00:00
return 0;
2021-10-02 15:58:31 +00:00
},
2018-12-08 21:35:53 +00:00
2021-10-02 15:58:31 +00:00
getClientHeight(el, margin) {
2019-11-19 11:54:24 +00:00
if (el) {
let height = el.clientHeight;
if (margin) {
let style = getComputedStyle(el);
2022-09-14 11:26:01 +00:00
2019-11-19 11:54:24 +00:00
height += parseFloat(style.marginTop) + parseFloat(style.marginBottom);
}
return height;
}
2022-09-14 11:26:01 +00:00
return 0;
2021-10-02 15:58:31 +00:00
},
2019-11-19 11:54:24 +00:00
2021-10-02 15:58:31 +00:00
getViewport() {
2018-12-08 21:35:53 +00:00
let win = window,
d = document,
e = d.documentElement,
g = d.getElementsByTagName('body')[0],
w = win.innerWidth || e.clientWidth || g.clientWidth,
h = win.innerHeight || e.clientHeight || g.clientHeight;
return { width: w, height: h };
2021-10-02 15:58:31 +00:00
},
2018-12-08 21:35:53 +00:00
2021-10-02 15:58:31 +00:00
getOffset(el) {
if (el) {
let rect = el.getBoundingClientRect();
return {
top: rect.top + (window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0),
2022-09-14 11:26:01 +00:00
left: rect.left + (window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0)
};
}
2018-12-08 21:35:53 +00:00
return {
top: 'auto',
left: 'auto'
2018-12-08 21:35:53 +00:00
};
2021-10-02 15:58:31 +00:00
},
2018-12-08 21:35:53 +00:00
2021-10-02 15:58:31 +00:00
index(element) {
if (element) {
let children = this.getParentNode(element)?.childNodes;
let num = 0;
2022-09-14 11:26:01 +00:00
for (let i = 0; i < children.length; i++) {
if (children[i] === element) return num;
if (children[i].nodeType === 1) num++;
}
2018-12-08 21:35:53 +00:00
}
2022-09-14 11:26:01 +00:00
2018-12-08 21:35:53 +00:00
return -1;
2021-10-02 15:58:31 +00:00
},
2018-12-08 21:35:53 +00:00
2023-10-26 00:54:11 +00:00
addMultipleClasses(element, classNames) {
if (element && classNames) {
[classNames]
.flat()
.filter(Boolean)
.forEach((cNames) => cNames.split(' ').forEach((className) => this.addClass(element, className)));
}
},
removeMultipleClasses(element, classNames) {
if (element && classNames) {
[classNames]
.flat()
.filter(Boolean)
.forEach((cNames) => cNames.split(' ').forEach((className) => this.removeClass(element, className)));
2018-12-08 21:35:53 +00:00
}
2021-10-02 15:58:31 +00:00
},
2018-12-08 21:35:53 +00:00
2021-10-02 15:58:31 +00:00
addClass(element, className) {
if (element && className && !this.hasClass(element, className)) {
2022-09-14 11:26:01 +00:00
if (element.classList) element.classList.add(className);
else element.className += ' ' + className;
}
2021-10-02 15:58:31 +00:00
},
2018-12-08 21:35:53 +00:00
2021-10-02 15:58:31 +00:00
removeClass(element, className) {
if (element && className) {
2022-09-14 11:26:01 +00:00
if (element.classList) element.classList.remove(className);
else element.className = element.className.replace(new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)', 'gi'), ' ');
}
2021-10-02 15:58:31 +00:00
},
2018-12-08 21:35:53 +00:00
2021-10-02 15:58:31 +00:00
hasClass(element, className) {
if (element) {
2022-09-14 11:26:01 +00:00
if (element.classList) return element.classList.contains(className);
else return new RegExp('(^| )' + className + '( |$)', 'gi').test(element.className);
}
return false;
2021-10-02 15:58:31 +00:00
},
2018-12-08 21:35:53 +00:00
2023-05-19 10:32:44 +00:00
addStyles(element, styles = {}) {
if (element) {
Object.entries(styles).forEach(([key, value]) => (element.style[key] = value));
}
},
2021-10-02 15:58:31 +00:00
find(element, selector) {
2022-12-08 11:04:25 +00:00
return this.isElement(element) ? element.querySelectorAll(selector) : [];
2021-10-02 15:58:31 +00:00
},
2018-12-08 21:35:53 +00:00
2021-10-02 15:58:31 +00:00
findSingle(element, selector) {
return this.isElement(element) ? (element.matches(selector) ? element : element.querySelector(selector)) : null;
2021-10-02 15:58:31 +00:00
},
2018-12-08 21:35:53 +00:00
createElement(type, attributes = {}, ...children) {
if (type) {
const element = document.createElement(type);
this.setAttributes(element, attributes);
element.append(...children);
return element;
}
return undefined;
},
setAttribute(element, attribute = '', value) {
if (this.isElement(element) && value !== null && value !== undefined) {
element.setAttribute(attribute, value);
}
},
setAttributes(element, attributes = {}) {
if (this.isElement(element)) {
const computedStyles = (rule, value) => {
2023-07-04 00:12:39 +00:00
const styles = element?.$attrs?.[rule] ? [element?.$attrs?.[rule]] : [];
return [value].flat().reduce((cv, v) => {
if (v !== null && v !== undefined) {
const type = typeof v;
if (type === 'string' || type === 'number') {
cv.push(v);
} else if (type === 'object') {
const _cv = Array.isArray(v)
? computedStyles(rule, v)
: Object.entries(v).map(([_k, _v]) => (rule === 'style' && (!!_v || _v === 0) ? `${_k.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase()}:${_v}` : !!_v ? _k : undefined));
cv = _cv.length ? cv.concat(_cv.filter((c) => !!c)) : cv;
}
}
return cv;
}, styles);
};
Object.entries(attributes).forEach(([key, value]) => {
if (value !== undefined && value !== null) {
const matchedEvent = key.match(/^on(.+)/);
if (matchedEvent) {
element.addEventListener(matchedEvent[1].toLowerCase(), value);
} else if (key === 'p-bind') {
this.setAttributes(element, value);
} else {
value = key === 'class' ? [...new Set(computedStyles('class', value))].join(' ').trim() : key === 'style' ? computedStyles('style', value).join(';').trim() : value;
2023-07-04 00:12:39 +00:00
(element.$attrs = element.$attrs || {}) && (element.$attrs[key] = value);
element.setAttribute(key, value);
}
}
});
}
},
2023-05-19 10:32:44 +00:00
getAttribute(element, name) {
if (this.isElement(element)) {
2023-05-19 10:32:44 +00:00
const value = element.getAttribute(name);
if (!isNaN(value)) {
return +value;
}
if (value === 'true' || value === 'false') {
return value === 'true';
}
return value;
}
return undefined;
},
isAttributeEquals(element, name, value) {
return this.isElement(element) ? this.getAttribute(element, name) === value : false;
2023-05-19 10:32:44 +00:00
},
isAttributeNotEquals(element, name, value) {
return !this.isAttributeEquals(element, name, value);
},
2021-10-02 15:58:31 +00:00
getHeight(el) {
if (el) {
let height = el.offsetHeight;
let style = getComputedStyle(el);
2018-12-08 21:35:53 +00:00
height -= parseFloat(style.paddingTop) + parseFloat(style.paddingBottom) + parseFloat(style.borderTopWidth) + parseFloat(style.borderBottomWidth);
2018-12-08 21:35:53 +00:00
return height;
}
2022-09-14 11:26:01 +00:00
return 0;
2021-10-02 15:58:31 +00:00
},
2018-12-08 21:35:53 +00:00
2021-10-02 15:58:31 +00:00
getWidth(el) {
if (el) {
let width = el.offsetWidth;
let style = getComputedStyle(el);
2018-12-08 21:35:53 +00:00
width -= parseFloat(style.paddingLeft) + parseFloat(style.paddingRight) + parseFloat(style.borderLeftWidth) + parseFloat(style.borderRightWidth);
2018-12-08 21:35:53 +00:00
return width;
}
2022-09-14 11:26:01 +00:00
return 0;
2021-10-02 15:58:31 +00:00
},
2018-12-08 21:35:53 +00:00
2024-01-21 10:43:57 +00:00
absolutePosition(element, target, gutter = true) {
if (element) {
2023-05-19 10:32:44 +00:00
const elementDimensions = element.offsetParent ? { width: element.offsetWidth, height: element.offsetHeight } : this.getHiddenElementDimensions(element);
const elementOuterHeight = elementDimensions.height;
const elementOuterWidth = elementDimensions.width;
const targetOuterHeight = target.offsetHeight;
const targetOuterWidth = target.offsetWidth;
const targetOffset = target.getBoundingClientRect();
const windowScrollTop = this.getWindowScrollTop();
const windowScrollLeft = this.getWindowScrollLeft();
const viewport = this.getViewport();
2024-01-21 10:43:57 +00:00
let top,
left,
origin = 'top';
if (targetOffset.top + targetOuterHeight + elementOuterHeight > viewport.height) {
top = targetOffset.top + windowScrollTop - elementOuterHeight;
2024-01-21 10:43:57 +00:00
origin = 'bottom';
if (top < 0) {
top = windowScrollTop;
}
2022-09-14 11:26:01 +00:00
} else {
top = targetOuterHeight + targetOffset.top + windowScrollTop;
2020-08-04 12:56:47 +00:00
}
2018-12-08 21:35:53 +00:00
2022-09-14 11:26:01 +00:00
if (targetOffset.left + elementOuterWidth > viewport.width) left = Math.max(0, targetOffset.left + windowScrollLeft + targetOuterWidth - elementOuterWidth);
else left = targetOffset.left + windowScrollLeft;
2018-12-08 21:35:53 +00:00
element.style.top = top + 'px';
element.style.left = left + 'px';
2024-01-21 10:43:57 +00:00
element.style.transformOrigin = origin;
gutter && (element.style.marginTop = origin === 'bottom' ? $dt('{anchor.gutter} * -1') : $dt('anchor.gutter'));
}
2021-10-02 15:58:31 +00:00
},
2018-12-08 21:35:53 +00:00
2024-01-21 10:43:57 +00:00
relativePosition(element, target, gutter = true) {
if (element) {
2023-05-19 10:32:44 +00:00
const elementDimensions = element.offsetParent ? { width: element.offsetWidth, height: element.offsetHeight } : this.getHiddenElementDimensions(element);
const targetHeight = target.offsetHeight;
const targetOffset = target.getBoundingClientRect();
const viewport = this.getViewport();
2024-01-21 10:43:57 +00:00
let top,
left,
origin = 'top';
2022-09-14 11:26:01 +00:00
if (targetOffset.top + targetHeight + elementDimensions.height > viewport.height) {
top = -1 * elementDimensions.height;
2024-01-21 10:43:57 +00:00
origin = 'bottom';
2022-09-14 11:26:01 +00:00
if (targetOffset.top + top < 0) {
top = -1 * targetOffset.top;
}
2022-09-14 11:26:01 +00:00
} else {
top = targetHeight;
2020-08-04 12:56:47 +00:00
}
2018-12-08 21:35:53 +00:00
if (elementDimensions.width > viewport.width) {
// element wider then viewport and cannot fit on screen (align at left side of viewport)
left = targetOffset.left * -1;
2022-09-14 11:26:01 +00:00
} else if (targetOffset.left + elementDimensions.width > viewport.width) {
// element wider then viewport but can be fit on screen (align at right side of viewport)
left = (targetOffset.left + elementDimensions.width - viewport.width) * -1;
2022-09-14 11:26:01 +00:00
} else {
// element fits on screen (align with target)
left = 0;
}
2018-12-08 21:35:53 +00:00
element.style.top = top + 'px';
element.style.left = left + 'px';
2024-01-21 10:43:57 +00:00
element.style.transformOrigin = origin;
gutter && (element.style.marginTop = origin === 'bottom' ? $dt('{anchor.gutter} * -1') : $dt('anchor.gutter'));
}
2021-10-02 15:58:31 +00:00
},
2019-10-01 13:00:26 +00:00
2023-11-09 14:18:51 +00:00
nestedPosition(element, level) {
if (element) {
const parentItem = element.parentElement;
const elementOffset = this.getOffset(parentItem);
const viewport = this.getViewport();
const sublistWidth = element.offsetParent ? element.offsetWidth : this.getHiddenElementOuterWidth(element);
const itemOuterWidth = this.getOuterWidth(parentItem.children[0]);
let left;
if (parseInt(elementOffset.left, 10) + itemOuterWidth + sublistWidth > viewport.width - this.calculateScrollbarWidth()) {
if (parseInt(elementOffset.left, 10) < sublistWidth) {
// for too small screens
if (level % 2 === 1) {
left = parseInt(elementOffset.left, 10) ? '-' + parseInt(elementOffset.left, 10) + 'px' : '100%';
} else if (level % 2 === 0) {
left = viewport.width - sublistWidth - this.calculateScrollbarWidth() + 'px';
}
} else {
left = '-100%';
}
} else {
left = '100%';
}
element.style.top = '0px';
element.style.left = left;
}
},
getParentNode(element) {
let parent = element?.parentNode;
2024-01-25 12:35:35 +00:00
if (parent && parent instanceof ShadowRoot && parent.host) {
parent = parent.host;
}
return parent;
},
2021-10-02 15:58:31 +00:00
getParents(element, parents = []) {
const parent = this.getParentNode(element);
return parent === null ? parents : this.getParents(parent, parents.concat([parent]));
2021-10-02 15:58:31 +00:00
},
2021-10-02 15:58:31 +00:00
getScrollableParents(element) {
let scrollableParents = [];
if (element) {
let parents = this.getParents(element);
const overflowRegex = /(auto|scroll)/;
2022-09-14 11:26:01 +00:00
const overflowCheck = (node) => {
try {
let styleDeclaration = window['getComputedStyle'](node, null);
2022-09-14 11:26:01 +00:00
return overflowRegex.test(styleDeclaration.getPropertyValue('overflow')) || overflowRegex.test(styleDeclaration.getPropertyValue('overflowX')) || overflowRegex.test(styleDeclaration.getPropertyValue('overflowY'));
} catch (err) {
return false;
}
};
for (let parent of parents) {
let scrollSelectors = parent.nodeType === 1 && parent.dataset['scrollselectors'];
2022-09-14 11:26:01 +00:00
if (scrollSelectors) {
let selectors = scrollSelectors.split(',');
2022-09-14 11:26:01 +00:00
for (let selector of selectors) {
let el = this.findSingle(parent, selector);
2022-09-14 11:26:01 +00:00
if (el && overflowCheck(el)) {
scrollableParents.push(el);
}
}
}
2021-03-02 10:29:07 +00:00
if (parent.nodeType !== 9 && overflowCheck(parent)) {
scrollableParents.push(parent);
}
}
}
return scrollableParents;
2021-10-02 15:58:31 +00:00
},
2021-10-02 15:58:31 +00:00
getHiddenElementOuterHeight(element) {
if (element) {
element.style.visibility = 'hidden';
element.style.display = 'block';
let elementHeight = element.offsetHeight;
2022-09-14 11:26:01 +00:00
element.style.display = 'none';
element.style.visibility = 'visible';
2018-12-08 21:35:53 +00:00
return elementHeight;
}
2022-09-14 11:26:01 +00:00
return 0;
2021-10-02 15:58:31 +00:00
},
2018-12-08 21:35:53 +00:00
2021-10-02 15:58:31 +00:00
getHiddenElementOuterWidth(element) {
if (element) {
element.style.visibility = 'hidden';
element.style.display = 'block';
let elementWidth = element.offsetWidth;
2022-09-14 11:26:01 +00:00
element.style.display = 'none';
element.style.visibility = 'visible';
2018-12-08 21:35:53 +00:00
return elementWidth;
}
2022-09-14 11:26:01 +00:00
return 0;
2021-10-02 15:58:31 +00:00
},
2018-12-08 21:35:53 +00:00
2021-10-02 15:58:31 +00:00
getHiddenElementDimensions(element) {
if (element) {
let dimensions = {};
2022-09-14 11:26:01 +00:00
element.style.visibility = 'hidden';
element.style.display = 'block';
dimensions.width = element.offsetWidth;
dimensions.height = element.offsetHeight;
element.style.display = 'none';
element.style.visibility = 'visible';
return dimensions;
}
2022-09-14 11:26:01 +00:00
return 0;
2021-10-02 15:58:31 +00:00
},
2018-12-08 21:35:53 +00:00
2021-10-02 15:58:31 +00:00
fadeIn(element, duration) {
if (element) {
element.style.opacity = 0;
2018-12-08 21:35:53 +00:00
let last = +new Date();
let opacity = 0;
2022-09-14 11:26:01 +00:00
let tick = function () {
opacity = +element.style.opacity + (new Date().getTime() - last) / duration;
element.style.opacity = opacity;
last = +new Date();
2018-12-08 21:35:53 +00:00
if (+opacity < 1) {
(window.requestAnimationFrame && requestAnimationFrame(tick)) || setTimeout(tick, 16);
}
};
2018-12-08 21:35:53 +00:00
tick();
}
2021-10-02 15:58:31 +00:00
},
2018-12-08 21:35:53 +00:00
2021-10-02 15:58:31 +00:00
fadeOut(element, ms) {
if (element) {
let opacity = 1,
interval = 50,
duration = ms,
gap = interval / duration;
2018-12-08 21:35:53 +00:00
let fading = setInterval(() => {
opacity -= gap;
2018-12-08 21:35:53 +00:00
if (opacity <= 0) {
opacity = 0;
clearInterval(fading);
}
2019-10-01 13:00:26 +00:00
element.style.opacity = opacity;
}, interval);
}
2021-10-02 15:58:31 +00:00
},
2018-12-08 21:35:53 +00:00
2021-10-02 15:58:31 +00:00
getUserAgent() {
2018-12-08 21:35:53 +00:00
return navigator.userAgent;
2021-10-02 15:58:31 +00:00
},
2018-12-08 21:35:53 +00:00
2021-10-02 15:58:31 +00:00
appendChild(element, target) {
2022-09-14 11:26:01 +00:00
if (this.isElement(target)) target.appendChild(element);
else if (target.el && target.elElement) target.elElement.appendChild(element);
else throw new Error('Cannot append ' + target + ' to ' + element);
2021-10-02 15:58:31 +00:00
},
2018-12-08 21:35:53 +00:00
2022-12-08 11:04:25 +00:00
isElement(obj) {
return typeof HTMLElement === 'object' ? obj instanceof HTMLElement : obj && typeof obj === 'object' && obj !== null && obj.nodeType === 1 && typeof obj.nodeName === 'string';
},
2021-10-02 15:58:31 +00:00
scrollInView(container, item) {
2018-12-08 21:35:53 +00:00
let borderTopValue = getComputedStyle(container).getPropertyValue('borderTopWidth');
let borderTop = borderTopValue ? parseFloat(borderTopValue) : 0;
let paddingTopValue = getComputedStyle(container).getPropertyValue('paddingTop');
let paddingTop = paddingTopValue ? parseFloat(paddingTopValue) : 0;
let containerRect = container.getBoundingClientRect();
let itemRect = item.getBoundingClientRect();
2022-09-14 11:26:01 +00:00
let offset = itemRect.top + document.body.scrollTop - (containerRect.top + document.body.scrollTop) - borderTop - paddingTop;
2018-12-08 21:35:53 +00:00
let scroll = container.scrollTop;
let elementHeight = container.clientHeight;
let itemHeight = this.getOuterHeight(item);
if (offset < 0) {
container.scrollTop = scroll + offset;
2022-09-14 11:26:01 +00:00
} else if (offset + itemHeight > elementHeight) {
2018-12-08 21:35:53 +00:00
container.scrollTop = scroll + offset - elementHeight + itemHeight;
}
2021-10-02 15:58:31 +00:00
},
2018-12-08 21:35:53 +00:00
2021-10-02 15:58:31 +00:00
clearSelection() {
if (window.getSelection) {
if (window.getSelection().empty) {
2018-12-08 21:35:53 +00:00
window.getSelection().empty();
} else if (window.getSelection().removeAllRanges && window.getSelection().rangeCount > 0 && window.getSelection().getRangeAt(0).getClientRects().length > 0) {
2018-12-08 21:35:53 +00:00
window.getSelection().removeAllRanges();
}
2022-09-14 11:26:01 +00:00
} else if (document['selection'] && document['selection'].empty) {
2018-12-08 21:35:53 +00:00
try {
document['selection'].empty();
2022-09-14 11:26:01 +00:00
} catch (error) {
2018-12-08 21:35:53 +00:00
//ignore IE bug
}
}
2021-10-02 15:58:31 +00:00
},
2018-12-08 21:35:53 +00:00
2022-12-08 11:04:25 +00:00
getSelection() {
if (window.getSelection) return window.getSelection().toString();
else if (document.getSelection) return document.getSelection().toString();
else if (document['selection']) return document['selection'].createRange().text;
return null;
},
2021-10-02 15:58:31 +00:00
calculateScrollbarWidth() {
2022-09-14 11:26:01 +00:00
if (this.calculatedScrollbarWidth != null) return this.calculatedScrollbarWidth;
2019-10-01 13:00:26 +00:00
2022-09-14 11:26:01 +00:00
let scrollDiv = document.createElement('div');
this.addStyles(scrollDiv, {
width: '100px',
height: '100px',
overflow: 'scroll',
position: 'absolute',
top: '-9999px'
});
2018-12-08 21:35:53 +00:00
document.body.appendChild(scrollDiv);
let scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;
2022-09-14 11:26:01 +00:00
2018-12-08 21:35:53 +00:00
document.body.removeChild(scrollDiv);
this.calculatedScrollbarWidth = scrollbarWidth;
2019-10-01 13:00:26 +00:00
2018-12-08 21:35:53 +00:00
return scrollbarWidth;
2021-10-02 15:58:31 +00:00
},
2018-12-08 21:35:53 +00:00
calculateBodyScrollbarWidth() {
return window.innerWidth - document.documentElement.offsetWidth;
},
2021-10-02 15:58:31 +00:00
getBrowser() {
if (!this.browser) {
2018-12-08 21:35:53 +00:00
let matched = this.resolveUserAgent();
2022-09-14 11:26:01 +00:00
2018-12-08 21:35:53 +00:00
this.browser = {};
if (matched.browser) {
this.browser[matched.browser] = true;
this.browser['version'] = matched.version;
}
if (this.browser['chrome']) {
this.browser['webkit'] = true;
} else if (this.browser['webkit']) {
this.browser['safari'] = true;
}
}
return this.browser;
2021-10-02 15:58:31 +00:00
},
2018-12-08 21:35:53 +00:00
2021-10-02 15:58:31 +00:00
resolveUserAgent() {
2018-12-08 21:35:53 +00:00
let ua = navigator.userAgent.toLowerCase();
2022-09-14 11:26:01 +00:00
let match = /(chrome)[ ]([\w.]+)/.exec(ua) || /(webkit)[ ]([\w.]+)/.exec(ua) || /(opera)(?:.*version|)[ ]([\w.]+)/.exec(ua) || /(msie) ([\w.]+)/.exec(ua) || (ua.indexOf('compatible') < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua)) || [];
2018-12-08 21:35:53 +00:00
return {
2022-09-14 11:26:01 +00:00
browser: match[1] || '',
version: match[2] || '0'
2018-12-08 21:35:53 +00:00
};
2021-10-02 15:58:31 +00:00
},
2018-12-08 21:35:53 +00:00
2021-10-02 15:58:31 +00:00
isVisible(element) {
return element && element.offsetParent != null;
2021-10-02 15:58:31 +00:00
},
2019-10-30 07:42:52 +00:00
2021-10-02 15:58:31 +00:00
invokeElementMethod(element, methodName, args) {
2022-09-14 11:26:01 +00:00
element[methodName].apply(element, args);
},
isExist(element) {
return !!(element !== null && typeof element !== 'undefined' && element.nodeName && this.getParentNode(element));
2021-10-02 15:58:31 +00:00
},
isClient() {
return !!(typeof window !== 'undefined' && window.document && window.document.createElement);
},
2022-09-14 11:26:01 +00:00
focus(el, options) {
el && document.activeElement !== el && el.focus(options);
},
2022-12-08 11:04:25 +00:00
isFocusableElement(element, selector = '') {
return this.isElement(element)
? element.matches(`button:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${selector},
[href][clientHeight][clientWidth]:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${selector},
input:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${selector},
select:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${selector},
textarea:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${selector},
[tabIndex]:not([tabIndex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${selector},
[contenteditable]:not([tabIndex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${selector}`)
: false;
},
getFocusableElements(element, selector = '') {
2022-09-14 11:26:01 +00:00
let focusableElements = this.find(
element,
`button:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${selector},
[href][clientHeight][clientWidth]:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${selector},
input:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${selector},
select:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${selector},
textarea:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${selector},
[tabIndex]:not([tabIndex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${selector},
[contenteditable]:not([tabIndex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${selector}`
);
let visibleFocusableElements = [];
2022-09-14 11:26:01 +00:00
for (let focusableElement of focusableElements) {
2022-09-14 11:26:01 +00:00
if (getComputedStyle(focusableElement).display != 'none' && getComputedStyle(focusableElement).visibility != 'hidden') visibleFocusableElements.push(focusableElement);
}
return visibleFocusableElements;
2021-10-02 15:58:31 +00:00
},
2020-05-02 09:19:17 +00:00
getFirstFocusableElement(element, selector) {
const focusableElements = this.getFocusableElements(element, selector);
2022-09-14 11:26:01 +00:00
return focusableElements.length > 0 ? focusableElements[0] : null;
},
2022-12-08 11:04:25 +00:00
getLastFocusableElement(element, selector) {
const focusableElements = this.getFocusableElements(element, selector);
return focusableElements.length > 0 ? focusableElements[focusableElements.length - 1] : null;
},
getNextFocusableElement(container, element, selector) {
const focusableElements = this.getFocusableElements(container, selector);
const index = focusableElements.length > 0 ? focusableElements.findIndex((el) => el === element) : -1;
const nextIndex = index > -1 && focusableElements.length >= index + 1 ? index + 1 : -1;
return nextIndex > -1 ? focusableElements[nextIndex] : null;
},
getPreviousElementSibling(element, selector) {
let previousElement = element.previousElementSibling;
while (previousElement) {
if (previousElement.matches(selector)) {
return previousElement;
} else {
previousElement = previousElement.previousElementSibling;
}
}
return null;
},
getNextElementSibling(element, selector) {
let nextElement = element.nextElementSibling;
while (nextElement) {
if (nextElement.matches(selector)) {
return nextElement;
} else {
nextElement = nextElement.nextElementSibling;
}
}
return null;
},
2021-10-02 15:58:31 +00:00
isClickable(element) {
if (element) {
const targetNode = element.nodeName;
const parentNode = element.parentElement && element.parentElement.nodeName;
return (
targetNode === 'INPUT' ||
targetNode === 'TEXTAREA' ||
targetNode === 'BUTTON' ||
targetNode === 'A' ||
parentNode === 'INPUT' ||
parentNode === 'TEXTAREA' ||
parentNode === 'BUTTON' ||
parentNode === 'A' ||
!!element.closest('.p-button, .p-checkbox, .p-radiobutton') // @todo Add [data-pc-section="button"]
);
}
return false;
2021-10-02 15:58:31 +00:00
},
2021-10-02 15:58:31 +00:00
applyStyle(element, style) {
if (typeof style === 'string') {
element.style.cssText = style;
2022-09-14 11:26:01 +00:00
} else {
for (let prop in style) {
element.style[prop] = style[prop];
}
}
2021-10-02 15:58:31 +00:00
},
2021-04-28 07:34:17 +00:00
2021-10-02 15:58:31 +00:00
isIOS() {
2021-04-28 07:34:17 +00:00
return /iPad|iPhone|iPod/.test(navigator.userAgent) && !window['MSStream'];
2021-10-02 15:58:31 +00:00
},
2021-04-28 07:34:17 +00:00
2021-10-02 15:58:31 +00:00
isAndroid() {
2021-04-28 07:34:17 +00:00
return /(android)/i.test(navigator.userAgent);
2021-10-02 15:58:31 +00:00
},
2021-10-02 15:58:31 +00:00
isTouchDevice() {
2022-09-14 11:26:01 +00:00
return 'ontouchstart' in window || navigator.maxTouchPoints > 0 || navigator.msMaxTouchPoints > 0;
},
hasCSSAnimation(element) {
if (element) {
const style = getComputedStyle(element);
const animationDuration = parseFloat(style.getPropertyValue('animation-duration') || '0');
return animationDuration > 0;
}
return false;
},
hasCSSTransition(element) {
if (element) {
const style = getComputedStyle(element);
const transitionDuration = parseFloat(style.getPropertyValue('transition-duration') || '0');
return transitionDuration > 0;
}
return false;
},
exportCSV(csv, filename) {
let blob = new Blob([csv], {
type: 'application/csv;charset=utf-8;'
});
if (window.navigator.msSaveOrOpenBlob) {
navigator.msSaveOrOpenBlob(blob, filename + '.csv');
2022-09-14 11:26:01 +00:00
} else {
let link = document.createElement('a');
if (link.download !== undefined) {
link.setAttribute('href', URL.createObjectURL(blob));
link.setAttribute('download', filename + '.csv');
link.style.display = 'none';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
2022-09-14 11:26:01 +00:00
} else {
csv = 'data:text/csv;charset=utf-8,' + csv;
window.open(encodeURI(csv));
}
}
},
blockBodyScroll(className = 'p-overflow-hidden') {
document.body.style.setProperty($dtp('scrollbar.width').name, this.calculateBodyScrollbarWidth() + 'px');
this.addClass(document.body, className);
},
unblockBodyScroll(className = 'p-overflow-hidden') {
document.body.style.removeProperty($dtp('scrollbar.width').name);
this.removeClass(document.body, className);
}
2022-09-14 11:26:01 +00:00
};