From fef2adee9f5117b3791eac32f429dd190bfa4038 Mon Sep 17 00:00:00 2001 From: Josh Morel Date: Thu, 3 May 2018 18:49:36 -0400 Subject: [PATCH 1/2] fix toggle any filter for multiselect checkbox groups: if all selected --> others deselected if other selected --> any deselected if none selected --> any selected --- app/assets/v2/js/pages/dashboard.js | 43 ++++++++++++----------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/app/assets/v2/js/pages/dashboard.js b/app/assets/v2/js/pages/dashboard.js index df67f1129a7..3410ced5913 100644 --- a/app/assets/v2/js/pages/dashboard.js +++ b/app/assets/v2/js/pages/dashboard.js @@ -118,28 +118,21 @@ var set_filter_header = function() { $('#filter').html(filter_status); }; -// TODO: Refactor function : -// Deselect option 'any' when another filter is selected -// Selects option 'any' when no filter is applied -// TODO : Disable other filters when 'any' is selected -var disableAny = function() { - for (var i = 0; i < sidebar_keys.length; i++) { - var key = sidebar_keys[i]; - var tag = ($('input[name=' + key + '][value]')); - - tag.map(function(index, input) { - if ($(input).prop('checked')) { - if (input.value === 'any') { - $('input[name=' + key + '][value=any]').prop('checked', true); - } else { - $('input[name=' + key + '][value=any]').prop('checked', false); - } - } - }); - - if ($('input[name=' + key + ']:checked').length === 0) { - $('input[name=' + key + '][value=any]').prop('checked', true); - } +var toggleAny = function(event) { + if (!event) return; + var key = event.target.name; + var anyOption = $('input[name=' + key + '][value=any]'); + // Selects option 'any' when no filter is applied + if ($('input[name=' + key + ']:checked').length === 0) { + anyOption.prop('checked', true); + return; + } + if (event.target.value === 'any') { + // Deselect other filters when 'any' is selected + $('input[name=' + key + '][value!=any]').prop('checked', false); + } else { + // Deselect option 'any' when another filter is selected + anyOption.prop('checked', false); } }; @@ -368,10 +361,10 @@ var trigger_scroll = debounce(function() { $(window).scroll(trigger_scroll); $('body').bind('touchmove', trigger_scroll); -var refreshBounties = function() { +var refreshBounties = function(event) { save_sidebar_latest(); set_filter_header(); - disableAny(); + toggleAny(event); getFilters(); $('.nonefound').css('display', 'none'); @@ -679,7 +672,7 @@ $(document).ready(function() { // sidebar filters $('.sidebar_search input[type=checkbox], .sidebar_search label').change(function(e) { - refreshBounties(); + refreshBounties(e); e.preventDefault(); }); From 8438d4510200f5fbd366b364a05a2a3cccac32a5 Mon Sep 17 00:00:00 2001 From: Josh Morel Date: Thu, 3 May 2018 19:30:27 -0400 Subject: [PATCH 2/2] fix eslint error --- app/assets/v2/js/pages/dashboard.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/assets/v2/js/pages/dashboard.js b/app/assets/v2/js/pages/dashboard.js index 3410ced5913..462248f520b 100644 --- a/app/assets/v2/js/pages/dashboard.js +++ b/app/assets/v2/js/pages/dashboard.js @@ -119,9 +119,11 @@ var set_filter_header = function() { }; var toggleAny = function(event) { - if (!event) return; + if (!event) + return; var key = event.target.name; var anyOption = $('input[name=' + key + '][value=any]'); + // Selects option 'any' when no filter is applied if ($('input[name=' + key + ']:checked').length === 0) { anyOption.prop('checked', true);