primevue-mirror/components/lib/tooltip/Tooltip.js

461 lines
14 KiB
JavaScript
Raw Normal View History

2023-06-21 13:58:32 +00:00
import { BaseDirective } from 'primevue/basedirective';
2022-12-08 11:04:25 +00:00
import { ConnectedOverlayScrollHandler, DomHandler, ObjectUtils, UniqueComponentId, ZIndexUtils } from 'primevue/utils';
2022-09-06 12:03:37 +00:00
2023-04-10 08:01:28 +00:00
let timer;
2023-06-21 13:58:32 +00:00
function bindEvents(el, options) {
2022-09-06 12:03:37 +00:00
const modifiers = el.$_ptooltipModifiers;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (modifiers.focus) {
2023-06-21 13:58:32 +00:00
el.$_focusevent = (event) => onFocus(event, options);
el.addEventListener('focus', el.$_focusevent);
2022-09-06 12:03:37 +00:00
el.addEventListener('blur', onBlur);
2022-09-14 11:26:01 +00:00
} else {
2023-06-21 13:58:32 +00:00
el.$_mouseenterevent = (event) => onMouseEnter(event, options);
el.addEventListener('mouseenter', el.$_mouseenterevent);
2022-09-06 12:03:37 +00:00
el.addEventListener('mouseleave', onMouseLeave);
el.addEventListener('click', onClick);
}
2022-12-08 11:04:25 +00:00
el.addEventListener('keydown', onKeydown);
2022-09-06 12:03:37 +00:00
}
function unbindEvents(el) {
const modifiers = el.$_ptooltipModifiers;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (modifiers.focus) {
2023-06-21 13:58:32 +00:00
el.removeEventListener('focus', el.$_focusevent);
el.$_focusevent = null;
2022-09-06 12:03:37 +00:00
el.removeEventListener('blur', onBlur);
2022-09-14 11:26:01 +00:00
} else {
2023-06-21 13:58:32 +00:00
el.removeEventListener('mouseenter', el.$_mouseenterevent);
el.$_mouseenterevent = null;
2022-09-06 12:03:37 +00:00
el.removeEventListener('mouseleave', onMouseLeave);
el.removeEventListener('click', onClick);
}
2022-12-08 11:04:25 +00:00
el.removeEventListener('keydown', onKeydown);
2022-09-06 12:03:37 +00:00
}
function bindScrollListener(el) {
if (!el.$_ptooltipScrollHandler) {
2022-09-14 11:26:01 +00:00
el.$_ptooltipScrollHandler = new ConnectedOverlayScrollHandler(el, function () {
2022-09-06 12:03:37 +00:00
hide(el);
});
}
el.$_ptooltipScrollHandler.bindScrollListener();
}
function unbindScrollListener(el) {
if (el.$_ptooltipScrollHandler) {
el.$_ptooltipScrollHandler.unbindScrollListener();
}
}
2023-06-21 13:58:32 +00:00
function onMouseEnter(event, options) {
2023-04-09 15:11:56 +00:00
const el = event.currentTarget;
const showDelay = el.$_ptooltipShowDelay;
2023-06-21 13:58:32 +00:00
show(el, options, showDelay);
2022-09-06 12:03:37 +00:00
}
function onMouseLeave(event) {
2023-04-09 15:11:56 +00:00
const el = event.currentTarget;
const hideDelay = el.$_ptooltipHideDelay;
2023-05-11 12:04:11 +00:00
hide(el, hideDelay);
2022-09-06 12:03:37 +00:00
}
2023-06-21 13:58:32 +00:00
function onFocus(event, options) {
2023-04-09 15:11:56 +00:00
const el = event.currentTarget;
const showDelay = el.$_ptooltipShowDelay;
2023-06-21 13:58:32 +00:00
show(el, options, showDelay);
2022-09-06 12:03:37 +00:00
}
function onBlur(event) {
2023-04-09 15:11:56 +00:00
const el = event.currentTarget;
const hideDelay = el.$_ptooltipHideDelay;
2023-05-11 12:04:11 +00:00
hide(el, hideDelay);
2022-09-06 12:03:37 +00:00
}
function onClick(event) {
2023-04-09 15:11:56 +00:00
const el = event.currentTarget;
const hideDelay = el.$_ptooltipHideDelay;
2023-05-11 12:04:11 +00:00
hide(el, hideDelay);
2022-09-06 12:03:37 +00:00
}
2022-12-08 11:04:25 +00:00
function onKeydown(event) {
2023-06-19 08:42:45 +00:00
const el = event.currentTarget;
const hideDelay = el.$_ptooltipHideDelay;
2023-06-20 07:45:15 +00:00
2023-04-09 15:11:56 +00:00
event.code === 'Escape' && hide(event.currentTarget, hideDelay);
2022-12-08 11:04:25 +00:00
}
2023-06-21 13:58:32 +00:00
function tooltipActions(el, options) {
2023-06-20 07:45:15 +00:00
if (el.$_ptooltipDisabled || !DomHandler.isExist(el)) {
2023-04-10 08:01:28 +00:00
return;
}
2022-09-06 12:03:37 +00:00
2023-06-21 13:58:32 +00:00
let tooltipElement = create(el, options);
2022-09-14 11:26:01 +00:00
2023-04-10 08:01:28 +00:00
align(el);
2023-06-21 13:58:32 +00:00
!el.$_ptooltipUnstyled && DomHandler.fadeIn(tooltipElement, 250);
2022-09-06 12:03:37 +00:00
2023-04-10 08:01:28 +00:00
window.addEventListener('resize', function onWindowResize() {
if (!DomHandler.isTouchDevice()) {
hide(el);
}
2023-04-09 15:11:56 +00:00
2023-04-10 08:01:28 +00:00
this.removeEventListener('resize', onWindowResize);
});
2023-04-09 15:11:56 +00:00
2023-04-10 08:01:28 +00:00
bindScrollListener(el);
ZIndexUtils.set('tooltip', tooltipElement, el.$_ptooltipZIndex);
}
2022-09-06 12:03:37 +00:00
2023-06-21 13:58:32 +00:00
function show(el, options, showDelay) {
2023-04-09 15:11:56 +00:00
if (showDelay !== undefined) {
2023-06-21 13:58:32 +00:00
timer = setTimeout(() => tooltipActions(el, options), showDelay);
2023-04-09 15:11:56 +00:00
} else {
2023-06-21 13:58:32 +00:00
tooltipActions(el, options);
2023-04-09 15:11:56 +00:00
}
2022-09-06 12:03:37 +00:00
}
2023-04-10 08:01:28 +00:00
function tooltipRemoval(el) {
remove(el);
unbindScrollListener(el);
}
2023-04-09 15:11:56 +00:00
2023-04-10 08:01:28 +00:00
function hide(el, hideDelay) {
2023-04-09 15:11:56 +00:00
clearTimeout(timer);
if (hideDelay !== undefined) {
2023-04-10 08:01:28 +00:00
setTimeout(() => tooltipRemoval(el), hideDelay);
2023-04-09 15:11:56 +00:00
} else {
2023-04-10 08:01:28 +00:00
tooltipRemoval(el);
2023-04-09 15:11:56 +00:00
}
2022-09-06 12:03:37 +00:00
}
function getTooltipElement(el) {
return document.getElementById(el.$_ptooltipId);
}
2023-06-21 13:58:32 +00:00
function create(el, options) {
2022-12-08 11:04:25 +00:00
const id = el.$_ptooltipIdAttr !== '' ? el.$_ptooltipIdAttr : UniqueComponentId() + '_tooltip';
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
el.$_ptooltipId = id;
let container = document.createElement('div');
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
container.id = id;
2023-06-14 14:10:55 +00:00
container.setAttribute('data-pc-section', 'root');
container.setAttribute('data-pc-name', 'tooltip');
2022-09-06 12:03:37 +00:00
let tooltipArrow = document.createElement('div');
2022-09-14 11:26:01 +00:00
2023-06-14 14:10:55 +00:00
tooltipArrow.setAttribute('data-pc-section', 'arrow');
2022-09-06 12:03:37 +00:00
container.appendChild(tooltipArrow);
let tooltipText = document.createElement('div');
2023-06-14 14:10:55 +00:00
tooltipText.setAttribute('data-pc-section', 'text');
if (!el.$_ptooltipUnstyled) {
tooltipArrow.className = 'p-tooltip-arrow';
tooltipText.className = 'p-tooltip-text';
}
2022-09-06 12:03:37 +00:00
if (el.$_ptooltipEscape) {
tooltipText.innerHTML = el.$_ptooltipValue;
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
tooltipText.innerHTML = '';
tooltipText.appendChild(document.createTextNode(el.$_ptooltipValue));
}
2022-12-08 11:04:25 +00:00
container.setAttribute('role', 'tooltip');
2022-09-06 12:03:37 +00:00
container.appendChild(tooltipText);
document.body.appendChild(container);
container.style.display = 'inline-block';
if (el.$_ptooltipFitContent) {
container.style.width = 'fit-content';
}
2023-06-21 13:58:32 +00:00
el.$_pDirectiveElement = container;
BaseDirective.directiveElement = container;
BaseDirective.handleCSS('tooltip', container, options);
2023-06-14 14:10:55 +00:00
2022-09-06 12:03:37 +00:00
return container;
}
function remove(el) {
if (el) {
let tooltipElement = getTooltipElement(el);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (tooltipElement && tooltipElement.parentElement) {
ZIndexUtils.clear(tooltipElement);
document.body.removeChild(tooltipElement);
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
el.$_ptooltipId = null;
}
}
function align(el) {
const modifiers = el.$_ptooltipModifiers;
if (modifiers.top) {
alignTop(el);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (isOutOfBounds(el)) {
alignBottom(el);
if (isOutOfBounds(el)) {
alignTop(el);
}
}
2022-09-14 11:26:01 +00:00
} else if (modifiers.left) {
2022-09-06 12:03:37 +00:00
alignLeft(el);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (isOutOfBounds(el)) {
alignRight(el);
if (isOutOfBounds(el)) {
alignTop(el);
if (isOutOfBounds(el)) {
alignBottom(el);
if (isOutOfBounds(el)) {
alignLeft(el);
}
}
}
}
2022-09-14 11:26:01 +00:00
} else if (modifiers.bottom) {
2022-09-06 12:03:37 +00:00
alignBottom(el);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (isOutOfBounds(el)) {
alignTop(el);
if (isOutOfBounds(el)) {
alignBottom(el);
}
}
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
alignRight(el);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (isOutOfBounds(el)) {
alignLeft(el);
if (isOutOfBounds(el)) {
alignTop(el);
if (isOutOfBounds(el)) {
alignBottom(el);
if (isOutOfBounds(el)) {
alignRight(el);
}
}
}
}
}
}
function getHostOffset(el) {
let offset = el.getBoundingClientRect();
let targetLeft = offset.left + DomHandler.getWindowScrollLeft();
let targetTop = offset.top + DomHandler.getWindowScrollTop();
2022-09-14 11:26:01 +00:00
return { left: targetLeft, top: targetTop };
2022-09-06 12:03:37 +00:00
}
function alignRight(el) {
preAlign(el, 'right');
let tooltipElement = getTooltipElement(el);
let hostOffset = getHostOffset(el);
let left = hostOffset.left + DomHandler.getOuterWidth(el);
let top = hostOffset.top + (DomHandler.getOuterHeight(el) - DomHandler.getOuterHeight(tooltipElement)) / 2;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
tooltipElement.style.left = left + 'px';
tooltipElement.style.top = top + 'px';
}
function alignLeft(el) {
preAlign(el, 'left');
let tooltipElement = getTooltipElement(el);
let hostOffset = getHostOffset(el);
let left = hostOffset.left - DomHandler.getOuterWidth(tooltipElement);
let top = hostOffset.top + (DomHandler.getOuterHeight(el) - DomHandler.getOuterHeight(tooltipElement)) / 2;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
tooltipElement.style.left = left + 'px';
tooltipElement.style.top = top + 'px';
}
function alignTop(el) {
preAlign(el, 'top');
let tooltipElement = getTooltipElement(el);
let hostOffset = getHostOffset(el);
let left = hostOffset.left + (DomHandler.getOuterWidth(el) - DomHandler.getOuterWidth(tooltipElement)) / 2;
let top = hostOffset.top - DomHandler.getOuterHeight(tooltipElement);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
tooltipElement.style.left = left + 'px';
tooltipElement.style.top = top + 'px';
}
function alignBottom(el) {
preAlign(el, 'bottom');
let tooltipElement = getTooltipElement(el);
let hostOffset = getHostOffset(el);
let left = hostOffset.left + (DomHandler.getOuterWidth(el) - DomHandler.getOuterWidth(tooltipElement)) / 2;
let top = hostOffset.top + DomHandler.getOuterHeight(el);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
tooltipElement.style.left = left + 'px';
tooltipElement.style.top = top + 'px';
}
function preAlign(el, position) {
let tooltipElement = getTooltipElement(el);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
tooltipElement.style.left = -999 + 'px';
tooltipElement.style.top = -999 + 'px';
2023-06-14 14:10:55 +00:00
tooltipElement.className += el.$_ptooltipUnstyled ? el.$_ptooltipClass : ` p-tooltip p-component p-tooltip-${position} ${el.$_ptooltipClass || ''}`;
2022-09-06 12:03:37 +00:00
}
function isOutOfBounds(el) {
let tooltipElement = getTooltipElement(el);
let offset = tooltipElement.getBoundingClientRect();
let targetTop = offset.top;
let targetLeft = offset.left;
let width = DomHandler.getOuterWidth(tooltipElement);
let height = DomHandler.getOuterHeight(tooltipElement);
let viewport = DomHandler.getViewport();
2022-09-14 11:26:01 +00:00
return targetLeft + width > viewport.width || targetLeft < 0 || targetTop < 0 || targetTop + height > viewport.height;
2022-09-06 12:03:37 +00:00
}
function getTarget(el) {
2022-09-14 11:26:01 +00:00
return DomHandler.hasClass(el, 'p-inputwrapper') ? DomHandler.findSingle(el, 'input') : el;
2022-09-06 12:03:37 +00:00
}
function getModifiers(options) {
// modifiers
if (options.modifiers && Object.keys(options.modifiers).length) {
return options.modifiers;
}
// arg
if (options.arg && typeof options.arg === 'object') {
return Object.entries(options.arg).reduce((acc, [key, val]) => {
if (key === 'event' || key === 'position') acc[val] = true;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
return acc;
}, {});
}
return {};
}
2023-06-21 13:58:32 +00:00
const Tooltip = BaseDirective.extend('tooltip', {
2022-09-06 12:03:37 +00:00
beforeMount(el, options) {
let target = getTarget(el);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
target.$_ptooltipModifiers = getModifiers(options);
if (!options.value) return;
else if (typeof options.value === 'string') {
target.$_ptooltipValue = options.value;
target.$_ptooltipDisabled = false;
target.$_ptooltipEscape = false;
target.$_ptooltipClass = null;
target.$_ptooltipFitContent = true;
2023-06-14 14:10:55 +00:00
target.$_ptooltipIdAttr = '';
2023-05-11 12:04:11 +00:00
target.$_ptooltipShowDelay = 0;
target.$_ptooltipHideDelay = 0;
2022-09-14 11:26:01 +00:00
} else if (typeof options.value === 'object' && options.value) {
2022-12-08 11:04:25 +00:00
if (ObjectUtils.isEmpty(options.value.value) || options.value.value.trim() === '') return;
2022-09-06 12:03:37 +00:00
else {
target.$_ptooltipValue = options.value.value;
target.$_ptooltipDisabled = !!options.value.disabled === options.value.disabled ? options.value.disabled : false;
target.$_ptooltipEscape = !!options.value.escape === options.value.escape ? options.value.escape : false;
2023-06-14 14:10:55 +00:00
target.$_ptooltipClass = options.value.class || '';
2022-09-06 12:03:37 +00:00
target.$_ptooltipFitContent = !!options.value.fitContent === options.value.fitContent ? options.value.fitContent : true;
2022-12-08 11:04:25 +00:00
target.$_ptooltipIdAttr = options.value.id || '';
2023-04-09 15:11:56 +00:00
target.$_ptooltipShowDelay = options.value.showDelay || 0;
target.$_ptooltipHideDelay = options.value.hideDelay || 0;
2022-09-06 12:03:37 +00:00
}
}
2023-06-14 14:10:55 +00:00
if (options.instance.$primevue && options.instance.$primevue.config) {
2023-06-21 13:58:32 +00:00
let _config = options.instance.$primevue && options.instance.$primevue.config;
2023-06-14 14:10:55 +00:00
2023-06-21 13:58:32 +00:00
target.$_ptooltipZIndex = _config.zIndex.tooltip;
target.$_ptooltipUnstyled = _config.unstyled || false;
2022-09-06 12:03:37 +00:00
}
2023-06-21 13:58:32 +00:00
bindEvents(target, options);
2022-09-06 12:03:37 +00:00
},
updated(el, options) {
let target = getTarget(el);
2023-06-14 14:10:55 +00:00
2022-09-06 12:03:37 +00:00
target.$_ptooltipModifiers = getModifiers(options);
2023-06-21 13:58:32 +00:00
unbindEvents(target);
2022-09-06 12:03:37 +00:00
2022-12-08 11:04:25 +00:00
if (!options.value) {
return;
}
2022-09-06 12:03:37 +00:00
if (typeof options.value === 'string') {
target.$_ptooltipValue = options.value;
target.$_ptooltipDisabled = false;
target.$_ptooltipEscape = false;
target.$_ptooltipClass = null;
2022-12-08 11:04:25 +00:00
target.$_ptooltipIdAttr = '';
2023-05-11 12:04:11 +00:00
target.$_ptooltipShowDelay = 0;
target.$_ptooltipHideDelay = 0;
2022-12-08 11:04:25 +00:00
2023-06-21 13:58:32 +00:00
bindEvents(target, options);
2022-09-14 11:26:01 +00:00
} else if (typeof options.value === 'object' && options.value) {
2023-02-09 03:59:52 +00:00
if (ObjectUtils.isEmpty(options.value.value) || options.value.value.trim() === '') {
2023-06-21 13:58:32 +00:00
unbindEvents(target, options);
2023-06-14 14:10:55 +00:00
2022-12-08 11:04:25 +00:00
return;
} else {
2022-09-06 12:03:37 +00:00
target.$_ptooltipValue = options.value.value;
target.$_ptooltipDisabled = !!options.value.disabled === options.value.disabled ? options.value.disabled : false;
target.$_ptooltipEscape = !!options.value.escape === options.value.escape ? options.value.escape : false;
2023-06-14 14:10:55 +00:00
target.$_ptooltipClass = options.value.class || '';
2022-09-06 12:03:37 +00:00
target.$_ptooltipFitContent = !!options.value.fitContent === options.value.fitContent ? options.value.fitContent : true;
2022-12-08 11:04:25 +00:00
target.$_ptooltipIdAttr = options.value.id || '';
2023-05-11 12:04:11 +00:00
target.$_ptooltipShowDelay = options.value.showDelay || 0;
target.$_ptooltipHideDelay = options.value.hideDelay || 0;
2022-12-08 11:04:25 +00:00
2023-06-21 13:58:32 +00:00
bindEvents(target, options);
2022-09-06 12:03:37 +00:00
}
}
2023-06-21 13:58:32 +00:00
},
unmounted(el, options) {
let target = getTarget(el);
2023-06-14 14:10:55 +00:00
2023-06-21 13:58:32 +00:00
remove(target);
unbindEvents(target, options);
if (target.$_ptooltipScrollHandler) {
target.$_ptooltipScrollHandler.destroy();
target.$_ptooltipScrollHandler = null;
2023-06-14 14:10:55 +00:00
}
2022-09-06 12:03:37 +00:00
}
2023-06-21 13:58:32 +00:00
});
2022-09-06 12:03:37 +00:00
export default Tooltip;