-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimeView.js
152 lines (133 loc) · 5.13 KB
/
TimeView.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* Programming contest management system
* Copyright © 2012 Luca Wehrstedt <luca.wehrstedt@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
function format_time(time, full) {
var h = Math.floor(time / 3600);
var m = Math.floor((time % 3600) / 60);
var s = Math.floor(time % 60);
h = full && h < 10 ? "0" + h : "" + h;
m = m < 10 ? "0" + m : "" + m;
s = s < 10 ? "0" + s : "" + s;
return (h + ":" + m + ":" + s);
};
function _get_time() {
// Return the seconds since January 1, 1970 00:00:00 UTC
return $.now() / 1000;
}
var TimeView = new function () {
var self = this;
// possible values:
// - 0: elapsed time
// - 1: remaining time
// - 2: current (clock) time
self.status = 0;
self.init = function () {
window.setInterval(function() {
self.on_timer();
}, 1000);
self.on_timer();
$("#TimeView_selector_elapsed").click(function () {
self.status = 0;
self.on_timer();
$("#TimeView_selector").removeClass("open");
});
$("#TimeView_selector_remaining").click(function () {
self.status = 1;
self.on_timer();
$("#TimeView_selector").removeClass("open");
});
$("#TimeView_selector_current").click(function () {
self.status = 2;
self.on_timer();
$("#TimeView_selector").removeClass("open");
});
$("#TimeView_expand").click(function () {
$("#TimeView_selector").toggleClass("open");
});
$("#TimeView_selector").click(function (event) {
event.stopPropagation();
return false;
});
$("body").on("click", function () {
$("#TimeView_selector").removeClass("open");
})
};
self.on_timer = function () {
var cur_time = _get_time();
var c = null;
// contests are iterated sorted by begin time
// and the first one that's still running is chosen
for (var j in DataStore.contest_list) {
var contest = DataStore.contest_list[j];
if (cur_time <= contest['end']) {
c = contest;
break;
}
}
if (c == null) {
$("#TimeView_name").text();
} else {
$("#TimeView_name").text(c["name"]);
}
var date = new Date(cur_time * 1000);
var today = new Date(date.getFullYear(), date.getMonth(), date.getDate());
var time = cur_time - today.getTime() / 1000;
var full_time = false;
if (c == null) {
// no "next contest": always show the clock
$("#TimeView").removeClass("elapsed remaining pre_cont cont");
$("#TimeView").addClass("current post_cont");
full_time = true;
} else {
if (cur_time < c['begin']) {
// the next contest has yet to start: show remaining or clock
$("#TimeView").removeClass("cont post_cont");
$("#TimeView").addClass("pre_cont");
if (self.status == 2) {
$("#TimeView").removeClass("elapsed remaining");
$("#TimeView").addClass("current");
full_time = true;
} else {
$("#TimeView").removeClass("elapsed current");
$("#TimeView").addClass("remaining");
time = cur_time - c['begin'];
}
} else {
// the next contest already started: all options available
$("#TimeView").removeClass("pre_cont post_cont");
$("#TimeView").addClass("cont");
if (self.status == 2) {
$("#TimeView").removeClass("elapsed remaining");
$("#TimeView").addClass("current");
full_time = true;
} else if (self.status == 1) {
$("#TimeView").removeClass("elapsed current");
$("#TimeView").addClass("remaining");
time = cur_time - c['end'];
} else {
$("#TimeView").removeClass("remaining current");
$("#TimeView").addClass("elapsed");
time = cur_time - c['begin'];
}
}
}
var time_str = format_time(Math.abs(Math.floor(time)), full_time);
if (time < 0) {
time_str = '-' + time_str;
}
$("#TimeView_time").text(time_str);
};
};