Skip to content

Commit 00bc368

Browse files
scumlmatthiasktim-schilling
committed
Attach handlers to djdebug instead of document
Adds example page for turbo Add a few additional links for the tips on htmx and Turbo. Providing the links to the libraries' documentation may eventually go stale, but it should help the developer understand what's actually happening. Co-authored-by: Matthias Kestenholz <[email protected]> Co-authored-by: Tim Schilling <[email protected]>
1 parent d04b9d1 commit 00bc368

File tree

10 files changed

+211
-102
lines changed

10 files changed

+211
-102
lines changed

.github/workflows/test.yml

+2-7
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,6 @@ jobs:
5151
restore-keys: |
5252
${{ matrix.python-version }}-v1-
5353
54-
- name: Install enchant (only for docs)
55-
run: |
56-
sudo apt-get -qq update
57-
sudo apt-get -y install enchant
58-
5954
- name: Install dependencies
6055
run: |
6156
python -m pip install --upgrade pip
@@ -122,10 +117,10 @@ jobs:
122117
restore-keys: |
123118
${{ matrix.python-version }}-v1-
124119
125-
- name: Install enchant (only for docs) and gdal-bin (for postgis)
120+
- name: Install gdal-bin (for postgis)
126121
run: |
127122
sudo apt-get -qq update
128-
sudo apt-get -y install enchant gdal-bin
123+
sudo apt-get -y install gdal-bin
129124
130125
- name: Install dependencies
131126
run: |

debug_toolbar/static/debug_toolbar/js/toolbar.js

