Skip to content

Commit

Permalink
[admin] Add goto in subnet ui #39
Browse files Browse the repository at this point in the history
Closes #39
  • Loading branch information
purhan committed Nov 6, 2020
1 parent b034f1e commit 4484da1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
40 changes: 40 additions & 0 deletions openwisp_ipam/static/openwisp-ipam/js/subnet.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function initHostsInfiniteScroll($, current_subnet, address_add_url, address_cha
fetchedPages = [],
busy = false,
nextPageUrl = '/api/v1/subnet/' + current_subnet + '/hosts/',
searchQuery = '',
lastRenderedPage = 0; //1 based indexing (0 -> no page rendered)
function addressListItem(addr) {
var id = normalizeIP(addr.address);
Expand All @@ -58,6 +59,42 @@ function initHostsInfiniteScroll($, current_subnet, address_add_url, address_cha
});
return div;
}
function validateIp(ip_address, callback) {
if (ip_address === '') {
callback(true);
return;
}
$.ajax({
type: 'GET',
url: '/api/v1/subnet/' + current_subnet + '/hosts/?start=' + ip_address,
success: function (res) {
callback(res.results[0].address === ip_address);
},
error: function (error) {
callback(false);
throw error;
},
});
}
function goTo() {
var input = $("#goto-input").val().toLowerCase().trim();
validateIp(input, function (isValid) {
if (isValid) {
$("#invalid-address").hide();
if (input !== searchQuery) {
searchQuery = input;
nextPageUrl = '/api/v1/subnet/' + current_subnet + '/hosts/?start=' + searchQuery;
$('#subnet-visual').empty();
fetchedPages = [];
lastRenderedPage = 0;
busy = false;
onUpdate();
}
} else {
$("#invalid-address").show();
}
});
}
function appendPage() {
$('.subnet-visual').append(pageContainer(fetchedPages[lastRenderedPage]));
if (lastRenderedPage >= renderedPages) {
Expand Down Expand Up @@ -117,6 +154,9 @@ function initHostsInfiniteScroll($, current_subnet, address_add_url, address_cha
}
}
}
$("#goto-button").on("click", function () {
goTo();
});
$('.subnet-visual').scroll(onUpdate);
onUpdate();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,16 @@ <h2>{% trans 'Network Hierarchical View' %}</h2>
<div id="graph"></div>

<h3>{% trans 'Subnet Visual Display' %}</h3>
<section class="subnet-visual{% if original.subnet.version == 6 %} ipv6{% endif %}">
<div>
<form onsubmit="return false">
<input id="goto-input" onkeypress="return event.keyCode != 13;" placeholder="Jump to" />
<input id="goto-button" type="button" value="Go" />
</form>
<ul id="invalid-address" class="errorlist" style="display:none;">
<li>Address not valid for subnet</li>
</ul>
</div>
<section id="subnet-visual" class="subnet-visual{% if original.subnet.version == 6 %} ipv6{% endif %}">
</section>
{% endif %}
{% endblock %}
Expand All @@ -37,6 +46,12 @@ <h3>{% trans 'Subnet Visual Display' %}</h3>
{{ block.super }}
{% if original and not is_popup %}
<script type="text/javascript">
document.querySelector("#goto-input").addEventListener("keyup", event => {
event.preventDefault();
if (event.key == "Enter") {
document.querySelector("#goto-button").click();
}
});
var current_subnet = '{{ original.pk }}';
var IpAddUrl = '{% url ipaddress_add_url %}'
var IpChangeUrl = '{% url ipaddress_change_url "1234" %}'
Expand Down Expand Up @@ -102,4 +117,4 @@ <h3>{% trans 'Subnet Visual Display' %}</h3>
Plotly.newPlot('graph', data, layout);
</script>
{% endif %}
{% endblock %}
{% endblock %}

0 comments on commit 4484da1

Please sign in to comment.