Skip to content

Commit 6d16e08

Browse files
committed
Use localStorage API to track toolbar state
Replaces storing the state in cookies. For simple key/value storage, the API is simpler. Including a cookie with each HTTP request/response is not necessary in modern browsers. https://developer.mozilla.org/en-US/docs/Web/API/Storage
1 parent 799f585 commit 6d16e08

File tree

3 files changed

+10
-23
lines changed

3 files changed

+10
-23
lines changed

debug_toolbar/middleware.py

-4
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,6 @@ def __call__(self, request):
8282
):
8383
return response
8484

85-
# Collapse the toolbar by default if SHOW_COLLAPSED is set.
86-
if toolbar.config["SHOW_COLLAPSED"] and "djdt" not in request.COOKIES:
87-
response.set_cookie("djdt", "hide", 864000)
88-
8985
# Insert the toolbar in the response.
9086
content = response.content.decode(response.charset)
9187
insert_before = dt_settings.get_config()["INSERT_BEFORE"]

debug_toolbar/static/debug_toolbar/js/toolbar.js

+9-19
Original file line numberDiff line numberDiff line change
@@ -199,19 +199,17 @@
199199
document.removeEventListener('mousemove', onHandleMove);
200200
if (djdt.handleDragged) {
201201
event.preventDefault();
202-
djdt.cookie.set('djdttop', handle.offsetTop, {
203-
path: '/',
204-
expires: 10
205-
});
202+
localStorage.setItem('djdt.top', handle.offsetTop);
206203
setTimeout(function () {
207204
djdt.handleDragged = false;
208205
}, 10);
209206
}
210207
});
211-
if (djdt.cookie.get('djdt') === 'hide') {
212-
djdt.hide_toolbar(false);
213-
} else {
208+
const show = localStorage.getItem('djdt.show') || djDebug.dataset.defaultShow;
209+
if (show === 'true') {
214210
djdt.show_toolbar();
211+
} else {
212+
djdt.hide_toolbar();
215213
}
216214
},
217215
hide_panels: function() {
@@ -224,7 +222,7 @@
224222
e.classList.remove('djdt-active');
225223
});
226224
},
227-
hide_toolbar: function(setCookie) {
225+
hide_toolbar: function() {
228226
djdt.hide_panels();
229227

230228
const djDebug = document.getElementById('djDebug');
@@ -233,20 +231,15 @@
233231
const handle = document.querySelector('#djDebugToolbarHandle');
234232
$$.show(handle);
235233
// set handle position
236-
let handleTop = djdt.cookie.get('djdttop');
234+
let handleTop = localStorage.getItem('djdt.top');
237235
if (handleTop) {
238236
handleTop = Math.min(handleTop, window.innerHeight - handle.offsetHeight);
239237
handle.style.top = handleTop + 'px';
240238
}
241239

242240
document.removeEventListener('keydown', onKeyDown);
243241

244-
if (setCookie) {
245-
djdt.cookie.set('djdt', 'hide', {
246-
path: '/',
247-
expires: 10
248-
});
249-
}
242+
localStorage.setItem('djdt.show', 'false');
250243
},
251244
hide_one_level: function() {
252245
const djDebug = document.getElementById('djDebug');
@@ -263,10 +256,7 @@
263256
const djDebug = document.getElementById('djDebug');
264257
$$.hide(djDebug.querySelector('#djDebugToolbarHandle'));
265258
$$.show(djDebug.querySelector('#djDebugToolbar'));
266-
djdt.cookie.set('djdt', 'show', {
267-
path: '/',
268-
expires: 10
269-
});
259+
localStorage.setItem('djdt.show', 'true');
270260
},
271261
cookie: {
272262
get: function(key){

debug_toolbar/templates/debug_toolbar/base.html

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<script src="{% static 'debug_toolbar/js/toolbar.js' %}" async></script>
55
<div id="djDebug" class="djdt-hidden" dir="ltr"
66
{% if toolbar.store_id %}data-store-id="{{ toolbar.store_id }}" data-render-panel-url="{% url 'djdt:render_panel' %}"{% endif %}
7+
data-default-show="{% if toolbar.config.SHOW_COLLAPSED %}false{% else %}true{% endif %}"
78
{{ toolbar.config.ROOT_TAG_EXTRA_ATTRS|safe }}>
89
<div class="djdt-hidden" id="djDebugToolbar">
910
<ul id="djDebugPanelList">

0 commit comments

Comments
 (0)