2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-29 12:06:44 +00:00

Many more refactoring

This commit is contained in:
Oliver Walters 2020-04-11 14:14:45 +10:00
parent 5d2441776e
commit 64f8034a4c
5 changed files with 142 additions and 85 deletions

View File

@ -97,6 +97,16 @@ function addTableFilter(tableKey, filterKey, filterValue) {
} }
/*
* Clear all the custom filters for a given table
*/
function clearTableFilters(tableKey) {
saveTableFilters(tableKey, {});
return {};
}
/* /*
* Return a list of the "available" filters for a given table key. * 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. * A filter is "available" if it is not already being used to filter the table.
@ -208,6 +218,103 @@ function generateFilterInput(tableKey, filterKey) {
} }
/**
* Configure a filter list for a given table
*
* @param {*} tableKey - string lookup key for filter settings
* @param {*} table - bootstrapTable element to update
* @param {*} target - name of target element on page
*/
function setupFilterList(tableKey, table, target) {
if (!target || target.length == 0) {
target = '#filter-list-" + tableKey';
}
var tag = `filter-tag-${tableKey}`;
var add = `filter-add-${tableKey}`;
var clear = `filter-clear-${tableKey}`;
var make = `filter-make-${tableKey}`;
console.log(`Refilling filter list: ${tableKey}`);
var filters = loadTableFilters(tableKey);
var element = $(target);
element.empty();
element.append(`<button class='btn btn-default' id='${add}'>Add filter</button>`);
element.append(`<button class='btn btn-default' id='${clear}'>Clear filters</button>`);
for (var key in filters) {
var value = getFilterOptionValue(tableKey, key, filters[key]);
var title = getFilterTitle(tableKey, key);
element.append(`<li>${title} = ${value}<span ${tag}='${key}' class='close'>x</span></li>`);
}
// Add a callback for adding a new filter
element.find(`#${add}`).click(function() {
var html = '<div>';
html += generateAvailableFilterList(tableKey);
html += generateFilterInput(tableKey);
html += `<button class='btn btn-default' id='${make}'>Add</button>`;
html += '</div>';
element.append(html);
// Add a callback for when the filter tag selection is changed
element.find(`#filter-tag-${tableKey}`).on('change', function() {
var list = element.find(`#filter-value-${tableKey}`);
list.replaceWith(generateFilterInput(tableKey, this.value));
});
// Add a callback for when the new filter is created
element.find(`#filter-make-${tableKey}`).click(function() {
var tag = element.find(`#filter-tag-${tableKey}`).val();
var val = element.find(`#filter-value-${tableKey}`).val();
var filters = addTableFilter(tableKey, tag, val);
reloadStockTable(table, filters);
// Run this function again
setupFilterList(tableKey, table, target);
});
});
// Add a callback for clearing all the filters
element.find(`#${clear}`).click(function() {
var filters = clearTableFilters(tableKey);
reloadStockTable(table, filters);
setupFilterList(tableKey, table, target);
});
// Add callback for deleting each filter
element.find(".close").click(function(event) {
var me = $(this);
var filter = me.attr(`filter-tag-${tableKey}`);
var filters = removeTableFilter(tableKey, filter);
reloadStockTable(table, filters);
// Run this function again!
setupFilterList(tableKey, table, target);
});
}
/** /**
* Return the pretty title for the given table and filter selection. * Return the pretty title for the given table and filter selection.
* If no title is provided, default to the key value. * If no title is provided, default to the key value.

View File

@ -15,73 +15,6 @@ function getStockLocations(filters={}, options={}) {
} }
function createStockFilter() {
var html = generateAvailableFilterList("stock");
// Add in a (blank) selection for filter value
html += generateFilterInput("stock" );
//html += `<div id='filter-value' name='value'></select>`;
html += `<button class='btn btn-default' id='filter-make'>Add</Button>`;
var div = $("#add-new-filter");
div.html(html);
div.find("#filter-make").click(function() {
var tag = div.find("#filter-tag-stock").val();
var val = div.find("#filter-value-stock").val();
addTableFilter("stock", tag, val);
});
div.find('#filter-tag-stock').on('change', function() {
var list = div.find('#filter-value-stock');
list.replaceWith(generateFilterInput("stock", this.value));
});
}
function clearStockFilters() {
// TODO
console.log("clear stock filters");
}
function updateStockFilterList(filterListElement, table) {
var filters = loadTableFilters("stock");
for (var key in filters) {
var value = getFilterOptionValue("stock", key, filters[key]);
$(filterListElement).append(`<li>${key} = ${value}<span filter-tag-stock='${key}' class='close'>x</span></li>` );
}
// Whenever the callback is called, pass the original parameters through
$(filterListElement).find(".close").click(function(event) {
var element = $(this);
var tag = element.attr('filter-tag-stock');
// Clear out any existing elements
$(filterListElement).empty();
var filters = removeTableFilter("stock", tag);
reloadStockTable(table, filters);
// Call this function again to re-update the filterss
updateStockFilterList(filterListElement, table);
});
}
/* Functions for interacting with stock management forms /* Functions for interacting with stock management forms
*/ */
@ -160,7 +93,7 @@ function loadStockTable(table, options) {
// It will be required if the table is refreshed // It will be required if the table is refreshed
//options.original = original; //options.original = original;
updateStockFilterList(filterListElement, table); setupFilterList("stock", table, filterListElement);
$("#filter-add").click(function() { $("#filter-add").click(function() {
createStockFilter(); createStockFilter();

View File

@ -14,6 +14,9 @@ register = template.Library()
@register.simple_tag(takes_context=True) @register.simple_tag(takes_context=True)
def load_status_codes(context): def load_status_codes(context):
"""
Make the various StatusCodes available to the page context
"""
context['order_status_codes'] = OrderStatus.list() context['order_status_codes'] = OrderStatus.list()
context['stock_status_codes'] = StockStatus.list() context['stock_status_codes'] = StockStatus.list()

View File

@ -18,9 +18,10 @@
</ul> </ul>
</div> </div>
{% endif %} {% endif %}
<div class='filters' id='#filter-list-stock'>
<!-- An empty div in which the filter list will be constructed -->
</div>
<div class='filters'> <div class='filters'>
<button class='btn btn-default' id='filter-add'>Add filter</button>
<button class='btn btn-default' id='filter-clear'>Clear filters</button>
<ul class='filter-list' id='stock-filter-list'> <ul class='filter-list' id='stock-filter-list'>
<!-- This is an empty list which will be populated with the stock table filters --> <!-- This is an empty list which will be populated with the stock table filters -->
</ul> </ul>

View File

@ -1,10 +1,10 @@
{% load i18n %} {% load i18n %}
{% load inventree_extras %} {% load inventree_extras %}
<script type='text/javascript'>
{% load_status_codes %} {% load_status_codes %}
<script type='text/javascript'>
function getAvailableTableFilters(tableKey) { function getAvailableTableFilters(tableKey) {
tableKey = tableKey.toLowerCase(); tableKey = tableKey.toLowerCase();
@ -12,23 +12,23 @@ function getAvailableTableFilters(tableKey) {
// Filters for the "Stock" table // Filters for the "Stock" table
if (tableKey == 'stock') { if (tableKey == 'stock') {
return { return {
'cascade': { cascade: {
'type': 'bool', type: 'bool',
'description': '{% trans "Include stock in sublocations" %}', description: '{% trans "Include stock in sublocations" %}',
'title': '{% trans "Include sublocations" %}', title: '{% trans "Include sublocations" %}',
}, },
'active': { active: {
'type': 'bool', type: 'bool',
'title': '{% trans "Active parts" %}', title: '{% trans "Active parts" %}',
'description': '{% trans "Show stock for active parts" %}', description: '{% trans "Show stock for active parts" %}',
}, },
'status': { 'status': {
'options': { options: {
{% for opt in stock_status_codes %}'{{ opt.value }}': '{{ opt.key }}', {% for opt in stock_status_codes %}'{{ opt.value }}': '{{ opt.key }}',
{% endfor %} {% endfor %}
}, },
'title': '{% trans "Stock status" %}', title: '{% trans "Stock status" %}',
'description': '{% trans "Stock status" %}', description: '{% trans "Stock status" %}',
}, },
}; };
} }
@ -36,6 +36,13 @@ function getAvailableTableFilters(tableKey) {
// Filters for the "Build" table // Filters for the "Build" table
if (tableKey == 'build') { if (tableKey == 'build') {
return { return {
status: {
title: '{% trans "Build status" %}',
options: {
{% for opt in build_status_codes %}'{{ opt.value }}': '{{ opt.key }}',
{% endfor %}
},
},
}; };
} }
@ -43,11 +50,17 @@ function getAvailableTableFilters(tableKey) {
// Filters for the "Order" table // Filters for the "Order" table
if (tableKey == "order") { if (tableKey == "order") {
return { return {
status: {
title: '{% trans "Order status" %}',
options: {
{% for opt in order_status_codes %}'{{ opt.value }}': '{{ opt.key }}',
{% endfor %}
},
},
}; };
} }
// Filters fro the "Parts" table // Filters for the "Parts" table
if (tableKey == "parts") { if (tableKey == "parts") {
return { return {
}; };