]> iEval git - gruntmaster-page.git/blob - js/90-tracker.js
Update 10-bootstrap-modal.js
[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 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
37 clean_tracker();
38 $('#tracker_userlink').html(data.name);
39 $('#tracker_solved').html(solvednr);
40 $('#tracker_attempted').html(attemptednr);
41 $('#tracker_contests').html(data.contests.length);
42 $('#tracker_log').attr('href', '/log/?owner=' + user);
43
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])
48 $(this).parent().prepend(tick());
49 else if(attempted[id])
50 $(this).parent().prepend(xmark());
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])
57 $(this).parent().prepend(tick());
58 });
59
60 if(location.pathname == '/log/')
61 $('table').find('tbody').find('tr').each(function() {
62 if($(this).find('.owner').find('a').attr('href') != '/us/' + user)
63 return;
64 $(this).find('.id').append($(this).find('.r0').size() ? tick() : xmark());
65 });
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 });
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 update_tracker();
79 var lastfetch = localStorage.getItem('tracker_lastfetch');
80 if(Date.now() - lastfetch > 60 * 10 * 1000)
81 refresh_tracker();
82 }
83
84 function refresh_tracker(){
85 var user = localStorage.getItem('tracker_username');
86 var xhr = new XMLHttpRequest();
87 xhr.open('GET', '/us/' + user + '?format=json');
88 xhr.onload = function () {
89 localStorage.setItem('tracker_data', this.responseText);
90 localStorage.setItem('tracker_lastfetch', Date.now());
91 update_tracker();
92 };
93 xhr.send();
94 }
95 window.refresh_tracker = refresh_tracker;
96 window.start_tracking = start_tracking;
97
98 function stop_tracking(){
99 clean_tracker();
100 localStorage.removeItem('tracker_username');
101 localStorage.removeItem('tracker_data');
102 localStorage.removeItem('tracker_lastfetch');
103 $('#tracker_tracking').addClass('hidden');
104 }
105
106 $( document ).ready(function(){
107 $('#tracker').detach().prependTo($('#sidebar'));
108 $('#tracker_stop').on('click', stop_tracking);
109 $('#track_user').on('click', function() { stop_tracking(); start_tracking($(this).data('user')) });
110 $('#submitform').on('submit', function() { localStorage.removeItem('tracker_lastfetch') });
111
112 if(localStorage.getItem('tracker_username'))
113 start_tracking(localStorage.getItem('tracker_username'));
114 });
115 })();
This page took 0.062432 seconds and 4 git commands to generate.