-
Notifications
You must be signed in to change notification settings - Fork 1.1k
(WIP) Adds support to allow Htmx compatibility #1569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
2e310bf
3cb6f71
97330ee
4068382
b6a377b
7e61c10
a931fed
32806b8
a47ae06
7468906
359bb0d
dfc6aa6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,83 @@ | ||
import { $$, ajaxForm } from "./utils.js"; | ||
import { $$, ajaxForm, pluckData, replaceToolbarState } from "./utils.js"; | ||
|
||
const djDebug = document.getElementById("djDebug"); | ||
|
||
$$.on(djDebug, "click", ".switchHistory", function (event) { | ||
event.preventDefault(); | ||
const newStoreId = this.dataset.storeId; | ||
const tbody = this.closest("tbody"); | ||
function difference(setA, setB) { | ||
const _difference = new Set(setA); | ||
for (const elem of setB) { | ||
_difference.delete(elem); | ||
} | ||
return _difference; | ||
} | ||
|
||
function switchHistory(newStoreId) { | ||
const formTarget = djDebug.querySelector( | ||
".switchHistory[data-store-id='" + newStoreId + "']" | ||
); | ||
const tbody = formTarget.closest("tbody"); | ||
|
||
const highlighted = tbody.querySelector(".djdt-highlighted"); | ||
if (highlighted) { | ||
highlighted.classList.remove("djdt-highlighted"); | ||
} | ||
this.closest("tr").classList.add("djdt-highlighted"); | ||
formTarget.closest("tr").classList.add("djdt-highlighted"); | ||
|
||
ajaxForm(this).then(function (data) { | ||
djDebug.setAttribute("data-store-id", newStoreId); | ||
// Check if response is empty, it could be due to an expired store_id. | ||
ajaxForm(formTarget).then(function (data) { | ||
if (Object.keys(data).length === 0) { | ||
const container = document.getElementById("djdtHistoryRequests"); | ||
container.querySelector( | ||
'button[data-store-id="' + newStoreId + '"]' | ||
).innerHTML = "Switch [EXPIRED]"; | ||
} else { | ||
Object.keys(data).forEach(function (panelId) { | ||
const panel = document.getElementById(panelId); | ||
if (panel) { | ||
panel.outerHTML = data[panelId].content; | ||
document.getElementById("djdt-" + panelId).outerHTML = | ||
data[panelId].button; | ||
} | ||
}); | ||
} | ||
replaceToolbarState(newStoreId, data); | ||
}); | ||
}); | ||
} | ||
|
||
$$.on(djDebug, "click", ".refreshHistory", function (event) { | ||
event.preventDefault(); | ||
function refreshHistory() { | ||
const formTarget = djDebug.querySelector(".refreshHistory"); | ||
const container = document.getElementById("djdtHistoryRequests"); | ||
ajaxForm(this).then(function (data) { | ||
// Remove existing rows first then re-populate with new data | ||
container | ||
.querySelectorAll("tr[data-store-id]") | ||
.forEach(function (node) { | ||
node.remove(); | ||
const oldIds = new Set( | ||
pluckData(container.querySelectorAll("tr[data-store-id]"), "storeId") | ||
); | ||
|
||
return ajaxForm(formTarget) | ||
.then(function (data) { | ||
// Remove existing rows first then re-populate with new data | ||
container | ||
.querySelectorAll("tr[data-store-id]") | ||
.forEach(function (node) { | ||
node.remove(); | ||
}); | ||
data.requests.forEach(function (request) { | ||
container.innerHTML = request.content + container.innerHTML; | ||
}); | ||
data.requests.forEach(function (request) { | ||
container.innerHTML = request.content + container.innerHTML; | ||
}) | ||
.then(function () { | ||
const allIds = new Set( | ||
pluckData( | ||
container.querySelectorAll("tr[data-store-id]"), | ||
"storeId" | ||
) | ||
); | ||
const newIds = difference(allIds, oldIds); | ||
const lastRequestId = newIds.values().next().value; | ||
return { | ||
allIds, | ||
newIds, | ||
lastRequestId, | ||
}; | ||
}); | ||
}); | ||
} | ||
|
||
$$.on(djDebug, "click", ".switchHistory", function (event) { | ||
event.preventDefault(); | ||
switchHistory(this.dataset.storeId); | ||
}); | ||
|
||
$$.on(djDebug, "click", ".refreshHistory", function (event) { | ||
event.preventDefault(); | ||
refreshHistory(); | ||
}); | ||
|
||
window.djdt.refreshHistory = refreshHistory; | ||
window.djdt.switchHistory = switchHistory; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
import { $$, ajax } from "./utils.js"; | ||
import { | ||
$$, | ||
ajax, | ||
controller, | ||
resetAbortController, | ||
replaceToolbarState, | ||
} from "./utils.js"; | ||
|
||
function onKeyDown(event) { | ||
if (event.keyCode === 27) { | ||
|
@@ -8,6 +14,10 @@ function onKeyDown(event) { | |
|
||
const djdt = { | ||
handleDragged: false, | ||
abort() { | ||
controller.abort(); | ||
resetAbortController(); | ||
}, | ||
Comment on lines
+17
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like the only part of the code that uses the controller and the signal. Outside of allowing another third party panel to abort a request, what purpose does this serve? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Calling abort will unregister the click handlers - the intent here is that init will configure the JS click handlers and this will undo it to put us back in the state we were immediately before init. The Purpose of that is that for boosted requests / Turbolinks the new page will contain it's own toolbar html, and I was finding conflicts with multiple click handlers registered. |
||
init() { | ||
const djDebug = document.getElementById("djDebug"); | ||
$$.show(djDebug); | ||
|
@@ -142,19 +152,23 @@ const djdt = { | |
}); | ||
}); | ||
|
||
document | ||
.getElementById("djHideToolBarButton") | ||
.addEventListener("click", function (event) { | ||
document.getElementById("djHideToolBarButton").addEventListener( | ||
"click", | ||
function (event) { | ||
event.preventDefault(); | ||
djdt.hide_toolbar(); | ||
}); | ||
document | ||
.getElementById("djShowToolBarButton") | ||
.addEventListener("click", function () { | ||
}, | ||
{ signal: controller.signal } | ||
); | ||
document.getElementById("djShowToolBarButton").addEventListener( | ||
"click", | ||
function () { | ||
if (!djdt.handleDragged) { | ||
djdt.show_toolbar(); | ||
} | ||
}); | ||
}, | ||
{ signal: controller.signal } | ||
); | ||
let startPageY, baseY; | ||
const handle = document.getElementById("djDebugToolbarHandle"); | ||
function onHandleMove(event) { | ||
|
@@ -174,25 +188,31 @@ const djdt = { | |
djdt.handleDragged = true; | ||
} | ||
} | ||
document | ||
.getElementById("djShowToolBarButton") | ||
.addEventListener("mousedown", function (event) { | ||
document.getElementById("djShowToolBarButton").addEventListener( | ||
"mousedown", | ||
function (event) { | ||
event.preventDefault(); | ||
startPageY = event.pageY; | ||
baseY = handle.offsetTop - startPageY; | ||
document.addEventListener("mousemove", onHandleMove); | ||
}); | ||
document.addEventListener("mouseup", function (event) { | ||
document.removeEventListener("mousemove", onHandleMove); | ||
if (djdt.handleDragged) { | ||
event.preventDefault(); | ||
localStorage.setItem("djdt.top", handle.offsetTop); | ||
requestAnimationFrame(function () { | ||
djdt.handleDragged = false; | ||
}); | ||
djdt.ensure_handle_visibility(); | ||
} | ||
}); | ||
}, | ||
{ signal: controller.signal } | ||
); | ||
document.addEventListener( | ||
"mouseup", | ||
function (event) { | ||
document.removeEventListener("mousemove", onHandleMove); | ||
if (djdt.handleDragged) { | ||
event.preventDefault(); | ||
localStorage.setItem("djdt.top", handle.offsetTop); | ||
requestAnimationFrame(function () { | ||
djdt.handleDragged = false; | ||
}); | ||
djdt.ensure_handle_visibility(); | ||
} | ||
}, | ||
{ signal: controller.signal } | ||
); | ||
const show = | ||
localStorage.getItem("djdt.show") || djDebug.dataset.defaultShow; | ||
if (show === "true") { | ||
|
@@ -228,7 +248,9 @@ const djdt = { | |
const handle = document.getElementById("djDebugToolbarHandle"); | ||
$$.show(handle); | ||
djdt.ensure_handle_visibility(); | ||
window.addEventListener("resize", djdt.ensure_handle_visibility); | ||
window.addEventListener("resize", djdt.ensure_handle_visibility, { | ||
signal: controller.signal, | ||
}); | ||
document.removeEventListener("keydown", onKeyDown); | ||
|
||
localStorage.setItem("djdt.show", "false"); | ||
|
@@ -247,7 +269,9 @@ const djdt = { | |
} | ||
}, | ||
show_toolbar() { | ||
document.addEventListener("keydown", onKeyDown); | ||
document.addEventListener("keydown", onKeyDown, { | ||
signal: controller.signal, | ||
}); | ||
$$.hide(document.getElementById("djDebugToolbarHandle")); | ||
$$.show(document.getElementById("djDebugToolbar")); | ||
localStorage.setItem("djdt.show", "true"); | ||
|
@@ -295,16 +319,44 @@ const djdt = { | |
}, | ||
}, | ||
}; | ||
|
||
const origOpen = XMLHttpRequest.prototype.open; | ||
XMLHttpRequest.prototype.open = function () { | ||
this.addEventListener("load", function () { | ||
if ( | ||
this.responseURL !== "" && | ||
this.responseURL.indexOf("__debug__") === -1 | ||
) { | ||
let signed = this.getResponseHeader("dj-toolbar-signature"); | ||
const store_id = this.getResponseHeader("dj-toolbar-store-id"); | ||
const history_sidebar_url = this.getResponseHeader( | ||
"dj-history-sidebar-url" | ||
); | ||
if (signed !== null) { | ||
signed = encodeURIComponent(signed); | ||
const dest = `${history_sidebar_url}?signed=${signed}`; | ||
ajax(dest).then(function (data) { | ||
replaceToolbarState(store_id, data); | ||
}); | ||
} | ||
} | ||
}); | ||
origOpen.apply(this, arguments); | ||
}; | ||
|
||
window.djdt = { | ||
show_toolbar: djdt.show_toolbar, | ||
hide_toolbar: djdt.hide_toolbar, | ||
init: djdt.init, | ||
abort: djdt.abort, | ||
close: djdt.hide_one_level, | ||
cookie: djdt.cookie, | ||
}; | ||
|
||
if (document.readyState !== "loading") { | ||
djdt.init(); | ||
} else { | ||
document.addEventListener("DOMContentLoaded", djdt.init); | ||
document.addEventListener("DOMContentLoaded", djdt.init, { | ||
signal: controller.signal, | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this isn't likely to change between requests, this could be moved out of the per-request cycle and into the template as a JS variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