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