diff options
author | emkael <emkael@tlen.pl> | 2017-06-20 16:20:24 +0200 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2017-06-20 16:20:24 +0200 |
commit | e4ce29ed1dc11a459cf1d165cd258d95f45e17e6 (patch) | |
tree | 14f766be1af75c5062f8d0acce9296ce0abc4690 /http/res | |
parent | 5ec21d8a5794599672a26e5abf7912b0e7f2a3c5 (diff) |
Inified way to handle parameter changes
Diffstat (limited to 'http/res')
-rw-r--r-- | http/res/ranking.js | 49 |
1 files changed, 31 insertions, 18 deletions
diff --git a/http/res/ranking.js b/http/res/ranking.js index fca05e2..61cb856 100644 --- a/http/res/ranking.js +++ b/http/res/ranking.js @@ -195,25 +195,40 @@ var ranking = { $(window).on('hashchange', ranking.readHash).trigger('hashchange'); }); - $('button[data-filter-action]').click(function(ev) { + + var handleParams = function(callback, target, ev) { if (!ranking.filtersDisabled) { ranking.filtersDisabled = true; $('table.data-table, .filters .panel-body').css('opacity', 0.1); - var button = $(this); + var oldHash = location.hash.replace(/^#/, ''); + var newParams = callback(target, ev); + var newHash = ranking.constructHash(newParams); + if (oldHash != newHash) { + location.hash = newHash; + } else { + $('table.data-table, .filters .panel-body').css('opacity', 1); + ranking.filtersDisabled = false; + } + } + }; + + $('button[data-filter-action]').click(function(ev) { + handleParams(function(target, ev) { + var button = target; var params = ranking.parseHash(location.hash); var param = params.get(button.attr('data-filter-action')); var value = $('input[data-filter-field="' + button.attr('data-filter-action') + '"]').val().trim().toLowerCase(); - params.set(button.attr('data-filter-action'), [value]); - params.delete('page'); - location.hash = ranking.constructHash(params); - } + if (value.length > 0) { + params.set(button.attr('data-filter-action'), [value]); + params.delete('page'); + } + return params; + }, $(this), ev); }); $('button[data-filter]').click(function(ev) { - if (!ranking.filtersDisabled) { - ranking.filtersDisabled = true; - $('table.data-table, .filters .panel-body').css('opacity', 0.1); - var button = $(this); + handleParams(function(target, ev) { + var button = target; var params = ranking.parseHash(location.hash); var param = params.get(button.attr('data-filter')); var value = button.attr('data-value'); @@ -232,20 +247,18 @@ var ranking = { params.set(button.attr('data-filter'), param); } params.delete('page'); - location.hash = ranking.constructHash(params); - } + return params; + }, $(this), ev); }); $('button[data-clear]').click(function() { - if (!ranking.filtersDisabled) { - ranking.filtersDisabled = true; - $('table.data-table, .filters .panel-body').css('opacity', 0.1); - var button = $(this); + handleParams(function(target, ev) { + var button = target; var params = ranking.parseHash(location.hash); params.delete(button.attr('data-clear')); params.delete('page'); - location.hash = ranking.constructHash(params); - } + return params; + }, $(this)); }); $(document).on('click', 'button.paginator-prev, button.paginator-next, button.paginator-page', ranking.changePage); |