1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-05 21:06:24 -07:00
gochan/frontend/js/settings.js

152 lines
4.8 KiB
JavaScript
Raw Normal View History

2022-07-01 11:16:13 -07:00
/* global webroot, defaultStyle, styles */
import $ from "jquery";
import { showLightBox } from "./dom/lightbox";
import { initTopBar, TopBarButton } from "./dom/topbar";
import { getBooleanStorageVal, getStorageVal, setStorageVal } from "./storage";
import { initPostPreviews } from "./postutil";
import { closeQR, initQR } from "./dom/qr";
import { initWatcher } from "./watcher/watcher";
2022-05-07 17:15:27 -07:00
/**
* @type {TopBarButton}
*/
let $settingsButton = null;
/**
* @type {Map<string,Setting>}
*/
const settings = new Map();
2022-05-07 17:15:27 -07:00
class Setting {
/**
* @param {string} key The name of the setting
* @param {string} title text that gets shown in the Settings lightbox
2022-05-07 17:15:27 -07:00
* @param {string} defaultVal the setting's default value
* @param {string} onSave function that gets called when you save the settings
*/
2022-05-07 17:15:27 -07:00
constructor(key, title, defaultVal = "", onSave = () => {}) {
this.key = key;
this.title = title;
2022-05-07 17:15:27 -07:00
this.defaultVal = defaultVal;
this.onSave = onSave;
this.element = null;
}
2022-05-07 17:15:27 -07:00
getElementValue() {
2022-08-05 23:38:42 +00:00
if(this.element === null) return "";
2022-05-07 17:15:27 -07:00
return this.element.val();
}
2022-05-07 17:15:27 -07:00
setElementValue(newVal) {
2022-08-05 23:38:42 +00:00
if(this.element === null) return;
2022-05-07 17:15:27 -07:00
this.element.val(newVal);
}
2022-05-07 17:15:27 -07:00
getStorageValue() {
return getStorageVal(this.key, this.defaultVal);
}
2022-05-07 17:15:27 -07:00
setStorageValue(newVal) {
setStorageVal(this.key, newVal);
}
2022-05-07 17:15:27 -07:00
createElement(selector = "<input/>", props = {}) {
return $(selector).prop(props).prop({
id: this.key,
name: this.key
2022-05-07 17:15:27 -07:00
});
}
}
2022-05-07 17:15:27 -07:00
class DropdownSetting extends Setting {
constructor(key, title, options = [], defaultVal = "", onSave = () => {}) {
super(key, title, defaultVal, onSave);
this.element = this.createElement("<select/>");
for(const option of options) {
$("<option/>").val(option.val).text(option.text).appendTo(this.element);
}
2022-05-07 17:15:27 -07:00
this.element.val(this.getStorageValue());
}
}
2022-05-07 17:15:27 -07:00
class BooleanSetting extends Setting {
constructor(key, title, defaultVal = false, onSave = () => {}) {
super(key, title, defaultVal, onSave);
this.element = this.createElement("<input/>", {
type: "checkbox",
checked: this.getStorageValue()
});
}
getElementValue() {
return this.element.prop("checked");
}
setElementValue(newVal) {
2022-07-01 15:52:51 -07:00
this.element.prop({checked: newVal});
2022-05-07 17:15:27 -07:00
}
getStorageValue() {
let val = super.getStorageValue();
return val == true || val == "true";
}
}
2022-05-07 17:15:27 -07:00
class NumberSetting extends Setting {
2022-07-25 15:57:15 -07:00
constructor(key, title, defaultVal = 0, minMax = {min: null, max: null}, onSave = () => {}) {
2022-05-07 17:15:27 -07:00
super(key, title, defaultVal, onSave);
2022-07-25 15:57:15 -07:00
let props = {
2022-05-07 17:15:27 -07:00
type: "number"
2022-07-25 15:57:15 -07:00
};
if(typeof minMax.min == "number" && !isNaN(minMax.min))
props.min = minMax.min;
if(typeof minMax.max == "number" && !isNaN(minMax.max))
props.max = minMax.max;
this.element = this.createElement("<input />", props).val(this.getStorageValue());
2022-05-07 17:15:27 -07:00
}
getStorageValue() {
let val = Number.parseFloat(super.getStorageValue());
2022-07-01 11:16:13 -07:00
if(isNaN(val))
2022-05-07 17:15:27 -07:00
val = this.defaultVal;
return val;
}
}
function createLightbox() {
let settingsHTML =
2022-05-07 17:15:27 -07:00
`<div id="settings-container" style="overflow:auto"><table width="100%"><colgroup><col span="1" width="50%"><col span="1" width="50%"></colgroup></table></div><div class="lightbox-footer"><hr /><button id="save-settings-button">Save Settings</button></div>`;
showLightBox("Settings", settingsHTML);
$("button#save-settings-button").on("click", () => {
2022-07-01 11:16:13 -07:00
settings.forEach((setting, key) => {
2022-05-07 17:15:27 -07:00
setStorageVal(key, setting.getElementValue());
setting.onSave();
});
});
let $settingsTable = $("#settings-container table");
2022-07-01 11:16:13 -07:00
settings.forEach((setting) => {
2022-05-07 17:15:27 -07:00
let $tr = $("<tr/>").appendTo($settingsTable);
$("<td/>").append($("<b/>").text(setting.title)).appendTo($tr);
$("<td/>").append(setting.element).appendTo($tr);
});
}
$(() => {
let styleOptions = [];
for(const style of styles) {
styleOptions.push({text: style.Name, val: style.Filename});
}
2022-05-07 17:15:27 -07:00
settings.set("style", new DropdownSetting("style", "Style", styleOptions, defaultStyle, function() {
document.getElementById("theme").setAttribute("href",
`${webroot}css/${this.getElementValue()}`);
}));
settings.set("pintopbar", new BooleanSetting("pintopbar", "Pin top bar", true, initTopBar));
settings.set("enableposthover", new BooleanSetting("enableposthover", "Preview post on hover", false, initPostPreviews));
settings.set("enablepostclick", new BooleanSetting("enablepostclick", "Preview post on click", true, initPostPreviews));
settings.set("useqr", new BooleanSetting("useqr", "Use Quick Reply box", true, () => {
if(getBooleanStorageVal("useqr", true)) initQR();
else closeQR();
}));
2022-07-25 15:57:15 -07:00
settings.set("watcherseconds", new NumberSetting("watcherseconds", "Check watched threads every # seconds", 10, {
min: 2
2022-08-07 18:05:26 -07:00
}, initWatcher));
settings.set("persistentqr", new BooleanSetting("persistentqr", "Persistent Quick Reply", false));
2022-05-07 17:15:27 -07:00
2022-08-05 23:38:42 +00:00
if($settingsButton === null)
$settingsButton = new TopBarButton("Settings", createLightbox, {
before: "a#watcher"
});
});