2020-03-16 15:15:43 +00:00
var use _local _storage = storage _check ( ) ;
2020-03-16 15:28:27 +00:00
var html _element = null ;
2020-03-16 15:15:43 +00:00
function dgel ( id ) {
return document . getElementById ( id ) ;
}
function storage _check ( ) {
// based on https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API
try {
let x = 'test' ;
localStorage . setItem ( x , x ) ;
localStorage . removeItem ( x ) ;
return true ;
}
catch ( e ) {
// fall back to cookies
// i guess this might break if someone has cookies and localstorage disabled but people with first party cookies blocked are used to things breaking anyway ;)
return false ;
}
}
function theme _get ( ) {
// true if night theme
if ( use _local _storage ) {
// weakly typed languages. not even once.
return localStorage . getItem ( 'theme' ) == 'true' ;
} else {
return document . cookie == 'theme=true' ;
}
}
function theme _set ( night ) {
if ( use _local _storage ) {
localStorage . setItem ( 'theme' , night ) ;
} else {
document . cookie = ` theme= ${ night } ` ;
}
}
function update _page _theme ( ) {
let night = theme _get ( ) ;
if ( night ) {
html _element . classList . add ( 'night' ) ;
} else {
html _element . classList . remove ( 'night' ) ;
}
}
window . addEventListener ( 'DOMContentLoaded' , ( event ) => {
2020-03-16 15:28:27 +00:00
html _element = document . getElementsByTagName ( 'html' ) [ 0 ] ;
2020-03-16 15:15:43 +00:00
update _page _theme ( ) ;
2020-03-16 15:28:27 +00:00
2020-03-16 15:15:43 +00:00
dgel ( 'theme-control' ) . onclick = function ( ) {
2020-03-16 15:28:27 +00:00
// only add the 'ready' class upon clicking the button
// this avoids the page transitioning every time you change the page with night theme on
html _element . classList . add ( 'ready' ) ;
2020-03-16 15:15:43 +00:00
theme _set ( ! theme _get ( ) ) ;
update _page _theme ( ) ;
} ;
2020-03-25 10:39:23 +00:00
dgel ( 'hamburger' ) . onclick = function ( ) {
this . classList . toggle ( 'active' ) ;
dgel ( 'header' ) . classList . toggle ( 'active' ) ;
dgel ( 'main' ) . classList . toggle ( 'active' ) ;
}
2020-03-16 15:15:43 +00:00
} ) ;