]> iEval git - gruntmaster-page.git/blame - js/90-tracker.js
Cache remote_user per-request
[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(){
65d5cb3b 15 if(!localStorage.getItem('tracker_data'))
7093e9ba 16 return;
65d5cb3b
MG
17 const data = JSON.parse(localStorage.getItem('tracker_data'));
18 const user = localStorage.getItem('tracker_username');
19 const contests = {}, solved = {}, attempted = {};
20 let solvednr = 0, attemptednr = 0;
8345760a
MG
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 33 clean_tracker();
dee46c5b
MG
34 q('#tracker_userlink').innerHTML = data.name;
35 q('#tracker_solved').innerHTML = solvednr;
36 q('#tracker_attempted').innerHTML = attemptednr;
37 q('#tracker_contests').innerHTML = data.contests.length;
38 q('#tracker_log').setAttribute('href', '/log/?owner=' + user);
d31924ef 39
7093e9ba 40 if(location.pathname == '/pb/')
8345760a 41 $('table .name a').each(el => {
65d5cb3b 42 const 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 50 $('table .name a').each(el => {
65d5cb3b 51 const 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 57 $('table tbody tr').each(el => {
65d5cb3b 58 const owner = el.querySelector('.owner a');
7093e9ba
MG
59 if(!owner || owner.getAttribute('href') != '/us/' + user)
60 return;
65d5cb3b 61 const state = el.getElementsByClassName('r0').length ? TICK : XMARK;
7093e9ba
MG
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);
dee46c5b
MG
76 q('#tracker_userlink').setAttribute('href', '/us/' + user);
77 q('#tracker_userlink').innerHTML = user;
7093e9ba
MG
78 tracker.classList.remove('hidden');
79 update_tracker();
65d5cb3b 80 const lastfetch = localStorage.getItem('tracker_lastfetch');
7093e9ba
MG
81 if(Date.now() - lastfetch > 60 * 10 * 1000)
82 refresh_tracker();
83}
d31924ef 84
7093e9ba 85function refresh_tracker(){
65d5cb3b
MG
86 const user = localStorage.getItem('tracker_username');
87 const xhr = new XMLHttpRequest();
7093e9ba 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>');
dee46c5b 107 const sidebar = q('#sidebar');
7093e9ba
MG
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.0545 seconds and 4 git commands to generate.