Search lots and devices

This commit is contained in:
RubenPX 2022-04-11 11:44:48 +00:00
parent 63d99998a2
commit 756c7bd1c4
2 changed files with 574 additions and 409 deletions

View file

@ -1,4 +1,4 @@
$(document).ready(function() { $(document).ready(function () {
var show_allocate_form = $("#allocateModal").data('show-action-form'); var show_allocate_form = $("#allocateModal").data('show-action-form');
var show_datawipe_form = $("#datawipeModal").data('show-action-form'); var show_datawipe_form = $("#datawipeModal").data('show-action-form');
var show_trade_form = $("#tradeLotModal").data('show-action-form'); var show_trade_form = $("#tradeLotModal").data('show-action-form');
@ -71,9 +71,9 @@ function removeLot() {
function removeTag() { function removeTag() {
var devices = $(".deviceSelect").filter(':checked'); var devices = $(".deviceSelect").filter(':checked');
var devices_id = $.map(devices, function(x) { return $(x).attr('data')}); var devices_id = $.map(devices, function (x) { return $(x).attr('data') });
if (devices_id.length == 1) { if (devices_id.length == 1) {
var url = "/inventory/tag/devices/"+devices_id[0]+"/del/"; var url = "/inventory/tag/devices/" + devices_id[0] + "/del/";
window.location.href = url; window.location.href = url;
} else { } else {
$("#unlinkTagAlertModal").click(); $("#unlinkTagAlertModal").click();
@ -82,7 +82,7 @@ function removeTag() {
function addTag() { function addTag() {
var devices = $(".deviceSelect").filter(':checked'); var devices = $(".deviceSelect").filter(':checked');
var devices_id = $.map(devices, function(x) { return $(x).attr('data')}); var devices_id = $.map(devices, function (x) { return $(x).attr('data') });
if (devices_id.length == 1) { if (devices_id.length == 1) {
$("#addingTagModal .pol").hide(); $("#addingTagModal .pol").hide();
$("#addingTagModal .btn-primary").show(); $("#addingTagModal .btn-primary").show();
@ -146,8 +146,8 @@ function get_device_list() {
$("#actionModal .devices-count").html(devices_count); $("#actionModal .devices-count").html(devices_count);
/* Insert the correct value in the input devicesList */ /* Insert the correct value in the input devicesList */
var devices_id = $.map(devices, function(x) { return $(x).attr('data')}).join(","); var devices_id = $.map(devices, function (x) { return $(x).attr('data') }).join(",");
$.map($(".devicesList"), function(x) { $.map($(".devicesList"), function (x) {
$(x).val(devices_id); $(x).val(devices_id);
}); });
@ -166,17 +166,159 @@ function get_device_list() {
return typ + " " + manuf + " " + dhid; return typ + " " + manuf + " " + dhid;
}); });
description = $.map(list_devices, function(x) { return x }).join(", "); description = $.map(list_devices, function (x) { return x }).join(", ");
$(".enumeration-devices").html(description); $(".enumeration-devices").html(description);
} }
function export_file(type_file) { function export_file(type_file) {
var devices = $(".deviceSelect").filter(':checked'); var devices = $(".deviceSelect").filter(':checked');
var devices_id = $.map(devices, function(x) { return $(x).attr('data-device-dhid')}).join(","); var devices_id = $.map(devices, function (x) { return $(x).attr('data-device-dhid') }).join(",");
if (devices_id){ if (devices_id) {
var url = "/inventory/export/"+type_file+"/?ids="+devices_id; var url = "/inventory/export/" + type_file + "/?ids=" + devices_id;
window.location.href = url; window.location.href = url;
} else { } else {
$("#exportAlertModal").click(); $("#exportAlertModal").click();
} }
} }
window.addEventListener("DOMContentLoaded", () => {
var searchForm = document.getElementById("SearchForm")
var inputSearch = document.querySelector("#SearchForm > input")
var doSearch = true
const Api = {
/**
* get lots id
* @returns get lots
*/
async get_lots() {
var request = await this.doRequest(API_URLS.lots, "GET", null)
if (request != undefined) return request.items
throw request
},
/**
* Get filtered devices info
* @param {number[]} ids devices ids
* @returns full detailed device list
*/
async get_devices(id) {
var request = await this.doRequest(API_URLS.devices + '?filter={"devicehub_id": ["' + id + '"]}', "GET", null)
if (request != undefined) return request.items
throw request
},
/**
*
* @param {string} url URL to be requested
* @param {String} type Action type
* @param {String | Object} body body content
* @returns {Object[]}
*/
async doRequest(url, type, body) {
var result;
try {
result = await $.ajax({
url: url,
type: type,
headers: {
"Authorization": API_URLS.Auth_Token
},
body: body
});
return result
} catch (error) {
console.error(error)
throw error
}
}
}
searchForm.addEventListener("submit", (event) => {
event.preventDefault();
})
var timeoutHandler = setTimeout(() => { }, 1000)
var dropdownList = document.getElementById("dropdown-search-list")
inputSearch.addEventListener("input", (e) => {
console.log("Timeout restart")
clearTimeout(timeoutHandler)
timeoutHandler = setTimeout(async () => {
console.log("timeout start")
dropdownList.innerHTML = `
<li id="deviceSearchLoader" class="dropdown-item">
<i class="bi bi-laptop"></i>
<div class="spinner-border spinner-border-sm" role="status">
<span class="visually-hidden">Loading...</span>
</div>
</li>
<li id="lotSearchLoader" class="dropdown-item">
<i class="bi bi-folder2"></i>
<div class="spinner-border spinner-border-sm" role="status">
<span class="visually-hidden">Loading...</span>
</div>
</li>
`
var searchText = e.target.value
try {
Api.get_devices(searchText.toUpperCase()).then(devices => {
dropdownList.querySelector("#deviceSearchLoader").style = "display: none"
for (let i = 0; i < devices.length; i++) {
const device = devices[i];
// See: ereuse_devicehub/resources/device/models.py
var verboseName = `${device.type} ${device.manufacturer} ${device.model}`
const templateString = `
<li>
<a class="dropdown-item" href="${API_URLS.devices_detail.replace("ReplaceTEXT", device.devicehubID)}" style="display: flex; align-items: center;" href="#">
<i class="bi bi-laptop"></i>
<span style="margin-right: 10px">${verboseName}</span>
<span class="badge bg-secondary" style="margin-left: auto;">${device.devicehubID}</span>
</a>
</li>`
dropdownList.innerHTML += templateString
if (i == 4) { // Limit to 4 resullts
break;
}
}
})
} catch (error) {
console.log(error)
}
try {
Api.get_lots().then(lots => {
dropdownList.querySelector("#lotSearchLoader").style = "display: none"
for (let i = 0; i < lots.length; i++) {
const lot = lots[i];
if (lot.name.toUpperCase().includes(searchText.toUpperCase())) {
const templateString = `
<li>
<a class="dropdown-item" href="${API_URLS.lots_detail.replace("ReplaceTEXT", lot.id)}" style="display: flex; align-items: center;" href="#">
<i class="bi bi-folder2"></i>
<span style="margin-right: 10px">${lot.name}</span>
</a>
</li>`
dropdownList.innerHTML += templateString
if (i == 4) {
break;
}
}
}
})
} catch (error) {
console.log(error)
}
}, 1000)
})
})

View file

@ -12,9 +12,20 @@
</div><!-- End Logo --> </div><!-- End Logo -->
<div class="search-bar"> <div class="search-bar">
<form class="search-form d-flex align-items-center" method="POST" action="#"> <form class="search-form d-flex align-items-center" method="POST" id="SearchForm" action="#">
<input type="text" name="query" placeholder="Search" title="Enter search keyword"> <input class="dropdown-toggle" type="text" name="query" placeholder="Search" title="Enter search keyword" autocomplete="off" id="dropdownSearch" data-bs-toggle="dropdown" aria-expanded="false">
<button type="submit" title="Search"><i class="bi bi-search"></i></button> <button type="submit" title="Search"><i class="bi bi-search"></i></button>
<ul class="dropdown-menu" autoClose="outside" aria-labelledby="dropdownSearch" id="dropdown-search-list" style="min-width: 100px;">
<li><a class="dropdown-item" style="display: flex; align-items: center;" href="#"><i class="bi bi-laptop"></i><span style="margin-right: 10px">This is a extra large device name to test long titles</span> <span class="badge bg-secondary" style="margin-left: auto;">{DHID ID}</span></a></li>
<li><a class="dropdown-item" style="display: flex; align-items: center;" href="#"><i class="bi bi-laptop"></i> <span>Device Name 2</span> <span class="badge bg-secondary" style="margin-left: auto;">{DHID ID}</span></a></li>
<li><a class="dropdown-item" style="display: flex; align-items: center;" href="#"><i class="bi bi-laptop"></i> <span>Device Name 3</span> <span class="badge bg-secondary" style="margin-left: auto;">{DHID ID}</span></a></li>
<li><a class="dropdown-item" style="display: flex; align-items: center;" href="#"><i class="bi bi-laptop"></i> <span>Device Name 4</span> <span class="badge bg-secondary" style="margin-left: auto;">{DHID ID}</span></a></li>
<li><a class="dropdown-item" style="display: flex; align-items: center;" href="#"><i class="bi bi-folder2"></i> lot 1</a></li>
<li><a class="dropdown-item" style="display: flex; align-items: center;" href="#"><i class="bi bi-folder2"></i> lot 2</a></li>
<li><a class="dropdown-item" style="display: flex; align-items: center;" href="#"><i class="bi bi-folder2"></i> lot 3</a></li>
<li><a class="dropdown-item" style="display: flex; align-items: center;" href="#"><i class="bi bi-folder2"></i> lot 4</a></li>
</ul>
</form> </form>
</div><!-- End Search Bar --> </div><!-- End Search Bar -->
@ -224,4 +235,16 @@
</div> </div>
</footer><!-- End Footer --> </footer><!-- End Footer -->
<!-- API_CALLS -->
<script>
const API_URLS = {
Auth_Token: `Basic ${btoa("{{ current_user.token }}:")}`, //
currentUserID: "{{ current_user.id }}",
lots: "{{ url_for('Lot.main') }}",
lots_detail: "{{ url_for('inventory.lotdevicelist', lot_id='ReplaceTEXT') }}",
devices: "{{ url_for('Device.main') }}",
devices_detail: "{{ url_for('inventory.device_details', id='ReplaceTEXT')}}"
}
</script>
{% endblock body %} {% endblock body %}