apple2js/js/options.ts

95 lines
2.6 KiB
TypeScript
Raw Normal View History

import Prefs from './prefs';
export const BOOLEAN_OPTION = 'BOOLEAN_OPTION';
export const SELECT_OPTION = 'SELECT_OPTION';
export interface Option {
2022-05-10 15:04:20 +00:00
name: string;
label: string;
type: string;
defaultVal: string | boolean;
}
export interface BooleanOption extends Option {
2022-05-10 15:04:20 +00:00
type: typeof BOOLEAN_OPTION;
defaultVal: boolean;
}
export interface SelectOption extends Option {
2022-05-10 15:04:20 +00:00
type: typeof SELECT_OPTION;
defaultVal: string;
values: Array<{name: string; value: string}>;
}
export interface OptionSection {
2022-05-10 15:04:20 +00:00
name: string;
options: Option[];
}
export interface OptionHandler {
2022-05-10 15:04:20 +00:00
getOptions: () => OptionSection[];
setOption: (name: string, value: string | boolean) => void;
}
export class Options {
private prefs: Prefs = new Prefs();
private options: Record<string, Option> = {};
private handlers: Record<string, OptionHandler> = {};
private sections: OptionSection[] = [];
addOptions(handler: OptionHandler) {
const sections = handler.getOptions();
for (const section of sections) {
const { options } = section;
for (const option of options) {
const { name } = option;
this.handlers[name] = handler;
this.options[name] = option;
const value = this.getOption(name);
if (value != null) {
handler.setOption(name, value);
}
}
this.sections.push(section);
}
}
getOption(name: string): string | boolean | undefined {
const option = this.options[name];
if (option) {
const { name, defaultVal, type } = option;
const stringVal = String(defaultVal);
const prefVal = this.prefs.readPref(name, stringVal);
switch (type) {
case BOOLEAN_OPTION:
return prefVal === 'true';
default:
return prefVal;
}
}
}
setOption(name: string, value: string | boolean) {
if (name in this.options) {
const handler = this.handlers[name];
const option = this.options[name];
this.prefs.writePref(name, String(value));
switch (option.type) {
case BOOLEAN_OPTION:
handler.setOption(name, Boolean(value));
break;
default:
handler.setOption(name, String(value));
}
}
}
getOptions() {
return this.options;
}
getSections() {
return this.sections;
}
}