mirror of
https://github.com/Eggbertx/gochan.git
synced 2025-08-03 19:56:22 -07:00
Completely revamp settings, also use localStorage
This commit is contained in:
parent
00e6a3558a
commit
795361947e
3 changed files with 212 additions and 84 deletions
|
@ -1,114 +1,242 @@
|
||||||
import { LightBox, showLightBox } from "./lightbox";
|
import { LightBox, showLightBox } from "./lightbox";
|
||||||
import { TopBarButton } from "./topbar";
|
import { TopBarButton } from "./topbar";
|
||||||
import { getCookie, setCookie } from "./cookies";
|
import { getCookie, setCookie } from "./cookies";
|
||||||
export let $settingsMenu = null;
|
|
||||||
export let settings = [];
|
|
||||||
let settingsLB = null;
|
|
||||||
|
|
||||||
export function getSetting(id) {
|
const validTypes = ["text", "textarea", "password", "number", "checkbox", "select"];
|
||||||
for(let s = 0; s < settings.length; s++) {
|
const genericOptions = {
|
||||||
if(settings[s].id == id) return settings[s];
|
type: "text",
|
||||||
}
|
dropdownOptions: null, // [{val: "", text: ""}]
|
||||||
return {};
|
defaultVal: null,
|
||||||
|
onSave: () => {},
|
||||||
|
customProperties: {},
|
||||||
|
customCSS: {}
|
||||||
|
};
|
||||||
|
|
||||||
|
export let $settingsMenu = null;
|
||||||
|
let $settingsTable = null;
|
||||||
|
export let settings = [];
|
||||||
|
|
||||||
|
function getStorageVal(key, defaultVal) {
|
||||||
|
if(localStorage == undefined)
|
||||||
|
return getCookie(key, {default: defaultVal});
|
||||||
|
let val = localStorage.getItem(key);
|
||||||
|
if(val === null && defaultVal !== undefined)
|
||||||
|
return defaultVal;
|
||||||
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
class Setting {
|
function setStorageVal(key, val) {
|
||||||
constructor(id, text, type, defaultVal, cb, options) {
|
if(localStorage == undefined)
|
||||||
this.id = id;
|
setCookie(key, val);
|
||||||
this.text = text;
|
else
|
||||||
this.type = type; // text, textarea, checkbox, select
|
localStorage.setItem(key, val);
|
||||||
this.defaultVal = defaultVal;
|
}
|
||||||
this.options = [];
|
|
||||||
if(getCookie(this.id) == undefined) {
|
function genericDefaultVal(type, options = []) {
|
||||||
this.setCookie(this.defaultVal, 7);
|
switch(type) {
|
||||||
|
case "text":
|
||||||
|
case "textarea":
|
||||||
|
case "password":
|
||||||
|
return "";
|
||||||
|
case "number":
|
||||||
|
return 0;
|
||||||
|
case "checkbox":
|
||||||
|
return false;
|
||||||
|
case "select":
|
||||||
|
if(Array.isArray(options) && options.length > 0)
|
||||||
|
return options[0];
|
||||||
|
return "";
|
||||||
|
default:
|
||||||
|
return "";;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function fixOptions(options) {
|
||||||
|
let fixed = {}
|
||||||
|
|
||||||
|
if(validTypes.indexOf(options.type) > -1)
|
||||||
|
fixed.type = options.type;
|
||||||
|
else
|
||||||
|
fixed.type = validTypes[0];
|
||||||
|
|
||||||
|
fixed.defaultVal = options.defaultVal;
|
||||||
|
if(fixed.defaultVal == undefined)
|
||||||
|
fixed.defaultVal = genericDefaultVal(fixed, options.dropdownOptions);
|
||||||
|
|
||||||
|
if(options.hasOwnProperty("onSave"))
|
||||||
|
fixed.onSave = options.onSave;
|
||||||
|
else
|
||||||
|
fixed.onSave = () => {};
|
||||||
|
|
||||||
|
if(options.hasOwnProperty("customProperties"))
|
||||||
|
fixed.customProperties = options.customProperties;
|
||||||
|
else
|
||||||
|
fixed.customProperties = genericOptions.customProperties;
|
||||||
|
|
||||||
|
if(options.hasOwnProperty("dropdownOptions"))
|
||||||
|
fixed.dropdownOptions = options.dropdownOptions;
|
||||||
|
else
|
||||||
|
fixed.dropdownOptions = genericOptions.dropdownOptions;
|
||||||
|
|
||||||
|
if(options.hasOwnProperty("customCSS"))
|
||||||
|
fixed.customCSS = options.customCSS;
|
||||||
|
else
|
||||||
|
fixed.customCSS = genericOptions.customCSS;
|
||||||
|
|
||||||
|
return fixed;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export class Setting {
|
||||||
|
/**
|
||||||
|
* @param {string} key The name of the setting
|
||||||
|
* @param {string} title text that gets shown in the Settings lightbox
|
||||||
|
*/
|
||||||
|
constructor(key, title, options = genericOptions) {
|
||||||
|
this.key = key;
|
||||||
|
this.title = title;
|
||||||
|
|
||||||
|
let fixedOpts = fixOptions(options);
|
||||||
|
this.type = fixedOpts.type;
|
||||||
|
this.defaultVal = fixedOpts.defaultVal;
|
||||||
|
this.onSave = fixedOpts.onSave;
|
||||||
|
this.customProperties = fixedOpts.customProperties;
|
||||||
|
this.dropdownOptions = fixedOpts.dropdownOptions;
|
||||||
|
this.customCSS = fixedOpts.customCSS;
|
||||||
|
this.element = this.createElement();
|
||||||
|
}
|
||||||
|
saveElementValue() {
|
||||||
|
let val = this.element.val();
|
||||||
|
if(this.type == "checkbox") {
|
||||||
|
val = this.element.prop("checked");
|
||||||
}
|
}
|
||||||
if(this.type == "select") this.options = options;
|
console.log(this.key);
|
||||||
this.cb = () => {};
|
console.log(this.element[0]);
|
||||||
if(cb) this.cb = cb;
|
console.log(val);
|
||||||
|
setStorageVal(this.key, val);
|
||||||
}
|
}
|
||||||
|
setValue(newVal) {
|
||||||
save(newVal, expires) {
|
setStorageVal(this.key, newVal);
|
||||||
setCookie(this.id, newVal, expires);
|
|
||||||
this.cb();
|
|
||||||
}
|
}
|
||||||
|
getValue() {
|
||||||
getCookie(type = "string", defaultVal) {
|
return getStorageVal(this.key, this.defaultVal);
|
||||||
let val = getCookie(this.id, {type: type, default: defaultVal});
|
|
||||||
|
|
||||||
if(this.type == "checkbox") val = (val == "true");
|
|
||||||
return val;
|
|
||||||
}
|
}
|
||||||
|
createElement() {
|
||||||
setCookie(val,expires) {
|
let selector = "<input/>";
|
||||||
setCookie(this.id, val,expires);
|
let props = {
|
||||||
}
|
id: this.key,
|
||||||
|
name: this.key
|
||||||
getVal() {
|
}
|
||||||
let elem = document.getElementById(this.id);
|
let propKeys = Object.keys(this.customProperties);
|
||||||
if(elem != null) {
|
for(const key in propKeys) {
|
||||||
if(elem.type == "checkbox") return elem.checked;
|
props[key] = this.customProperties[key];
|
||||||
return elem.value;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
html() {
|
switch (this.type) {
|
||||||
let html = "";
|
case "text":
|
||||||
switch(this.type) {
|
props.type = "text";
|
||||||
case "checkbox":
|
if(this.defaultVal === null)
|
||||||
if(this.getCookie() == true)
|
this.defaultVal = "";
|
||||||
html = `<input id="${this.id}" type="checkbox" checked="checked" />`;
|
|
||||||
else
|
|
||||||
html = `<input id="${this.id}" type="checkbox" />`;
|
|
||||||
break;
|
|
||||||
case "select":
|
|
||||||
html = `<select id="${this.id}" name="${this.id}" style="min-width:50%">`;
|
|
||||||
for(const option of this.options) {
|
|
||||||
html += `<option value="${option.val}"`;
|
|
||||||
if(this.getCookie() == option.val) html += `selected="${this.getCookie()}"`;
|
|
||||||
html += `>${option.text}</option>`;
|
|
||||||
}
|
|
||||||
html += "</select>";
|
|
||||||
break;
|
break;
|
||||||
case "textarea":
|
case "textarea":
|
||||||
html = `<textarea id="${this.id}" name="${this.id}">${this.getCookie()}</textarea>`;
|
selector = "<textarea/>"
|
||||||
|
if(this.defaultVal === null)
|
||||||
|
this.defaultVal = "";
|
||||||
|
break;
|
||||||
|
case "number":
|
||||||
|
props.type = "number";
|
||||||
|
if(this.defaultVal === null)
|
||||||
|
this.defaultVal = 0;
|
||||||
|
break;
|
||||||
|
case "checkbox":
|
||||||
|
props.type = "checkbox";
|
||||||
|
if(this.defaultVal === null)
|
||||||
|
this.defaultVal = false;
|
||||||
|
break;
|
||||||
|
case "select":
|
||||||
|
if(this.dropdownOptions === null)
|
||||||
|
break;
|
||||||
|
selector = "<select/>";
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
html = `<input id="${this.id}" type="checkbox" val="${this.getCookie()}" />`;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return html;
|
let $elem = $(selector);
|
||||||
|
if(this.type == "select") {
|
||||||
|
for(const option of this.dropdownOptions) {
|
||||||
|
$("<option/>").val(option.val).text(option.text).appendTo($elem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$elem.prop(props);
|
||||||
|
if(Object.keys(this.customCSS).length > 0)
|
||||||
|
$elem.css(this.customCSS);
|
||||||
|
|
||||||
|
let val = this.getValue();
|
||||||
|
// console.log(this.key, "=>", val, "default:", this.defaultVal);
|
||||||
|
if(this.type == "checkbox") {
|
||||||
|
console.log(this.key, val, "checked:", val == "true");
|
||||||
|
$elem.prop({checked: val == "true" || val == true});
|
||||||
|
} else {
|
||||||
|
$elem.val(val);
|
||||||
|
}
|
||||||
|
return $elem;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function initSettings() {
|
export function initSettings() {
|
||||||
let settingsHTML =
|
let settingsHTML =
|
||||||
`<div id="settings-container" style="overflow:auto"><table width="100%"><colgroup><col span="1" width="50%"><col span="1" width="50%"></colgroup>`;
|
`<div id="settings-container" style="overflow:auto"><table width="100%"><colgroup><col span="1" width="50%"><col span="1" width="50%"></colgroup>`;
|
||||||
|
|
||||||
|
let styleOptions = [];
|
||||||
|
for(const style of styles) {
|
||||||
|
styleOptions.push({text: style.Name, val: style.Filename});
|
||||||
|
}
|
||||||
settings.push(
|
settings.push(
|
||||||
new Setting("style", "Style", "select", defaultStyle, function() {
|
new Setting("style", "Style", {
|
||||||
document.getElementById("theme").setAttribute("href",
|
type: "select",
|
||||||
`${webroot}css/${this.getCookie(defaultStyle)}`
|
dropdownOptions: styleOptions,
|
||||||
)
|
defaultVal: defaultStyle
|
||||||
}, []),
|
}),
|
||||||
new Setting("pintopbar", "Pin top bar", "checkbox", true),
|
new Setting("pintopbar", "Pin top bar", {
|
||||||
new Setting("enableposthover", "Preview post on hover", "checkbox", true),
|
type: "checkbox",
|
||||||
new Setting("enablepostclick", "Preview post on click", "checkbox", true),
|
defaultVal: true
|
||||||
new Setting("useqr", "Use Quick Reply box", "checkbox", true)
|
}),
|
||||||
|
new Setting("enableposthover", "Preview post on hover", {
|
||||||
|
type: "checkbox",
|
||||||
|
defaultVal: false
|
||||||
|
}),
|
||||||
|
new Setting("enablepostclick", "Preview post on click", {
|
||||||
|
type: "checkbox",
|
||||||
|
defaultVal: true
|
||||||
|
}),
|
||||||
|
new Setting("useqr", "Use Quick Reply box", {
|
||||||
|
type: "checkbox",
|
||||||
|
defaultVal: true
|
||||||
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
for(const style of styles) {
|
settingsHTML += `</table></div><div class="lightbox-footer"><hr /><button id="save-settings-button">Save Settings</button></div>`;
|
||||||
settings[0].options.push({text: style.Name, val: style.Filename});
|
|
||||||
}
|
|
||||||
for(const setting of settings) {
|
|
||||||
settingsHTML += `<tr><td><b>${setting.text}:</b></td><td>${setting.html()}</td></tr>`
|
|
||||||
}
|
|
||||||
settingsHTML += "</table></div><div class=\"lightbox-footer\"><hr /><button id=\"save-settings-button\">Save Settings</button></div>";
|
|
||||||
|
|
||||||
$settingsMenu = new TopBarButton("Settings", () => {
|
$settingsMenu = new TopBarButton("Settings", () => {
|
||||||
showLightBox("Settings", settingsHTML);
|
showLightBox("Settings", settingsHTML);
|
||||||
|
if($settingsTable === null) {
|
||||||
|
$settingsTable = $("#settings-container table");
|
||||||
|
for(const setting of settings) {
|
||||||
|
let $tr = $("<tr/>").appendTo($settingsTable);
|
||||||
|
$("<td/>").append($("<b/>").text(setting.title)).appendTo($tr);
|
||||||
|
$("<td/>").append(setting.element).appendTo($tr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$("#settings-container").find("input,select,textarea").on("change", function(e) {
|
||||||
|
let key = e.currentTarget.id;
|
||||||
|
let val = e.currentTarget.value;
|
||||||
|
let type = e.currentTarget.attributes.getNamedItem("type")
|
||||||
|
console.log(type);
|
||||||
|
console.log(key, "=>", val);
|
||||||
|
})
|
||||||
$("button#save-settings-button").on("click", () => {
|
$("button#save-settings-button").on("click", () => {
|
||||||
for(const setting of settings) {
|
for(const setting of settings) {
|
||||||
setting.save(setting.getVal());
|
setting.saveElementValue();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Add table
Add a link
Reference in a new issue