+66-77
Original file line numberDiff line numberDiff line change
@@ -18,65 +18,60 @@ function getDebugElement() {
1818
const djdt = {
1919
handleDragged: false,
2020
init() {
21-
$$.on(
22-
document.body,
23-
"click",
24-
"#djDebugPanelList li a",
25-
function (event) {
26-
event.preventDefault();
27-
if (!this.className) {
28-
return;
29-
}
30-
const panelId = this.className;
31-
const current = document.getElementById(panelId);
32-
if ($$.visible(current)) {
33-
djdt.hidePanels();
34-
} else {
35-
djdt.hidePanels();
21+
const djDebug = getDebugElement();
22+
$$.on(djDebug, "click", "#djDebugPanelList li a", function (event) {
23+
event.preventDefault();
24+
if (!this.className) {
25+
return;
26+
}
27+
const panelId = this.className;
28+
const current = document.getElementById(panelId);
29+
if ($$.visible(current)) {
30+
djdt.hidePanels();
31+
} else {
32+
djdt.hidePanels();
3633

37-
$$.show(current);
38-
this.parentElement.classList.add("djdt-active");
34+
$$.show(current);
35+
this.parentElement.classList.add("djdt-active");
3936

40-
const djDebug = getDebugElement();
41-
const inner = current.querySelector(
42-
".djDebugPanelContent .djdt-scroll"
43-
),
44-
storeId = djDebug.dataset.storeId;
45-
if (storeId && inner.children.length === 0) {
46-
const url = new URL(
47-
djDebug.dataset.renderPanelUrl,
48-
window.location
49-
);
50-
url.searchParams.append("store_id", storeId);
51-
url.searchParams.append("panel_id", panelId);
52-
ajax(url).then(function (data) {
53-
inner.previousElementSibling.remove(); // Remove AJAX loader
54-
inner.innerHTML = data.content;
55-
$$.executeScripts(data.scripts);
56-
$$.applyStyles(inner);
57-
djDebug.dispatchEvent(
58-
new CustomEvent("djdt.panel.render", {
59-
detail: { panelId: panelId },
60-
})
61-
);
62-
});
63-
} else {
37+
const inner = current.querySelector(
38+
".djDebugPanelContent .djdt-scroll"
39+
),
40+
storeId = djDebug.dataset.storeId;
41+
if (storeId && inner.children.length === 0) {
42+
const url = new URL(
43+
djDebug.dataset.renderPanelUrl,
44+
window.location
45+
);
46+
url.searchParams.append("store_id", storeId);
47+
url.searchParams.append("panel_id", panelId);
48+
ajax(url).then(function (data) {
49+
inner.previousElementSibling.remove(); // Remove AJAX loader
50+
inner.innerHTML = data.content;
51+
$$.executeScripts(data.scripts);
52+
$$.applyStyles(inner);
6453
djDebug.dispatchEvent(
6554
new CustomEvent("djdt.panel.render", {
6655
detail: { panelId: panelId },
6756
})
6857
);
69-
}
58+
});
59+
} else {
60+
djDebug.dispatchEvent(
61+
new CustomEvent("djdt.panel.render", {
62+
detail: { panelId: panelId },
63+
})
64+
);
7065
}
7166
}
72-
);
73-
$$.on(document.body, "click", "#djDebug .djDebugClose", function () {
67+
});
68+
$$.on(djDebug, "click", ".djDebugClose", function () {
7469
djdt.hideOneLevel();
7570
});
7671
$$.on(
77-
document.body,
72+
djDebug,
7873
"click",
79-
"#djDebug .djDebugPanelButton input[type=checkbox]",
74+
".djDebugPanelButton input[type=checkbox]",
8075
function () {
8176
djdt.cookie.set(
8277
this.dataset.cookie,
@@ -90,7 +85,7 @@ const djdt = {
9085
);
9186

9287
// Used by the SQL and template panels
93-
$$.on(document.body, "click", "#djDebug .remoteCall", function (event) {
88+
$$.on(djDebug, "click", ".remoteCall", function (event) {
9489
event.preventDefault();
9590

9691
let url;
@@ -113,7 +108,7 @@ const djdt = {
113108
});
114109

115110
// Used by the cache, profiling and SQL panels
116-
$$.on(document.body, "click", "#djDebug .djToggleSwitch", function () {
111+
$$.on(djDebug, "click", ".djToggleSwitch", function () {
117112
const id = this.dataset.toggleId;
118113
const toggleOpen = "+";
119114
const toggleClose = "-";
@@ -150,12 +145,12 @@ const djdt = {
150145
});
151146
});
152147

153-
$$.on(document.body, "click", "#djHideToolBarButton", function (event) {
148+
$$.on(djDebug, "click", "#djHideToolBarButton", function (event) {
154149
event.preventDefault();
155150
djdt.hideToolbar();
156151
});
157152

158-
$$.on(document.body, "click", "#djShowToolBarButton", function () {
153+
$$.on(djDebug, "click", "#djShowToolBarButton", function () {
159154
if (!djdt.handleDragged) {
160155
djdt.showToolbar();
161156
}
@@ -179,35 +174,29 @@ const djdt = {
179174
djdt.handleDragged = true;
180175
}
181176
}
182-
$$.on(
183-
document.body,
184-
"mousedown",
185-
"#djShowToolBarButton",
186-
function (event) {
187-
event.preventDefault();
188-
startPageY = event.pageY;
189-
baseY = handle.offsetTop - startPageY;
190-
document.addEventListener("mousemove", onHandleMove);
177+
$$.on(djDebug, "mousedown", "#djShowToolBarButton", function (event) {
178+
event.preventDefault();
179+
startPageY = event.pageY;
180+
baseY = handle.offsetTop - startPageY;
181+
document.addEventListener("mousemove", onHandleMove);
191182

192-
document.addEventListener(
193-
"mouseup",
194-
function (event) {
195-
document.removeEventListener("mousemove", onHandleMove);
196-
if (djdt.handleDragged) {
197-
event.preventDefault();
198-
localStorage.setItem("djdt.top", handle.offsetTop);
199-
requestAnimationFrame(function () {
200-
djdt.handleDragged = false;
201-
});
202-
djdt.ensureHandleVisibility();
203-
}
204-
},
205-
{ once: true }
206-
);
207-
}
208-
);
183+
document.addEventListener(
184+
"mouseup",
185+
function (event) {
186+
document.removeEventListener("mousemove", onHandleMove);
187+
if (djdt.handleDragged) {
188+
event.preventDefault();
189+
localStorage.setItem("djdt.top", handle.offsetTop);
190+
requestAnimationFrame(function () {
191+
djdt.handleDragged = false;
192+
});
193+
djdt.ensureHandleVisibility();
194+
}
195+
},
196+
{ once: true }
197+
);
198+
});
209199

210-
const djDebug = getDebugElement();
211200
// Make sure the debug element is rendered at least once.
212201
// showToolbar will continue to show it in the future if the
213202
// entire DOM is reloaded.

docs/configuration.rst

+2
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ Toolbar options
9898

9999
The toolbar keeps up to this many results in memory.
100100

101+
.. _ROOT_TAG_EXTRA_ATTRS:
102+
101103
* ``ROOT_TAG_EXTRA_ATTRS``
102104

103105
Default: ``''``

docs/spelling_wordlist.txt

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ fallbacks
88
flamegraph
99
flatpages
1010
frontend
11+
Hotwire
12+
htmx
1113
inlining
1214
isort
1315
Jazzband

docs/tips.rst

+38
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,44 @@ Browsers have become more aggressive with caching static assets, such as
2020
JavaScript and CSS files. Check your browser's development console, and if
2121
you see errors, try a hard browser refresh or clearing your cache.
2222

23+
Working with htmx and Turbo
24+
----------------------------
25+
26+
Libraries such as `htmx <https://htmx.org/>`_ and
27+
`Turbo <https://turbo.hotwired.dev/>`_ need additional configuration to retain
28+
the toolbar handle element through page renders. This can be done by
29+
configuring the :ref:`ROOT_TAG_EXTRA_ATTRS <ROOT_TAG_EXTRA_ATTRS>` to include
30+
the relevant JavaScript library's attribute.
31+
32+
htmx
33+
~~~~
34+
35+
The attribute `htmx <https://htmx.org/>`_ uses is
36+
`hx-preserve <https://htmx.org/attributes/hx-preserve/>`_.
37+
38+
Update your settings to include:
39+
40+
.. code-block:: python
41+
42+
DEBUG_TOOLBAR_CONFIG = {
43+
"ROOT_TAG_EXTRA_ATTRS": "hx-preserve"
44+
}
45+
46+
Hotwire Turbo
47+
~~~~~~~~~~~~~
48+
49+
The attribute `Turbo <https://turbo.hotwired.dev/>`_ uses is
50+
`data-turbo-permanent <https://turbo.hotwired.dev/reference/attributes>`_
51+
52+
Update your settings to include:
53+
54+
.. code-block:: python
55+
56+
DEBUG_TOOLBAR_CONFIG = {
57+
"ROOT_TAG_EXTRA_ATTRS": "data-turbo-permanent"
58+
}
59+
60+
2361
Performance considerations
2462
--------------------------
2563

example/settings.py

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161

6262
WSGI_APPLICATION = "example.wsgi.application"
6363

64+
DEBUG_TOOLBAR_CONFIG = {"ROOT_TAG_EXTRA_ATTRS": "data-turbo-permanent hx-preserve"}
6465

6566
# Cache and database
6667

example/templates/htmx/boost.html

+19-16
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,28 @@
33
<html>
44
<head>
55
<meta http-equiv="content-type" content="text/html; charset=utf-8">
6-
<title>Index of Tests</title>
7-
<script src="//unpkg.com/[email protected].2"></script>
6+
<title>Index of Tests (htmx)</title>
7+
<script src="//unpkg.com/[email protected].4"></script>
88
</head>
99
<body hx-boost="true">
10-
<h1>Index of Tests</h1>
10+
<h1>Index of Tests (htmx) - Page {{page_num|default:"1"}}</h1>
11+
12+
<p>
13+
For the debug panel to remain through page navigation, add the setting:
14+
<pre>
15+
DEBUG_TOOLBAR_CONFIG = {
16+
"ROOT_TAG_EXTRA_ATTRS": "hx-preserve"
17+
}
18+
</pre>
19+
</p>
20+
1121
<ul>
12-
<li><a href="/jquery/">jQuery 3.3.1</a></li>
13-
<li><a href="/mootools/">MooTools 1.6.0</a></li>
14-
<li><a href="/prototype/">Prototype 1.7.3.0</a></li>
22+
<li><a href="/admin/">Django Admin</a></li>
23+
<li><a href="{% url "htmx" %}">HTMX - Page 1</a></li>
24+
<li><a href="{% url "htmx2" %}">HTMX- Page 2</a></li>
1525
</ul>
16-
<p><a href="/admin/">Django Admin</a></p>
17-
<script>
18-
if (typeof window.htmx !== "undefined") {
19-
htmx.on("htmx:afterSettle", function(detail) {
20-
if (typeof window.djdt !== "undefined" && detail.target instanceof HTMLBodyElement) {
21-
djdt.show_toolbar();
22-
}
23-
});
24-
}
25-
</script>
26+
27+
<a href="{% url "home" %}">Home</a>
28+
2629
</body>
2730
</html>

example/templates/index.html

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ <h1>Index of Tests</h1>
1212
<li><a href="/jquery/">jQuery 3.3.1</a></li>
1313
<li><a href="/mootools/">MooTools 1.6.0</a></li>
1414
<li><a href="/prototype/">Prototype 1.7.3.0</a></li>
15+
<li><a href="/turbo/">Hotwire Turbo</a></li>
16+
<li><a href="/htmx/">htmx</a></li>
1517
</ul>
1618
<p><a href="/admin/">Django Admin</a></p>
1719
{% endcache %}

0 commit comments

Comments
 (0)