Skip to content

Commit 0298ac5

Browse files
feat(templates/dotype-wizard): add cdc field in create wizard (#330)
* feat(templates/dotype-wizard): add cdc field in create wizard * refactor(templates/dotype-wizards): better function name * test(templates/dotype-wizards): add tests for CDC field handling
1 parent 09e7af5 commit 0298ac5

File tree

4 files changed

+65
-5
lines changed

4 files changed

+65
-5
lines changed

__snapshots__/DOType wizards.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,13 @@
693693
pattern="([ -~]|[…]|[ -퟿]|[-�]|[𐀀\-􏿿])*"
694694
>
695695
</wizard-textfield>
696+
<wizard-textfield
697+
helper="[scl.cdc]"
698+
label="cdc"
699+
pattern="(SPS)|(DPS)|(INS)|(ENS)|(ACT)|(ACD)|(SEC)|(BCR)|(HST)|(VSS)|(MV)|(CMV)|(SAV)|(WYE)|(DEL)|(SEQ)|(HMV)|(HWYE)|(HDEL)|(SPC)|(DPC)|(INC)|(ENC)|(BSC)|(ISC)|(APC)|(BAC)|(SPG)|(ING)|(ENG)|(ORG)|(TSG)|(CUG)|(VSG)|(ASG)|(CURVE)|(CSG)|(DPL)|(LPL)|(CSD)|(CST)|(BTS)|(UTS)|(LTS)|(GTS)|(MTS)|(NTS)|(STS)|(CTS)|(OTS)|(VSD)"
700+
required=""
701+
>
702+
</wizard-textfield>
696703
</div>
697704
<mwc-button
698705
dialogaction="close"
@@ -1025,7 +1032,7 @@
10251032
dialoginitialfocus=""
10261033
helper="[scl.name]"
10271034
label="name"
1028-
pattern="[a-z][0-9,A-Z,a-z]*"
1035+
pattern="[a-z][0-9A-Za-z]*"
10291036
required=""
10301037
>
10311038
>
@@ -1217,7 +1224,7 @@
12171224
dialoginitialfocus=""
12181225
helper="[scl.name]"
12191226
label="name"
1220-
pattern="[a-z][0-9,A-Z,a-z]*"
1227+
pattern="[a-z][0-9A-Za-z]*"
12211228
required=""
12221229
>
12231230
>

src/editors/templates/dotype-wizards.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
isPublic,
1313
newActionEvent,
1414
newWizardEvent,
15-
patterns,
1615
SCLTag,
1716
selector,
1817
Wizard,
@@ -34,6 +33,7 @@ import { SingleSelectedEvent } from '@material/mwc-list/mwc-list-foundation';
3433
import { ListItem } from '@material/mwc-list/mwc-list-item';
3534
import { List } from '@material/mwc-list';
3635
import { createDaWizard, editDAWizard } from '../../wizards/da.js';
36+
import { patterns } from '../../wizards/foundation/limits.js';
3737

3838
function updateSDoAction(element: Element): WizardActor {
3939
return (inputs: WizardInput[]): EditorAction[] => {
@@ -144,7 +144,7 @@ function sDOWizard(options: WizardOptions): Wizard | undefined {
144144
.maybeValue=${name}
145145
helper="${translate('scl.name')}"
146146
required
147-
pattern="${patterns.alphanumericFirstLowerCase}"
147+
pattern="${patterns.tRestrName1stL}"
148148
dialogInitialFocus
149149
>
150150
></wizard-textfield
@@ -191,6 +191,8 @@ function addPredefinedDOType(
191191
if (existId) return [];
192192

193193
const desc = getValue(inputs.find(i => i.label === 'desc')!);
194+
const cdc = getValue(inputs.find(i => i.label === 'cdc')!)!;
195+
194196
const values = <Select>inputs.find(i => i.label === 'values');
195197
const selectedElement = values.selected
196198
? templates.querySelector(`DOType[id="${values.selected.value}"]`)
@@ -200,6 +202,7 @@ function addPredefinedDOType(
200202
: parent.ownerDocument.createElement('DOType');
201203

202204
element.setAttribute('id', id);
205+
element.setAttribute('cdc', cdc);
203206
if (desc) element.setAttribute('desc', desc);
204207

205208
const actions: Create[] = [];
@@ -221,6 +224,22 @@ function addPredefinedDOType(
221224
};
222225
}
223226

227+
function onSelectTemplateDOType(e: Event, templates: Document): void {
228+
const cdcUI = <Select>(
229+
(<Select>e.target).parentElement!.querySelector(
230+
'wizard-textfield[label="cdc"]'
231+
)!
232+
);
233+
234+
const doTypeId = (<Select>e.target).value;
235+
const cdc =
236+
templates.querySelector(`DOType[id="${doTypeId}"]`)?.getAttribute('cdc') ??
237+
null;
238+
239+
if (cdc) cdcUI.value = cdc;
240+
cdcUI.disabled = true;
241+
}
242+
224243
export function createDOTypeWizard(
225244
parent: Element,
226245
templates: Document
@@ -240,6 +259,7 @@ export function createDOTypeWizard(
240259
icon="playlist_add_check"
241260
label="values"
242261
helper="${translate('dotype.wizard.enums')}"
262+
@selected=${(e: Event) => onSelectTemplateDOType(e, templates)}
243263
>
244264
${Array.from(templates.querySelectorAll('DOType')).map(
245265
datype =>
@@ -273,6 +293,12 @@ export function createDOTypeWizard(
273293
nullable
274294
pattern="${patterns.normalizedString}"
275295
></wizard-textfield>`,
296+
html`<wizard-textfield
297+
label="cdc"
298+
helper="${translate('scl.cdc')}"
299+
required
300+
pattern="${patterns.cdc}"
301+
></wizard-textfield>`,
276302
],
277303
},
278304
];

src/wizards/foundation/limits.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,14 @@ export const patterns = {
2323
alphanumericFirstUpperCase: '[A-Z][0-9,A-Z,a-z]*',
2424
asciName: '[A-Za-z][0-9,A-Z,a-z_]*',
2525
lnClass: '[A-Z]{4,4}',
26-
restrName1stU: '[a-z][0-9A-Za-z]*',
26+
tRestrName1stL: '[a-z][0-9A-Za-z]*',
2727
abstractDataAttributeName:
2828
'((T)|(Test)|(Check)|(SIUnit)|(Open)|(SBO)|(SBOw)|(Cancel)|[a-z][0-9A-Za-z]*)',
29+
cdc:
30+
'(SPS)|(DPS)|(INS)|(ENS)|(ACT)|(ACD)|(SEC)|(BCR)|(HST)|(VSS)|(MV)|(CMV)|(SAV)|' +
31+
'(WYE)|(DEL)|(SEQ)|(HMV)|(HWYE)|(HDEL)|(SPC)|(DPC)|(INC)|(ENC)|(BSC)|(ISC)|(APC)|(BAC)|' +
32+
'(SPG)|(ING)|(ENG)|(ORG)|(TSG)|(CUG)|(VSG)|(ASG)|(CURVE)|(CSG)|(DPL)|(LPL)|(CSD)|(CST)|' +
33+
'(BTS)|(UTS)|(LTS)|(GTS)|(MTS)|(NTS)|(STS)|(CTS)|(OTS)|(VSD)',
2934
};
3035

3136
export const maxLength = {

test/integration/editors/templates/dotype-wizarding.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ describe('DOType wizards', () => {
3838
describe('defines a createDOTypeWizard', () => {
3939
let selector: Select;
4040
let idField: WizardTextField;
41+
let cdcField: WizardTextField;
4142
let primayAction: HTMLElement;
4243
beforeEach(async () => {
4344
const button = <HTMLElement>(
@@ -54,6 +55,9 @@ describe('DOType wizards', () => {
5455
idField = <WizardTextField>(
5556
parent.wizardUI.dialog?.querySelector('wizard-textfield[label="id"]')
5657
);
58+
cdcField = <WizardTextField>(
59+
parent.wizardUI.dialog?.querySelector('wizard-textfield[label="cdc"]')
60+
);
5761
primayAction = <HTMLElement>(
5862
parent.wizardUI.dialog?.querySelector(
5963
'mwc-button[slot="primaryAction"]'
@@ -67,13 +71,31 @@ describe('DOType wizards', () => {
6771
it('allows to add empty DOTypes to the project', async () => {
6872
expect(doc.querySelector('DOType[id="myGeneralDOType"]')).to.not.exist;
6973
idField.maybeValue = 'myGeneralDOType';
74+
cdcField.maybeValue = 'SPS';
7075
await parent.requestUpdate();
7176
primayAction.click();
7277
await parent.updateComplete;
7378
expect(doc.querySelector('DOType[id="myGeneralDOType"]')).to.exist;
7479
});
80+
it('allows to define CDC only for empty DOType creation', async () => {
81+
await cdcField.updateComplete;
82+
expect(cdcField.disabled).to.not.be.true;
83+
selector.value = 'OpenSCD_ENS_Health';
84+
await cdcField.requestUpdate();
85+
expect(cdcField.disabled).to.be.true;
86+
});
87+
it('requires CDC definition for empty DOTypes', async () => {
88+
expect(doc.querySelector('DOType[id="myGeneralDOType"]')).to.not.exist;
89+
idField.maybeValue = 'myGeneralDOType';
90+
cdcField.maybeValue = null;
91+
await parent.requestUpdate();
92+
primayAction.click();
93+
await parent.updateComplete;
94+
expect(doc.querySelector('DOType[id="myGeneralDOType"]')).to.not.exist;
95+
});
7596
it('respects the sequence defined in the standard', async () => {
7697
idField.maybeValue = 'myGeneralDOType';
98+
cdcField.maybeValue = 'SPS';
7799
await parent.requestUpdate();
78100
primayAction.click();
79101
await parent.updateComplete;

0 commit comments

Comments
 (0)