diff options
author | MichaĆ <emkael@tlen.pl> | 2017-06-20 17:53:52 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-20 17:53:52 +0200 |
commit | 210e4aa3a62eae078d31a7ad5ed2f53089b4e01d (patch) | |
tree | fbae45d98c224e79a72b141b6cc657810daf714c /http | |
parent | dd86138412dc4eb6c7d1ce61f3a3308b5ce85043 (diff) | |
parent | a0d4e684dd0254f26c70c3e59f73c3685680e757 (diff) |
Merge pull request #9 from emkael/name-filter
Name filter
Diffstat (limited to 'http')
-rw-r--r-- | http/res/ranking.css | 1 | ||||
-rw-r--r-- | http/res/ranking.js | 72 |
2 files changed, 59 insertions, 14 deletions
diff --git a/http/res/ranking.css b/http/res/ranking.css index b05db9c..76b67ae 100644 --- a/http/res/ranking.css +++ b/http/res/ranking.css @@ -1,3 +1,4 @@ +#filters div[role="group"] { margin: 10px; } .table-hover > tbody > tr.gold > td { background-color: #FFEF96 } .table-hover > tbody > tr.silver > td { background-color: #E5E5E5 } .table-hover > tbody > tr.bronze > td { background-color: #E1CAB4 } diff --git a/http/res/ranking.js b/http/res/ranking.js index 1cf09ab..61cb856 100644 --- a/http/res/ranking.js +++ b/http/res/ranking.js @@ -71,7 +71,7 @@ var ranking = { readHash : function() { var params = ranking.parseHash(location.hash); - var allParams = ['age', 'gender', 'region']; + var allParams = ['age', 'gender', 'region', 'name']; var paramsChanged = false; allParams.forEach(function(param) { var newParam = params.get(param) || []; @@ -86,13 +86,25 @@ var ranking = { $('button[data-filter="' + param + '"][data-value="' + value + '"]').addClass('btn-primary'); }); }); + var filtersPresent = false; allParams.forEach(function(param) { - if ($('button[data-filter="' + param + '"].btn-primary').size() == 0) { + if ($('button[data-filter="' + param + '"]').size() > 0 + && $('button[data-filter="' + param + '"].btn-primary').size() == 0) { $('button[data-clear="' + param + '"]').addClass('btn-primary'); } else { - $('#filters').collapse(); + filtersPresent |= ($('button[data-filter="' + param + '"]').size() > 0); + } + var field = $('input[data-filter-field="' + param + '"]'); + if (field.size() > 0) { + field.val(params.get(param)); + if (field.val().length > 0) { + filtersPresent = true; + } } }); + if (filtersPresent) { + $('#filters').collapse(); + } if (paramsChanged) { $('table.table-paginate').paginate('clear'); ranking.filterRows(params); @@ -117,7 +129,12 @@ var ranking = { var row = $(this); var hidden = false; params.forEach(function(value, param) { - if (param != 'page') { + if (param == 'name') { + if (row.find('td.' + param).text().trim().toLowerCase().search(value.join('')) == -1) { + row.hide(); + hidden = true; + } + } else if (param != 'page') { if (value.indexOf(row.find('td.' + param).text().trim()) == -1) { row.hide(); hidden = true; @@ -178,11 +195,40 @@ var ranking = { $(window).on('hashchange', ranking.readHash).trigger('hashchange'); }); - $('button[data-filter]').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(); + 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) { + 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'); @@ -201,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); |