allow searching for multiple criteria separated by space

This commit is contained in:
Heilig, Eric committed 2018-12-17 14:01:38 -07:00
1 parent abc94a16c1
commit a8fb5197fd
1 file changed
+22 -18
+22 -18
View File
@@ -3,35 +3,39 @@ _lastData = [];
function drawcontents(data) { function drawcontents(data) {
data = data || _lastData; //Cache data so we can drawcontents without waiting for the server, for the search box. data = data || _lastData; //Cache data so we can drawcontents without waiting for the server, for the search box.
_lastData = data; _lastData = data;
const search = new RegExp(document.querySelector("#search").value, 'i'); // allow searching for multiple criteria separated by space
data = data.filter(function(item) { var searchArgs = document.querySelector("#search").value;
searchArgs = searchArgs.trim();
searchArgs = '(' + searchArgs.replace(/ +/g,")|(") + ')';
const search = new RegExp(searchArgs, 'i');
data = data.filter(function(item) {
return search.test(item.name); return search.test(item.name);
}) })
sortByKey(data, "count"); sortByKey(data, "count");
const table = document.querySelector("#contents tbody"); //tables have tbody inserted automatically const table = document.querySelector("#contents tbody"); //tables have tbody inserted automatically
const rows = table.children; const rows = table.children;
//update existing rows or create new ones //update existing rows or create new ones
data.forEach(function(item, i) { data.forEach(function(item, i) {
// format count to have .s in it for large number readability (1,000,000) // format count to have .s in it for large number readability (1,000,000)
item.count = item.count.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); item.count = item.count.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
let row = rows[i]; let row = rows[i];
if(!row) { if(!row) {
row = document.createElement('tr'); row = document.createElement('tr');
row.innerHTML = "<td><img width=32 height=32></td><td class=name></td><td class=count></td>"; row.innerHTML = "<td><img width=32 height=32></td><td class=name></td><td class=count></td>";
table.appendChild(row); table.appendChild(row);
} }
const img = row.querySelector('img'); const img = row.querySelector('img');
const imgName = getImageFromName(item.name); const imgName = getImageFromName(item.name);
if(img.getAttribute('src') !== imgName) { if(img.getAttribute('src') !== imgName) {
img.setAttribute('src',imgName); img.setAttribute('src',imgName);
} }
const name = row.querySelector('.name'); const name = row.querySelector('.name');
if(name.textContent !== item.name) { if(name.textContent !== item.name) {
name.textContent = item.name; name.textContent = item.name;
@@ -39,13 +43,13 @@ function drawcontents(data) {
makeItemGraphs(item.name); makeItemGraphs(item.name);
} }
} }
const count = row.querySelector('.count'); const count = row.querySelector('.count');
if(count.textContent !== ''+item.count) { if(count.textContent !== ''+item.count) {
count.textContent = item.count; count.textContent = item.count;
} }
}); });
//remove excess rows, for example, after filtering //remove excess rows, for example, after filtering
while (data.length < rows.length) { while (data.length < rows.length) {
table.removeChild(rows[data.length]); table.removeChild(rows[data.length]);
@@ -77,13 +81,13 @@ function sortByKey(array, key) {
}); });
} }
document.querySelector("#search").addEventListener('input', function() { document.querySelector("#search").addEventListener('input', function() {
drawcontents(); drawcontents();
}); });
// Make graph of cluster IO items // Make graph of cluster IO items
/*getJSON("/api/getStats", function(err, data){ /*getJSON("/api/getStats", function(err, data){
}*/ }*/
function getItemStatData(item, callback){ function getItemStatData(item, callback){
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
@@ -119,9 +123,9 @@ function makeItemGraphs(itemName){
HTML += '<div id="contentGraph'+instanceID+'" class="storageGraph" style="width: 100%;"></div>'; HTML += '<div id="contentGraph'+instanceID+'" class="storageGraph" style="width: 100%;"></div>';
["place", "remove"].forEach(statistic => { ["place", "remove"].forEach(statistic => {
console.log("Making chart for "+instanceID+"'s "+statistic+" statistic"); console.log("Making chart for "+instanceID+"'s "+statistic+" statistic");
setTimeout(function(){ setTimeout(function(){
let xmlhttp = new XMLHttpRequest(); // new HttpRequest instance let xmlhttp = new XMLHttpRequest(); // new HttpRequest instance
xmlhttp.open("POST", "/api/getStats"); xmlhttp.open("POST", "/api/getStats");
xmlhttp.setRequestHeader("Content-Type", "application/json"); xmlhttp.setRequestHeader("Content-Type", "application/json");
xmlhttp.onreadystatechange = function(){ xmlhttp.onreadystatechange = function(){
@@ -135,7 +139,7 @@ function makeItemGraphs(itemName){
} catch(e) {} } catch(e) {}
let whatGraphIsIt = "Item IO from "; let whatGraphIsIt = "Item IO from ";
// if(statistic == "remove") whatGraphIsIt = "Items taken from cluster by"; // if(statistic == "remove") whatGraphIsIt = "Items taken from cluster by";
let chartData = JSON.parse(xmlhttp.response).data; let chartData = JSON.parse(xmlhttp.response).data;
switch(statistic){ switch(statistic){
case "place": case "place":
@@ -191,7 +195,7 @@ function drawChart(selector, chartData, title) {
e.dataSeries.visible = false; e.dataSeries.visible = false;
} else { } else {
e.dataSeries.visible = true; e.dataSeries.visible = true;
} }
e.chart.render(); e.chart.render();
} }
}, },
@@ -201,4 +205,4 @@ function drawChart(selector, chartData, title) {
// console.log(chart) // console.log(chart)
chart.render(); chart.render();
return chart; return chart;
} }