$.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], decodeURI(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(), paramChanged: function(params, param) { var newParam = params.get(param) || []; var oldParam = ranking.savedParams.get(param) || []; return (newParam.length != oldParam.length) || (newParam.join(',') != oldParam.join(',')); }, readHash : function() { var params = ranking.parseHash(location.hash); var allParams = ['age', 'gender', 'region', 'name']; var paramsChanged = false; allParams.forEach(function(param) { paramsChanged |= ranking.paramChanged(params, param); }); if (ranking.paramChanged(params, 'pagesize')) { $('table.data-table').removeAttr('data-paginate'); } $('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'); }); }); var filtersPresent = false; allParams.forEach(function(param) { if ($('button[data-filter="' + param + '"]').size() > 0 && $('button[data-filter="' + param + '"].btn-primary').size() == 0) { $('button[data-clear="' + param + '"]').addClass('btn-primary'); } else { 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); ranking.savedParams = params; } $('table.data-table, .filters .panel-body').css('opacity', 1); ranking.filtersDisabled = false; ranking.paginate(params); }, paginate: function(params) { var pagesize = params.get('pagesize') ? parseInt(params.get('pagesize')[0]) : parseInt($('select#pagesize').val()); var page = params.get('page') || [0]; var count = $('table.table-paginate').paginate(pagesize, parseInt(page[0])); $('table.data-table tbody tr[data-paginate-visible=1]').eq(0).addClass('gold'); $('table.data-table tbody tr[data-paginate-visible=1]').eq(1).addClass('silver'); $('table.data-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); $(document).trigger('pagesizeChanged', { 'count': count, 'size': pagesize }); }, filtersDisabled: false, filterRows : function(params) { $('table.data-table tbody tr').show().removeClass('gold silver bronze').each(function() { var row = $(this); var hidden = false; params.forEach(function(value, param) { if (param == 'name') { if (row.find('td.' + param).text().trim().toLowerCase().search(value.join('')) == -1) { row.hide(); hidden = true; } } else if (param.substr(0, 4) != '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 = $('