my_blog/public/assets/js/main.js

62 lines
1.6 KiB
JavaScript

const color_theme_id = "color-theme";
const font_id = "fonts";
function save_accessibility_settings(theme, font) {
document.cookie = "theme=" + theme + "; path=/";
document.cookie = "font=" + font +"; path=/";
}
function get_saved_accessibility_settings() {
let cookies = document.cookie.split("; ");
let theme = null;
let font = null;
for (let cookie of cookies) {
let [key, value] = cookie.split("=");
console.log("'" + key + "' '" + value + "'");
if (key == "theme") {
theme = value;
}
if (key == "font") {
font = value;
}
}
if (theme != null) {
let color_theme = document.getElementById(color_theme_id);
for (option of color_theme.options) {
if (option.value == theme) {
option.selected = true;
break;
}
}
}
if (font != null) {
let fonts = document.getElementById(font_id);
for (option of fonts.options) {
if (option.value == font) {
option.selected = true;
break;
}
}
}
return [theme, font];
}
function load_accessibility_settings(theme, font) {
const body = document.getElementsByTagName("body")[0];
if (theme.length != 0) {
body.setAttribute("class", theme);
}
if (font.length != 0) {
let class_attribute = body.getAttribute("class");
body.setAttribute("class", class_attribute+" "+font);
}
}
function apply_accessibility_options() {
const theme = document.getElementById(color_theme_id).value;
const font = document.getElementById(font_id).value;
save_accessibility_settings(theme, font);
load_accessibility_settings(theme, font);
}
function first_load() {
let [theme, font] = get_saved_accessibility_settings();
load_accessibility_settings(theme, font);
}