Skip to content

Commit 4794454

Browse files
author
Dennis Labordus
authored
Merge pull request #198 from com-pas/add-version-menu
Added menu to save as version to existing SCL File
2 parents 1f70b61 + 94545b8 commit 4794454

26 files changed

+771
-298
lines changed

public/js/plugins.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,15 @@ export const officialPlugins = [
144144
requireDoc: true,
145145
position: 'top',
146146
},
147+
{
148+
name: 'Save as version',
149+
src: '/src/menu/CompasSaveAsVersion.js',
150+
icon: 'save',
151+
default: true,
152+
kind: 'menu',
153+
requireDoc: true,
154+
position: 'top',
155+
},
147156
{
148157
name: 'Validate using OCL',
149158
src: '/src/validators/CompasValidateSchema.js',

src/compas/CompasOpen.ts

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,39 +21,47 @@ import '../WizardDivider.js';
2121
import './CompasSclTypeList.js';
2222
import './CompasSclList.js';
2323

24+
import { nothing } from 'lit-html';
25+
import { buildDocName } from './foundation.js';
26+
2427
/* Event that will be used when an SCL Document is retrieved. */
2528
export interface DocRetrievedDetail {
2629
localFile: boolean;
2730
doc: Document;
2831
docName?: string;
32+
docId?: string;
2933
}
3034
export type DocRetrievedEvent = CustomEvent<DocRetrievedDetail>;
3135
export function newDocRetrievedEvent(
3236
localFile: boolean,
3337
doc: Document,
34-
docName?: string
38+
docName?: string,
39+
docId?: string
3540
): DocRetrievedEvent {
3641
return new CustomEvent<DocRetrievedDetail>('doc-retrieved', {
3742
bubbles: true,
3843
composed: true,
39-
detail: { localFile, doc, docName },
44+
detail: { localFile, doc, docName, docId },
4045
});
4146
}
4247

4348
@customElement('compas-open')
4449
export default class CompasOpenElement extends LitElement {
4550
@property()
4651
selectedType: string | undefined;
52+
@property()
53+
allowLocalFile = true;
4754

4855
@query('#scl-file')
4956
private sclFileUI!: HTMLInputElement;
5057

51-
private async getSclDocument(id?: string): Promise<void> {
52-
const sclDocument = await CompasSclDataService()
53-
.getSclDocument(this.selectedType ?? '', id ?? '')
58+
private async getSclDocument(docId?: string): Promise<void> {
59+
const doc = await CompasSclDataService()
60+
.getSclDocument(this.selectedType ?? '', docId ?? '')
5461
.catch(reason => createLogEvent(this, reason));
55-
if (sclDocument instanceof Document) {
56-
this.dispatchEvent(newDocRetrievedEvent(false, sclDocument));
62+
if (doc instanceof Document) {
63+
const docName = buildDocName(doc.documentElement);
64+
this.dispatchEvent(newDocRetrievedEvent(false, doc, docName, docId));
5765
}
5866
}
5967

@@ -66,7 +74,6 @@ export default class CompasOpenElement extends LitElement {
6674
const doc = new DOMParser().parseFromString(text, 'application/xml');
6775

6876
this.dispatchEvent(newDocRetrievedEvent(true, doc, docName));
69-
this.sclFileUI.onchange = null;
7077
}
7178

7279
private renderFileSelect(): TemplateResult {
@@ -84,10 +91,8 @@ export default class CompasOpenElement extends LitElement {
8491
<mwc-button
8592
label="${translate('compas.open.selectFileButton')}"
8693
@click=${() => {
87-
const input = <HTMLInputElement | null>(
88-
this.shadowRoot!.querySelector('#scl-file')
89-
);
90-
input?.click();
94+
this.sclFileUI.value = '';
95+
this.sclFileUI.click();
9196
}}
9297
>
9398
</mwc-button>
@@ -129,11 +134,13 @@ export default class CompasOpenElement extends LitElement {
129134

130135
render(): TemplateResult {
131136
return html`
132-
<wizard-divider></wizard-divider>
133-
<section>
134-
<h3>${translate('compas.open.localTitle')}</h3>
135-
${this.renderFileSelect()}
136-
</section>
137+
${this.allowLocalFile
138+
? html`<wizard-divider></wizard-divider>
139+
<section>
140+
<h3>${translate('compas.open.localTitle')}</h3>
141+
${this.renderFileSelect()}
142+
</section>`
143+
: nothing}
137144
<wizard-divider></wizard-divider>
138145
<section>
139146
<h3>${translate('compas.open.compasTitle')}</h3>

src/compas/CompasSave.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import './CompasComment.js';
4242
import './CompasLabelsField.js';
4343
import './CompasLoading.js';
4444
import './CompasSclTypeSelect.js';
45+
import { nothing } from 'lit-html';
4546

4647
/* Event that will be used when an SCL Document is saved. */
4748
export type DocSavedEvent = CustomEvent<void>;
@@ -56,6 +57,8 @@ export function newDocSavedEvent(): DocSavedEvent {
5657
export default class CompasSaveElement extends CompasExistsIn(LitElement) {
5758
@property()
5859
doc!: XMLDocument;
60+
@property()
61+
allowLocalFile = true;
5962

6063
@query('mwc-textfield#name')
6164
private nameField!: TextFieldBase;
@@ -223,11 +226,13 @@ export default class CompasSaveElement extends CompasExistsIn(LitElement) {
223226

224227
render(): TemplateResult {
225228
return html`
226-
<wizard-divider></wizard-divider>
227-
<section>
228-
<h3>${translate('compas.save.localTitle')}</h3>
229-
${this.renderSaveFilePart()}
230-
</section>
229+
${this.allowLocalFile
230+
? html` <wizard-divider></wizard-divider>
231+
<section>
232+
<h3>${translate('compas.save.localTitle')}</h3>
233+
${this.renderSaveFilePart()}
234+
</section>`
235+
: nothing}
231236
<wizard-divider></wizard-divider>
232237
<section>
233238
<h3>${translate('compas.save.compasTitle')}</h3>

src/compas/foundation.ts

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { get } from 'lit-translate';
22

33
import { newLogEvent, newOpenDocEvent } from '../foundation.js';
4+
import {
5+
COMPAS_SCL_PRIVATE_TYPE,
6+
getCompasSclFileType,
7+
getCompasSclName,
8+
getPrivate,
9+
} from './private.js';
410

511
const FILE_EXTENSION_LENGTH = 3;
612

@@ -26,43 +32,37 @@ export function stripExtensionFromName(docName: string): string {
2632
return name;
2733
}
2834

35+
export function buildDocName(sclElement: Element): string {
36+
const headerElement = sclElement.querySelector(':scope > Header');
37+
const privateElement = getPrivate(sclElement, COMPAS_SCL_PRIVATE_TYPE);
38+
39+
const version = headerElement?.getAttribute('version') ?? '';
40+
const name = getCompasSclName(privateElement)?.textContent ?? '';
41+
const type = getCompasSclFileType(privateElement)?.textContent ?? 'SCD';
42+
43+
let docName = name;
44+
if (docName === '') {
45+
docName = headerElement?.getAttribute('id') ?? '';
46+
}
47+
docName += '-' + version + '.' + type?.toLowerCase();
48+
return docName;
49+
}
50+
2951
export function updateDocumentInOpenSCD(
3052
element: Element,
3153
doc: Document,
3254
docName?: string
3355
): void {
34-
const id =
35-
(doc.querySelectorAll(':root > Header') ?? [])
36-
.item(0)
37-
?.getAttribute('id') ?? '';
38-
39-
if (!docName) {
40-
const version =
41-
(doc.querySelectorAll(':root > Header') ?? [])
42-
.item(0)
43-
?.getAttribute('version') ?? '';
44-
const name =
45-
(
46-
doc.querySelectorAll(':root > Private[type="compas_scl"] > SclName') ??
47-
[]
48-
).item(0)?.textContent ?? '';
49-
const type =
50-
(
51-
doc.querySelectorAll(
52-
':root > Private[type="compas_scl"] > SclFileType'
53-
) ?? []
54-
).item(0)?.textContent ?? '';
55-
56-
docName = name;
57-
if (docName === '') {
58-
docName = id;
59-
}
60-
docName += '-' + version + '.' + type?.toLowerCase();
61-
}
56+
const headerElement = doc.querySelector(':root > Header');
57+
const id = headerElement?.getAttribute('id') ?? '';
6258

6359
element.dispatchEvent(newLogEvent({ kind: 'reset' }));
6460
element.dispatchEvent(
65-
newOpenDocEvent(doc, docName, { detail: { docId: id } })
61+
newOpenDocEvent(
62+
doc,
63+
docName ? docName : buildDocName(doc.documentElement),
64+
{ detail: { docId: id } }
65+
)
6666
);
6767
}
6868

src/compas/private.ts

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function createPrivate(parent: Element, type: string): Element {
2222
export function getCompasSclName(
2323
privateElement: Element | null
2424
): Element | null {
25-
return privateElement?.querySelector(`SclName`) ?? null;
25+
return privateElement?.querySelector(`:scope > SclName`) ?? null;
2626
}
2727

2828
export function createCompasSclName(parent: Element, value: string): Element {
@@ -37,6 +37,44 @@ export function createCompasSclName(parent: Element, value: string): Element {
3737
return newSclNameElement;
3838
}
3939

40+
export function copyCompasSclName(
41+
fromParent: Element | null,
42+
toParent: Element | null
43+
): void {
44+
if (fromParent && toParent) {
45+
const fromSclNameElement = getCompasSclName(fromParent);
46+
const toSclNameElement = getCompasSclName(toParent);
47+
48+
if (toSclNameElement && fromSclNameElement) {
49+
toSclNameElement.textContent = fromSclNameElement.textContent;
50+
} else if (toSclNameElement) {
51+
toSclNameElement.textContent = '';
52+
}
53+
}
54+
}
55+
56+
export function getCompasSclFileType(
57+
privateElement: Element | null
58+
): Element | null {
59+
return privateElement?.querySelector(`:scope > SclFileType`) ?? null;
60+
}
61+
62+
export function copyCompasSclFileType(
63+
fromParent: Element | null,
64+
toParent: Element | null
65+
): void {
66+
if (fromParent && toParent) {
67+
const fromSclFileTypeElement = getCompasSclFileType(fromParent);
68+
const toSclFileTypeElement = getCompasSclFileType(toParent);
69+
70+
if (toSclFileTypeElement && fromSclFileTypeElement) {
71+
toSclFileTypeElement.textContent = fromSclFileTypeElement.textContent;
72+
} else if (toSclFileTypeElement) {
73+
toSclFileTypeElement.textContent = '';
74+
}
75+
}
76+
}
77+
4078
export function getLabels(privateElement: Element): Element | null {
4179
return (
4280
Array.from(privateElement.querySelectorAll(`:scope > Labels`)).find(
@@ -70,6 +108,25 @@ export function createLabel(labelsElement: Element, value: string): Element {
70108
return labelElement;
71109
}
72110

111+
export function copyCompasLabels(
112+
fromParent: Element | null,
113+
toParent: Element | null
114+
): void {
115+
if (fromParent && toParent) {
116+
const fromLabels = getLabels(fromParent);
117+
const toLabels = getLabels(toParent);
118+
119+
if (toLabels) {
120+
toParent.removeChild(toLabels);
121+
}
122+
if (fromLabels) {
123+
toParent.appendChild(
124+
toParent.ownerDocument.adoptNode(fromLabels.cloneNode(true))
125+
);
126+
}
127+
}
128+
}
129+
73130
export function addPrefixAndNamespaceToDocument(
74131
element: Element,
75132
namespace: string,

src/menu/CompasSave.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default class CompasSaveMenuPlugin extends LitElement {
4040
render(): TemplateResult {
4141
return html`<mwc-dialog
4242
id="compas-save-dlg"
43-
heading="${translate('compas.save.title')}"
43+
heading="${translate('compas.save.saveTitle')}"
4444
>
4545
${!this.doc || !this.docName
4646
? html`<compas-loading></compas-loading>`

src/menu/CompasSaveAs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default class CompasSaveAsMenuPlugin extends LitElement {
3838
render(): TemplateResult {
3939
return html`<mwc-dialog
4040
id="compas-save-as-dlg"
41-
heading="${translate('compas.save.title')}"
41+
heading="${translate('compas.save.saveAsTitle')}"
4242
>
4343
${!this.doc || !this.docName
4444
? html`<compas-loading></compas-loading>`

0 commit comments

Comments
 (0)