forked from jenkinsci/jenkins
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
104 lines (96 loc) · 2.45 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import tippy from "tippy.js";
import behaviorShim from "@/util/behavior-shim";
const TOOLTIP_BASE = {
arrow: false,
theme: "tooltip",
animation: "tooltip",
appendTo: document.body,
};
/**
* 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 registerTooltip(element) {
if (element._tippy && element._tippy.props.theme === "tooltip") {
element._tippy.destroy();
}
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
)
);
}
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
)
);
}
}
/**
* 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) {
const tooltip = tippy(
element,
Object.assign(
{
trigger: "hover",
offset: [0, 0],
content: text,
onShow(instance) {
setTimeout(() => {
instance.hide();
}, 3000);
},
},
TOOLTIP_BASE
)
);
tooltip.show();
}
function init() {
behaviorShim.specify(
"[tooltip], [data-html-tooltip]",
"-tooltip-",
1000,
(element) => {
registerTooltip(element);
}
);
window.hoverNotification = hoverNotification;
}
export default { init };