|
| 1 | +import {GenericModal} from './dialogs.js'; |
| 2 | + |
| 3 | +class SettingsDialog extends GenericModal { |
| 4 | + constructor(modalId, settingsData) { |
| 5 | + super(modalId); |
| 6 | + this._settingsData = settingsData; |
| 7 | + this._settings = {} |
| 8 | + } |
| 9 | + |
| 10 | + async open(settings) { |
| 11 | + let p = super.open(); |
| 12 | + const cancelButton = this._currentModal.querySelector("button.cancel-button"); |
| 13 | + this._addDialogElement('cancelButton', cancelButton, 'click', this._closeModal); |
| 14 | + const okButton = this._currentModal.querySelector("button.ok-button"); |
| 15 | + this._addDialogElement('okButton', okButton, 'click', this._handleOkButton); |
| 16 | + |
| 17 | + const contentDiv = this._currentModal.querySelector("#settings-content"); |
| 18 | + contentDiv.innerHTML = ''; |
| 19 | + |
| 20 | + for (const setting of this._settingsData) { |
| 21 | + const label = document.createElement('label'); |
| 22 | + label.textContent = setting.label; |
| 23 | + if (setting.icon) { |
| 24 | + const icon = document.createElement('i'); |
| 25 | + icon.className = `fa-solid fa-${setting.icon} setting-item-icon`; |
| 26 | + label.prepend(icon); |
| 27 | + } |
| 28 | + label.htmlFor = `setting-${setting.key}`; |
| 29 | + contentDiv.appendChild(label); |
| 30 | + |
| 31 | + const control = await this._createControl(setting); |
| 32 | + control.value = settings[setting.key]; |
| 33 | + contentDiv.appendChild(control); |
| 34 | + } |
| 35 | + |
| 36 | + return p; |
| 37 | + } |
| 38 | + |
| 39 | + async _handleOkButton() { |
| 40 | + let settings = {} |
| 41 | + for (const setting of this._settingsData) { |
| 42 | + const control = this._currentModal.querySelector(`#setting-${setting.key}`); |
| 43 | + settings[setting.key] = control.value; |
| 44 | + } |
| 45 | + this._returnValue(settings); |
| 46 | + } |
| 47 | + |
| 48 | + async _createControl(settingData) { |
| 49 | + // Return the created control |
| 50 | + let control; |
| 51 | + if (settingData.type === 'select') { |
| 52 | + control = document.createElement('select'); |
| 53 | + for (const optionValue of settingData.options) { |
| 54 | + const option = document.createElement('option'); |
| 55 | + option.value = optionValue; |
| 56 | + option.textContent = optionValue.charAt(0).toUpperCase() + optionValue.slice(1); |
| 57 | + control.appendChild(option); |
| 58 | + } |
| 59 | + } |
| 60 | + control.id = `setting-${settingData.key}`; |
| 61 | + |
| 62 | + // this will also call this._addDialogElement to add event listeners as needed |
| 63 | + this._addDialogElement(`setting-${settingData.key}`, control); |
| 64 | + return control; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +class Settings { |
| 69 | + // This is a class that handles loading/saving settings as well as providing a settings dialog |
| 70 | + constructor() { |
| 71 | + // This will hold the layout/save data for the settings |
| 72 | + this._settingsData = [ |
| 73 | + { key: 'theme', type: 'select', label: 'Editor Theme', icon: 'palette', options: ['dark', 'light'], default: 'dark' } |
| 74 | + ]; |
| 75 | + this._settings = {}; |
| 76 | + this._loadSettings(); |
| 77 | + |
| 78 | + this._settingsDialog = new SettingsDialog('settings', this._settingsData); |
| 79 | + } |
| 80 | + |
| 81 | + _loadSettings() { |
| 82 | + // Load all saved settings or defaults |
| 83 | + for (const setting of this._settingsData) { |
| 84 | + this._settings[setting.key] = this._loadSetting(setting.key, setting.default); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + _saveSettings() { |
| 89 | + // Save all settings |
| 90 | + for (const key in this._settings) { |
| 91 | + this._saveSetting(key, this._settings[key]); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + _loadSetting(setting, defaultValue) { |
| 96 | + let value = JSON.parse(window.localStorage.getItem(setting)); |
| 97 | + if (value == null) { |
| 98 | + return defaultValue; |
| 99 | + } |
| 100 | + |
| 101 | + return value; |
| 102 | + } |
| 103 | + |
| 104 | + _saveSetting(setting, value) { |
| 105 | + window.localStorage.setItem(setting, JSON.stringify(value)); |
| 106 | + } |
| 107 | + |
| 108 | + getSetting(key) { |
| 109 | + return this._settings[key]; |
| 110 | + } |
| 111 | + |
| 112 | + async showDialog() { |
| 113 | + this._settings = await this._settingsDialog.open(this._settings); |
| 114 | + if (this._settings) { |
| 115 | + this._saveSettings(); |
| 116 | + return true; |
| 117 | + } |
| 118 | + return false; |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | + |
| 123 | +export { |
| 124 | + Settings |
| 125 | +}; |
0 commit comments