istio.io/src/js/styleSwitcher.js

1 line
2.2 KiB
JavaScript

"use strict";
function applyStyleSheet(title) {
let i, a;
for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
if (a.getAttribute("rel").indexOf("style") !== -1 && a.getAttribute("title")) {
a.disabled = a.getAttribute("title") !== title;
}
}
// set the active theme menu item
let item = document.getElementById("light-theme-item");
if (item !== null) {
if (title === "light") {
item.classList.add("active");
} else {
item.classList.remove("active");
}
}
item = document.getElementById("dark-theme-item");
if (item !== null) {
if (title === "dark") {
item.classList.add("active");
} else {
item.classList.remove("active");
}
}
}
function getPreferredStyleSheet() {
let i, a;
for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
if (a.getAttribute("rel").indexOf("style") !== -1
&& a.getAttribute("rel").indexOf("alt") === -1
&& a.getAttribute("title")) {
return a.getAttribute("title");
}
}
return null;
}
function createCookie(name, value, days) {
let expires = "";
if (days) {
const date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
}
document.cookie = name + "=" + value + expires + "; path=/";
}
function readCookie(name) {
const nameEQ = name + "=";
const ca = document.cookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1, c.length);
}
if (c.indexOf(nameEQ) === 0) {
return c.substring(nameEQ.length, c.length);
}
}
return null;
}
function setActiveStyleSheet(title) {
applyStyleSheet(title);
createCookie("style", title);
}
function loadActiveStyleSheet() {
let cookie = readCookie("style");
if (cookie === null) {
applyStyleSheet(getPreferredStyleSheet());
} else {
applyStyleSheet(cookie);
}
}
window.onload = function (e) {
loadActiveStyleSheet();
};
loadActiveStyleSheet();