| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- import { types } from "assets/types.js";
- class option {
- #name;
- #options;
- #default;
- constructor(name) {
- types.check_string(name);
- name = name.trim();
- if (name.length === 0) {
- throw new TypeError("Name can not be blank.");
- }
- this.#name = name;
- this.#default = undefined;
- this.#options = new Set();
- }
- #sanitize_option(option) {
- types.check_string(option);
- option = option.trim();
- if (option.length === 0) {
- throw new TypeError("Option could not be blank.");
- }
- return option;
- }
- set _default(option) {
- option = this.#sanitize_option(option);
-
- this.#default = option;
- this.#options.add(option);
- }
- _add_option(option) {
- this.#options.add(this.#sanitize_option(option));
- }
- get options() {
- return Array.from(this.#options.values());
- }
- get default() {
- if (this.#default === undefined) {
- throw new ReferenceError("Default option is not setup yet.");
- }
- return this.#default;
- }
- select(option) {
- if (!this.#options.has(option)) {
- throw new Error("Setting has not option \"" + option + "\".");
- }
- if (this.default === option) {
- localStorage.removeItem(this.#name);
- return;
- }
- localStorage.setItem(this.#name, option);
- }
-
- get selected() {
- const read = localStorage.getItem(this.#name);
- if (read === null) {
- return this.default;
- }
- if (!this.#options.has(read)) {
- return this.default;
- }
- return read;
- }
- }
- export { option };
|