From 0c5f23ef98d53dce0aebb8b67d0019b3795c4a4e Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 11 Apr 2020 10:57:02 +1000 Subject: [PATCH] More filter functionality --- .../static/script/inventree/filters.js | 143 +++++++++++++++++- 1 file changed, 141 insertions(+), 2 deletions(-) diff --git a/InvenTree/InvenTree/static/script/inventree/filters.js b/InvenTree/InvenTree/static/script/inventree/filters.js index 26799a6560..d26c7ef0ca 100644 --- a/InvenTree/InvenTree/static/script/inventree/filters.js +++ b/InvenTree/InvenTree/static/script/inventree/filters.js @@ -17,7 +17,7 @@ * * @param {*} tableKey - string key lookup for the table */ -function getFilterOptions(tableKey) { +function getAvailableTableFilters(tableKey) { tableKey = tableKey.toLowerCase(); @@ -53,6 +53,59 @@ function getFilterOptions(tableKey) { } +/* + * Return a list of the "available" filters for a given table key. + * A filter is "available" if it is not already being used to filter the table. + * Once a filter is selected, it will not be returned here. + */ +function getRemainingTableFilters(tableKey) { + + var filters = loadTableFilters(tableKey, ''); + + var remaining = getAvailableTableFilters(tableKey); + + for (var key in filters) { + // Delete the filter if it is already in use + delete remaining[key]; + } + + return remaining; +} + + + +/* + * Return the filter settings for a given table and key combination. + * Return empty object if the combination does not exist. + */ +function getFilterSettings(tableKey, filterKey) { + + return getAvailableTableFilters(tableKey)[filterKey] || {}; +} + + +/* + * Return a set of key:value options for the given filter. + * If no options are specified (e.g. for a number field), + * then a null object is returned. + */ +function getFilterOptionList(tableKey, filterKey) { + + var settings = getFilterSettings(tableKey, filterKey); + + if (settings.type == 'bool') { + return { + 'true': '1', + 'false': '0', + }; + } else if ('options' in settings) { + return settings.options; + } + + return null; +} + + /** * Load table filters for the given table from session storage * @@ -107,4 +160,90 @@ function saveTableFilters(tableKey, filters) { console.log(`Saving filters for table '${tableKey}' - ${filterstring}`); inventreeSave(lookup, filterstring); -} \ No newline at end of file +} + + +/* + * Remove a named filter parameter + */ +function removeTableFilter(tableKey, filterKey) { + + var filters = loadTableFilters(tableKey, ''); + + delete filters[filterKey]; + + saveTableFilters(tableKey, filters); + + // Return a copy of the updated filters + return filters; +} + + +function addTableFilter(tableKey, filterKey, filterValue) { + + var filters = loadTableFilters(tableKey, ''); + + filters[filterKey] = filterValue; + + saveTableFilters(tableKey, filters); + + // Return a copy of the updated filters + return filters; +} + + +/** + * Return the pretty title for the given table and filter selection. + * If no title is provided, default to the key value. + * + */ +function getFilterTitle(tableKey, filterKey) { + var settings = getFilterSettings(tableKey, filterKey); + return settings.title || filterKey; +} + + +/* + * Return a description for the given table and filter selection. + */ +function getFilterDescription(tableKey, filterKey) { + var settings = getFilterSettings(tableKey, filterKey); + return settings.description || filterKey; +} + + +/* + * Return the display value for a particular option + */ +function getFilterOptionValue(tableKey, filterKey, valueKey) { + + var filter = getFilterOption(tableKey, filterKey); + + var value = String(valueKey); + + // Lookup for boolean options + if (filter.type == 'bool') { + if (value == '1') return 'true'; + if (value == '0') return 'false'; + + return value; + } + + // Iterate through a list of options + if ('options' in filter) { + for (var option in filter.options) { + var v = String(filter.options[option]); + + if (v == valueKey) { + return option; + } + } + + // Could not find a match + return value; + } + + // Cannot map to a display string - return the original text + return value; +} +