Skip to content
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

1843 new ajax request resets whole view if historypanel is enabled #1872

Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
096fdf4
New AJAX request resets whole view if HistoryPanel is enabled
elineda Jan 15, 2024
f499e41
New AJAX request resets whole view if HistoryPanel is enabled
elineda Jan 18, 2024
73c96eb
New AJAX request resets whole view if HistoryPanel is enabled
elineda Jan 23, 2024
def324a
New AJAX request resets whole view if HistoryPanel is enabled
elineda Jan 24, 2024
150e11b
New AJAX request resets whole view if HistoryPanel is enabled
elineda Jan 24, 2024
0419cd7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 24, 2024
c6d3b65
Add ajax in spelling_wordlist.txt
elineda Jan 24, 2024
bd2a24e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 24, 2024
d23bbd2
New AJAX request resets whole view if HistoryPanel is enabled
elineda Jan 24, 2024
5c3da4c
Merge remote-tracking branch 'origin/1843-new-ajax-request-resets-who…
elineda Jan 24, 2024
1d2ed26
Merge branch 'main' into 1843-new-ajax-request-resets-whole-view-if-h…
elineda Jan 25, 2024
4cb7900
Documentation suggestions
tim-schilling Jan 25, 2024
20fad64
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 25, 2024
59e6a28
Update for line lengths.
tim-schilling Jan 25, 2024
43a6966
Clean-up Tim's English in the changelog
tim-schilling Jan 26, 2024
ba03e9f
New AJAX request resets whole view if HistoryPanel is enabled
elineda Jan 29, 2024
0fa7647
New AJAX request resets whole view if HistoryPanel is enabled
elineda Jan 29, 2024
ea556df
New AJAX request resets whole view if HistoryPanel is enabled
elineda Jan 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions debug_toolbar/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"SQL_WARNING_THRESHOLD": 500, # milliseconds
"OBSERVE_REQUEST_CALLBACK": "debug_toolbar.toolbar.observe_request",
"TOOLBAR_LANGUAGE": None,
"UDPATE_ON_AJAX": False,
elineda marked this conversation as resolved.
Show resolved Hide resolved
}


