bde-liste/static/js/main.js

102 lines
2.4 KiB
JavaScript

const base_url = "https://oss110metz.rezo-rm.fr/"
var page = ['#main-page', '#admin-page', '#login-page', '#demande-page'];
var historyHandler = [];
window.onpopstate = history.onpushstate = function(e) {
if (e.state) {
show_page(e.state, true);
} else {
history.back();
}
};
history.pushState("#main-page", "", "")
function show_page(id, historyPush) {
//si le z-index de la nouvelle page est inférieur on fait un transi 'forward' sinon 'backward'
for(i in page) {
$(page[i]).hide().removeClass("away");
}
$hs = $(history.state).show();
if(!historyPush)
$hs.addClass('away');
$id = $(id).show();
if(!historyPush) {
history.pushState(id, "", "")
}
}
$("#login-button").click(function (e) {
var data = JSON.stringify({
user: $("#login").val(),
password: $("#password").val()
});
$("#password").val('');
$.ajax({
type: "POST",
url: base_url + "login",
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if(data.success) {
localStorage.setItem("token", data.token);
localStorage.setItem("user", data.user);
show_page('#demande-page');
} else {
}
}
});
});
$("#prank-button").click(function () {
if(localStorage.getItem('token')) {
show_page('#demande-page');
}
else {
show_page('#login-page');
}
});
$(window).on("load", function() {
/* Register the Service Worker */
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('./js/sw.js');
}
for(i in page) {
$(page[i]).css('z-index', page.length - i);
}
/* load timer */
window.setInterval(function() {
var time = $("#timer").text();
var t = time.split(":").map(t => parseInt(t));
t[2] -= 1;
if (t[2] < 0) {
t[2] = 59;
t[1] -= 1;
}
if (t[1] < 0) {
t[1] = 59;
t[0] -= 1;
}
if (t[0] < 0) {
t[0] = 0;
t[1] = 0;
t[2] = 0;
}
$("#timer").text((new Date('December 17, 1995 ' + t.join(':'))).toLocaleTimeString('default', {
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
}));
}, 1000);
});