Web/scripts/main.js

63 lines
1.7 KiB
JavaScript

let dark = getCookie("dark") === "true";
let cookieNotification = getCookie("notification") !== "true";
if (dark) {
document.write('<link rel="stylesheet" href="dark.css">')
}
$(function () {
let openModals = function (hash) {
if (hash && hash.length > 1) {
if (hash === '#impressum') {
$('#impressum-modal').modal('show');
}
}
}
let hash = window.location.hash;
openModals(hash);
$('a').on('click', function () {
openModals($(this).attr('href'));
});;
$('.navbar-collapse a').on('click', function () {
$('.navbar-collapse').collapse('hide');
});
$('#blobheart').on('click', function () {
$('#navbar-image').attr('src', 'res/heart.png');
});
let mode = $('#dark-mode');
mode.prop('checked', dark);
mode.on('click', function () {
setCookie("dark", $(this).prop('checked'), 365);
window.location.hash = "";
location.reload();
});
if (hash.startsWith("#blog-")) {
var anchor = $(hash);
if (anchor.length) {
openBlogPost(hash.substring(6));
$('html, body').animate({
scrollTop: anchor.offset().top
}, 0)
}
}
});
function getCookie(key) {
let c = document.cookie;
if (!c)
return undefined;
let start = c.indexOf(key + "=") + key.length + 1;
let end = c.indexOf(";", start);
return c.substring(start, end < 0 ? c.length : end);
}
function setCookie(key, value, days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
document.cookie = key + "=" + value + "; expires=" + date.toUTCString();
}