Skip to content

Commit 3ea0900

Browse files
committed
allowing history refresh and switch to be scripted
1 parent b6183a3 commit 3ea0900

File tree

2 files changed

+70
-20
lines changed

2 files changed

+70
-20
lines changed
Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
1-
import { $$, ajaxForm } from "./utils.js";
1+
import { $$, ajaxForm, pluckData } from "./utils.js";
22

33
const djDebug = document.getElementById("djDebug");
44

5-
$$.on(djDebug, "click", ".switchHistory", function (event) {
6-
event.preventDefault();
7-
const newStoreId = this.dataset.storeId;
8-
const tbody = this.closest("tbody");
5+
function difference(setA, setB) {
6+
const _difference = new Set(setA);
7+
for (const elem of setB) {
8+
_difference.delete(elem);
9+
}
10+
return _difference;
11+
}
12+
13+
function switchHistory(newStoreId) {
14+
const formTarget = djDebug.querySelector(
15+
".switchHistory[data-store-id='" + newStoreId + "']"
16+
);
17+
const tbody = formTarget.closest("tbody");
918

1019
const highlighted = tbody.querySelector(".djdt-highlighted");
1120
if (highlighted) {
1221
highlighted.classList.remove("djdt-highlighted");
1322
}
14-
this.closest("tr").classList.add("djdt-highlighted");
23+
formTarget.closest("tr").classList.add("djdt-highlighted");
1524

16-
ajaxForm(this).then(function (data) {
25+
ajaxForm(formTarget).then(function (data) {
1726
djDebug.setAttribute("data-store-id", newStoreId);
1827
// Check if response is empty, it could be due to an expired store_id.
1928
if (Object.keys(data).length === 0) {
@@ -32,20 +41,53 @@ $$.on(djDebug, "click", ".switchHistory", function (event) {
3241
});
3342
}
3443
});
35-
});
44+
}
3645

37-
$$.on(djDebug, "click", ".refreshHistory", function (event) {
38-
event.preventDefault();
46+
function refreshHistory() {
47+
const formTarget = djDebug.querySelector(".refreshHistory");
3948
const container = document.getElementById("djdtHistoryRequests");
40-
ajaxForm(this).then(function (data) {
41-
// Remove existing rows first then re-populate with new data
42-
container
43-
.querySelectorAll("tr[data-store-id]")
44-
.forEach(function (node) {
45-
node.remove();
49+
const oldIds = new Set(
50+
pluckData(container.querySelectorAll("tr[data-store-id]"), "storeId")
51+
);
52+
53+
return ajaxForm(formTarget)
54+
.then(function (data) {
55+
// Remove existing rows first then re-populate with new data
56+
container
57+
.querySelectorAll("tr[data-store-id]")
58+
.forEach(function (node) {
59+
node.remove();
60+
});
61+
data.requests.forEach(function (request) {
62+
container.innerHTML = request.content + container.innerHTML;
4663
});
47-
data.requests.forEach(function (request) {
48-
container.innerHTML = request.content + container.innerHTML;
64+
})
65+
.then(function () {
66+
const allIds = new Set(
67+
pluckData(
68+
container.querySelectorAll("tr[data-store-id]"),
69+
"storeId"
70+
)
71+
);
72+
const newIds = difference(allIds, oldIds);
73+
const lastRequestId = newIds.values().next().value;
74+
return {
75+
allIds,
76+
newIds,
77+
lastRequestId,
78+
};
4979
});
50-
});
80+
}
81+
82+
$$.on(djDebug, "click", ".switchHistory", function (event) {
83+
event.preventDefault();
84+
switchHistory(this.dataset.storeId);
85+
});
86+
87+
$$.on(djDebug, "click", ".refreshHistory", function (event) {
88+
event.preventDefault();
89+
refreshHistory();
5190
});
91+
92+
window.djdt.refreshHistory = refreshHistory;
93+
window.djdt.switchHistory = switchHistory;

debug_toolbar/static/debug_toolbar/js/utils.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ function ajax(url, init) {
9898
});
9999
}
100100

101+
function pluckData(array, key) {
102+
const data = [];
103+
array.forEach(function (obj) {
104+
data.push(obj.dataset[key]);
105+
});
106+
return data;
107+
}
108+
101109
function ajaxForm(element) {
102110
const form = element.closest("form");
103111
const url = new URL(form.action);
@@ -111,4 +119,4 @@ function ajaxForm(element) {
111119
return ajax(url, ajaxData);
112120
}
113121

114-
export { $$, ajax, ajaxForm, controller, resetAbortController };
122+
export { $$, ajax, ajaxForm, controller, resetAbortController, pluckData };

0 commit comments

Comments
 (0)