Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-70178] Improve tooltip performance #7464

Merged
merged 2 commits into from
Dec 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 48 additions & 68 deletions war/src/main/js/components/tooltips/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,79 +8,59 @@ const TOOLTIP_BASE = {
appendTo: document.body,
};

let tooltipInstances = [];
const globalPlugin = {
fn(instance) {
return {
onCreate() {
tooltipInstances = tooltipInstances.concat(instance);
},
onDestroy() {
tooltipInstances = tooltipInstances.filter((i) => i !== instance);
},
};
},
};

tippy.setDefaultProps({
plugins: [globalPlugin],
});

/**
* Registers tooltips for the page
* If called again, destroys existing tooltips and registers them again (useful for progressive rendering)
* @param {HTMLElement} container - Registers the tooltips for the given container
* Registers tooltips for the given element
* If called again, destroys any existing tooltip for the element and
* registers them again (useful for progressive rendering)
* @param {HTMLElement} element - Registers the tooltips for the given element
*/
function registerTooltips(container) {
if (!container) {
container = document;
function registerTooltip(element) {
if (element._tippy) {
element._tippy.destroy();
}

tooltipInstances.forEach((instance) => {
if (instance.props.container === container) {
instance.destroy();
}
});

tippy(
container.querySelectorAll(
'[tooltip]:not([tooltip=""]):not([data-html-tooltip])'
),
Object.assign(
{
content: (element) =>
element.getAttribute("tooltip").replace(/<br[ /]?\/?>|\\n/g, "\n"),
container: container,
onCreate(instance) {
instance.reference.setAttribute("title", instance.props.content);
},
onShow(instance) {
instance.reference.removeAttribute("title");
},
onHidden(instance) {
instance.reference.setAttribute("title", instance.props.content);
if (
element.hasAttribute("tooltip") &&
!element.hasAttribute("data-html-tooltip")
) {
tippy(
element,
Object.assign(
{
content: (element) =>
element.getAttribute("tooltip").replace(/<br[ /]?\/?>|\\n/g, "\n"),
onCreate(instance) {
instance.reference.setAttribute("title", instance.props.content);
},
onShow(instance) {
instance.reference.removeAttribute("title");
},
onHidden(instance) {
instance.reference.setAttribute("title", instance.props.content);
},
},
},
TOOLTIP_BASE
)
);
TOOLTIP_BASE
)
);
}

tippy(
container.querySelectorAll("[data-html-tooltip]"),
Object.assign(
{
content: (element) => element.getAttribute("data-html-tooltip"),
allowHTML: true,
container: container,
onCreate(instance) {
instance.props.interactive =
instance.reference.getAttribute("data-tooltip-interactive") ===
"true";
if (element.hasAttribute("data-html-tooltip")) {
tippy(
element,
Object.assign(
{
content: (element) => element.getAttribute("data-html-tooltip"),
allowHTML: true,
onCreate(instance) {
instance.props.interactive =
instance.reference.getAttribute("data-tooltip-interactive") ===
"true";
},
},
},
TOOLTIP_BASE
)
);
TOOLTIP_BASE
)
);
}
}

/**
Expand Down Expand Up @@ -113,8 +93,8 @@ function init() {
"[tooltip], [data-html-tooltip]",
"-tooltip-",
1000,
function () {
registerTooltips(null);
(element) => {
registerTooltip(element);
}
);

Expand Down