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