Skip to content

Commit 94c05e9

Browse files
refactor: tooltips behavior and tooltips in bookmark editor (Stirling-Tools#4136)
Co-authored-by: Reece Browne <[email protected]>
1 parent 69b2701 commit 94c05e9

File tree

2 files changed

+35
-44
lines changed

2 files changed

+35
-44
lines changed

app/core/src/main/resources/static/js/navbar.js

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -75,39 +75,46 @@ function setupDropdowns() {
7575
});
7676
}
7777

78-
window.tooltipSetup = () => {
79-
const tooltipElements = document.querySelectorAll('[title]');
78+
function tooltipSetup() {
79+
// initialize global tooltip element or get reference
80+
let customTooltip = document.getElementById("customTooltip");
81+
if (!customTooltip) {
82+
customTooltip = document.createElement("div");
83+
customTooltip.id = "customTooltip";
84+
customTooltip.className = "btn-tooltip";
85+
document.body.appendChild(customTooltip);
86+
}
8087

81-
tooltipElements.forEach((element) => {
82-
const tooltipText = element.getAttribute('title');
83-
element.removeAttribute('title');
84-
element.setAttribute('data-title', tooltipText);
85-
const customTooltip = document.createElement('div');
86-
customTooltip.className = 'btn-tooltip';
87-
customTooltip.textContent = tooltipText;
88+
function updateTooltipPosition(event, text) {
89+
if (window.innerWidth >= 1200) {
90+
customTooltip.textContent = text;
91+
customTooltip.style.display = "block";
92+
customTooltip.style.left = `${event.pageX + 10}px`;
93+
customTooltip.style.top = `${event.pageY + 10}px`;
94+
}
95+
}
8896

89-
document.body.appendChild(customTooltip);
97+
function hideTooltip() {
98+
customTooltip.style.display = "none";
99+
}
90100

91-
element.addEventListener('mouseenter', (event) => {
92-
if (window.innerWidth >= 1200) {
93-
customTooltip.style.display = 'block';
94-
customTooltip.style.left = `${event.pageX + 10}px`;
95-
customTooltip.style.top = `${event.pageY + 10}px`;
96-
}
97-
});
101+
// find uninitialized tooltips and set up event listeners
102+
const tooltipElements = document.querySelectorAll("[title]");
98103

99-
element.addEventListener('mousemove', (event) => {
100-
if (window.innerWidth >= 1200) {
101-
customTooltip.style.left = `${event.pageX + 10}px`;
102-
customTooltip.style.top = `${event.pageY + 10}px`;
103-
}
104-
});
104+
tooltipElements.forEach((element) => {
105+
const tooltipText = element.getAttribute("title");
106+
element.removeAttribute("title");
107+
element.setAttribute("data-title", tooltipText); // no UI effect, just for reference
105108

106-
element.addEventListener('mouseleave', () => {
107-
customTooltip.style.display = 'none';
108-
});
109+
element.addEventListener("mouseenter", (event) => updateTooltipPosition(event, tooltipText));
110+
element.addEventListener("mousemove", (event) => updateTooltipPosition(event, tooltipText));
111+
element.addEventListener("mouseleave", hideTooltip);
112+
113+
// in case UI moves and mouseleave is not triggered, tooltip is readded when mouse is moved over the element
114+
element.addEventListener("click", hideTooltip);
109115
});
110116
};
117+
window.tooltipSetup = tooltipSetup;
111118

112119
// Override the bootstrap dropdown styles for mobile
113120
function fixNavbarDropdownStyles() {

app/core/src/main/resources/static/js/pages/edit-table-of-contents.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,7 @@ document.addEventListener("DOMContentLoaded", function () {
316316
updateBookmarkData();
317317

318318
// Initialize tooltips for dynamically added elements
319-
if (typeof $ !== "undefined") {
320-
$('[data-bs-toggle="tooltip"]').tooltip();
321-
}
319+
window.tooltipSetup();
322320
}
323321

324322
// Create the main bookmark element with collapsible interface
@@ -390,8 +388,6 @@ document.addEventListener("DOMContentLoaded", function () {
390388
childCount.style.fontSize = "0.7rem";
391389
childCount.style.padding = "0.2em 0.5em";
392390
childCount.textContent = bookmark.children.length;
393-
childCount.setAttribute("data-bs-toggle", "tooltip");
394-
childCount.setAttribute("data-bs-placement", "top");
395391
childCount.title = `${bookmark.children.length} child bookmark${bookmark.children.length > 1 ? "s" : ""}`;
396392
toggleContainer.appendChild(childCount);
397393
} else {
@@ -577,10 +573,6 @@ document.addEventListener("DOMContentLoaded", function () {
577573
button.type = "button";
578574
button.className = `btn ${className} btn-bookmark-action`;
579575
button.innerHTML = `<span class="material-symbols-rounded">${icon}</span>`;
580-
581-
// Use Bootstrap tooltips
582-
button.setAttribute("data-bs-toggle", "tooltip");
583-
button.setAttribute("data-bs-placement", "top");
584576
button.title = title;
585577

586578
button.addEventListener("click", clickHandler);
@@ -601,25 +593,17 @@ document.addEventListener("DOMContentLoaded", function () {
601593
// Update the add bookmark button appearance with clear visual cue
602594
addBookmarkBtn.innerHTML = '<span class="material-symbols-rounded">add</span> Add Top-level Bookmark';
603595
addBookmarkBtn.className = "btn btn-primary btn-add-bookmark top-level";
604-
605-
// Use Bootstrap tooltips
606-
addBookmarkBtn.setAttribute("data-bs-toggle", "tooltip");
607-
addBookmarkBtn.setAttribute("data-bs-placement", "top");
608596
addBookmarkBtn.title = "Add a new top-level bookmark";
609597

610598
// Add icon to empty state button as well
611599
const updateEmptyStateButton = function () {
612600
const emptyStateBtn = document.querySelector(".btn-add-first-bookmark");
613601
if (emptyStateBtn) {
614602
emptyStateBtn.innerHTML = '<span class="material-symbols-rounded">add</span> Add First Bookmark';
615-
emptyStateBtn.setAttribute("data-bs-toggle", "tooltip");
616-
emptyStateBtn.setAttribute("data-bs-placement", "top");
617603
emptyStateBtn.title = "Add first bookmark";
618604

619605
// Initialize tooltips for the empty state button
620-
if (typeof $ !== "undefined") {
621-
$('[data-bs-toggle="tooltip"]').tooltip();
622-
}
606+
window.tooltipSetup();
623607
}
624608
};
625609

0 commit comments

Comments
 (0)