'use strict';
-var $;
+let $;
* Copyright (c) 2014 Denis Ciccale (@tdecs)
* Released under MIT license
*/
-!function (b, c, d, e) {
+(function (b, c, d, e) {
/*
* init function (internal use)
// representation instead of an object
splice: c.splice
}
-}(document, [], 'prototype');
+})(document, [], 'prototype');
function m(html) {
- var div = document.createElement("div");
+ const div = document.createElement("div");
div.innerHTML = html;
return div.firstChild;
}
$(function() {
if(!document.getElementById('sidebar')) {
- let content = $('main')[0];
+ const content = $('main')[0];
content.innerHTML = '<div class="col-md-9">' + content.innerHTML + '</div><aside id="sidebar" class="col-md-3"></aside>';
content.classList.add('row');
}
-var ext_table = {
+const ext_table = {
'c' : 'C',
'cc' : 'CPP',
}
function divine_format() {
- var filename = $('#prog')[0].value;
- var ext = /\.([^.]*)$/.exec(filename)[1];
+ const filename = $('#prog')[0].value;
+ const ext = /\.([^.]*)$/.exec(filename)[1];
if(ext_table[ext])
$('#prog_format')[0].value = ext_table[ext];
$(function(){
- let result = $('#result')[0];
+ const result = $('#result')[0];
$('.jsform > input[type="submit"]').on('click', e => {
- let form_data = new FormData(this.parentElement);
- let form = this.parentNode;
- let xhr = new XMLHttpRequest();
+ const form_data = new FormData(this.parentElement);
+ const form = this.parentNode;
+ const xhr = new XMLHttpRequest();
xhr.open(form.getAttribute('method'), form.getAttribute('action'));
xhr.onload = () => result.innerHTML = this.responseText;
xhr.onerror = () => result.innerHTML = 'Error!';
};
$(function(){
- let path = location.pathname;
- for (let nav in NAVS)
+ const path = location.pathname;
+ for (const nav in NAVS)
if(path.match(NAVS[nav]))
$('#nav-' + nav)[0].classList.add('active');
});
}
$(function() {
- var theme_ul = '<ul class="list-inline" id="themes">' +
+ const theme_ul = '<ul class="list-inline" id="themes">' +
'<li><a role="button" data-theme="cyborg">Black</a>' +
'<li><a role="button" data-theme="slate">Grey</a>' +
'<li><a role="button" data-theme="readable">White</a></ul>';
- var sidebar = $('#sidebar')[0];
+ const sidebar = $('#sidebar')[0];
sidebar.insertBefore(m(theme_ul), sidebar.firstChild);
$('#themes a').on('click', e => set_style(e.target.dataset.theme, true));
});
-let theme = localStorage.getItem("theme");
-if(theme) {
- if(theme == "cerulean" || theme == "cosmo")
- theme = "readable";
- set_style(theme, false);
-}
+const theme = localStorage.getItem("theme");
+if(theme)
+ set_style(theme == "cerulean" || theme == "cosmo" ? "readable" : theme, false);
let offset = 0;
function update_timer(timer){
- var start = parseInt(timer.dataset.start);
- var stop = parseInt(timer.dataset.stop);
- var value = parseInt(timer.dataset.value);
- var now = Math.floor(Date.now() / 1000) + offset;
+ const start = parseInt(timer.dataset.start);
+ const stop = parseInt(timer.dataset.stop);
+ const value = parseInt(timer.dataset.value);
+ const now = Math.floor(Date.now() / 1000) + offset;
- var left = stop - now;
- var total = stop - start;
- if(left < 0)
- left = 0;
+ const left = Math.max(stop - now, 0);
+ const total = stop - start;
- if(value) {
- value = Math.max(value * 3 / 10, value * left / total);
- timer.innerHTML = Math.floor(value);
- } else {
- var hours = Math.floor(left / 60 / 60);
+ if(value)
+ timer.innerHTML = Math.floor(Math.max(value * 3 / 10, value * left / total));
+ else {
+ let hours = Math.floor(left / 60 / 60);
hours = hours < 10 ? '0' + hours : hours;
- var minutes = Math.floor(left / 60) % 60;
+ let minutes = Math.floor(left / 60) % 60;
minutes = minutes < 10 ? '0' + minutes : minutes;
- var seconds = left % 60;
+ let seconds = left % 60;
seconds = seconds < 10 ? '0' + seconds : seconds;
timer.innerHTML = hours + ':' + minutes + ':' + seconds;
}
setInterval(() => update_timer(item), 1000);
});
if($('.timer').length > 0) {
- let xhr = new XMLHttpRequest();
+ const xhr = new XMLHttpRequest();
xhr.open('HEAD', '/');
xhr.onload = () => {
- let srvtime = Date.parse(xhr.getResponseHeader('Date'));
+ const srvtime = Date.parse(xhr.getResponseHeader('Date'));
offset = Math.ceil((srvtime - Date.now()) / 1000);
console.log("Offset from server: " + offset);
};
let tracker;
function update_tracker(){
- let data = localStorage.getItem('tracker_data');
- let user = localStorage.getItem('tracker_username');
- if(!data)
+ if(!localStorage.getItem('tracker_data'))
return;
- data = JSON.parse(data);
- let contests = {}, solved = {}, attempted = {}, solvednr = 0, attemptednr = 0;
+ const data = JSON.parse(localStorage.getItem('tracker_data'));
+ const user = localStorage.getItem('tracker_username');
+ const contests = {}, solved = {}, attempted = {};
+ let solvednr = 0, attemptednr = 0;
data.problems.forEach(e => {
if(e.solved) {
solved[e.problem] = 1;
if(location.pathname == '/pb/')
$('table .name a').each(el => {
- let id = el.getAttribute('href').split('?', 2)[0];
+ const id = el.getAttribute('href').split('?', 2)[0];
if(solved[id])
el.insertAdjacentHTML('beforebegin', TICK);
else if(attempted[id])
if(location.pathname == '/ct/')
$('table .name a').each(el => {
- let id = el.getAttribute('href').substr(4);
+ const id = el.getAttribute('href').substr(4);
if(contests[id])
el.insertAdjacentHTML('beforebegin', TICK);
});
if(location.pathname == '/log/')
$('table tbody tr').each(el => {
- let owner = el.querySelector('.owner a');
+ const owner = el.querySelector('.owner a');
if(!owner || owner.getAttribute('href') != '/us/' + user)
return;
- var state = el.getElementsByClassName('r0').length ? TICK : XMARK;
+ const state = el.getElementsByClassName('r0').length ? TICK : XMARK;
el.getElementsByClassName('id')[0].innerHTML += state;
});
$('#tracker_userlink')[0].innerHTML = user;
tracker.classList.remove('hidden');
update_tracker();
- var lastfetch = localStorage.getItem('tracker_lastfetch');
+ const lastfetch = localStorage.getItem('tracker_lastfetch');
if(Date.now() - lastfetch > 60 * 10 * 1000)
refresh_tracker();
}
function refresh_tracker(){
- var user = localStorage.getItem('tracker_username');
- var xhr = new XMLHttpRequest();
+ const user = localStorage.getItem('tracker_username');
+ const xhr = new XMLHttpRequest();
xhr.open('GET', '/us/' + user + '?format=json');
xhr.onload = () => {
localStorage.setItem('tracker_data', xhr.responseText);
$(function(){
tracker = m('<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>');
- var sidebar = $('#sidebar')[0];
+ const sidebar = $('#sidebar')[0];
sidebar.insertBefore(tracker, sidebar.firstChild);
$('#tracker_stop').on('click', stop_tracking);
$('#track_user').on('click', function() { stop_tracking(); start_tracking(this.dataset.user) });
$(function(){
- var sidebar = $('#sidebar')[0];
- var login = document.createElement('div');
+ const sidebar = $('#sidebar')[0];
+ const login = document.createElement('div');
login.innerHTML = '<a role="button" id="login" class="show">Log in</a>';
sidebar.insertBefore(login, sidebar.firstChild);
$('#login').on('click', e => {
- var xhr = new XMLHttpRequest();
+ const xhr = new XMLHttpRequest();
xhr.open('GET', '/login');
xhr.onload = () => {
if(xhr.status != 200)
return;
- var username = xhr.responseText;
+ const username = xhr.responseText;
sessionStorage.setItem('login_username', username);
start_tracking(username);
login.innerHTML = 'Probably logged in as ' + username;
return false;
});
- var username = sessionStorage.getItem('login_username');
+ const username = sessionStorage.getItem('login_username');
if(username)
login.innerHTML = 'Probably logged in as ' + username;
});