]>
Commit | Line | Data |
---|---|---|
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'); | |
d31924ef | 78 | update_tracker(); |
63b23993 MG |
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'); | |
d31924ef | 86 | var xhr = new XMLHttpRequest(); |
d3892d73 | 87 | xhr.open('GET', '/us/' + user + '?format=json'); |
d31924ef MG |
88 | xhr.onload = function () { |
89 | localStorage.setItem('tracker_data', this.responseText); | |
63b23993 | 90 | localStorage.setItem('tracker_lastfetch', Date.now()); |
d31924ef MG |
91 | update_tracker(); |
92 | }; | |
93 | xhr.send(); | |
94 | } | |
63b23993 | 95 | window.refresh_tracker = refresh_tracker; |
462db4aa | 96 | window.start_tracking = start_tracking; |
d31924ef MG |
97 | |
98 | function stop_tracking(){ | |
c0672e69 | 99 | clean_tracker(); |
d31924ef MG |
100 | localStorage.removeItem('tracker_username'); |
101 | localStorage.removeItem('tracker_data'); | |
63b23993 | 102 | localStorage.removeItem('tracker_lastfetch'); |
d31924ef | 103 | $('#tracker_tracking').addClass('hidden'); |
d31924ef MG |
104 | } |
105 | ||
106 | $( document ).ready(function(){ | |
2d5a49fc | 107 | $('#tracker').detach().prependTo($('#sidebar')); |
d31924ef | 108 | $('#tracker_stop').on('click', stop_tracking); |
f1c090e7 | 109 | $('#track_user').on('click', function() { stop_tracking(); start_tracking($(this).data('user')) }); |
63b23993 | 110 | $('#submitform').on('submit', function() { localStorage.removeItem('tracker_lastfetch') }); |
d31924ef MG |
111 | |
112 | if(localStorage.getItem('tracker_username')) | |
113 | start_tracking(localStorage.getItem('tracker_username')); | |
114 | }); | |
115 | })(); |