forked from jenkinsci/jenkins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilds-card.js
More file actions
157 lines (136 loc) · 4.86 KB
/
builds-card.js
File metadata and controls
157 lines (136 loc) · 4.86 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
import debounce from "lodash/debounce";
import behaviorShim from "@/util/behavior-shim";
// Card/item controls
const buildHistoryPage = document.getElementById("buildHistoryPage");
const pageSearch = buildHistoryPage.querySelector(".jenkins-search");
const pageSearchInput = buildHistoryPage.querySelector("input");
const ajaxUrl = buildHistoryPage.getAttribute("page-ajax");
const card = document.querySelector("#jenkins-builds");
const contents = card.querySelector("#jenkins-build-history");
const container = card.querySelector(".app-builds-container");
const loadingBuilds = card.querySelector("#loading-builds");
const noBuilds = card.querySelector("#no-builds");
// Pagination controls
const paginationControls = document.querySelector("#controls");
const paginationPrevious = document.querySelector("#up");
const paginationNext = document.querySelector("#down");
// Refresh variables
let buildRefreshTimeout;
const updateBuildsRefreshInterval = 5000;
/**
* Refresh the 'Builds' card
* @param {QueryParameters} options
*/
function load(options = {}) {
/** @type {QueryParameters} */
cancelRefreshTimeout();
const params = Object.assign({}, options, { search: pageSearchInput.value });
const paginationOrFirst =
buildHistoryPage.dataset.pageHasUp === "false" ||
"older-than" in params ||
"newer-than" in params;
// Avoid fetching if the page isn't visible
if (document.hidden) {
return;
}
createRefreshTimeout();
// When we're not on the first page and this is not a load due to pagination
// we need to set the correct value for older-than so we fetch the same set of runs
if (!paginationOrFirst) {
params["older-than"] = (
BigInt(buildHistoryPage.dataset.pageEntryNewest) + 1n
).toString();
}
fetch(ajaxUrl + toQueryString(params)).then((rsp) => {
if (rsp.ok) {
rsp.text().then((responseText) => {
container.classList.remove("app-builds-container--loading");
pageSearch.classList.remove("jenkins-search--loading");
// Show the 'No builds' text if there are no builds
if (responseText.trim() === "") {
contents.innerHTML = "";
noBuilds.style.display = "block";
loadingBuilds.style.display = "none";
updateCardControls({
pageHasUp: false,
pageHasDown: false,
pageEntryNewest: false,
pageEntryOldest: false,
});
return;
}
// Show the refreshed builds list
contents.innerHTML = responseText;
loadingBuilds.style.display = "none";
behaviorShim.applySubtree(contents);
// Show the card controls
const div = document.createElement("div");
div.innerHTML = responseText;
const innerChild = div.children[0];
updateCardControls({
pageHasUp: innerChild.dataset.pageHasUp === "true",
pageHasDown: innerChild.dataset.pageHasDown === "true",
pageEntryNewest: innerChild.dataset.pageEntryNewest,
pageEntryOldest: innerChild.dataset.pageEntryOldest,
});
});
} else {
console.error("Failed to load 'Builds' card, response from API is:", rsp);
}
});
}
/**
* Shows/hides the card's pagination controls depending on the passed parameter
* @param {CardControlsOptions} parameters
*/
function updateCardControls(parameters) {
paginationControls.classList.toggle(
"jenkins-hidden",
!parameters.pageHasUp && !parameters.pageHasDown,
);
paginationPrevious.classList.toggle(
"app-builds-container__button--disabled",
!parameters.pageHasUp,
);
paginationNext.classList.toggle(
"app-builds-container__button--disabled",
!parameters.pageHasDown,
);
buildHistoryPage.dataset.pageEntryNewest = parameters.pageEntryNewest;
buildHistoryPage.dataset.pageEntryOldest = parameters.pageEntryOldest;
buildHistoryPage.dataset.pageHasUp = parameters.pageHasUp;
}
paginationPrevious.addEventListener("click", () => {
load({ "newer-than": buildHistoryPage.dataset.pageEntryNewest });
});
paginationNext.addEventListener("click", () => {
load({ "older-than": buildHistoryPage.dataset.pageEntryOldest });
});
function createRefreshTimeout() {
cancelRefreshTimeout();
buildRefreshTimeout = window.setTimeout(
() => load(),
updateBuildsRefreshInterval,
);
}
function cancelRefreshTimeout() {
if (buildRefreshTimeout) {
window.clearTimeout(buildRefreshTimeout);
buildRefreshTimeout = undefined;
}
}
const debouncedLoad = debounce(() => {
load();
}, 150);
document.addEventListener("DOMContentLoaded", function () {
pageSearchInput.addEventListener("input", function () {
container.classList.add("app-builds-container--loading");
pageSearch.classList.add("jenkins-search--loading");
debouncedLoad();
});
container.classList.add("app-builds-container--loading");
load();
window.addEventListener("focus", function () {
load();
});
});