forked from jenkinsci/jenkins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
282 lines (252 loc) · 7.23 KB
/
utils.js
File metadata and controls
282 lines (252 loc) · 7.23 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import Templates from "@/components/dropdowns/templates";
import makeKeyboardNavigable from "@/util/keyboard";
import tippy from "tippy.js";
import behaviorShim from "@/util/behavior-shim";
const SELECTED_ITEM_CLASS = "jenkins-dropdown__item--selected";
/*
* Generates the dropdowns for the given element
* Preloads the data on hover for speed
* @param element - the element to generate the dropdown for
* @param callback - called to retrieve the list of dropdown items
*/
function generateDropdown(element, callback, immediate, options = {}) {
if (element._tippy && element._tippy.props.theme === "dropdown") {
element._tippy.destroy();
}
tippy(
element,
Object.assign(
{},
Templates.dropdown(),
{
onCreate(instance) {
const onload = () => {
if (instance.loaded) {
return;
}
document.addEventListener("click", (event) => {
const isClickOnReference = instance.reference.contains(
event.target,
);
// Don't close the dropdown if the user is interacting with a SELECT menu inside of it
const isSelect = event.target.tagName === "SELECT";
if (!isClickOnReference && !isSelect) {
instance.clickToHide = true;
instance.hide();
}
});
callback(instance);
};
if (immediate) {
onload();
} else {
["mouseenter", "focus"].forEach((event) => {
instance.reference.addEventListener(event, onload);
});
}
},
onHide(instance) {
if (
instance.props.trigger === "mouseenter" &&
!instance.clickToHide
) {
const dropdowns = document.querySelectorAll("[data-tippy-root]");
const isMouseOverAnyDropdown = Array.from(dropdowns).some(
(dropdown) => dropdown.matches(":hover"),
);
return !isMouseOverAnyDropdown;
}
instance.clickToHide = false;
return true;
},
},
options,
),
);
}
/*
* Generates the contents for the dropdown
*/
function generateDropdownItems(items, compact) {
const menuItems = document.createElement("div");
menuItems.classList.add("jenkins-dropdown");
if (compact === true) {
menuItems.classList.add("jenkins-dropdown--compact");
}
items
.map((item) => {
if (item.type === "CUSTOM") {
return item.contents;
}
if (item.type === "HEADER") {
return Templates.heading(item.label);
}
if (item.type === "SEPARATOR") {
return Templates.separator();
}
if (item.type === "DISABLED") {
return Templates.disabled(item.label);
}
const menuItem = Templates.menuItem(item);
if (item.subMenu != null) {
tippy(
menuItem,
Object.assign({}, Templates.dropdown(), {
content: generateDropdownItems(item.subMenu()),
trigger: "mouseenter",
placement: "right-start",
offset: [-8, 0],
}),
);
}
return menuItem;
})
.forEach((item) => menuItems.appendChild(item));
if (items.length === 0) {
menuItems.appendChild(Templates.placeholder("No items"));
}
makeKeyboardNavigable(
menuItems,
() => menuItems.querySelectorAll(".jenkins-dropdown__item"),
SELECTED_ITEM_CLASS,
(selectedItem, key, evt) => {
if (!selectedItem) {
return;
}
switch (key) {
case "ArrowLeft": {
const root = selectedItem.closest("[data-tippy-root]");
if (root) {
const tippyReference = root._tippy;
if (tippyReference) {
tippyReference.hide();
}
}
break;
}
case "ArrowRight": {
const tippyRef = selectedItem._tippy;
if (!tippyRef) {
break;
}
tippyRef.show();
tippyRef.props.content
.querySelector(".jenkins-dropdown__item")
.classList.add(SELECTED_ITEM_CLASS);
break;
}
default:
if (selectedItem.onkeypress) {
selectedItem.onkeypress(evt);
}
}
},
(container) => {
const isVisible =
window.getComputedStyle(container).visibility === "visible";
const isLastDropdown = Array.from(
document.querySelectorAll(".jenkins-dropdown"),
)
.filter((dropdown) => container !== dropdown)
.filter(
(dropdown) =>
window.getComputedStyle(dropdown).visibility === "visible",
)
.every(
(dropdown) =>
!(
container.compareDocumentPosition(dropdown) &
Node.DOCUMENT_POSITION_FOLLOWING
),
);
return isVisible && isLastDropdown;
},
);
behaviorShim.applySubtree(menuItems);
return menuItems;
}
function convertHtmlToItems(children) {
const items = [];
Array.from(children).forEach((child) => {
const attributes = child.dataset;
const type = child.dataset.dropdownType;
switch (type) {
case "ITEM": {
const item = {
label: attributes.dropdownText,
id: attributes.dropdownId,
icon: attributes.dropdownIcon,
iconXml: attributes.dropdownIcon,
clazz: attributes.dropdownClazz,
};
if (attributes.dropdownHref) {
item.url = attributes.dropdownHref;
item.type = "link";
} else {
item.type = "button";
}
if (attributes.dropdownBadgeSeverity) {
item.badge = {
text: attributes.dropdownBadgeText,
tooltip: attributes.dropdownBadgeTooltip,
severity: attributes.dropdownBadgeSeverity,
};
}
items.push(item);
break;
}
case "SUBMENU":
items.push({
type: "ITEM",
label: attributes.dropdownText,
icon: attributes.dropdownIcon,
iconXml: attributes.dropdownIcon,
subMenu: () => convertHtmlToItems(child.content.children),
});
break;
case "SEPARATOR":
items.push({ type: type });
break;
case "HEADER":
items.push({ type: type, label: attributes.dropdownText });
break;
case "CUSTOM":
items.push({ type: type, contents: child.content.cloneNode(true) });
break;
}
});
return items;
}
function validateDropdown(e) {
if (e.targetUrl) {
const method = e.getAttribute("checkMethod") || "post";
try {
FormChecker.delayedCheck(e.targetUrl(), method, e.targetElement);
} catch (x) {
console.warn(x);
}
}
}
function getMaxSuggestionCount(e, defaultValue) {
return parseInt(e.dataset["maxsuggestions"]) || defaultValue;
}
function debounce(callback) {
callback.running = false;
return () => {
if (!callback.running) {
callback.running = true;
setTimeout(() => {
callback();
callback.running = false;
}, 300);
}
};
}
export default {
convertHtmlToItems,
generateDropdown,
generateDropdownItems,
validateDropdown,
getMaxSuggestionCount,
debounce,
};