forked from jenkinsci/jenkins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbreadcrumbs-overflow.js
More file actions
271 lines (243 loc) · 8.1 KB
/
breadcrumbs-overflow.js
File metadata and controls
271 lines (243 loc) · 8.1 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
import Utils from "@/components/dropdowns/utils";
import { createElementFromHtml } from "@/util/dom";
import Path from "@/util/path";
/**
* Maps context menu items from the server response to dropdown item format.
* This follows the same pattern used in jumplists.js for consistency.
*/
function mapContextMenuItems(items) {
return items.map((item) => {
if (item.type === "HEADER") {
return { type: "HEADER", label: item.displayName };
}
if (item.type === "SEPARATOR") {
return { type: "SEPARATOR" };
}
return {
icon: item.icon,
iconXml: item.iconXml,
label: item.displayName,
url: item.url,
type: item.post || item.requiresConfirmation ? "button" : "link",
badge: item.badge,
onClick: () => {
if (item.post || item.requiresConfirmation) {
if (item.requiresConfirmation) {
// dialog, crumb, notificationBar are globals from Jenkins
dialog
.confirm(item.displayName, { message: item.message })
.then(() => {
const form = document.createElement("form");
form.setAttribute("method", item.post ? "POST" : "GET");
form.setAttribute("action", item.url);
if (item.post) {
crumb.appendToForm(form);
}
document.body.appendChild(form);
form.submit();
});
} else {
fetch(item.url, {
method: "post",
headers: crumb.wrap({}),
}).then((rsp) => {
if (rsp.ok) {
notificationBar.show(
item.displayName + ": Done.",
notificationBar.SUCCESS,
);
} else {
notificationBar.show(
item.displayName + ": Failed.",
notificationBar.ERROR,
);
}
});
}
}
},
subMenu: item.subMenu ? () => mapContextMenuItems(item.subMenu.items) : null,
};
});
}
/**
* Creates a dropdown content callback for a collapsed breadcrumb item.
* This fetches the context menu and children context menu on demand.
*/
function createContextMenuCallback(hasModel, hasChildren, href) {
return (instance) => {
const sections = {
model: null,
children: null,
};
const fetchSection = (urlSuffix) => {
return fetch(Path.combinePath(href, urlSuffix))
.then((response) => response.json())
.then((json) => Utils.generateDropdownItems(mapContextMenuItems(json.items)));
};
const promises = [];
if (hasModel === "true") {
promises.push(
fetchSection("contextMenu").then((section) => {
section.prepend(
createElementFromHtml(
`<p class="jenkins-dropdown__heading">Actions</p>`,
),
);
sections.model = section;
}),
);
}
if (hasChildren === "true") {
promises.push(
fetchSection("childrenContextMenu").then((section) => {
section.prepend(
createElementFromHtml(
`<p class="jenkins-dropdown__heading">Navigation</p>`,
),
);
sections.children = section;
}),
);
}
Promise.all(promises)
.then(() => {
const container = document.createElement("div");
container.className = "jenkins-dropdown__split-container";
if (sections.model && !sections.children) {
container.appendChild(sections.model);
} else if (!sections.model && sections.children) {
container.appendChild(sections.children);
} else if (sections.model && sections.children) {
// Merge both sections into one dropdown for proper a11y
const dropbox = sections.model;
Array.from(sections.children.children).forEach((item) => {
dropbox.appendChild(item);
});
container.appendChild(dropbox);
}
instance.setContent(container);
})
.catch((error) => {
console.log(`Breadcrumb context menu fetch failed: ${error}`);
})
.finally(() => {
instance.loaded = true;
});
};
}
export default function computeBreadcrumbs() {
document
.querySelectorAll(".jenkins-breadcrumbs__list-item.jenkins-hidden")
.forEach((e) => {
e.classList.remove("jenkins-hidden");
});
if (!breadcrumbsBarOverflows()) {
removeOverflowButton();
return;
}
const items = [];
const breadcrumbs = Array.from(
document.querySelectorAll(`[data-type="breadcrumb-item"]`),
);
const breadcrumbsOverflow = generateOverflowButton().querySelector("button");
while (breadcrumbsBarOverflows()) {
const item = breadcrumbs.shift();
if (!item) {
break;
}
items.push(item);
item.classList.add("jenkins-hidden");
}
Utils.generateDropdown(
breadcrumbsOverflow,
(instance) => {
const mappedItems = items.map((e) => {
let href = e.querySelector("a");
let tooltip;
if (href) {
href = href.href;
}
if (e.textContent.length > 26) {
tooltip = e.textContent;
}
return {
type: "link",
clazz: "jenkins-breadcrumbs__overflow-item",
label: e.textContent,
url: href,
tooltip,
};
});
const content = Utils.generateDropdownItems(mappedItems);
// Attach context menu dropdowns to overflow items that had them originally
items.forEach((breadcrumbItem, index) => {
const dropdownIndicator =
breadcrumbItem.querySelector(".dropdown-indicator");
if (!dropdownIndicator) {
return;
}
const hasModel = dropdownIndicator.getAttribute("data-model");
const hasChildren = dropdownIndicator.getAttribute("data-children");
const dataHref = dropdownIndicator.getAttribute("data-href");
if ((hasModel === "true" || hasChildren === "true") && dataHref) {
const overflowMenuItem = content.children[index];
if (overflowMenuItem) {
// Attach nested dropdown using the same pattern as jumplists.js
Utils.generateDropdown(
overflowMenuItem,
createContextMenuCallback(hasModel, hasChildren, dataHref),
false,
{
trigger: "mouseenter",
placement: "right-start",
offset: [-8, 0],
animation: "tooltip",
touch: false,
},
);
}
}
});
instance.setContent(content);
},
true,
{
trigger: "click focus",
offset: [0, 10],
animation: "tooltip",
},
);
}
function breadcrumbsBarOverflows() {
const breadcrumbsBar = document.querySelector("#breadcrumbBar");
return breadcrumbsBar.scrollWidth > breadcrumbsBar.offsetWidth;
}
function generateOverflowButton() {
// If an overflow menu already exists let's use that
const overflowMenu = document.querySelector(
".jenkins-breadcrumbs__list-item .jenkins-button",
);
if (overflowMenu) {
return overflowMenu.parentNode;
}
// Generate an overflow menu to store breadcrumbs
const logo = document.querySelector(".jenkins-breadcrumbs__list-item");
const element =
createElementFromHtml(`<li class="jenkins-breadcrumbs__list-item"><button class="jenkins-button jenkins-button--tertiary"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
<circle cx="256" cy="256" r="45" fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="32"/>
<circle cx="441" cy="256" r="45" fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="32"/>
<circle cx="71" cy="256" r="45" fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="32"/>
</svg>
</button></li>`);
logo.after(element);
return element;
}
function removeOverflowButton() {
const breadcrumbsOverflow = document.querySelector(
".jenkins-breadcrumbs__list-item .jenkins-button",
);
if (breadcrumbsOverflow) {
breadcrumbsOverflow.parentNode.remove();
}
}