1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-05 08:46:23 -07:00
gochan/frontend/ts/settings.ts

199 lines
6.4 KiB
TypeScript
Raw Normal View History

2022-07-01 11:16:13 -07:00
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";
2023-05-13 23:46:41 -07:00
let $settingsButton: TopBarButton = null;
2023-05-13 23:46:41 -07:00
const settings: Map<string, Setting<boolean|number|string,HTMLElement>> = new Map();
2023-05-13 23:46:41 -07:00
type ElementValue = string|number|string[];
class Setting<T = any, E extends HTMLElement = HTMLElement> {
key: string;
title: string;
defaultVal: T;
onSave: () => any;
element: JQuery<E>
/**
2023-05-13 23:46:41 -07:00
* @param key The name of the setting
* @param title text that gets shown in the Settings lightbox
* @param defaultVal the setting's default value
* @param onSave function that gets called when you save the settings
*/
2023-05-13 23:46:41 -07:00
constructor(key: string, title: string, defaultVal:T, onSave = () => {}) {
this.key = key;
this.title = title;
2022-05-07 17:15:27 -07:00
this.defaultVal = defaultVal;
this.onSave = onSave;
this.element = null;
}
2023-05-13 23:46:41 -07:00
getElementValue(): T {
if(this.element === null) return this.defaultVal;
return this.element.val() as T;
}
2023-05-13 23:46:41 -07:00
setElementValue(newVal: T) {
2022-08-05 23:38:42 +00:00
if(this.element === null) return;
2023-05-13 23:46:41 -07:00
this.element.val(newVal as ElementValue);
}
2023-05-13 23:46:41 -07:00
getStorageValue(): T {
return getStorageVal(this.key, this.defaultVal.toString()) as T;
}
2023-05-13 23:46:41 -07:00
setStorageValue(newVal: T) {
2022-05-07 17:15:27 -07:00
setStorageVal(this.key, newVal);
}
2022-05-07 17:15:27 -07:00
createElement(selector = "<input/>", props = {}) {
2023-05-13 23:46:41 -07:00
return $<E>(selector).prop(props).prop({
id: this.key,
name: this.key
2022-05-07 17:15:27 -07:00
});
}
}
2023-05-13 23:46:41 -07:00
class TextSetting extends Setting<string, HTMLTextAreaElement> {
constructor(key: string, title: string, defaultVal = "", onSave = () => {}) {
super(key, title, defaultVal, onSave);
this.element = this.createElement("<textarea/>");
this.element.text(defaultVal);
let val = this.getStorageValue();
if(val != "") {
this.setElementValue(val);
}
}
setElementValue(text = "") {
this.element.text(text);
}
}
2023-05-13 23:46:41 -07:00
class DropdownSetting<T> extends Setting<ElementValue, HTMLSelectElement> {
constructor(key: string, title: string, options:any[] = [], defaultVal: ElementValue, onSave = () => {}) {
2022-05-07 17:15:27 -07:00
super(key, title, defaultVal, onSave);
this.element = this.createElement("<select/>");
for(const option of options) {
2023-05-13 23:46:41 -07:00
let s: HTMLSelectElement
$<HTMLSelectElement>("<option/>").val(option.val).text(option.text).appendTo(this.element);
}
2022-05-07 17:15:27 -07:00
this.element.val(this.getStorageValue());
}
}
2023-05-13 23:46:41 -07:00
class BooleanSetting extends Setting<boolean, HTMLInputElement> {
constructor(key: string, title: string, defaultVal = false, onSave = () => {}) {
2022-05-07 17:15:27 -07:00
super(key, title, defaultVal, onSave);
this.element = this.createElement("<input/>", {
type: "checkbox",
checked: this.getStorageValue()
});
}
getElementValue() {
return this.element.prop("checked");
}
2023-05-13 23:46:41 -07:00
setElementValue(newVal: boolean) {
this.element.prop({checked: newVal?"on":"off"});
2022-05-07 17:15:27 -07:00
}
getStorageValue() {
let val = super.getStorageValue();
2023-05-13 23:46:41 -07:00
return val == true;
}
}
2023-05-13 23:46:41 -07:00
interface MinMax {
type?: string;
min?: number;
max?: number;
}
class NumberSetting extends Setting<number, HTMLInputElement> {
constructor(key: string, title: string, defaultVal = 0, minMax: MinMax = {min: null, max: null}, onSave = () => {}) {
2022-05-07 17:15:27 -07:00
super(key, title, defaultVal, onSave);
2023-05-13 23:46:41 -07:00
let props: MinMax = {
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() {
2023-05-13 23:46:41 -07:00
let val = Number.parseFloat(super.getStorageValue() as unknown as string);
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 =
'<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>';
2022-05-07 17:15:27 -07:00
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);
});
}
/**
* executes the custom JavaScript set in the settings
*/
export function setCustomJS() {
let customJS = getStorageVal("customjs");
if(customJS != "") {
eval(customJS);
}
}
/**
* applies the custom CSS set in the settings
*/
export function setCustomCSS() {
let customCSS = getStorageVal("customcss");
if(customCSS != "") {
$("style#customCSS").remove();
$("<style/>").prop({
id: "customCSS"
}).html(customCSS)
.appendTo(document.head);
}
}
$(() => {
let styleOptions = [];
for(const style of styles) {
styleOptions.push({text: style.Name, val: style.Filename});
}
2023-05-13 23:46:41 -07:00
settings.set("style", new DropdownSetting<string>("style", "Style", styleOptions, defaultStyle, function() {
2022-05-07 17:15:27 -07:00
document.getElementById("theme").setAttribute("href",
`${webroot}css/${this.getElementValue()}`);
2023-05-13 23:46:41 -07:00
}) as Setting);
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
settings.set("customjs", new TextSetting("customjs", "Custom JavaScript (ran on page load)", ""));
settings.set("customcss", new TextSetting("customcss", "Custom CSS", "", setCustomCSS));
2022-08-05 23:38:42 +00:00
if($settingsButton === null)
$settingsButton = new TopBarButton("Settings", createLightbox, {
before: "a#watcher"
});
});