Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added menu to save as version to existing SCL File #198

Merged
merged 2 commits into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions public/js/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@ export const officialPlugins = [
requireDoc: true,
position: 'top',
},
{
name: 'Save as version',
src: '/src/menu/CompasSaveAsVersion.js',
icon: 'save',
default: true,
kind: 'menu',
requireDoc: true,
position: 'top',
},
{
name: 'Validate using OCL',
src: '/src/validators/CompasValidateSchema.js',
Expand Down
41 changes: 24 additions & 17 deletions src/compas/CompasOpen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,47 @@ import '../WizardDivider.js';
import './CompasSclTypeList.js';
import './CompasSclList.js';

import { nothing } from 'lit-html';
import { buildDocName } from './foundation.js';

/* Event that will be used when an SCL Document is retrieved. */
export interface DocRetrievedDetail {
localFile: boolean;
doc: Document;
docName?: string;
docId?: string;
}
export type DocRetrievedEvent = CustomEvent<DocRetrievedDetail>;
export function newDocRetrievedEvent(
localFile: boolean,
doc: Document,
docName?: string
docName?: string,
docId?: string
): DocRetrievedEvent {
return new CustomEvent<DocRetrievedDetail>('doc-retrieved', {
bubbles: true,
composed: true,
detail: { localFile, doc, docName },
detail: { localFile, doc, docName, docId },
});
}

@customElement('compas-open')
export default class CompasOpenElement extends LitElement {
@property()
selectedType: string | undefined;
@property()
allowLocalFile = true;

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

private async getSclDocument(id?: string): Promise<void> {
const sclDocument = await CompasSclDataService()
.getSclDocument(this.selectedType ?? '', id ?? '')
private async getSclDocument(docId?: string): Promise<void> {
const doc = await CompasSclDataService()
.getSclDocument(this.selectedType ?? '', docId ?? '')
.catch(reason => createLogEvent(this, reason));
if (sclDocument instanceof Document) {
this.dispatchEvent(newDocRetrievedEvent(false, sclDocument));
if (doc instanceof Document) {
const docName = buildDocName(doc.documentElement);
this.dispatchEvent(newDocRetrievedEvent(false, doc, docName, docId));
}
}

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

this.dispatchEvent(newDocRetrievedEvent(true, doc, docName));
this.sclFileUI.onchange = null;
}

private renderFileSelect(): TemplateResult {
Expand All @@ -84,10 +91,8 @@ export default class CompasOpenElement extends LitElement {
<mwc-button
label="${translate('compas.open.selectFileButton')}"
@click=${() => {
const input = <HTMLInputElement | null>(
this.shadowRoot!.querySelector('#scl-file')
);
input?.click();
this.sclFileUI.value = '';
this.sclFileUI.click();
}}
>
</mwc-button>
Expand Down Expand Up @@ -129,11 +134,13 @@ export default class CompasOpenElement extends LitElement {

render(): TemplateResult {
return html`
<wizard-divider></wizard-divider>
<section>
<h3>${translate('compas.open.localTitle')}</h3>
${this.renderFileSelect()}
</section>
${this.allowLocalFile
? html`<wizard-divider></wizard-divider>
<section>
<h3>${translate('compas.open.localTitle')}</h3>
${this.renderFileSelect()}
</section>`
: nothing}
<wizard-divider></wizard-divider>
<section>
<h3>${translate('compas.open.compasTitle')}</h3>
Expand Down
15 changes: 10 additions & 5 deletions src/compas/CompasSave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import './CompasComment.js';
import './CompasLabelsField.js';
import './CompasLoading.js';
import './CompasSclTypeSelect.js';
import { nothing } from 'lit-html';

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

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

render(): TemplateResult {
return html`
<wizard-divider></wizard-divider>
<section>
<h3>${translate('compas.save.localTitle')}</h3>
${this.renderSaveFilePart()}
</section>
${this.allowLocalFile
? html` <wizard-divider></wizard-divider>
<section>
<h3>${translate('compas.save.localTitle')}</h3>
${this.renderSaveFilePart()}
</section>`
: nothing}
<wizard-divider></wizard-divider>
<section>
<h3>${translate('compas.save.compasTitle')}</h3>
Expand Down
58 changes: 29 additions & 29 deletions src/compas/foundation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { get } from 'lit-translate';

import { newLogEvent, newOpenDocEvent } from '../foundation.js';
import {
COMPAS_SCL_PRIVATE_TYPE,
getCompasSclFileType,
getCompasSclName,
getPrivate,
} from './private.js';

const FILE_EXTENSION_LENGTH = 3;

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

export function buildDocName(sclElement: Element): string {
const headerElement = sclElement.querySelector(':scope > Header');
const privateElement = getPrivate(sclElement, COMPAS_SCL_PRIVATE_TYPE);

const version = headerElement?.getAttribute('version') ?? '';
const name = getCompasSclName(privateElement)?.textContent ?? '';
const type = getCompasSclFileType(privateElement)?.textContent ?? 'SCD';

let docName = name;
if (docName === '') {
docName = headerElement?.getAttribute('id') ?? '';
}
docName += '-' + version + '.' + type?.toLowerCase();
return docName;
}

export function updateDocumentInOpenSCD(
element: Element,
doc: Document,
docName?: string
): void {
const id =
(doc.querySelectorAll(':root > Header') ?? [])
.item(0)
?.getAttribute('id') ?? '';

if (!docName) {
const version =
(doc.querySelectorAll(':root > Header') ?? [])
.item(0)
?.getAttribute('version') ?? '';
const name =
(
doc.querySelectorAll(':root > Private[type="compas_scl"] > SclName') ??
[]
).item(0)?.textContent ?? '';
const type =
(
doc.querySelectorAll(
':root > Private[type="compas_scl"] > SclFileType'
) ?? []
).item(0)?.textContent ?? '';

docName = name;
if (docName === '') {
docName = id;
}
docName += '-' + version + '.' + type?.toLowerCase();
}
const headerElement = doc.querySelector(':root > Header');
const id = headerElement?.getAttribute('id') ?? '';

element.dispatchEvent(newLogEvent({ kind: 'reset' }));
element.dispatchEvent(
newOpenDocEvent(doc, docName, { detail: { docId: id } })
newOpenDocEvent(
doc,
docName ? docName : buildDocName(doc.documentElement),
{ detail: { docId: id } }
)
);
}

Expand Down
59 changes: 58 additions & 1 deletion src/compas/private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function createPrivate(parent: Element, type: string): Element {
export function getCompasSclName(
privateElement: Element | null
): Element | null {
return privateElement?.querySelector(`SclName`) ?? null;
return privateElement?.querySelector(`:scope > SclName`) ?? null;
}

export function createCompasSclName(parent: Element, value: string): Element {
Expand All @@ -37,6 +37,44 @@ export function createCompasSclName(parent: Element, value: string): Element {
return newSclNameElement;
}

export function copyCompasSclName(
fromParent: Element | null,
toParent: Element | null
): void {
if (fromParent && toParent) {
const fromSclNameElement = getCompasSclName(fromParent);
const toSclNameElement = getCompasSclName(toParent);

if (toSclNameElement && fromSclNameElement) {
toSclNameElement.textContent = fromSclNameElement.textContent;
} else if (toSclNameElement) {
toSclNameElement.textContent = '';
}
}
}

export function getCompasSclFileType(
privateElement: Element | null
): Element | null {
return privateElement?.querySelector(`:scope > SclFileType`) ?? null;
}

export function copyCompasSclFileType(
fromParent: Element | null,
toParent: Element | null
): void {
if (fromParent && toParent) {
const fromSclFileTypeElement = getCompasSclFileType(fromParent);
const toSclFileTypeElement = getCompasSclFileType(toParent);

if (toSclFileTypeElement && fromSclFileTypeElement) {
toSclFileTypeElement.textContent = fromSclFileTypeElement.textContent;
} else if (toSclFileTypeElement) {
toSclFileTypeElement.textContent = '';
}
}
}

export function getLabels(privateElement: Element): Element | null {
return (
Array.from(privateElement.querySelectorAll(`:scope > Labels`)).find(
Expand Down Expand Up @@ -70,6 +108,25 @@ export function createLabel(labelsElement: Element, value: string): Element {
return labelElement;
}

export function copyCompasLabels(
fromParent: Element | null,
toParent: Element | null
): void {
if (fromParent && toParent) {
const fromLabels = getLabels(fromParent);
const toLabels = getLabels(toParent);

if (toLabels) {
toParent.removeChild(toLabels);
}
if (fromLabels) {
toParent.appendChild(
toParent.ownerDocument.adoptNode(fromLabels.cloneNode(true))
);
}
}
}

export function addPrefixAndNamespaceToDocument(
element: Element,
namespace: string,
Expand Down
2 changes: 1 addition & 1 deletion src/menu/CompasSave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class CompasSaveMenuPlugin extends LitElement {
render(): TemplateResult {
return html`<mwc-dialog
id="compas-save-dlg"
heading="${translate('compas.save.title')}"
heading="${translate('compas.save.saveTitle')}"
>
${!this.doc || !this.docName
? html`<compas-loading></compas-loading>`
Expand Down
2 changes: 1 addition & 1 deletion src/menu/CompasSaveAs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default class CompasSaveAsMenuPlugin extends LitElement {
render(): TemplateResult {
return html`<mwc-dialog
id="compas-save-as-dlg"
heading="${translate('compas.save.title')}"
heading="${translate('compas.save.saveAsTitle')}"
>
${!this.doc || !this.docName
? html`<compas-loading></compas-loading>`
Expand Down
Loading