You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
devdocs/assets/javascripts/app/settings.js

253 lines
6.1 KiB

// TODO: This file was created by bulk-decaffeinate.
// Sanity-check the conversion and remove this comment.
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* DS103: Rewrite code to no longer use __guard__, or convert again using --optional-chaining
* DS104: Avoid inline assignments
* DS206: Consider reworking classes to avoid initClass
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
*/
1 year ago
(function () {
let PREFERENCE_KEYS = undefined;
let INTERNAL_KEYS = undefined;
app.Settings = class Settings {
static initClass() {
PREFERENCE_KEYS = [
1 year ago
"hideDisabled",
"hideIntro",
"manualUpdate",
"fastScroll",
"arrowScroll",
"analyticsConsent",
"docs",
"dark", // legacy
"theme",
"layout",
"size",
"tips",
"noAutofocus",
"autoInstall",
"spaceScroll",
"spaceTimeout",
];
1 year ago
INTERNAL_KEYS = ["count", "schema", "version", "news"];
this.prototype.LAYOUTS = [
1 year ago
"_max-width",
"_sidebar-hidden",
"_native-scrollbars",
"_text-justify-hyphenate",
];
1 year ago
this.defaults = {
count: 0,
hideDisabled: false,
hideIntro: false,
news: 0,
manualUpdate: false,
schema: 1,
analyticsConsent: false,
1 year ago
theme: "auto",
spaceScroll: 1,
1 year ago
spaceTimeout: 0.5,
};
}
constructor() {
1 year ago
this.store = new CookiesStore();
this.cache = {};
1 year ago
this.autoSupported =
window.matchMedia("(prefers-color-scheme)").media !== "not all";
if (this.autoSupported) {
1 year ago
this.darkModeQuery = window.matchMedia("(prefers-color-scheme: dark)");
this.darkModeQuery.addListener(() => this.setTheme(this.get("theme")));
}
}
get(key) {
let left;
1 year ago
if (this.cache.hasOwnProperty(key)) {
return this.cache[key];
}
this.cache[key] =
(left = this.store.get(key)) != null
? left
: this.constructor.defaults[key];
if (
key === "theme" &&
this.cache[key] === "auto" &&
!this.darkModeQuery
) {
return (this.cache[key] = "default");
} else {
return this.cache[key];
}
}
set(key, value) {
this.store.set(key, value);
delete this.cache[key];
1 year ago
if (key === "theme") {
this.setTheme(value);
}
}
del(key) {
this.store.del(key);
delete this.cache[key];
}
hasDocs() {
1 year ago
try {
return !!this.store.get("docs");
} catch (error) {}
}
getDocs() {
1 year ago
return (
__guard__(this.store.get("docs"), (x) => x.split("/")) ||
app.config.default_docs
);
}
setDocs(docs) {
1 year ago
this.set("docs", docs.join("/"));
}
getTips() {
1 year ago
return __guard__(this.store.get("tips"), (x) => x.split("/")) || [];
}
setTips(tips) {
1 year ago
this.set("tips", tips.join("/"));
}
setLayout(name, enable) {
this.toggleLayout(name, enable);
1 year ago
const layout = (this.store.get("layout") || "").split(" ");
$.arrayDelete(layout, "");
if (enable) {
1 year ago
if (layout.indexOf(name) === -1) {
layout.push(name);
}
} else {
$.arrayDelete(layout, name);
}
if (layout.length > 0) {
1 year ago
this.set("layout", layout.join(" "));
} else {
1 year ago
this.del("layout");
}
}
hasLayout(name) {
1 year ago
const layout = (this.store.get("layout") || "").split(" ");
return layout.indexOf(name) !== -1;
}
setSize(value) {
1 year ago
this.set("size", value);
}
dump() {
return this.store.dump();
}
export() {
const data = this.dump();
1 year ago
for (var key of Array.from(INTERNAL_KEYS)) {
delete data[key];
}
return data;
}
import(data) {
let key, value;
const object = this.export();
for (key in object) {
value = object[key];
1 year ago
if (!data.hasOwnProperty(key)) {
this.del(key);
}
}
for (key in data) {
value = data[key];
1 year ago
if (PREFERENCE_KEYS.indexOf(key) !== -1) {
this.set(key, value);
}
}
}
reset() {
this.store.reset();
this.cache = {};
}
initLayout() {
1 year ago
if (this.get("dark") === 1) {
this.set("theme", "dark");
this.del("dark");
}
this.setTheme(this.get("theme"));
for (var layout of Array.from(this.LAYOUTS)) {
this.toggleLayout(layout, this.hasLayout(layout));
}
this.initSidebarWidth();
}
setTheme(theme) {
1 year ago
if (theme === "auto") {
theme = this.darkModeQuery.matches ? "dark" : "default";
}
1 year ago
const { classList } = document.documentElement;
classList.remove("_theme-default", "_theme-dark");
classList.add("_theme-" + theme);
this.updateColorMeta();
}
updateColorMeta() {
1 year ago
const color = getComputedStyle(document.documentElement)
.getPropertyValue("--headerBackground")
.trim();
$("meta[name=theme-color]").setAttribute("content", color);
}
toggleLayout(layout, enable) {
1 year ago
const { classList } = document.body;
// sidebar is always shown for settings; its state is updated in app.views.Settings
1 year ago
if (
layout !== "_sidebar-hidden" ||
!(app.router != null ? app.router.isSettings : undefined)
) {
classList.toggle(layout, enable);
}
classList.toggle("_overlay-scrollbars", $.overlayScrollbarsEnabled());
}
initSidebarWidth() {
1 year ago
const size = this.get("size");
if (size) {
document.documentElement.style.setProperty(
"--sidebarWidth",
size + "px",
);
}
}
};
app.Settings.initClass();
return app.Settings;
})();
function __guard__(value, transform) {
1 year ago
return typeof value !== "undefined" && value !== null
? transform(value)
: undefined;
}