1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
$.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 = $('<div>').addClass('btn-group');
var pages = parseInt(Math.ceil(count / pagesize));
buttonGroup.attr('data-pages', pages);
buttonGroup.append($('<button>').addClass('btn btn-default paginator-prev').attr('type', 'button').text('<<'));
for (var i = 0; i < pages; i++) {
var pageBtn = $('<button>').addClass('btn btn-default paginator-page').attr('type', 'button').attr('data-page', i+1).text(i+1);
if (i+1 == page) {
pageBtn.addClass('btn-primary');
}
buttonGroup.append(pageBtn);
}
buttonGroup.append($('<button>').addClass('btn btn-default paginator-next').attr('type', 'button').text('>>'));
box.append(buttonGroup);
},
changePage: function() {
$('table.table').css('opacity', 0.1);
var btn = $(this);
var page = parseInt(btn.attr('data-page'));
var params = ranking.parseHash(location.hash);
var currentPage = params.get('page');
currentPage = currentPage ? parseInt(currentPage[0]) : 1;
if (btn.hasClass('paginator-prev')) {
page = currentPage - 1;
}
if (btn.hasClass('paginator-next')) {
page = currentPage + 1;
}
var count = parseInt(btn.closest('[data-pages]').attr('data-pages'));
if (page > 0 && page <= count) {
params.set('page', [page]);
location.hash = ranking.constructHash(params);
} else {
$('table.table').css('opacity', 1);
}
},
init : function() {
$('.container .table tbody tr').click(ranking.playerClick);
$(document).ready(function() {
$(window).on('hashchange', ranking.readHash).trigger('hashchange');
});
$('button[data-filter]').click(function() {
$('table.table').css('opacity', 0.1);
var button = $(this);
var params = ranking.parseHash(location.hash);
var param = params.get(button.attr('data-filter'));
var value = button.attr('data-value');
var index = param ? param.indexOf(value) : -1;
if (index > -1) {
param = param.filter(function(v) { return v != value; });
} else {
if (!param) {
param = [];
}
param.push(value);
}
if (!param.length) {
params.delete(button.attr('data-filter'));
} else {
params.set(button.attr('data-filter'), param);
}
params.delete('page');
location.hash = ranking.constructHash(params);
});
$('button[data-clear]').click(function() {
$('table.table').css('opacity', 0.1);
var button = $(this);
var params = ranking.parseHash(location.hash);
params.delete(button.attr('data-clear'));
params.delete('page');
location.hash = ranking.constructHash(params);
});
$(document).on('click', 'button.paginator-prev, button.paginator-next, button.paginator-page', ranking.changePage);
}
};
ranking.init();
|