ws4kp-linhanced/server/scripts/modules/utils/setting.mjs

198 lines
5 KiB
JavaScript
Raw Normal View History

2024-04-19 21:05:52 -05:00
import { parseQueryString } from '../share.mjs';
const SETTINGS_KEY = 'Settings';
class Setting {
2024-07-29 23:12:47 -05:00
constructor(shortName, name, type, defaultValue, changeAction, sticky, values) {
// store values
this.shortName = shortName;
this.name = name;
this.defaultValue = defaultValue;
this.myValue = defaultValue;
2024-07-29 23:12:47 -05:00
this.type = type ?? 'checkbox';
this.sticky = sticky;
2024-07-29 23:12:47 -05:00
this.values = values;
// a default blank change function is provided
2024-04-19 21:05:52 -05:00
this.changeAction = changeAction ?? (() => { });
// get value from url
2024-07-29 23:12:47 -05:00
const urlValue = parseQueryString()?.[`settings-${shortName}-${type}`];
2024-04-19 21:05:52 -05:00
let urlState;
2024-07-29 23:12:47 -05:00
if (type === 'checkbox' && urlValue !== undefined) {
2024-04-19 21:05:52 -05:00
urlState = urlValue === 'true';
}
2024-07-29 23:12:47 -05:00
if (type === 'select' && urlValue !== undefined) {
urlState = parseFloat(urlValue);
}
2025-02-23 23:29:39 -06:00
if (type === 'select' && urlValue !== undefined && Number.isNaN(urlState)) {
// couldn't parse as a float, store as a string
urlState = urlValue;
}
// get existing value if present
2024-04-19 21:05:52 -05:00
const storedValue = urlState ?? this.getFromLocalStorage();
if (sticky && storedValue !== null) {
this.myValue = storedValue;
}
// call the change function on startup
2024-07-29 23:12:47 -05:00
switch (type) {
2024-10-21 19:21:05 -05:00
case 'select':
this.selectChange({ target: { value: this.myValue } });
break;
case 'checkbox':
default:
this.checkboxChange({ target: { checked: this.myValue } });
2024-07-29 23:12:47 -05:00
}
}
generateSelect() {
// create a radio button set in the selected displays area
const label = document.createElement('label');
label.for = `settings-${this.shortName}-select`;
label.id = `settings-${this.shortName}-label`;
const span = document.createElement('span');
span.innerHTML = `${this.name} `;
label.append(span);
const select = document.createElement('select');
select.id = `settings-${this.shortName}-select`;
select.name = `settings-${this.shortName}-select`;
select.addEventListener('change', (e) => this.selectChange(e));
this.values.forEach(([value, text]) => {
const option = document.createElement('option');
2025-02-23 23:29:39 -06:00
if (typeof value === 'number') {
option.value = value.toFixed(2);
} else {
option.value = value;
}
2024-07-29 23:12:47 -05:00
option.innerHTML = text;
select.append(option);
});
label.append(select);
this.element = label;
// set the initial value
this.selectHighlight(this.myValue);
return label;
}
generateCheckbox() {
// create a checkbox in the selected displays area
const label = document.createElement('label');
label.for = `settings-${this.shortName}-checkbox`;
label.id = `settings-${this.shortName}-label`;
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.value = true;
checkbox.id = `settings-${this.shortName}-checkbox`;
checkbox.name = `settings-${this.shortName}-checkbox`;
checkbox.checked = this.myValue;
checkbox.addEventListener('change', (e) => this.checkboxChange(e));
const span = document.createElement('span');
span.innerHTML = this.name;
label.append(checkbox, span);
2024-07-29 23:12:47 -05:00
this.element = label;
return label;
}
checkboxChange(e) {
// update the state
this.myValue = e.target.checked;
this.storeToLocalStorage(this.myValue);
// call change action
this.changeAction(this.myValue);
}
2024-07-29 23:12:47 -05:00
selectChange(e) {
// update the value
this.myValue = parseFloat(e.target.value);
2025-02-23 23:29:39 -06:00
if (Number.isNaN(this.myValue)) {
// was a string, store as such
this.myValue = e.target.value;
}
2024-07-29 23:12:47 -05:00
this.storeToLocalStorage(this.myValue);
// call the change action
this.changeAction(this.myValue);
}
storeToLocalStorage(value) {
if (!this.sticky) return;
const allSettingsString = localStorage?.getItem(SETTINGS_KEY) ?? '{}';
const allSettings = JSON.parse(allSettingsString);
allSettings[this.shortName] = value;
localStorage?.setItem(SETTINGS_KEY, JSON.stringify(allSettings));
}
getFromLocalStorage() {
const allSettings = localStorage?.getItem(SETTINGS_KEY);
try {
if (allSettings) {
const storedValue = JSON.parse(allSettings)?.[this.shortName];
if (storedValue !== undefined) {
switch (this.type) {
2024-10-21 19:21:05 -05:00
case 'boolean':
return storedValue;
case 'select':
return storedValue;
default:
return null;
}
}
}
} catch {
return null;
}
return null;
}
get value() {
return this.myValue;
}
set value(newValue) {
// update the state
this.myValue = newValue;
2024-07-29 23:12:47 -05:00
switch (this.type) {
2024-10-21 19:21:05 -05:00
case 'select':
this.selectHighlight(newValue);
break;
case 'checkbox':
default:
this.element.checked = newValue;
2024-07-29 23:12:47 -05:00
}
this.storeToLocalStorage(this.myValue);
// call change action
this.changeAction(this.myValue);
}
2024-07-29 23:12:47 -05:00
selectHighlight(newValue) {
// set the dropdown to the provided value
this.element.querySelectorAll('option').forEach((elem) => {
2025-02-23 23:29:39 -06:00
elem.selected = (newValue?.toFixed?.(2) === elem.value) || (newValue === elem.value);
2024-07-29 23:12:47 -05:00
});
}
generate() {
switch (this.type) {
2024-10-21 19:21:05 -05:00
case 'select':
return this.generateSelect();
case 'checkbox':
default:
return this.generateCheckbox();
2024-07-29 23:12:47 -05:00
}
}
}
export default Setting;