Skip to content

Commit

Permalink
Improved readability for tooltip functions
Browse files Browse the repository at this point in the history
  • Loading branch information
milospp committed Jan 24, 2025
1 parent f9c5f7b commit 6fc495e
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions webapp/src/main/webapp/js/tooltip/tooltip-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ function setupTriggerHandlers(element, trigger, data) {
}
}

function isTooltipHidden(tooltip) {
// If tooltip is not defined or there are no tooltips in the document, it is hidden
if (!tooltip) return true;

// Fallback if tooltip is defined but not in the document, it is hidden
if (document.querySelectorAll('.tooltip').length === 0) return true

// Variable is deffined and there is at least one tooltip element in the document
return false;
}

function setupClickTrigger(element, data) {
let tooltip;

Expand All @@ -33,7 +44,7 @@ function setupClickTrigger(element, data) {
};

element.addEventListener('click', () => {
if (!tooltip || document.querySelectorAll('.tooltip').length === 0) {
if (isTooltipHidden(tooltip)) {
tooltip = setupTooltip(element, data);
} else {
tooltip = removeTooltip(tooltip);
Expand All @@ -54,7 +65,7 @@ function setupHoverTrigger(element, data) {

const showTooltip = () => {
clearTimeout(timeout);
if (!tooltip || document.querySelectorAll('.tooltip').length === 0) {
if (isTooltipHidden(tooltip)) {
tooltip = setupTooltip(element, data);
tooltip.addEventListener('mouseenter', () => clearTimeout(timeout));
tooltip.addEventListener('mouseleave', () => timeout = setTimeout(() => {tooltip = removeTooltip(tooltip)}, 300));
Expand Down

0 comments on commit 6fc495e

Please sign in to comment.