Skip to content

Commit 2e06ccd

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

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

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

Lines changed: 40 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,42 @@ function initHostsInfiniteScroll($, current_subnet, address_add_url, address_cha
5859
});
5960
return div;
6061
}
62+
function validateIp(ip_address, callback) {
63+
if (ip_address === '') {
64+
callback(true);
65+
return;
66+
}
67+
$.ajax({
68+
type: 'GET',
69+
url: '/api/v1/subnet/' + current_subnet + '/hosts/?start=' + ip_address,
70+
success: function (res) {
71+
callback(res.results[0].address === ip_address);
72+
},
73+
error: function (error) {
74+
callback(false);
75+
throw error;
76+
},
77+
});
78+
}
79+
function goTo() {
80+
var input = $("#goto-input").val().toLowerCase().trim();
81+
validateIp(input, function (isValid) {
82+
if (isValid) {
83+
$("#invalid-address").hide();
84+
if (input !== searchQuery) {
85+
searchQuery = input;
86+
nextPageUrl = '/api/v1/subnet/' + current_subnet + '/hosts/?start=' + searchQuery;
87+
$('#subnet-visual').empty();
88+
fetchedPages = [];
89+
lastRenderedPage = 0;
90+
busy = false;
91+
onUpdate();
92+
}
93+
} else {
94+
$("#invalid-address").show();
95+
}
96+
});
97+
}
6198
function appendPage() {
6299
$('.subnet-visual').append(pageContainer(fetchedPages[lastRenderedPage]));
63100
if (lastRenderedPage >= renderedPages) {
@@ -117,6 +154,9 @@ function initHostsInfiniteScroll($, current_subnet, address_add_url, address_cha
117154
}
118155
}
119156
}
157+
$("#goto-button").on("click", function () {
158+
goTo();
159+
});
120160
$('.subnet-visual').scroll(onUpdate);
121161
onUpdate();
122162
}

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

Lines changed: 16 additions & 1 deletion
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+
<form onsubmit="return false">
33+
<input id="goto-input" onkeypress="return event.keyCode != 13;" placeholder="Jump to" />
34+
<input id="goto-button" type="button" value="Go" />
35+
</form>
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", function (event) {
50+
event.preventDefault();
51+
if (event.key == "Enter") {
52+
document.querySelector("#goto-button").click();
53+
}
54+
});
4055
var current_subnet = '{{ original.pk }}';
4156
var IpAddUrl = '{% url ipaddress_add_url %}'
4257
var IpChangeUrl = '{% url ipaddress_change_url "1234" %}'

0 commit comments

Comments
 (0)