$.fn.paginate = function(pagesize, page) { var $this = this; if (pagesize == 'clear') { $this.removeAttr('data-paginate'); $this.find('[data-paginate-visible]').removeAttr('data-paginate-visible'); $this.find('tbody tr').show(); return; } var pagesize = pagesize || 50; var page = page || 1; var rows = $this.find('tbody tr'); if (!$this.attr('data-paginate')) { $this.attr('data-paginate', pagesize); rows.filter(':visible').attr('data-paginate-visible', 1); } else { pagesize = parseInt($this.attr('data-paginate')); } var visible = rows.filter('[data-paginate-visible=1]'); visible.each(function(index, row) { row = $(row); if (index >= (page-1)*pagesize && index < page*pagesize) { row.show(); } else { // rollin', rollin', rollin'... row.hide(); } }); return visible.size(); }; var ranking = { playerClick : function(ev) { if (!$(ev.target).closest('a').size()) { var row = $(this); var pid = row.find('td.pid').text(); if (pid) { var link = 'players/' + pid.trim() + '.html'; location.href = link; } } }, parseHash : function(hash) { hash = hash.replace(/^#/, ''); var hashParams = hash.split(';'); var params = new Map(); hashParams.forEach(function(hashParam) { var param = hashParam.split(':'); if (param.length > 1) { params.set(param[0], param[1].split(',')); } }); return params; }, constructHash : function(params) { var paramArray = []; params.forEach(function(value, key) { paramArray.push(key + ':' + value.join(',')); }); return paramArray.join(';'); }, savedParams : new Map(), readHash : function() { var params = ranking.parseHash(location.hash); var allParams = ['age', 'gender', 'region']; var paramsChanged = false; allParams.forEach(function(param) { var newParam = params.get(param) || []; var oldParam = ranking.savedParams.get(param) || []; if (newParam.length != oldParam.length || newParam.join(',') != oldParam.join(',')) { paramsChanged = true; } }); $('button.btn-primary').removeClass('btn-primary'); params.forEach(function(values, param) { values.forEach(function(value) { $('button[data-filter="' + param + '"][data-value="' + value + '"]').addClass('btn-primary'); }); }); allParams.forEach(function(param) { if ($('button[data-filter="' + param + '"].btn-primary').size() == 0) { $('button[data-clear="' + param + '"]').addClass('btn-primary'); } else { $('#filters').collapse(); } }); if (paramsChanged) { $('table.table-paginate').paginate('clear'); ranking.filterRows(params); ranking.savedParams = params; } $('table.table').css('opacity', 1); var pagesize = 40; var page = params.get('page') || [0]; var count = $('table.table-paginate').paginate(pagesize, parseInt(page[0])); $('table.table tbody tr[data-paginate-visible=1]').eq(0).addClass('gold'); $('table.table tbody tr[data-paginate-visible=1]').eq(1).addClass('silver'); $('table.table tbody tr[data-paginate-visible=1]').eq(2).addClass('bronze'); ranking.buildPaginator('#top-paginator', count, pagesize, page[0] || 1); ranking.buildPaginator('#bottom-paginator', count, pagesize, page[0] || 1); }, filterRows : function(params) { $('table.table tbody tr').show().removeClass('gold silver bronze').each(function() { var row = $(this); var hidden = false; params.forEach(function(value, param) { if (param != 'page') { if (value.indexOf(row.find('td.' + param).text().trim()) == -1) { row.hide(); hidden = true; } } }); if (!hidden) { row.show(); } }); }, buildPaginator: function(selector, count, pagesize, page) { var box = $(selector); box.html(''); var buttonGroup = $('