Expand Down
1 change: 1 addition & 0 deletions debug_toolbar/static/debug_toolbar/js/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,4 @@ $$.on(djDebug, "click", ".refreshHistory", function (event) {
event.preventDefault();
refreshHistory();
});
$$.onPanelRender(djDebug, "HistoryPanel", refreshHistory);
elineda marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 6 additions & 1 deletion debug_toolbar/static/debug_toolbar/js/toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const djdt = {
handleDragged: false,
init() {
const djDebug = getDebugElement();
window.djdt.update_on_ajax = djDebug.getAttribute("update-on-ajax") === "True";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not completely consistent but we generally prefer using camelCase in JavaScript code.

Also, other parts of the code use the dataset attribute, so this should be djDebug.dataset.updateOnAjax and data-update-on-ajax (or whatever the name of the attribute is). A data- prefix for attributes is a good idea anyway because such attributes are always valid HTML.

I wonder if there is a better place for this property though. Tim suggested using localStorage to allow switching between the auto update and manual update behavior on the fly, and this probably wouldn't work with the way it's proposed here. Also, I don't think we want to expose the attribute to external modification (just yet). Putting it in the same place as the handleDragged property may be a good idea?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tim suggested using localStorage to allow switching between the auto update and manual update behavior on the fly, and this probably wouldn't work with the way it's proposed here.

For some context, when Elineda and I spoke yesterday, we decided we could start with a settings control. If we find that it doesn't meet developers needs, we can add the JS to make it more stateful.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did change asked, but I don't put on a localstorage, I just with handleDragged.

$$.on(djDebug, "click", "#djDebugPanelList li a", function (event) {
event.preventDefault();
if (!this.className) {
Expand Down Expand Up @@ -55,6 +56,7 @@ const djdt = {
detail: { panelId: panelId },
})
);

tim-schilling marked this conversation as resolved.
Show resolved Hide resolved
});
} else {
djDebug.dispatchEvent(
Expand Down Expand Up @@ -274,7 +276,9 @@ const djdt = {
storeId = encodeURIComponent(storeId);
const dest = `${sidebarUrl}?store_id=${storeId}`;
slowjax(dest).then(function (data) {
replaceToolbarState(storeId, data);
if (window.djdt.update_on_ajax){
replaceToolbarState(storeId, data);
}
});
}

Expand Down Expand Up @@ -356,6 +360,7 @@ window.djdt = {
init: djdt.init,
close: djdt.hideOneLevel,
cookie: djdt.cookie,
update_on_ajax: false,
};

if (document.readyState !== "loading") {
Expand Down
2 changes: 1 addition & 1 deletion debug_toolbar/templates/debug_toolbar/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
data-sidebar-url="{{ history_url }}"
{% endif %}
data-default-show="{% if toolbar.config.SHOW_COLLAPSED %}false{% else %}true{% endif %}"
{{ toolbar.config.ROOT_TAG_EXTRA_ATTRS|safe }}>
{{ toolbar.config.ROOT_TAG_EXTRA_ATTRS|safe }} update-on-ajax="{{ toolbar.config.UDPATE_ON_AJAX|safe }}">
<div class="djdt-hidden" id="djDebugToolbar">
<ul id="djDebugPanelList">
<li><a id="djHideToolBarButton" href="#" title="{% trans 'Hide toolbar' %}">{% trans "Hide" %} »</a></li>
Expand Down
1 change: 1 addition & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Pending
``django.contrib.admindocs.utils.get_view_name``.
* Switched from black to the `ruff formatter
<https://astral.sh/blog/the-ruff-formatter>`__.
* Add a setting for refresh or not the toolbar if an ajax request occurs.
tim-schilling marked this conversation as resolved.
Show resolved Hide resolved

4.2.0 (2023-08-10)
------------------
Expand Down
9 changes: 9 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ Toolbar options
but want to render your application in French, you would set this to
``"en-us"`` and :setting:`LANGUAGE_CODE` to ``"fr"``.

.. _UDPATE_ON_AJAX:

* ``UDPATE_ON_AJAX``

Default: ``False``

This setting specifies if the whole toolbar need to be refresh if an ajax
request is done.
tim-schilling marked this conversation as resolved.
Show resolved Hide resolved

Panel options
~~~~~~~~~~~~~

Expand Down
1 change: 1 addition & 0 deletions docs/spelling_wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Pympler
Roboto
Transifex
Werkzeug
ajax
async
backend
backends
Expand Down
21 changes: 21 additions & 0 deletions tests/templates/ajax/ajax.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% extends "base.html" %}
{% block content %}
<div id="click_for_ajax">click for ajax</div>

<script>

let click_for_ajax = document.getElementById("click_for_ajax")
function send_ajax() {
let xhr = new XMLHttpRequest();
let url = '/json_view/';
xhr.open("GET", url, true);
xhr.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText);
}
}
xhr.send();
}
document.addEventListener("click", (event) => {send_ajax()});
</script>
{% endblock %}
17 changes: 17 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,3 +749,20 @@ def test_toolbar_language_will_render_to_locale_when_set_both(self):
)
self.assertIn("Query", table.text)
self.assertIn("Action", table.text)

def test_ajax_dont_refresh(self):
self.get("/ajax/")
make_ajax = self.selenium.find_element(By.ID, "click_for_ajax")
make_ajax.click()
history_panel = self.selenium.find_element(By.ID, "djdt-HistoryPanel")
self.assertIn("/ajax/", history_panel.text)
self.assertNotIn("/json_view/", history_panel.text)

@override_settings(DEBUG_TOOLBAR_CONFIG={"UDPATE_ON_AJAX": True})
def test_ajax_refresh(self):
self.get("/ajax/")
make_ajax = self.selenium.find_element(By.ID, "click_for_ajax")
make_ajax.click()
history_panel = self.selenium.find_element(By.ID, "djdt-HistoryPanel")
self.assertNotIn("/ajax/", history_panel.text)
self.assertIn("/json_view/", history_panel.text)
1 change: 1 addition & 0 deletions tests/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
path("cached_low_level_view/", views.cached_low_level_view),
path("json_view/", views.json_view),
path("redirect/", views.redirect_view),
path("ajax/", views.ajax_view),
path("login_without_redirect/", LoginView.as_view(redirect_field_name=None)),
path("admin/", admin.site.urls),
path("__debug__/", include("debug_toolbar.urls")),
Expand Down
4 changes: 4 additions & 0 deletions tests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ def listcomp_view(request):

def redirect_view(request):
return HttpResponseRedirect("/regular/redirect/")


def ajax_view(request):
return render(request, "ajax/ajax.html")
Loading