forked from OpenEnergyTools/oscd-template-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoscd-editor-template-generator.ts
More file actions
403 lines (345 loc) · 10.9 KB
/
oscd-editor-template-generator.ts
File metadata and controls
403 lines (345 loc) · 10.9 KB
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import { css, html, LitElement } from 'lit';
import { ScopedElementsMixin } from '@open-wc/scoped-elements/lit-element.js';
import {
insertSelectedLNodeType,
nsdToJson,
LNodeDescription,
} from '@openenergytools/scl-lib';
import { TreeGrid, TreeSelection } from '@openenergytools/tree-grid';
import { MdFab } from '@scopedelement/material-web/fab/MdFab.js';
import { MdIcon } from '@scopedelement/material-web/icon/MdIcon.js';
import { MdFilledSelect } from '@scopedelement/material-web/select/MdFilledSelect.js';
import { MdSelectOption } from '@scopedelement/material-web/select/MdSelectOption.js';
import { MdFilledSelect as MdOutlinedSelect } from '@scopedelement/material-web/select/MdOutlineSelect.js';
import { MdOutlinedTextField } from '@scopedelement/material-web/textfield/MdOutlinedTextField.js';
import { MdOutlinedButton } from '@scopedelement/material-web/button/outlined-button.js';
import { MdDialog } from '@scopedelement/material-web/dialog/dialog.js';
import { CdcChildren } from '@openenergytools/scl-lib/dist/tDataTypeTemplates/nsdToJson.js';
import { property, query, state } from 'lit/decorators.js';
import { Snackbar } from './components/snackbar.js';
import { CreateDataObjectDialog } from './components/create-do-dialog.js';
import { DescriptionDialog } from './components/description-dialog.js';
import { PreviewDialog } from './components/preview-dialog.js';
import { cdClasses, lnClass74 } from './constants.js';
import { NodeData, getSelectionByPath, processEnums } from './foundation.js';
import { newEditEventV2 } from '@omicronenergy/oscd-api/utils.js';
let lastLNodeType = 'LPHD';
let lastSelection = {};
let lastFilter = '';
export default class TemplateGenerator extends ScopedElementsMixin(LitElement) {
static scopedElements = {
'tree-grid': TreeGrid,
'md-filled-select': MdFilledSelect,
'md-select-option': MdSelectOption,
'md-outlined-select': MdOutlinedSelect,
'md-fab': MdFab,
'md-icon': MdIcon,
'md-outlined-button': MdOutlinedButton,
'md-dialog': MdDialog,
'md-outlined-text-field': MdOutlinedTextField,
'oscd-snackbar': Snackbar,
'create-data-object-dialog': CreateDataObjectDialog,
'description-dialog': DescriptionDialog,
'preview-dialog': PreviewDialog,
};
@property({ attribute: false })
doc?: XMLDocument;
@query('tree-grid')
treeUI!: TreeGrid;
@query('md-filled-select')
lNodeTypeUI?: MdFilledSelect;
@query('create-data-object-dialog')
createDOdialog!: CreateDataObjectDialog;
@query('description-dialog')
descriptionDialog!: DescriptionDialog;
@query('preview-dialog')
previewDialog!: PreviewDialog;
@state()
get selection(): TreeSelection {
if (!this.treeUI) {
return {};
}
return this.treeUI.selection;
}
set selection(selection: TreeSelection) {
this.treeUI.selection = selection;
}
@state()
get filter(): string {
if (!this.treeUI) {
return '';
}
return this.treeUI.filter ?? '';
}
set filter(filter: string) {
this.treeUI.filter = filter;
}
@state()
get lNodeType(): string {
return this.lNodeTypeUI?.value || lastLNodeType;
}
set lNodeType(lNodeType: string) {
if (!this.lNodeTypeUI) {
return;
}
this.lNodeTypeUI.value = lNodeType;
if (!this.lNodeTypeUI.value) {
this.lNodeTypeUI.value = lastLNodeType;
}
}
@state()
addedLNode = '';
@state()
snackbarMessage = '';
@state()
snackbarType: 'success' | 'error' = 'success';
disconnectedCallback() {
super.disconnectedCallback();
lastSelection = this.selection;
lastFilter = this.filter;
lastLNodeType = this.lNodeType;
}
async firstUpdated() {
await this.treeUI.updateComplete;
await this.lNodeTypeUI!.updateComplete;
this.treeUI.tree = nsdToJson(lastLNodeType) as Record<string, NodeData>;
this.lNodeType = lastLNodeType;
this.filter = lastFilter;
this.selection = lastSelection;
await this.treeUI.updateComplete;
this.autoSelectEnums();
}
saveTemplates(description: string) {
if (!this.doc) {
return;
}
const inserts = insertSelectedLNodeType(this.doc, this.treeUI.selection, {
class: this.lNodeType,
...(description !== undefined && { desc: description }),
data: this.treeUI.tree as LNodeDescription,
});
const newLNodeType = inserts.find(
insert => (insert.node as Element).tagName === 'LNodeType',
)?.node as Element;
if (newLNodeType) {
this.addedLNode = newLNodeType.getAttribute('id') ?? '';
}
this.dispatchEvent(newEditEventV2(inserts));
}
async reset() {
this.addedLNode = '';
this.treeUI.tree = nsdToJson(this.lNodeType) as Record<string, NodeData>;
this.selection = {};
this.filter = '';
this.requestUpdate();
this.treeUI.requestUpdate();
await this.treeUI.updateComplete;
this.autoSelectEnums();
}
private handleDOConfirm = (
cdcType: string,
doName: string,
namespace: string | null,
) => {
if (!cdcType || !doName) {
return;
}
try {
this.createDataObject(
cdcType as (typeof cdClasses)[number],
doName,
namespace,
);
this.showNotification(
`Data Object '${doName}' created successfully.`,
'success',
);
} catch {
this.showNotification(
'Failed to create Data Object. Please try again.',
'error',
);
}
};
private createDataObject(
cdcType: (typeof cdClasses)[number],
doName: string,
namespace: string | null,
): void {
let cdcChildren = nsdToJson(cdcType) as CdcChildren;
if (namespace) {
cdcChildren = {
...cdcChildren,
dataNs: {
...cdcChildren?.dataNs,
mandatory: true,
val: namespace,
},
};
}
const cdcDescription = {
tagName: 'DataObject',
type: cdcType,
descID: '',
presCond: 'O',
children: cdcChildren,
};
Object.assign(this.treeUI.tree, {
[doName]: cdcDescription,
});
this.treeUI.requestUpdate();
}
showNotification(message: string, type: 'success' | 'error'): void {
this.snackbarMessage = '';
setTimeout(() => {
this.snackbarMessage = message;
this.snackbarType = type;
}, 0);
}
private showPreview(): void {
this.previewDialog.selection = this.treeUI.selection;
this.previewDialog.show();
}
private updateSelectionAtPath(
selection: TreeSelection,
path: string[],
newSelection: TreeSelection,
): TreeSelection {
if (path.length === 0) {
return newSelection;
}
const [currentKey, ...remainingPath] = path;
return {
...selection,
[currentKey]: this.updateSelectionAtPath(
selection[currentKey] || {},
remainingPath,
newSelection,
),
};
}
private handleNodeSelected = (event: CustomEvent) => {
const { node, path } = event.detail;
const currentSelectionAtPath = getSelectionByPath(
this.treeUI.selection,
path,
);
const selectionWithEnums = processEnums(
currentSelectionAtPath,
node as NodeData,
);
this.treeUI.selection = this.updateSelectionAtPath(
this.treeUI.selection,
path,
selectionWithEnums,
);
this.treeUI.requestUpdate();
};
private autoSelectEnums(): void {
const tree = this.treeUI.tree as Record<string, NodeData>;
const newSelection = { ...this.treeUI.selection };
for (const [key, dataObject] of Object.entries(tree)) {
if (newSelection[key]) {
newSelection[key] = processEnums(newSelection[key], dataObject);
}
}
this.treeUI.selection = newSelection;
this.treeUI.requestUpdate();
}
render() {
return html`<div class="container">
<div class="btn-wrapper">
<md-outlined-button @click=${() => this.createDOdialog.show()}>
<md-icon slot="icon">add</md-icon>
Add Data Object
</md-outlined-button>
<md-filled-select @input=${this.reset}>
${lnClass74.map(
lNodeType =>
html`<md-select-option value=${lNodeType}
>${lNodeType}</md-select-option
>`,
)}
</md-filled-select>
</div>
<tree-grid @node-selected=${this.handleNodeSelected}></tree-grid>
</div>
${this.doc
? html`<div class="fab-wrapper">
<md-fab @click=${() => this.showPreview()} title="Preview">
<md-icon slot="icon">preview</md-icon>
</md-fab>
<md-fab
label="${this.addedLNode || 'Add Type'}"
@click=${() => this.descriptionDialog.show()}
>
<md-icon slot="icon">${this.addedLNode ? 'done' : 'add'}</md-icon>
</md-fab>
</div>`
: html``}
<create-data-object-dialog
.cdClasses=${cdClasses}
.tree=${this.treeUI?.tree}
.onConfirm=${this.handleDOConfirm}
></create-data-object-dialog>
<description-dialog
.onConfirm=${(description: string) => this.saveTemplates(description)}
.onCancel=${() => this.descriptionDialog.close()}
></description-dialog>
<preview-dialog
.tree=${this.treeUI?.tree}
.lNodeType=${this.lNodeType}
></preview-dialog>
<oscd-snackbar
.message=${this.snackbarMessage}
.type=${this.snackbarType}
></oscd-snackbar>`;
}
static styles = css`
/* * {
--md-sys-color-primary: var(--oscd-primary);
--md-sys-color-secondary: var(--oscd-secondary);
--md-sys-typescale-body-large-font: var(--oscd-theme-text-font);
--md-outlined-text-field-input-text-color: var(--oscd-base01);
--md-sys-color-surface: var(--oscd-base3);
--md-sys-color-on-surface: var(--oscd-base00);
--md-sys-color-on-primary: var(--oscd-base2);
--md-sys-color-on-surface-variant: var(--oscd-base00);
--md-menu-container-color: var(--oscd-base3);
font-family: var(--oscd-theme-text-font, 'Roboto');
--md-sys-color-surface-container-highest: var(--oscd-base2);
--md-list-item-activated-background: rgb(
from var(--oscd-primary) r g b / 0.38
);
--md-menu-item-selected-container-color: rgb(
from var(--oscd-primary) r g b / 0.38
);
--md-list-container-color: var(--oscd-base2);
--md-fab-container-color: var(--oscd-secondary);
--md-dialog-container-color: var(--oscd-base3);
--md-dialog-container-shape: 4px;
--md-text-button-container-shape: 4px;
} */
md-outlined-button {
text-transform: uppercase;
}
md-icon {
font-family: var(--oscd-theme-icon-font, 'Material Symbols Outlined');
}
.fab-wrapper {
position: fixed;
bottom: 32px;
right: 32px;
display: flex;
gap: 16px;
}
.container {
margin: 12px;
}
.btn-wrapper {
display: flex;
margin-bottom: 12px;
gap: 12px;
}
`;
}