mirror of
https://github.com/inventree/InvenTree.git
synced 2025-05-05 06:48:48 +00:00
Refactoring filtering code
This commit is contained in:
parent
613dd9d471
commit
58636139af
110
InvenTree/InvenTree/static/script/inventree/filters.js
Normal file
110
InvenTree/InvenTree/static/script/inventree/filters.js
Normal 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);
|
||||||
|
}
|
@ -14,78 +14,17 @@ function getStockLocations(filters={}, options={}) {
|
|||||||
return inventreeGet('/api/stock/location/', 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() {
|
function loadStockFilters() {
|
||||||
// Load the stock table filters from session-storage
|
|
||||||
var filterstring = inventreeLoad("stockfilters", "cascade=true");
|
|
||||||
|
|
||||||
if (filterstring.length == 0) {
|
return loadTableFilters("stock", "cascade=true");
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function saveStockFilters(filters) {
|
function saveStockFilters(filters) {
|
||||||
// Save the stock table filters to session storage
|
// Save the stock table filters to session storage
|
||||||
|
|
||||||
var strings = [];
|
saveTableFilters("stock", filters);
|
||||||
|
|
||||||
for (var key in filters) {
|
|
||||||
strings.push(key + "=" + filters[key]);
|
|
||||||
}
|
|
||||||
|
|
||||||
var filterstring = strings.join('&');
|
|
||||||
|
|
||||||
console.log("Saving stock filters: " + filterstring);
|
|
||||||
|
|
||||||
inventreeSave("stockfilters", filterstring);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function removeStockFilter(key) {
|
function removeStockFilter(key) {
|
||||||
@ -115,7 +54,7 @@ function createStockFilter() {
|
|||||||
|
|
||||||
var html = `<select id='filter-tag' name='tag'>`;
|
var html = `<select id='filter-tag' name='tag'>`;
|
||||||
|
|
||||||
var available = getAvailableStockFilters();
|
var available = getFilterOptions("stock");
|
||||||
|
|
||||||
var filters = loadStockFilters();
|
var filters = loadStockFilters();
|
||||||
|
|
||||||
|
@ -102,6 +102,7 @@ InvenTree
|
|||||||
<script type='text/javascript' src="{% static 'script/inventree/inventree.js' %}"></script>
|
<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/api.js' %}"></script>
|
||||||
<script type='text/javascript' src="{% static 'script/inventree/bom.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/tables.js' %}"></script>
|
||||||
<script type='text/javascript' src="{% static 'script/inventree/modals.js' %}"></script>
|
<script type='text/javascript' src="{% static 'script/inventree/modals.js' %}"></script>
|
||||||
<script type='text/javascript' src="{% static 'script/inventree/order.js' %}"></script>
|
<script type='text/javascript' src="{% static 'script/inventree/order.js' %}"></script>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user