This repository has been archived on 2023-02-26. You can view files and clone it, but cannot push or open issues or pull requests.
langue-phundrak-com/js/main.js

91 lines
2.8 KiB
JavaScript
Raw Normal View History

/*jshint esversion: 6 */
2019-08-24 23:28:24 +00:00
window.onload = function() {
reorganize_html();
create_theme_switcher();
2019-08-26 14:58:30 +00:00
roll_elems();
};
function roll_elems() {
// Make the rollable headers actually rollable and rolled
2019-09-03 01:35:33 +00:00
$(".outline-2").each(function() {
['h2', 'h3', 'h4', 'h5', 'h6'].forEach(htitle => {
$(this).find(htitle).each(function($header) {
$header = $(this);
if (isEmpty($header.next())) {
$header.next().remove();
}
$header.click(function() {
$header.nextAll().each(function() {
$(this).slideToggle(500);
});
$header.toggleClass('rotated');
});
2019-08-26 14:58:30 +00:00
});
2019-08-24 23:28:24 +00:00
});
});
}
2019-08-24 23:35:23 +00:00
function reorganize_html() {
// Remove table of contents
$("#table-of-contents").remove();
2019-08-24 23:35:23 +00:00
// Move the title out of the content div
2019-08-26 14:58:30 +00:00
$('#content').before('<div class="h1-container"><div class="highlight-h1"></div></div>');
$('.title').prependTo($('.h1-container'));
2019-08-24 23:35:23 +00:00
// Move the postamble in the content div
$('#postamble').appendTo($('#content'));
2019-08-24 23:35:23 +00:00
// Move each table in a div to handle large tables' overflow
$('table').each(function() {
2019-08-24 23:28:24 +00:00
$table = $(this);
$table.before('<div class="largetable"></div>');
$table.prependTo($table.prev());
});
}
function create_theme_switcher() {
2019-09-03 00:04:57 +00:00
// set the correct CSS depending on the cookie, dark is default
var light = isThemeLight();
$('body').append('<div class="themeBtn"><i class="fas fa-'
.concat(light ? 'moon' : 'sun').concat('"></i></div>'));
$('head').append('<link id="theme" rel="stylesheet" href="./css/'
.concat(light ? 'light' : 'dark').concat('.css">'));
2019-09-03 00:04:57 +00:00
// switch CSS files and button icon, set new cookie on theme switcher click
$('.themeBtn').click(function() {
2019-09-03 00:04:57 +00:00
console.log("Theme change");
var light = !isThemeLight();
console.log(light);
$("#theme").first().attr('href', './css/'
.concat(light ? 'light' : 'dark')
.concat('.css'));
$('.themeBtn').html('<i class="fas fa-'
.concat(light ? 'moon' : 'sun')
.concat('"></i>'));
Cookies.set('light-theme', light ? 'true' : 'false');
});
}
2019-08-26 14:58:30 +00:00
2019-09-03 00:04:57 +00:00
function isThemeLight() {
// set the css and button depending on the cookie found, dark is default
var light;
switch (Cookies.get('light-theme')) {
case 'true':
light = true;
break;
case null: // If no theme cookie is found, set dark by default
Cookies.set('light-theme', false);
/* falls through */
default:
light = false;
break;
}
return light;
}
2019-08-26 14:59:28 +00:00
function isEmpty(el) {
return !$.trim(el.html());
2019-08-26 14:58:30 +00:00
}