-
Notifications
You must be signed in to change notification settings - Fork 6
/
leaderboard.html
493 lines (421 loc) · 17.5 KB
/
leaderboard.html
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Leaderboard</title>
<script type="text/javascript" src="jquery-3.5.1.min.js"></script>
<style type="text/css">
html, body, div, figure, p, h1, h2, ul, li, input, button, textarea {margin:0; padding:0; box-sizing: border-box; font-family: sans-serif;}
body {background-color: #9b8975; }
h1 {padding:30px 0; text-align: center; font-size: 50px; color: #fff8dc;}
.leaderboard {list-style: none; width: 100%; height: 100%;}
.leaderboard .person {background-color: #2d3e50; padding: 0 0 0 70px; width: 100%; height: 70px; margin-bottom: 12px; overflow: hidden; position: relative; user-select: none;}
.leaderboard .person .icon {position: absolute; top: 0; left: 0; font-size: 50px; line-height: 70px; background: #fff8dc;}
.leaderboard .person .nickname {padding: 0 0 0 10px; font-size: 30px; line-height: 70px; color: #fff;}
.leaderboard .person .score {position: absolute; top: 0; right: 0; font-size: 50px; line-height: 70px; background: #dc3546; color: #fff; width: 120px; text-align: center; z-index: 10; cursor: pointer;}
.leaderboard .person.open .point-btns { right: 120px; }
.leaderboard .person .point-btns {position: absolute; list-style: none; top: 0; right: -100%; height: 100%; font-size: 0; transition: right 0.5s ease-out; z-index: 1;}
.leaderboard .person .point-btns .point-btn {display: inline-block; height: 100%; width: 70px; background-color: #ccc; font-size: 30px; text-align: center; line-height: 70px; cursor: pointer;}
.leaderboard .person .point-btns .point-btn--1 {background: #efd68d; color: #a79255;}
.leaderboard .person .point-btns .point-btn--2 {background: #f7ce52; color: #9a802f;}
.leaderboard .person .point-btns .point-btn--5 {background: #fec107; color: #735d1a;}
.leaderboard .person .point-btns .point-btn-1 {background: #5ace72; color: #bef5ca;}
.leaderboard .person .point-btns .point-btn-2 {background: #3dc159; color: #a0efb1;}
.leaderboard .person .point-btns .point-btn-5 {background: #29a744; color: #a0efb1; border-right: 2px solid #1e9237;}
/* leaderboard: details view */
.lb-box { padding: 0 20%; width:100%; }
/* leaderboard: list view */
.lb-box.list { overflow-y: hidden; padding:0; padding-bottom: 90px; }
.lb-box.list .leaderboard .person { width: 600px; margin: 6px 12px;}
.add-person {position: fixed; bottom: 30px; right: 30px; width: 50px; height: 50px; border-radius: 50%; background: #17a2b9; line-height: 50px; text-align: center; font-size: 40px; color: #fff; cursor: pointer; user-select: none;}
.add-person-popup {display: none; position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: 20; background-color: rgb(0 0 0 / 70%);}
body.show-add-person .add-person-popup {display: block;}
body.show-add-person {overflow: hidden;}
.add-person-popup .box {display: inline-block; height: 500px; width: 500px; background: #353a40; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); border: 6px dashed #fff8dc; text-align:center;}
.add-person-popup .box .random-icon {width: 200px; height: 200px; font-size: 100px; line-height: 200px; background: #fff; display: inline-block; border-radius: 50%; margin: 25px 0; border: none; padding: 0; text-align: center; color: #000;}
.add-person-popup .box input {height: 60px; line-height: 50px; font-size: 50px; border: none; background: #9b8975; color: #fff; text-align: center; width: 95%; margin-bottom: 10px;}
.add-person-popup .box input::-webkit-input-placeholder {color: #7d6c59;}
.add-person-popup .box #score {width: 250px;}
.add-person-popup .box button {cursor: pointer; text-align: center; position: absolute; bottom: 0; width: 50%; height: 60px; border: none; font-size: 25px; font-weight: 700; color: #fff;}
.add-person-popup .box button.ok {right: 0; background: #29a744;}
.add-person-popup .box button.cancel {left: 0; background: #1b252f;}
.edit {position: fixed; bottom: 30px; right: 90px; width: 50px; height: 50px; line-height: 50px; text-align: center; font-size: 40px; color: #fff; cursor: pointer; user-select: none;}
.edit-popup {display: none; position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: 20; background-color: rgb(0 0 0 / 70%);}
body.show-edit .edit-popup {display: block;}
body.show-edit {overflow: hidden;}
.edit-popup .box {display: inline-block; height: 500px; width: 500px; background: #353a40; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); border: 6px dashed #fff8dc; text-align:center;}
.edit-popup .box textarea {width: 100%; background: #353a40; box-sizing: border-box; font-size: 20px; color: #fff; border: none; padding: 15px; height: calc(100% - 60px);}
.edit-popup .box button {cursor: pointer; text-align: center; position: absolute; bottom: 0; width: 50%; height: 60px; border: none; font-size: 25px; font-weight: 700; color: #fff;}
.edit-popup .box button.ok {right: 0; background: #29a744;}
.edit-popup .box button.cancel {left: 0; background: #1b252f;}
.link {position: fixed; bottom: 30px; left: 30px; width: 50px; height: 50px; line-height: 50px; text-align: center; font-size: 40px; color: #fff; cursor: pointer; user-select: none;}
.link-popup {display: none; position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: 20; background-color: rgb(0 0 0 / 70%);}
body.show-link .link-popup {display: block;}
body.show-link {overflow: hidden;}
.link-popup .box {display: inline-block; height: 500px; width: 500px; background: #353a40; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); border: 6px dashed #fff8dc; text-align:center;}
.link-popup .box textarea {width: 100%; background: #353a40; box-sizing: border-box; font-size: 20px; color: #fff; border: none; padding: 15px; height: calc(100% - 60px);}
.link-popup .box textarea::selection {background: #ccc;}
.link-popup .box button {cursor: pointer; text-align: center; position: absolute; bottom: 0; width: 50%; height: 60px; border: none; font-size: 25px; font-weight: 700; color: #fff;}
.link-popup .box button.close {left: 0; background: #1b252f; width: 100%;}
.view-style {position: fixed; bottom: 30px; left: 90px; width: 50px; height: 50px; cursor: pointer; padding: 5px; user-select: none;}
.vs {display: block; background-color: #cbbdae; position: absolute; transition: all 0.25s ease;}
.vs {width: 40px; height: 7px;}
.vs-1 {top: 5px; left: 5px;}
.vs-2 {top: 16px; right: 5px;}
.vs-3 {bottom: 16px; left: 5px;}
.vs-4 {bottom: 5px; right: 5px;}
.list .vs {width: 18px; height: 18px;}
.list .vs-1 {top: 5px;}
.list .vs-2 {top: 5px;}
.list .vs-3 {bottom: 5px;}
.list .vs-4 {bottom: 5px;}
</style>
</head>
<body>
<h1 contenteditable="true" class="lb-header">Leaderboard</h1>
<div class="lb-box">
<ul class="leaderboard">
<!-- place for persons / isotope plugin -->
</ul>
</div>
<div class="add-person-popup">
<div class="box">
<input type="text" id="rand-icon" class="random-icon" value="🦊">
<br>
<input type="text" id="nickname" size="15" placeholder="Nickname">
<br>
<input type="number" id="score" placeholder="score">
<br>
<button class="cancel">CANCEL</button>
<button class="ok">OK</button>
</div>
</div>
<div class="edit-popup">
<div class="box">
<textarea id="tarea"></textarea>
<button class="cancel">CANCEL</button>
<button class="ok">OK</button>
</div>
</div>
<div class="link-popup">
<div class="box">
<textarea id="tarea-link"></textarea>
<button class="close">CLOSE</button>
</div>
</div>
<div class="add-person">+</div>
<div class="edit">📋</div>
<div class="link">🔗</div>
<div class="view-style list">
<div class="vs vs-1"></div>
<div class="vs vs-2"></div>
<div class="vs vs-3"></div>
<div class="vs vs-4"></div>
</div>
<script type="text/javascript" src="isotope-v3.0.6.pkgd.min.js"></script>
<script type="text/javascript" src="isotope-fitColumns-v1.1.4.js"></script>
<script type="text/javascript">
var leaderboard_iso = null;
var local_storage_available = false;
var data = [];
var emojis = "🐶 🐱 🐭 🐹 🐰 🦊 🐻 🐼 🐨 🐯 🦁 🐮 🐷 🐽 🐸 🐵 🙈 🙉 🙊 🐒 🐔 🐧 🐦 🐤 🐣 🐥 🦆 🦅 🦉 🦇 🐺 🐗 🐴 🦄 🐝 🐛 🦋 🐌 🐞 🐜 🦟 🦗 🕷 🕸 🦂 🐢 🐍 🦎 🦖 🦕 🐙 🦑 🦐 🦞 🦀 🐡 🐠 🐟 🐬 🐳 🐋 🦈 🐊 🐅 🐆 🦓 🦍 🦧 🐘 🦛 🦏 🐪 🐫 🦒 🦘 🐃 🐂 🐄 🐎 🐖 🐏 🐑 🦙 🐐 🦌 🐕 🐩 🦮 🐕🦺 🐈 🐓 🦃 🦚 🦜 🦢 🦩 🐇 🦝 🦨 🦡 🦦 🦥 🐁 🐀 🦔".split(" ");
var template = `
<li class="person">
<p class="icon">{{icon}}</p>
<p class="nickname">{{nickname}}</p>
<p class="score">{{score}}</p>
<ul class="point-btns">
<li class="point-btn point-btn--5" data-points-value="-5">-5</li>
<li class="point-btn point-btn--2" data-points-value="-2">-2</li>
<li class="point-btn point-btn--1" data-points-value="-1">-1</li>
<li class="point-btn point-btn-1" data-points-value="1">+1</li>
<li class="point-btn point-btn-2" data-points-value="2">+2</li>
<li class="point-btn point-btn-5" data-points-value="5">+5</li>
</ul>
</li>
`;
$( document ).ready(function() {
// check if local storage is available
// updates `local_storage_available` variable
test_local_storage();
// some button handlers...
initialize_basic_btn_listeners();
// intialize isotope plugin
leaderboard_iso = $('.leaderboard').isotope({
// options
itemSelector: '.person',
layoutMode: 'vertical',
getSortData: {
nickname: '.nickname',
score: '.score parseInt'
}
});
// 1st step:
// check for data in "URLSearchParams"
// If the url has data in the query string
// give priority to this data
url = new URL((window.location.href));
query_string = url.searchParams.get("q");
if (query_string != null) {
decoded_str = decodeURIComponent(window.atob(query_string));
data = JSON.parse(decoded_str);
// save data to local storage
// refresh without data in the url
if (local_storage_available) {
localStorage.setItem('leaderboard_data', JSON.stringify(data));
window.location.href = [location.protocol, '//', location.host, location.pathname].join('');
}
return;
}
// 2nd step:
// check for data in the local storage
if (local_storage_available) {
data = JSON.parse(localStorage.getItem('leaderboard_data'));
// add data to the UI
if (data != null) {
build_leaderboard();
} else {
data = [];
}
}
});
function sort_leaderboard(){
leaderboard_iso.isotope( 'updateSortData');
leaderboard_iso.isotope({ sortBy: ["score", "nickname"], sortAscending: {
score: false,
nickname: true,
}
});
}
function initialize_basic_btn_listeners() {
// adding a new person listeners
//
$('.add-person').on('click',function(){
scroll_to_top_smooth();
$("body").toggleClass("show-add-person");
// pick random animal emoji and clear form
$('#rand-icon').val(emojis[getRandomInt(0,emojis.length)]);
$('#nickname').val("");
$('#score').val("");
});
$('.add-person-popup .cancel').on('click',function(){
$("body").toggleClass("show-add-person");
});
$('.add-person-popup .ok').on('click',function(){
var new_person = {
icon: $('#rand-icon').val(),
nickname: $('#nickname').val(),
score: $('#score').val()
};
data.push(new_person);
build_leaderboard();
$("body").toggleClass("show-add-person");
});
// editing
//
$('.edit').on('click',function(){
scroll_to_top_smooth();
$("body").toggleClass("show-edit");
// load data to textarea
data_str = "";
data = data.sort( compare ).reverse();
data.forEach(function(person) {
data_str = data_str + (person.icon + "," + person.nickname + "," + person.score + "\n");
});
$("#tarea").val(data_str);
});
$('.edit-popup .cancel').on('click',function(){
$("body").toggleClass("show-edit");
});
$('.edit-popup .ok').on('click',function(){
read_data_from_text( $('#tarea').val().trim() );
build_leaderboard();
$("body").toggleClass("show-edit");
});
// link sharing
//
$('.link').on('click',function(){
scroll_to_top_smooth();
$("body").toggleClass("show-link");
// load data to textarea
$("#tarea-link").val(generate_share_link());
$('#tarea-link').select();
});
$('.link-popup .close').on('click',function(){
$("body").toggleClass("show-link");
});
// view style
//
$('.view-style').on('click',function() {
scroll_to_top_smooth();
$(".view-style").toggleClass("list"); // change the button state
// check the state from the leaderboard wrapper box
// destroy, recreate, sort the appropriate isotope instance
//
if ( $(".lb-box").hasClass("list") ) {
// has list -> change to details view
$(".lb-box").toggleClass("list");
leaderboard_iso.isotope('destroy');
leaderboard_iso = $('.leaderboard').isotope({
// options
itemSelector: '.person',
layoutMode: 'vertical',
getSortData: {
nickname: '.nickname',
score: '.score parseInt'
}
});
sort_leaderboard();
}
else {
// has details -> change to list view
$(".lb-box").toggleClass("list").height(window.innerHeight - $('.lb-header').outerHeight() -90);
leaderboard_iso.isotope('destroy');
leaderboard_iso = $('.leaderboard').isotope({
// options
itemSelector: '.person',
layoutMode: 'fitColumns',
getSortData: {
nickname: '.nickname',
score: '.score parseInt'
}
});
sort_leaderboard();
}
});
}
function initialize_person_btn_listeners() {
$('.person .score').on('click',function(){
$(this).parent().toggleClass("open");
});
// +/- points buttons
// also updates data[]
$('.point-btn').on('click', async function(event){
event.stopPropagation();
var $li = $(this).parent().parent();
var $score = $li.find('.score');
var current_score = parseInt( $score.text() );
var points_clicked = parseInt($(this).attr('data-points-value'));
var target_score = current_score + points_clicked;
// update UI
if (target_score > current_score) {
for (i=current_score ; i<=target_score ; i++) {
$score.text(i);
await sleep(40);
}
} else {
for (i=current_score ; i>=target_score ; i--) {
$score.text(i);
await sleep(40);
}
}
sort_leaderboard();
// update data[]
data.forEach(function(person) {
if ((person.nickname == $li.find('.nickname').text()) && (person.icon == $li.find('.icon').text())) {
person.score = target_score;
}
})
// save data to local storage
localStorage.setItem('leaderboard_data', JSON.stringify(data));
});
}
function build_leaderboard() {
// remove all current list items
$('.leaderboard').empty();
if (data.length == 0) return;
// append all person-list-items
data.forEach(function(person) {
$('.leaderboard').append(
template.replace(/{{icon}}/gm,person.icon)
.replace(/{{nickname}}/gm,person.nickname)
.replace(/{{score}}/gm,person.score));
})
// reload isotope
leaderboard_iso.isotope('reloadItems');
// save data to local storage
localStorage.setItem('leaderboard_data', JSON.stringify(data));
// add button listeners for score +/-
initialize_person_btn_listeners();
sort_leaderboard();
}
function read_data_from_text(data_str) {
data = [];
if (data_str == "") {return;}
var lines = data_str.split('\n');
lines.forEach(function(line) {
tokens = line.split(',');
data.push({
icon: tokens[0],
nickname: tokens[1],
score: tokens[2]
});
});
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
/**
* Returns a random number between min (inclusive) and max (exclusive)
*/
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
/**
* Returns a random integer between min (inclusive) and max (inclusive).
* The value is no lower than min (or the next integer greater than min
* if min isn't an integer) and no greater than max (or the next integer
* lower than max if max isn't an integer).
* Using Math.round() will give you a non-uniform distribution!
*/
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function generate_share_link() {
// encode to BASE64 the data object
base64_str = btoa(encodeURIComponent(JSON.stringify(data)));
share_url = new URL( [location.protocol, '//', location.host, location.pathname].join('') );
share_url.searchParams.append("q",base64_str)
return share_url.href;
}
function scroll_to_top_smooth() {
window.scrollTo({
top: 0,
// behavior: 'smooth'
});
}
function test_local_storage(){
var test = 'test';
try {
localStorage.setItem(test, test);
localStorage.removeItem(test);
local_storage_available = true;
} catch(e) {
local_storage_available = false;
}
}
function compare(personA, personB) {
scoreA = parseInt(personA.score);
scoreB = parseInt(personB.score);
if (scoreA < scoreB) {
return -1;
}
if (scoreA > scoreB) {
return 1;
}
// but...
// if they have the same score -> alphabetically
if (personA.nickname < personB.nickname) {
return -1;
}
if (personA.nickname > personB.nickname) {
return 1;
}
return 0;
}
</script>
</body>
</html>