Refactoring filtering code

This commit is contained in:
Oliver Walters 2020-04-11 10:14:31 +10:00
parent 613dd9d471
commit 58636139af
3 changed files with 114 additions and 64 deletions

View File

@ -0,0 +1,110 @@
/**
* Code for managing query filters / table options.
*
* Optional query filters are available to the user for various
* tables display in the web interface.
* These filters are saved to the web session, and should be
* persistent for a given table type.
*
* This makes use of the 'inventreeSave' and 'inventreeLoad' functions
* for writing to and reading from session storage.
*
*/
/**
* Return the custom filtering options available for a particular table
*
* @param {*} tableKey - string key lookup for the table
*/
function getFilterOptions(tableKey) {
tableKey = tableKey.toLowerCase();
// Filters for the "Stock" table
if (tableKey == 'stock') {
return {
'cascade': {
'type': 'bool',
'description': 'Include stock in sublocations',
'title': 'sublocations',
},
'active': {
'type': 'bool',
'title': 'part active',
'description': 'Show stock for active parts',
},
'status': {
'options': {
'OK': 10,
'ATTENTION': 50,
'DAMAGED': 55,
'DESTROYED': 60,
'LOST': 70
},
'description': 'Stock status',
}
};
}
// Finally, no matching key
return {};
}
/**
* Load table filters for the given table from session storage
*
* @param tableKey - String key for the particular table
* @param defaults - Default filters for this table e.g. 'cascade=1&location=5'
*/
function loadTableFilters(tableKey, defaults) {
var lookup = "table-filters-" + tableKey.toLowerCase();
var filterstring = inventreeLoad(lookup, defaults);
var filters = {};
console.log(`Loaded filters for table '${tableKey}' - ${filterstring}`);
filterstring.split("&").forEach(function(item, index) {
item = item.trim();
if (item.length > 0) {
var f = item.split('=');
if (f.length == 2) {
filters[f[0]] = f[1];
} else {
console.log(`Improperly formatted filter: ${item}`);
}
}
});
return filters;
}
/**
* Save table filters to session storage
*
* @param {*} tableKey - string key for the given table
* @param {*} filters - object of string:string pairs
*/
function saveTableFilters(tableKey, filters) {
var lookup = "table-filters-" + tableKey.toLowerCase();
var strings = [];
for (var key in filters) {
strings.push(`${key.trim()}=${String(filters[key]).trim()}`);
}
var filterstring = strings.join('&');
console.log(`Saving filters for table '${tableKey}' - ${filterstring}`);
inventreeSave(lookup, filterstring);
}

View File

@ -14,78 +14,17 @@ function getStockLocations(filters={}, options={}) {
return inventreeGet('/api/stock/location/', filters, options)
}
// A map of available filters for the stock table
function getAvailableStockFilters() {
return {
'cascade': {
'type': 'bool',
'description': 'Include stock in sublocations',
'title': 'sublocations',
},
'active': {
'type': 'bool',
'title': 'part active',
'description': 'Show stock for active parts',
},
'status': {
'options': {
'OK': 10,
'ATTENTION': 50,
'DAMAGED': 55,
'DESTROYED': 60,
'LOST': 70
},
'description': 'Stock status',
}
};
}
function loadStockFilters() {
// Load the stock table filters from session-storage
var filterstring = inventreeLoad("stockfilters", "cascade=true");
if (filterstring.length == 0) {
filterstring = 'cascade=true&status=60';
}
var split = filterstring.split("&");
var filters = {};
console.log("Loaded stock filters: " + filterstring);
split.forEach(function(item, index) {
if (item.length > 0) {
var f = item.split('=');
if (f.length == 2) {
filters[f[0]] = f[1];
} else {
console.log("Improperly formatted filter: " + item);
}
}
});
return filters;
return loadTableFilters("stock", "cascade=true");
}
function saveStockFilters(filters) {
// Save the stock table filters to session storage
var strings = [];
for (var key in filters) {
strings.push(key + "=" + filters[key]);
}
var filterstring = strings.join('&');
console.log("Saving stock filters: " + filterstring);
inventreeSave("stockfilters", filterstring);
saveTableFilters("stock", filters);
}
function removeStockFilter(key) {
@ -115,7 +54,7 @@ function createStockFilter() {
var html = `<select id='filter-tag' name='tag'>`;
var available = getAvailableStockFilters();
var available = getFilterOptions("stock");
var filters = loadStockFilters();

View File

@ -102,6 +102,7 @@ InvenTree
<script type='text/javascript' src="{% static 'script/inventree/inventree.js' %}"></script>
<script type='text/javascript' src="{% static 'script/inventree/api.js' %}"></script>
<script type='text/javascript' src="{% static 'script/inventree/bom.js' %}"></script>
<script type='text/javascript' src="{% static 'script/inventree/filters.js' %}"></script>
<script type='text/javascript' src="{% static 'script/inventree/tables.js' %}"></script>
<script type='text/javascript' src="{% static 'script/inventree/modals.js' %}"></script>
<script type='text/javascript' src="{% static 'script/inventree/order.js' %}"></script>