From 58636139af0a50dcb5af5b97dcf3aade4a168391 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 11 Apr 2020 10:14:31 +1000 Subject: [PATCH] Refactoring filtering code --- .../static/script/inventree/filters.js | 110 ++++++++++++++++++ .../static/script/inventree/stock.js | 67 +---------- InvenTree/templates/base.html | 1 + 3 files changed, 114 insertions(+), 64 deletions(-) create mode 100644 InvenTree/InvenTree/static/script/inventree/filters.js diff --git a/InvenTree/InvenTree/static/script/inventree/filters.js b/InvenTree/InvenTree/static/script/inventree/filters.js new file mode 100644 index 0000000000..26799a6560 --- /dev/null +++ b/InvenTree/InvenTree/static/script/inventree/filters.js @@ -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); +} \ No newline at end of file diff --git a/InvenTree/InvenTree/static/script/inventree/stock.js b/InvenTree/InvenTree/static/script/inventree/stock.js index c8d3f35cd6..d619ff52ee 100644 --- a/InvenTree/InvenTree/static/script/inventree/stock.js +++ b/InvenTree/InvenTree/static/script/inventree/stock.js @@ -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 = `