]> iEval git - gruntmaster-page.git/blob - js/90-tracker.js
1fa4905ed0887ba2f36221591fb3fe59b6b3bdb2
[gruntmaster-page.git] / js / 90-tracker.js
1 (function(){
2 'use strict';
3
4 function tick() { return $('<span class="tick tracker-mark">✔</span>') }
5 function xmark() { return $('<span class="x tracker-mark">✘</span>') }
6
7 function clean_tracker(){
8 $('.tracker-mark').detach();
9 $('.tracker-info').removeClass('tracker-info info');
10 }
11
12 var tracker;
13
14 function update_tracker(){
15 var data = localStorage.getItem('tracker_data');
16 var user = localStorage.getItem('tracker_username');
17 if(!data)
18 return;
19 data = JSON.parse(data);
20 var solved = {};
21 var attempted = {};
22 var solvednr = 0;
23 var attemptednr = 0;
24 data.problems.forEach(function(e){
25 if(e.solved){
26 solved[e.problem] = 1;
27 solvednr++;
28 } else {
29 attempted[e.problem] = 1;
30 attemptednr++;
31 }
32 });
33
34 var contests = {};
35 data.contests.forEach(function(e){
36 contests[e.contest] = 1;
37 });
38
39 clean_tracker();
40 $('#tracker_userlink').html(data.name);
41 $('#tracker_solved').html(solvednr);
42 $('#tracker_attempted').html(attemptednr);
43 $('#tracker_contests').html(data.contests.length);
44 $('#tracker_log').attr('href', '/log/?owner=' + user);
45
46 if(location.pathname == '/pb/')
47 $('table').find('.name').find('a').each(function() {
48 var id = $(this).attr('href').split('?', 2)[0];
49 if(solved[id])
50 $(this).parent().prepend(tick());
51 else if(attempted[id])
52 $(this).parent().prepend(xmark());
53 });
54
55 if(location.pathname == '/ct/')
56 $('table').find('.name').find('a').each(function() {
57 var id = $(this).attr('href').substr(4);
58 if(contests[id])
59 $(this).parent().prepend(tick());
60 });
61
62 if(location.pathname == '/log/')
63 $('table').find('tbody').find('tr').each(function() {
64 if($(this).find('.owner').find('a').attr('href') != '/us/' + user)
65 return;
66 $(this).find('.id').append($(this).find('.r0').size() ? tick() : xmark());
67 });
68
69 if(location.pathname.match(/^\/st\//) || location.pathname == '/us/')
70 $('table').find('tbody').find('tr').each(function() {
71 if($(this).find('.user').find('a').attr('href') == '/us/' + user)
72 $(this).addClass('info tracker-info');
73 });
74 }
75
76 function start_tracking(user){
77 localStorage.setItem('tracker_username', user);
78 $('#tracker_userlink').attr('href', '/us/' + user).html(user);
79 tracker.removeClass('hidden');
80 update_tracker();
81 var lastfetch = localStorage.getItem('tracker_lastfetch');
82 if(Date.now() - lastfetch > 60 * 10 * 1000)
83 refresh_tracker();
84 }
85
86 function refresh_tracker(){
87 var user = localStorage.getItem('tracker_username');
88 var xhr = new XMLHttpRequest();
89 xhr.open('GET', '/us/' + user + '?format=json');
90 xhr.onload = function () {
91 localStorage.setItem('tracker_data', this.responseText);
92 localStorage.setItem('tracker_lastfetch', Date.now());
93 update_tracker();
94 };
95 xhr.send();
96 }
97 window.refresh_tracker = refresh_tracker;
98 window.start_tracking = start_tracking;
99
100 function stop_tracking(){
101 clean_tracker();
102 localStorage.removeItem('tracker_username');
103 localStorage.removeItem('tracker_data');
104 localStorage.removeItem('tracker_lastfetch');
105 tracker.addClass('hidden');
106 }
107
108 $( document ).ready(function(){
109 tracker = $('<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>');
110 tracker.prependTo($('#sidebar'));
111 $('#tracker_stop').on('click', stop_tracking);
112 $('#track_user').on('click', function() { stop_tracking(); start_tracking($(this).data('user')) });
113 $('#submitform').on('submit', function() { localStorage.removeItem('tracker_lastfetch') });
114
115 if(localStorage.getItem('tracker_username'))
116 start_tracking(localStorage.getItem('tracker_username'));
117 });
118 })();
This page took 0.052187 seconds and 3 git commands to generate.