-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings-template-manager.js
207 lines (169 loc) · 6.3 KB
/
settings-template-manager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
* @fileoverview settings-template-manager.js - The settings template manager.
* @author Caspar Neervoort "UPLYNXED"
* @license MIT
* @version 0.0.0.1
*
* @note This file is far from finished, it's a mess right now while I still work out how I want to handle the templates.
*/
// UNFINISHED CODE - DO NOT USE
// This file contains configurable templates for the SettingsManager class.
// The templates are used to generate the HTML for the settings UI.
// The templates are created using a template class called SettingsTemplate.
// The templates are managaed by the SettingsTemplateManager class.
/**
* @class SettingsTemplateManager
* @classdesc The SettingsTemplateManager class is used to manage the templates used to generate the settings UI.
* @note The SettingsTemplateManager class is not meant to be instantiated directly.
*
* @param {SettingsManager} settingsManager - The settings manager instance that is using the templates.
* @param {Object} templates - The templates to use for the settings UI.
* @param {Object} templates.container - The container template to insert the settings UI into if specified, before it is inserted into the DOM.
* @param {Object} templates.section - The section template to use for settings sections. Settings will be sorted into sections if specified on the setting's object.
* @param {Object} templates.setting - The setting template to use for individual settings.
* @param {Object} templates.settingButton - The button template to use for buttons for each individual setting.
* @param {Object} templates.settingSubmenu - The submenu template to use for submenus for each individual setting.
* @param {Object} templates.settingOption - The option template to use for each individual option in a submenu for each individual setting.
*
* @example
* // Create a new SettingsTemplateManager instance.
* var settingsTemplateManager = new SettingsTemplateManager(settingsManager, templateList);
*/
class SettingsTemplateManager {
#templates = {};
/**
* @constructor
* @param {SettingsManager} settingsManager - The settings manager instance that is using the templates.
* @param {Object} templates - The templates to use for the settings UI.
* @param {Object} templates.container - The container template to insert the settings UI into if specified, before it is inserted into the DOM.
* @param {Object} templates.section - The section template to use for settings sections. Settings will be sorted into sections if specified on the setting's object.
* @param {Object} templates.setting - The setting template to use for individual settings.
* @param {Object} templates.settingButton - The button template to use for buttons for each individual setting.
* @param {Object} templates.settingSubmenu - The submenu template to use for submenus for each individual setting.
* @param {Object} templates.settingOption - The option template to use for each individual option in a submenu for each individual setting.
*
* @returns {SettingsTemplateManager} The SettingsTemplateManager instance.
*/
constructor(settingsManager, templates = {}) {
this.settingsManager = settingsManager;
this.#templates = templates;
return this;
}
init() {
this.#templates.container = this.#templates.container || {};
this.#templates.section = this.#templates.section || {};
this.#templates.setting = this.#templates.setting || {};
this.#templates.settingButton = this.#templates.settingButton || {};
this.#templates.settingSubmenu = this.#templates.settingSubmenu || {};
this.#templates.settingOption = this.#templates.settingOption || {};
}
getTemplate(templateName) {
return this.#templates[templateName];
}
setTemplate(templateName, template) {
this.#templates[templateName] = template;
}
getTemplates() {
return this.#templates;
}
setTemplates(templates) {
this.#templates = templates;
}
getSettingsManager() {
return this.settingsManager;
}
setSettingsManager(settingsManager) {
this.settingsManager = settingsManager;
}
// TODO: Implement tagged templates for this class.
/**
* @function generateHTML - Generates the HTML for an individual setting using a given template from the templatesList object.
* @param {Object} setting - The setting to generate the HTML for.
* @param {String} templateName - The name of the template to use for generating the HTML.
* @param {Object} [template] - The template to use for generating the HTML.
*
* @returns {String} The HTML for the setting.
*/
generateHTML(setting, templateName, template = {}) {
if (!templateName) {
throw new Error('No template name specified.');
}
if (!template) {
template = this.getTemplate(templateName);
if (!template) {
throw new Error(`No template found for ${templateName}.`);
}
}
let html = '';
let tmpl = {};
if(template.properties['setting']) {
tmpl.setting = this.settingsManager.getSetting(setting.name); // TODO: setting.name is not the correct property to use here.
}
if(template.properties['option']) {
tmpl.option = this.settingsManager.getOption(setting.name, setting.option); // TODO: setting.name is not the correct property to use here. setting.option is not the correct property to use here.
}
html = template.html;
}
}
/**
* @class SettingsTemplate
* @classdesc A class that represents a template for the settings UI.
*
*
*/
class SettingsTemplate {
constructor(){}
/**
* @method SettingsTemplate.getName
* @returns {string} The name of the template.
*/
}
var templateList = {
container: {
properties: [
],
html: `
<div class="menu">
${tmpl.content.content}
</div>
`
},
section: {
html: undefined
},
setting: {
html: undefined
},
settingButton: {
properties: [
"setting",
],
html: `
<button class="TopButton" name="${(tmpl.setting.d.type !== 'cycle') ? setting.s + "Top" : setting.s}">
<label>${tmpl.setting.name}</label>
<span>${tmpl.setting.value}</span>
</button>
`
},
settingSubmenu: {
properties: [
"setting",
],
html: `
<div class="SubMenu" id="${tmpl.setting.s}" tabindex="-1">
${tmpl.content.options}
</div>
`
},
settingOption: {
properties: [
"setting",
"option",
],
html: `
<button class="SubButton" name="${tmpl.setting.s}" value="${tmpl.option.value}">
<b>${tmpl.option.name}</b>
</button>
`
}
}