From 809046c6fbd544907b5d5f3bb554b71724c74661 Mon Sep 17 00:00:00 2001 From: Elliot Smith Date: Fri, 15 Jan 2016 13:00:50 +0200 Subject: bitbake: toastergui: refactor ToasterTable filtering The filter code for ToasterTable was difficult to follow and inflexible (not allowing different types of filter, for example). Refactor to a set of filter classes to make the structure cleaner and provide the flexibility needed for other filter types (e.g. date range filter). [YOCTO #8738] (Bitbake rev: 94031bb30bdaf665d0c8c68b591fcb7a17b6674d) Signed-off-by: Elliot Smith Signed-off-by: Ed Bartosh Signed-off-by: Richard Purdie --- bitbake/lib/toaster/toastergui/static/js/table.js | 80 +++++++++++++++++------ 1 file changed, 59 insertions(+), 21 deletions(-) (limited to 'bitbake/lib/toaster/toastergui/static/js') diff --git a/bitbake/lib/toaster/toastergui/static/js/table.js b/bitbake/lib/toaster/toastergui/static/js/table.js index c69c205d50..fa01ddf47e 100644 --- a/bitbake/lib/toaster/toastergui/static/js/table.js +++ b/bitbake/lib/toaster/toastergui/static/js/table.js @@ -415,38 +415,76 @@ function tableInit(ctx){ data: params, headers: { 'X-CSRFToken' : $.cookie('csrftoken')}, success: function (filterData) { - var filterActionRadios = $('#filter-actions-'+ctx.tableName); + /* + filterData structure: + + { + title: '', + filter_actions: [ + { + title: '<label for radio button inside the popup>', + name: '<name of the filter action>', + count: <number of items this filter will show> + } + ] + } - $('#filter-modal-title-'+ctx.tableName).text(filterData.title); + each filter_action gets a radio button; the value of this is + set to filterName + ':' + filter_action.name; e.g. - filterActionRadios.text(""); + in_current_project:in_project - for (var i in filterData.filter_actions){ - var filterAction = filterData.filter_actions[i]; + specifies the "in_project" action of the "in_current_project" + filter - var action = $('<label class="radio"><input type="radio" name="filter" value=""><span class="filter-title"></span></label>'); - var actionTitle = filterAction.title + ' (' + filterAction.count + ')'; + the filterName is set on the column filter icon, and corresponds + to a value in the table's filters property - var radioInput = action.children("input"); + when the filter popup's "Apply" button is clicked, the + value for the radio button which is checked is passed in the + querystring and applied to the queryset on the table + */ - if (Number(filterAction.count) == 0){ - radioInput.attr("disabled", "disabled"); - } + var filterActionRadios = $('#filter-actions-'+ctx.tableName); - action.children(".filter-title").text(actionTitle); + $('#filter-modal-title-'+ctx.tableName).text(filterData.title); - radioInput.val(filterName + ':' + filterAction.name); + filterActionRadios.text(""); - /* Setup the current selected filter, default to 'all' if - * no current filter selected. - */ - if ((tableParams.filter && - tableParams.filter === radioInput.val()) || - filterAction.name == 'all') { - radioInput.attr("checked", "checked"); + for (var i in filterData.filter_actions) { + var filterAction = filterData.filter_actions[i]; + var action = null; + + if (filterAction.type === 'toggle') { + var actionTitle = filterAction.title + ' (' + filterAction.count + ')'; + + action = $('<label class="radio">' + + '<input type="radio" name="filter" value="">' + + '<span class="filter-title">' + + actionTitle + + '</span>' + + '</label>'); + + var radioInput = action.children("input"); + if (Number(filterAction.count) == 0) { + radioInput.attr("disabled", "disabled"); + } + + radioInput.val(filterData.name + ':' + filterAction.action_name); + + /* Setup the current selected filter, default to 'all' if + * no current filter selected. + */ + if ((tableParams.filter && + tableParams.filter === radioInput.val()) || + filterAction.action_name == 'all') { + radioInput.attr("checked", "checked"); + } } - filterActionRadios.append(action); + if (action) { + filterActionRadios.append(action); + } } $('#filter-modal-'+ctx.tableName).modal('show'); -- cgit v1.2.3-54-g00ecf