Skip to content

Commit 1a3b6d7

Browse files
committed
[fix] Add comments and remove hardcode url
1 parent 5aa2e02 commit 1a3b6d7

3 files changed

Lines changed: 20 additions & 6 deletions

File tree

openwisp_monitoring/device/static/monitoring/js/device-map.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,12 @@
197197
let activeRequest = null;
198198
let loading = false;
199199
function fetchDevices(url, ms = 0, append) {
200+
// Nothing to fetch if there's no URL (e.g. last page reached).
200201
if (!url) return;
202+
// Skip duplicate scroll fetches; abort stale filter fetches.
201203
if (append && loading) return;
202204
clearTimeout(fetchDevicesTimeout);
205+
// Abort pending filter/search to prevent stale data.
203206
if (!append && activeRequest) {
204207
activeRequest.abort();
205208
}
@@ -219,24 +222,26 @@
219222
container.css("--table-spinner-top", `${spinnerTop}px`);
220223
container.addClass("is-loading-append");
221224
}
225+
// Debounce — wait ms (0/100/300ms) so rapid events (scroll/input) coalesce before the AJAX call.
222226
fetchDevicesTimeout = setTimeout(() => {
223227
let params = new URLSearchParams();
224228
const searchParam = el.find("#device-search").val().toLowerCase().trim();
225229
const statusParam = el.find("#status-filter").val();
226230
if (searchParam) {
227231
params.append("search", searchParam);
228232
}
229-
230233
if (statusParam) {
231234
statusParam.split(",").forEach((status) => {
232235
params.append("status", status);
233236
});
234237
}
235238
const queryString = params.toString();
239+
// Two fetch paths:
240+
// 1. Infinite scroll (append=true): use the pagination URL passed as-is.
241+
// 2. Filter/search (append=false): build URL by appending query params to base URL.
236242
let fetchUrl;
237-
// if append is true, that means we are fetching for infinite scroll
238243
if (append) {
239-
fetchUrl = url; // url is nextUrl, already contains params
244+
fetchUrl = url;
240245
} else {
241246
fetchUrl = queryString ? `${url}?${queryString}` : url;
242247
}
@@ -255,6 +260,7 @@
255260
if (!append) container.scrollTop(0);
256261
},
257262
error(_jqXHR, textStatus) {
263+
// Ignore aborted requests.
258264
if (textStatus === "abort") return;
259265
console.error(gettext("Could not load more devices from"), url);
260266
alert(gettext("Could not load more devices."));

openwisp_monitoring/device/static/monitoring/js/location-inline.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
return;
1414
}
1515

16+
const mapPageUrl = window._owMapPageUrl;
1617
const openLocationBtn = `
1718
<div class="form-row field-location-view-button view-on-map-div">
1819
<div>
1920
<div class="flex-container">
2021
<label for="id_devicelocation-0-map">${gettext("Map:")}</label>
21-
<a href="/admin/device_monitoring/map#${geoMapId}"
22+
<a href="${mapPageUrl}#${geoMapId}"
2223
id="open-location-btn"
2324
class="default-btn view-on-map-btn">
2425
${gettext("View on General Map")}
@@ -42,7 +43,7 @@
4243
<div>
4344
<div class="flex-container">
4445
<label for="id_devicelocation-0-indoor_map">${gettext("Map:")}</label>
45-
<a href="/admin/device_monitoring/map#${geoMapId};${indoorMapId}"
46+
<a href="${mapPageUrl}#${geoMapId};${indoorMapId}"
4647
id="open-indoor-device-btn"
4748
class="default-btn view-on-map-btn">
4849
${gettext("View on General Indoor Map")}

openwisp_monitoring/device/templates/admin/monitoring/device/change_form.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{% extends "admin/config/device/change_form.html" %}
2-
{% load i18n static %}
2+
{% load i18n static admin_urls %}
33
{% block after_field_sets %}
44
{% if not add and device_data %}
55
<div class="inline-group" id="ow-device-status">
@@ -607,3 +607,10 @@ <h2>{% trans 'Charts' %}</h2>
607607
</script>
608608
{% endif %}
609609
{% endblock %}
610+
611+
{% block admin_change_form_document_ready %}
612+
{{ block.super }}
613+
<script>
614+
window._owMapPageUrl = "{% url 'admin:device_monitoring_map_changelist' %}";
615+
</script>
616+
{% endblock %}

0 commit comments

Comments
 (0)