-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
/
breadcrumbs.js
285 lines (260 loc) · 7.66 KB
/
breadcrumbs.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
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
283
284
285
window.breadcrumbs = (function () {
/**
* This component actually renders the menu.
*
* @type {YAHOO.widget.Menu}
*/
var menu;
/**
* Used for fetching the content of the menu asynchronously from the server
*/
var xhr;
/**
* Current mouse cursor position in the page coordinate.
*
* @type {YAHOO.util.Point}
*/
// eslint-disable-next-line no-unused-vars
var mouse;
var logger = function () {};
// logger = function() { console.log.apply(console,arguments) }; // uncomment this line to enable logging
function makeMenuHtml(icon, iconXml, displayName) {
var displaynameSpan = "<span>" + displayName + "</span>";
if (iconXml != null) {
return iconXml + displaynameSpan;
}
if (icon === null) {
return "<span style='margin: 2px 4px 2px 2px;' />" + displaynameSpan;
}
// TODO: move this to the API response in a clean way
var isSvgSprite = icon.toLowerCase().indexOf("svg#") !== -1;
return isSvgSprite
? "<svg class='svg-icon' width='24' height='24' style='margin: 2px 4px 2px 2px;' aria-label='' focusable='false'>" +
"<use href='" +
icon +
"' />" +
"</svg>" +
displaynameSpan
: "<img src='" +
icon +
"' width=24 height=24 style='margin: 2px 4px 2px 2px;' alt=''>" +
displaynameSpan;
}
Event.observe(window, "load", function () {
menu = new YAHOO.widget.Menu("breadcrumb-menu", {
position: "dynamic",
hidedelay: 1000,
zIndex: 2001,
scrollincrement: 2,
});
});
Event.observe(document, "mousemove", function (ev) {
mouse = new YAHOO.util.Point(ev.pageX, ev.pageY);
});
function combinePath(a, b) {
var qs;
var i = a.indexOf("?");
if (i >= 0) {
qs = a.substring(i);
a = a.substring(0, i);
} else {
qs = "";
}
i = a.indexOf("#");
if (i >= 0) {
a = a.substring(0, i);
}
if (a.endsWith("/")) {
return a + b + qs;
}
return a + "/" + b + qs;
}
function postRequest(action, event, url) {
new Ajax.Request(url);
if (event.length == 1 && event[0].target != null) {
hoverNotification("Done.", event[0].target);
}
}
function requireConfirmation(action, event, cfg) {
if (confirm(cfg.displayName + ": are you sure?")) {
// TODO I18N
var form = document.createElement("form");
form.setAttribute("method", cfg.post ? "POST" : "GET");
form.setAttribute("action", cfg.url);
if (cfg.post) {
crumb.appendToForm(form);
}
document.body.appendChild(form);
form.submit();
}
}
/**
* Wraps a delayed action and its cancellation.
*/
// unsure if used in plugins
// eslint-disable-next-line no-unused-vars
function Delayed(action, timeout) {
this.schedule = function () {
this.cancel();
this.token = window.setTimeout(
function () {
this.token = null;
action();
}.bind(this),
timeout
);
logger("Scheduled %s", this.token);
};
this.cancel = function () {
if (this.token != null) {
logger("Cancelling %s", this.token);
window.clearTimeout(this.token);
}
this.token = null;
};
}
/**
* Called when the user clicks a mouse to show a context menu.
*
* If the mouse stays there for a while, a context menu gets displayed.
*
* @param {HTMLElement} e
* anchor tag
* @param {String} contextMenuUrl
* The URL that renders JSON for context menu. Optional.
*/
function invokeContextMenu(e, contextMenuUrl) {
contextMenuUrl = contextMenuUrl || "contextMenu";
function showMenu(items) {
menu.hide();
var pos = [e, "tl", "bl"];
if ($(e).hasClassName("tl-tr")) {
pos = [e, "tl", "tr"];
}
menu.cfg.setProperty("context", pos);
menu.clearContent();
menu.addItems(items);
menu.render("breadcrumb-menu-target");
menu.show();
}
// ignore the currently pending call
if (xhr) {
xhr.options.onComplete = function () {};
}
if (e.items) {
// use what's already loaded
showMenu(e.items());
} else {
// fetch menu on demand
xhr = new Ajax.Request(
combinePath(e.getAttribute("href"), contextMenuUrl),
{
onComplete: function (x) {
var items = x.responseText.evalJSON().items;
function fillMenuItem(e) {
if (e.type === "HEADER") {
e.text = makeMenuHtml(
e.icon,
e.iconXml,
"<span class='header'>" + e.displayName + "</span>"
);
e.disabled = true;
} else if (e.type === "SEPARATOR") {
e.text = "<span class='separator'>--</span>";
e.disabled = true;
} else {
e.text = makeMenuHtml(e.icon, e.iconXml, e.displayName);
}
if (e.subMenu != null) {
e.subMenu = {
id: "submenu" + iota++,
itemdata: e.subMenu.items.each(fillMenuItem),
};
}
if (e.requiresConfirmation) {
e.onclick = {
fn: requireConfirmation,
obj: { url: e.url, displayName: e.displayName, post: e.post },
};
delete e.url;
} else if (e.post) {
e.onclick = { fn: postRequest, obj: e.url };
delete e.url;
}
}
items.each(fillMenuItem);
e.items = function () {
return items;
};
showMenu(items);
},
}
);
}
return false;
}
Behaviour.specify("A.model-link", "breadcrumbs", 0, function (link) {
const isFirefox = navigator.userAgent.indexOf("Firefox") !== -1;
// Firefox adds unwanted lines when copying buttons in text, so use a span instead
const dropdownChevron = document.createElement(
isFirefox ? "span" : "button"
);
dropdownChevron.className = "jenkins-menu-dropdown-chevron";
dropdownChevron.addEventListener("click", function (e) {
e.preventDefault();
invokeContextMenu(link);
});
link.appendChild(dropdownChevron);
});
Behaviour.specify("#breadcrumbs LI.children", "breadcrumbs", 0, function (a) {
a.observe("click", function () {
invokeContextMenu(this, "childrenContextMenu");
});
});
/**
* @namespace breadcrumbs
* @class ContextMenu
* @constructor
*/
var ContextMenu = function () {
this.items = [];
};
ContextMenu.prototype = {
/**
* Creates a menu item.
*
* @return {breadcrumbs.MenuItem}
*/
add: function (url, icon, displayName) {
this.items.push({
url: url,
text: makeMenuHtml(icon, null, displayName),
});
return this;
},
};
return {
/**
* Activates the context menu for the specified breadcrumb element.
*
* @param {String|HTMLElement} li
* The LI tag to which you associate the menu (or its ID)
* @param {Function|breadcrumbs.ContextMenu} menu
* Pass in the configured menu object. If a function is given, this function
* is called each time a menu needs to be displayed. This is convenient for dynamically
* populating the content.
*/
attachMenu: function (li, menu) {
$(li).items =
typeof menu == "function"
? menu
: function () {
return menu.items;
};
$(li).addEventListener("click", function () {
invokeContextMenu($(li));
});
},
ContextMenu: ContextMenu,
};
})();