var ranking = { rankingData : {}, tableRowTemplate: undefined, loadData : function(url, callback) { $.getJSON(url, function(data) { ranking.rankingData = data; callback(); }); }, 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(force) { var params = ranking.parseHash(location.hash); var allParams = ['age', 'gender', 'region', 'name', 'page', 'pagesize']; var paramsChanged = false; if (force) { paramsChanged = true; } else { allParams.forEach(function(param) { paramsChanged |= ranking.paramChanged(params, param); }); } $('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) { ranking.filterRows(params); ranking.savedParams = params; } $('table.data-table, .filters .panel-body').css('opacity', 1); ranking.filtersDisabled = false; }, filtersDisabled: false, filterRows : function(params) { $('table.data-table tbody tr').remove(); var displayRows = []; ranking.rankingData.forEach(function(row) { var hidden = false; params.forEach(function(value, param) { if (param == 'name') { if (row['player'].trim().toLowerCase().search(value.join('')) == -1) { hidden = true; } } else if (param.substr(0, 4) != 'page') { if (value.indexOf(row[param].trim()) == -1) { hidden = true; } } }); if (!hidden) { displayRows.push(row); } }); var allCount = displayRows.length; var page = params.get('page') || [0]; page = parseInt(page[0]) || 1; var pagesize = params.get('pagesize') || [0]; pagesize = parseInt(pagesize[0]) || parseInt($('select#pagesize').val()); displayRows = displayRows.slice((page-1) * pagesize, page * pagesize); displayRows.forEach(function(row) { $('table.data-table tbody').append(ranking.buildTableRow(row)); }); ranking.buildPaginator('#top-paginator', allCount, pagesize, page); ranking.buildPaginator('#bottom-paginator', allCount, pagesize, page); $(document).trigger('pagesizeInfo', { 'count': allCount, 'size': pagesize }); if (page == 1) { $('table.data-table tbody tr').eq(0).addClass('gold'); $('table.data-table tbody tr').eq(1).addClass('silver'); $('table.data-table tbody tr').eq(2).addClass('bronze'); } }, buildTableRow: function(row) { var tableRow = ranking.tableRowTemplate.clone(true); tableRow.find('td.pid').text(row['pid']); tableRow.find('td.pidlink a').attr( 'href', 'https://msc.com.pl/cezar/?p=21&pid=' + row['pid'] ); tableRow.find('td.name').text(row['player']); tableRow.find('td.club').text(row['club']); ['gender', 'age', 'region'].forEach(function(category) { tableRow.find('td.' + category).text(row[category]); var categoryPlace = tableRow.find('td.' + category + '-place'); categoryPlace.find('.rank').text(row[category + '-place']); var badge = categoryPlace.find('.change'); badge.text(row[category + '-change']); badge.addClass('label-' + row[category + '-change-class']); if (row[category + '-place'] == 1) { tableRow.addClass('info'); } }); var scoreCell = tableRow.find('td.ranking span'); scoreCell.attr('title', row['score']); scoreCell.text(row['score'].toFixed(2)); tableRow.find('td.place .rank').text(row['place']); var badge = tableRow.find('td.place .change'); badge.text(row['place-change']); badge.addClass('label-' + row['place-change-class']); return tableRow; }, buildPaginator: function(selector, count, pagesize, page) { var box = $(selector); box.html(''); var buttonGroup = $('
').addClass('btn-group'); var pages = parseInt(Math.ceil(count / pagesize)); buttonGroup.attr('data-pages', pages); buttonGroup.append($('