Skip to content

Commit 1cf84c4

Browse files
committed
[admin] Add goto in subnet ui #39
Closes #39
1 parent b034f1e commit 1cf84c4

2 files changed

Lines changed: 56 additions & 2 deletions

File tree

openwisp_ipam/static/openwisp-ipam/js/subnet.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ function initHostsInfiniteScroll($, current_subnet, address_add_url, address_cha
3535
fetchedPages = [],
3636
busy = false,
3737
nextPageUrl = '/api/v1/subnet/' + current_subnet + '/hosts/',
38+
searchQuery = '',
3839
lastRenderedPage = 0; //1 based indexing (0 -> no page rendered)
3940
function addressListItem(addr) {
4041
var id = normalizeIP(addr.address);
@@ -58,6 +59,41 @@ function initHostsInfiniteScroll($, current_subnet, address_add_url, address_cha
5859
});
5960
return div;
6061
}
62+
async function validateIp(ip_address) {
63+
if (ip_address === '') return true;
64+
var isValid = false;
65+
await $.ajax({
66+
type: 'GET',
67+
url: '/api/v1/subnet/' + current_subnet + '/hosts/?start=' + ip_address,
68+
success: function (res) {
69+
isValid = (res.results[0].address == ip_address);
70+
},
71+
error: function (error) {
72+
busy = false;
73+
throw error;
74+
},
75+
});
76+
return isValid;
77+
}
78+
function goTo() {
79+
var input = $("#goto-input").val().toLowerCase().trim();
80+
validateIp(input).then(isValid => {
81+
if (isValid) {
82+
$("#invalid-address").hide();
83+
if (input !== searchQuery) {
84+
searchQuery = input;
85+
nextPageUrl = '/api/v1/subnet/' + current_subnet + '/hosts/?start=' + searchQuery;
86+
$('#subnet-visual').empty();
87+
fetchedPages = [];
88+
lastRenderedPage = 0;
89+
busy = false;
90+
onUpdate();
91+
}
92+
} else {
93+
$("#invalid-address").show();
94+
}
95+
});
96+
}
6197
function appendPage() {
6298
$('.subnet-visual').append(pageContainer(fetchedPages[lastRenderedPage]));
6399
if (lastRenderedPage >= renderedPages) {
@@ -117,6 +153,9 @@ function initHostsInfiniteScroll($, current_subnet, address_add_url, address_cha
117153
}
118154
}
119155
}
156+
$("#goto-button").on("click", function () {
157+
goTo();
158+
});
120159
$('.subnet-visual').scroll(onUpdate);
121160
onUpdate();
122161
}

openwisp_ipam/templates/admin/openwisp-ipam/subnet/change_form.html

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,16 @@ <h2>{% trans 'Network Hierarchical View' %}</h2>
2828
<div id="graph"></div>
2929

3030
<h3>{% trans 'Subnet Visual Display' %}</h3>
31-
<section class="subnet-visual{% if original.subnet.version == 6 %} ipv6{% endif %}">
31+
<div>
32+
<div>
33+
<input id="goto-input" placeholder="Jump to" />
34+
<input id="goto-button" type="button" value="Go" />
35+
</div>
36+
<ul id="invalid-address" class="errorlist" style="display:none;">
37+
<li>Address not valid for subnet</li>
38+
</ul>
39+
</div>
40+
<section id="subnet-visual" class="subnet-visual{% if original.subnet.version == 6 %} ipv6{% endif %}">
3241
</section>
3342
{% endif %}
3443
{% endblock %}
@@ -37,6 +46,12 @@ <h3>{% trans 'Subnet Visual Display' %}</h3>
3746
{{ block.super }}
3847
{% if original and not is_popup %}
3948
<script type="text/javascript">
49+
document.querySelector("#goto-input").addEventListener("keyup", event => {
50+
event.preventDefault();
51+
if (event.key == "Enter") {
52+
document.querySelector("#subnet-go").click();
53+
}
54+
});
4055
var current_subnet = '{{ original.pk }}';
4156
var IpAddUrl = '{% url ipaddress_add_url %}'
4257
var IpChangeUrl = '{% url ipaddress_change_url "1234" %}'
@@ -102,4 +117,4 @@ <h3>{% trans 'Subnet Visual Display' %}</h3>
102117
Plotly.newPlot('graph', data, layout);
103118
</script>
104119
{% endif %}
105-
{% endblock %}
120+
{% endblock %}

0 commit comments

Comments
 (0)