]>
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 | ||
bdaa8919 MG |
12 | var tracker; |
13 | ||
d31924ef MG |
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 | ||
c0672e69 | 39 | clean_tracker(); |
d31924ef MG |
40 | $('#tracker_userlink').html(data.name); |
41 | $('#tracker_solved').html(solvednr); | |
42 | $('#tracker_attempted').html(attemptednr); | |
43 | $('#tracker_contests').html(data.contests.length); | |
94d544ad | 44 | $('#tracker_log').attr('href', '/log/?owner=' + user); |
d31924ef | 45 | |
d31924ef MG |
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]) | |
21acb1d6 | 50 | $(this).parent().prepend(tick()); |
d31924ef | 51 | else if(attempted[id]) |
21acb1d6 | 52 | $(this).parent().prepend(xmark()); |
d31924ef MG |
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]) | |
21acb1d6 | 59 | $(this).parent().prepend(tick()); |
d31924ef MG |
60 | }); |
61 | ||
62 | if(location.pathname == '/log/') | |
63 | $('table').find('tbody').find('tr').each(function() { | |
c0672e69 | 64 | if($(this).find('.owner').find('a').attr('href') != '/us/' + user) |
d31924ef MG |
65 | return; |
66 | $(this).find('.id').append($(this).find('.r0').size() ? tick() : xmark()); | |
67 | }); | |
c0672e69 MG |
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 | }); | |
d31924ef MG |
74 | } |
75 | ||
76 | function start_tracking(user){ | |
77 | localStorage.setItem('tracker_username', user); | |
78 | $('#tracker_userlink').attr('href', '/us/' + user).html(user); | |
bdaa8919 | 79 | tracker.removeClass('hidden'); |
d31924ef | 80 | update_tracker(); |
63b23993 MG |
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'); | |
d31924ef | 88 | var xhr = new XMLHttpRequest(); |
d3892d73 | 89 | xhr.open('GET', '/us/' + user + '?format=json'); |
d31924ef MG |
90 | xhr.onload = function () { |
91 | localStorage.setItem('tracker_data', this.responseText); | |
63b23993 | 92 | localStorage.setItem('tracker_lastfetch', Date.now()); |
d31924ef MG |
93 | update_tracker(); |
94 | }; | |
95 | xhr.send(); | |
96 | } | |
63b23993 | 97 | window.refresh_tracker = refresh_tracker; |
462db4aa | 98 | window.start_tracking = start_tracking; |
d31924ef MG |
99 | |
100 | function stop_tracking(){ | |
c0672e69 | 101 | clean_tracker(); |
d31924ef MG |
102 | localStorage.removeItem('tracker_username'); |
103 | localStorage.removeItem('tracker_data'); | |
63b23993 | 104 | localStorage.removeItem('tracker_lastfetch'); |
bdaa8919 | 105 | tracker.addClass('hidden'); |
d31924ef MG |
106 | } |
107 | ||
108 | $( document ).ready(function(){ | |
bdaa8919 MG |
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')); | |
d31924ef | 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 | })(); |