]> iEval git - gruntmaster-page.git/blame - js/90-tracker.js
Merge branch 'master' into newmc
[gruntmaster-page.git] / js / 90-tracker.js
CommitLineData
8345760a
MG
1const TICK = '<span class="tick tracker-mark">✔</span>';
2const XMARK = '<span class="x tracker-mark">✘</span>';
d31924ef 3
7093e9ba 4function clean_tracker(){
8345760a
MG
5 $('.tracker-mark').each(e => e.parentNode.removeChild(e));
6 $('.tracker-info').each(e => {
7093e9ba
MG
7 e.classList.remove('tracker-info');
8 e.classList.remove('info');
9 });
10}
d31924ef 11
8345760a 12let tracker;
c0672e69 13
7093e9ba 14function update_tracker(){
8345760a
MG
15 let data = localStorage.getItem('tracker_data');
16 let user = localStorage.getItem('tracker_username');
7093e9ba
MG
17 if(!data)
18 return;
19 data = JSON.parse(data);
8345760a
MG
20 let contests = {}, solved = {}, attempted = {}, solvednr = 0, attemptednr = 0;
21 data.problems.forEach(e => {
22 if(e.solved) {
7093e9ba
MG
23 solved[e.problem] = 1;
24 solvednr++;
25 } else {
26 attempted[e.problem] = 1;
27 attemptednr++;
28 }
29 });
bdaa8919 30
8345760a 31 data.contests.forEach(e => contests[e.contest] = 1);
d31924ef 32
7093e9ba
MG
33 clean_tracker();
34 $('#tracker_userlink')[0].innerHTML = data.name;
35 $('#tracker_solved')[0].innerHTML = solvednr;
36 $('#tracker_attempted')[0].innerHTML = attemptednr;
37 $('#tracker_contests')[0].innerHTML = data.contests.length;
38 $('#tracker_log')[0].setAttribute('href', '/log/?owner=' + user);
d31924ef 39
7093e9ba 40 if(location.pathname == '/pb/')
8345760a
MG
41 $('table .name a').each(el => {
42 let id = el.getAttribute('href').split('?', 2)[0];
7093e9ba
MG
43 if(solved[id])
44 el.insertAdjacentHTML('beforebegin', TICK);
45 else if(attempted[id])
46 el.insertAdjacentHTML('beforebegin', XMARK);
47 });
d31924ef 48
7093e9ba 49 if(location.pathname == '/ct/')
8345760a
MG
50 $('table .name a').each(el => {
51 let id = el.getAttribute('href').substr(4);
7093e9ba
MG
52 if(contests[id])
53 el.insertAdjacentHTML('beforebegin', TICK);
54 });
d31924ef 55
7093e9ba 56 if(location.pathname == '/log/')
8345760a
MG
57 $('table tbody tr').each(el => {
58 let owner = el.querySelector('.owner a');
7093e9ba
MG
59 if(!owner || owner.getAttribute('href') != '/us/' + user)
60 return;
61 var state = el.getElementsByClassName('r0').length ? TICK : XMARK;
62 el.getElementsByClassName('id')[0].innerHTML += state;
63 });
d31924ef 64
7093e9ba 65 if(location.pathname.match(/^\/st\//) || location.pathname == '/us/')
8345760a 66 $('table tbody tr').each(el => {
7093e9ba
MG
67 if(el.querySelector('.user a').getAttribute('href') == '/us/' + user){
68 el.classList.add('info');
69 el.classList.add('tracker-info');
70 }
71 });
72}
c0672e69 73
7093e9ba
MG
74function start_tracking(user){
75 localStorage.setItem('tracker_username', user);
76 $('#tracker_userlink')[0].setAttribute('href', '/us/' + user);
77 $('#tracker_userlink')[0].innerHTML = user;
78 tracker.classList.remove('hidden');
79 update_tracker();
80 var lastfetch = localStorage.getItem('tracker_lastfetch');
81 if(Date.now() - lastfetch > 60 * 10 * 1000)
82 refresh_tracker();
83}
d31924ef 84
7093e9ba
MG
85function refresh_tracker(){
86 var user = localStorage.getItem('tracker_username');
87 var xhr = new XMLHttpRequest();
88 xhr.open('GET', '/us/' + user + '?format=json');
8345760a 89 xhr.onload = () => {
5d3da8f6 90 localStorage.setItem('tracker_data', xhr.responseText);
7093e9ba 91 localStorage.setItem('tracker_lastfetch', Date.now());
d31924ef 92 update_tracker();
7093e9ba
MG
93 };
94 xhr.send();
95}
d31924ef 96
7093e9ba
MG
97function stop_tracking(){
98 clean_tracker();
99 localStorage.removeItem('tracker_username');
100 localStorage.removeItem('tracker_data');
101 localStorage.removeItem('tracker_lastfetch');
102 tracker.classList.add('hidden');
103}
d31924ef 104
7093e9ba 105$(function(){
81c2ef98 106 tracker = m('<div id="tracker" class="hidden">Tracking <a id="tracker_userlink"></a>.<br><a id="tracker_stop" role="button">Stop tracking</a><br><a id="tracker_log">Job log</a><dl class="dl-horizontal"><dt>Solved</dt> <dd id="tracker_solved">?</dd><dt>Attempted</dt> <dd id="tracker_attempted">?</dd><dt>Contests</dt> <dd id="tracker_contests">?</dd></dl></div>');
7093e9ba
MG
107 var sidebar = $('#sidebar')[0];
108 sidebar.insertBefore(tracker, sidebar.firstChild);
109 $('#tracker_stop').on('click', stop_tracking);
110 $('#track_user').on('click', function() { stop_tracking(); start_tracking(this.dataset.user) });
8345760a 111 $('#submitform').on('submit', () => localStorage.removeItem('tracker_lastfetch'));
d31924ef 112
7093e9ba
MG
113 if(localStorage.getItem('tracker_username'))
114 start_tracking(localStorage.getItem('tracker_username'));
115});
This page took 0.050166 seconds and 4 git commands to generate.