Skip to content

Commit

Permalink
Now possible to reregister tooltips
Browse files Browse the repository at this point in the history
  • Loading branch information
janfaracik committed Mar 15, 2022
1 parent 7e6c576 commit df6847a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ function progressivelyRender(handler, callback, statusId) {
if (r.status == 'done') {
callback(r.data);
$(statusId).style.display = 'none';
registerTooltips();
} else if (r.status == 'canceled') {
// TODO ugly; replace with single tr of class=unknown?
$$('#' + statusId + ' .progress-bar-done')[0].innerHTML = 'Aborted.';
Expand All @@ -40,6 +41,7 @@ function progressivelyRender(handler, callback, statusId) {
$$('#' + statusId + ' .progress-bar-done')[0].style.width = (100 * r.status) + '%';
$$('#' + statusId + ' .progress-bar-left')[0].style.width = (100 - 100 * r.status) + '%';
checkNewsLater(500);
registerTooltips();
}
}
function checkNewsLater(timeout) {
Expand Down
47 changes: 31 additions & 16 deletions war/src/main/js/tooltips.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,43 @@
import tippy from "tippy.js"

tippy("[tooltip]", {
content: element => element.getAttribute("tooltip"),
arrow: false,
theme: "tooltip",
animation: "tooltip",
appendTo: document.body,
})
registerTooltips()

tippy("[html-tooltip]", {
content: element => element.getAttribute("html-tooltip"),
allowHTML: true,
arrow: false,
theme: "tooltip",
animation: "tooltip",
appendTo: document.body,
})
/**
* Registers tooltips for the page
* If called again, destroys existing tooltips and registers them again (useful for progressive rendering)
*/
function registerTooltips() {
[...document.querySelectorAll("*")].forEach(node => {
if (node._tippy) {
node._tippy.destroy()
}
})

tippy("[tooltip]", {
content: element => element.getAttribute("tooltip"),
arrow: false,
theme: "tooltip",
animation: "tooltip",
appendTo: document.body,
})

tippy("[html-tooltip]", {
content: element => element.getAttribute("html-tooltip"),
allowHTML: true,
arrow: false,
theme: "tooltip",
animation: "tooltip",
appendTo: document.body,
})
}

/**
* Displays a tooltip for three seconds on the provided element after interaction
* @param {string} text - The tooltip text
* @param {HTMLElement} element - The element to show the tooltip
*/
function hoverNotification(text, element) {
let tooltip = tippy(element, {
const tooltip = tippy(element, {
interactive: true,
trigger: "hover",
arrow: false,
Expand All @@ -41,4 +55,5 @@ function hoverNotification(text, element) {
tooltip.show()
}

window.registerTooltips = registerTooltips
window.hoverNotification = hoverNotification

0 comments on commit df6847a

Please sign in to comment.