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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
/* jshint esversion: 2024, module: true */
/**
* Structured Editor for systemd-networkd configuration
* @class StructuredEditor
*/
class StructuredEditor {
constructor(container) {
this.container = container;
this.schema = null;
this.currentFile = "";
}
/**
* Load configuration schema
* @param {Object} schema - Configuration schema from NetworkConfiguration.getSchema()
* @param {string} filename - File name
*/
loadSchema(schema, filename) {
if (!this.container) {
console.error("Structured editor container not found");
return;
}
try {
this.schema = schema;
this.currentFile = filename;
this.render();
} catch (error) {
console.error("Error loading schema:", error);
this.showError("Failed to load configuration schema: " + error.message);
}
}
/**
* Render the structured editor
*/
render() {
if (!this.container) {
console.error("Cannot render: container is null");
return;
}
if (!this.schema) {
this.container.innerHTML =
'<div class="error-message">No configuration schema loaded</div>';
return;
}
try {
this.container.innerHTML = this._createEditorHTML();
this._attachEventListeners();
} catch (error) {
console.error("Error rendering structured editor:", error);
this.showError("Failed to render editor: " + error.message);
}
}
/**
* Create editor HTML structure from schema
* @private
* @returns {string}
*/
_createEditorHTML() {
return `
<div class="structured-editor">
<div class="editor-sections">
${this._createSection("Match", this.schema.Match)}
${this._createSection("Link", this.schema.Link)}
${this._createSection("Network", this.schema.Network)}
${this._createSection("DHCP", this.schema.DHCP)}
${this._createArraySection("Address", this.schema.Address)}
${this._createArraySection("Route", this.schema.Route)}
</div>
</div>
`;
}
/**
* Create a regular section
* @private
* @param {string} sectionName - Section name
* @param {Object} sectionSchema - Section schema
* @returns {string}
*/
_createSection(sectionName, sectionSchema) {
if (!this._hasSectionValues(sectionSchema)) {
return "";
}
const fieldsHTML = Object.entries(sectionSchema)
.map(([key, field]) => this._createFieldInput(key, field))
.join("");
return `
<div class="config-section">
<h4>[${sectionName}]</h4>
<div class="config-table">
${fieldsHTML}
</div>
</div>
`;
}
/**
* Create an array section (Address, Route)
* @private
* @param {string} sectionName - Section name
* @param {Object} arraySchema - Array section schema
* @returns {string}
*/
_createArraySection(sectionName, arraySchema) {
if (!arraySchema.items || arraySchema.items.length === 0) {
return `
<div class="config-section">
<h4>[${sectionName}]</h4>
<p class="no-items">No ${sectionName.toLowerCase()} sections</p>
<button class="button small" data-add-section="${sectionName}">
Add ${sectionName} Section
</button>
</div>
`;
}
const sectionsHTML = arraySchema.items
.map((itemSchema, index) => {
const fieldsHTML = Object.entries(itemSchema)
.map(([key, field]) =>
this._createFieldInput(key, field, sectionName, index),
)
.join("");
return `
<div class="config-section">
<h4>[${sectionName}] ${index > 0 ? `#${index + 1}` : ""}</h4>
<div class="config-table">
${fieldsHTML}
<button class="button small warning remove-section"
data-section="${sectionName}" data-index="${index}">
Remove
</button>
</div>
</div>
`;
})
.join("");
return `
<div class="array-section">
${sectionsHTML}
<div class="config-section">
<button class="button small" data-add-section="${sectionName}">
Add Another ${sectionName} Section
</button>
</div>
</div>
`;
}
/**
* Create field input based on field schema
* @private
* @param {string} key - Field key
* @param {Object} field - Field schema
* @param {string} sectionName - Section name (for array sections)
* @param {number} index - Item index (for array sections)
* @returns {string}
*/
_createFieldInput(key, field, sectionName = null, index = null) {
const dataAttributes = [];
if (sectionName) {
dataAttributes.push(`data-section="${sectionName}"`);
dataAttributes.push(`data-index="${index}"`);
}
dataAttributes.push(`data-key="${key}"`);
const inputId = sectionName ? `${sectionName}-${index}-${key}` : key;
if (field.options?.enum) {
// Create select for enum fields
const optionsHTML = field.options.enum
.map(
(opt) =>
`<option value="${opt}" ${opt === field.value ? "selected" : ""}>${opt || "(not set)"}</option>`,
)
.join("");
return `
<div class="config-row">
<label class="config-label" for="${inputId}" title="${field.description}">
<abbr title="${field.description}">${key}</abbr>:
</label>
<select id="${inputId}" class="config-select" ${dataAttributes.join(" ")}>
${optionsHTML}
</select>
</div>
`;
} else {
// Create text input for other fields
return `
<div class="config-row">
<label class="config-label" for="${inputId}" title="${field.description}">
<abbr title="${field.description}">${key}</abbr>:
</label>
<input type="text"
id="${inputId}"
class="config-input"
${dataAttributes.join(" ")}
value="${field.value || ""}"
placeholder="${field.description}">
</div>
`;
}
}
/**
* Check if section has any values
* @private
* @param {Object} sectionSchema - Section schema
* @returns {boolean}
*/
_hasSectionValues(sectionSchema) {
return Object.values(sectionSchema).some(
(field) =>
field.value !== null && field.value !== undefined && field.value !== "",
);
}
/**
* Attach event listeners to the editor
* @private
*/
_attachEventListeners() {
// Input changes
this.container.querySelectorAll(".config-input").forEach((input) => {
input.addEventListener("input", (e) => this._onInputChange(e));
});
// Select changes
this.container.querySelectorAll(".config-select").forEach((select) => {
select.addEventListener("change", (e) => this._onSelectChange(e));
});
// Add section buttons
this.container.querySelectorAll("[data-add-section]").forEach((btn) => {
btn.addEventListener("click", (e) => this._onAddSection(e));
});
// Remove section buttons
this.container.querySelectorAll(".remove-section").forEach((btn) => {
btn.addEventListener("click", (e) => this._onRemoveSection(e));
});
}
/**
* Handle input changes
* @private
* @param {Event} event
*/
_onInputChange(event) {
const input = event.target;
this._updateFieldValue(input);
}
/**
* Handle select changes
* @private
* @param {Event} event
*/
_onSelectChange(event) {
const select = event.target;
this._updateFieldValue(select);
}
/**
* Update field value in schema
* @private
* @param {HTMLElement} element - Input or select element
*/
_updateFieldValue(element) {
const section = element.dataset.section;
const index = element.dataset.index
? parseInt(element.dataset.index)
: null;
const key = element.dataset.key;
const value = element.value;
if (!section) {
// Regular section (Match, Link, Network, DHCP)
if (this.schema[section] && this.schema[section][key]) {
this.schema[section][key].value = value || null;
}
} else if (
index !== null &&
this.schema[section] &&
this.schema[section].items
) {
// Array section item
if (
this.schema[section].items[index] &&
this.schema[section].items[index][key]
) {
this.schema[section].items[index][key].value = value || null;
}
}
}
/**
* Handle add section
* @private
* @param {Event} event
*/
_onAddSection(event) {
const sectionName = event.target.dataset.addSection;
this.emit("addSection", { section: sectionName });
}
/**
* Handle remove section
* @private
* @param {Event} event
*/
_onRemoveSection(event) {
const section = event.target.dataset.section;
const index = parseInt(event.target.dataset.index);
this.emit("removeSection", { section, index });
}
/**
* Show error message
* @param {string} message - Error message
*/
showError(message) {
if (this.container) {
this.container.innerHTML = `<div class="error-message">${message}</div>`;
}
}
/**
* Emit custom event
* @param {string} eventName - Event name
* @param {Object} detail - Event detail
*/
emit(eventName, detail) {
const event = new CustomEvent(eventName, { detail });
this.container.dispatchEvent(event);
}
/**
* Add event listener
* @param {string} eventName - Event name
* @param {Function} callback - Event callback
*/
on(eventName, callback) {
this.container.addEventListener(eventName, callback);
}
/**
* Get current schema
* @returns {Object|null}
*/
getSchema() {
return this.schema;
}
}
export { StructuredEditor };
|