Skip to content

Commit

Permalink
Merge pull request #189 from com-pas/add-label-during-save
Browse files Browse the repository at this point in the history
Add labels when saving a SCL File
  • Loading branch information
juancho0202 authored Sep 15, 2022
2 parents 5af0978 + 0824261 commit 29519c5
Show file tree
Hide file tree
Showing 57 changed files with 2,269 additions and 789 deletions.
4 changes: 2 additions & 2 deletions src/compas-editors/CompasVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,14 +367,14 @@ export default class CompasVersionsPlugin extends LitElement {

private getCurrentVersion(): string {
const header = this.doc.querySelector('Header');
return header!.getAttribute('version') ?? 'unknown';
return header?.getAttribute('version') ?? 'unknown';
}

private getCurrentName(): string {
const sclName = this.doc.querySelector(
'SCL > Private[type="compas_scl"] > SclName'
);
return sclName!.textContent ?? 'unknown';
return sclName?.textContent ?? 'unknown';
}

private renderLineInfo(item: Element): TemplateResult {
Expand Down
154 changes: 107 additions & 47 deletions src/compas-services/CompasSclDataService.ts
Original file line number Diff line number Diff line change
@@ -1,97 +1,142 @@
import {CompasSettings} from "../compas/CompasSettings.js";
import {extractSclFromResponse, handleError, handleResponse, parseXml} from "./foundation.js";
import { formatXml } from '../file.js';

export const SDS_NAMESPACE = 'https://www.lfenergy.org/compas/SclDataService/v1';
import { CompasSettings } from '../compas/CompasSettings.js';
import {
extractSclFromResponse,
handleError,
handleResponse,
parseXml,
} from './foundation.js';

export const SDS_NAMESPACE =
'https://www.lfenergy.org/compas/SclDataService/v1';

export enum ChangeSet {
MAJOR = "MAJOR",
MINOR = "MINOR",
PATCH = "PATCH",
MAJOR = 'MAJOR',
MINOR = 'MINOR',
PATCH = 'PATCH',
}

export interface CreateRequestBody {
sclName: string,
comment: string,
doc: Document
sclName: string;
comment: string | null;
doc: Document;
}

export interface UpdateRequestBody {
changeSet: ChangeSet,
comment: string,
doc: Document
changeSet: ChangeSet;
comment: string | null;
doc: Document;
}

export function CompasSclDataService() {

function getCompasSettings() {
return CompasSettings().compasSettings;
}

return {
listSclTypes(): Promise<Document> {
const sclUrl = getCompasSettings().sclDataServiceUrl + '/common/v1/type/list';
const sclUrl =
getCompasSettings().sclDataServiceUrl + '/common/v1/type/list';
return fetch(sclUrl)
.catch(handleError)
.then(handleResponse)
.then(parseXml);
},

listSclTypesAndOrder(): Promise<Element[]> {
return this.listSclTypes()
.then(xmlResponse => {
return Array.from(xmlResponse.querySelectorAll('*|Type') ?? [])
.sort((type1, type2) => {
const description1 = type1.getElementsByTagNameNS(SDS_NAMESPACE, "Description")!.item(0)!.textContent ?? "";
const description2 = type2.getElementsByTagNameNS(SDS_NAMESPACE, "Description")!.item(0)!.textContent ?? "";
return description1.localeCompare(description2)
});
})
return this.listSclTypes().then(xmlResponse => {
return Array.from(xmlResponse.querySelectorAll('*|Type') ?? []).sort(
(type1, type2) => {
const description1 =
type1
.getElementsByTagNameNS(SDS_NAMESPACE, 'Description')!
.item(0)!.textContent ?? '';
const description2 =
type2
.getElementsByTagNameNS(SDS_NAMESPACE, 'Description')!
.item(0)!.textContent ?? '';
return description1.localeCompare(description2);
}
);
});
},

listScls(type: string): Promise<Document> {
const sclUrl = getCompasSettings().sclDataServiceUrl + '/scl/v1/' + type + '/list';
const sclUrl =
getCompasSettings().sclDataServiceUrl + '/scl/v1/' + type + '/list';
return fetch(sclUrl)
.catch(handleError)
.then(handleResponse)
.then(parseXml);
},

listVersions(type: string, id: string): Promise<Document> {
const sclUrl = getCompasSettings().sclDataServiceUrl + '/scl/v1/' + type + '/' + id + "/versions";
const sclUrl =
getCompasSettings().sclDataServiceUrl +
'/scl/v1/' +
type +
'/' +
id +
'/versions';
return fetch(sclUrl)
.catch(handleError)
.then(handleResponse)
.then(parseXml);
},

getSclDocument(type: string, id: string): Promise<Document> {
const sclUrl = getCompasSettings().sclDataServiceUrl + '/scl/v1/' + type + '/' + id;
const sclUrl =
getCompasSettings().sclDataServiceUrl + '/scl/v1/' + type + '/' + id;
return fetch(sclUrl)
.catch(handleError)
.then(handleResponse)
.then(parseXml)
.then(extractSclFromResponse);
},

getSclDocumentVersion(type: string, id: string, version: string): Promise<Document> {
const sclUrl = getCompasSettings().sclDataServiceUrl + '/scl/v1/' + type + '/' + id + '/' + version;
getSclDocumentVersion(
type: string,
id: string,
version: string
): Promise<Document> {
const sclUrl =
getCompasSettings().sclDataServiceUrl +
'/scl/v1/' +
type +
'/' +
id +
'/' +
version;
return fetch(sclUrl)
.catch(handleError)
.then(handleResponse)
.then(parseXml)
.then(extractSclFromResponse);
},

deleteSclDocumentVersion(type: string, id: string, version: string): Promise<string> {
const sclUrl = getCompasSettings().sclDataServiceUrl + '/scl/v1/' + type + '/' + id + '/' + version;
return fetch(sclUrl, {method: 'DELETE'})
deleteSclDocumentVersion(
type: string,
id: string,
version: string
): Promise<string> {
const sclUrl =
getCompasSettings().sclDataServiceUrl +
'/scl/v1/' +
type +
'/' +
id +
'/' +
version;
return fetch(sclUrl, { method: 'DELETE' })
.catch(handleError)
.then(handleResponse);
},

deleteSclDocument(type: string, id: string): Promise<string> {
const sclUrl = getCompasSettings().sclDataServiceUrl + '/scl/v1/' + type + '/' + id;
return fetch(sclUrl, {method: 'DELETE'})
const sclUrl =
getCompasSettings().sclDataServiceUrl + '/scl/v1/' + type + '/' + id;
return fetch(sclUrl, { method: 'DELETE' })
.catch(handleError)
.then(handleResponse);
},
Expand All @@ -101,37 +146,52 @@ export function CompasSclDataService() {
return fetch(sclUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/xml'
'Content-Type': 'application/xml',
},
body: `<?xml version="1.0" encoding="UTF-8"?>
<sds:CreateRequest xmlns:sds="${SDS_NAMESPACE}">
<sds:Name>${body.sclName}</sds:Name>
<sds:Comment>${body.comment}</sds:Comment>
<sds:SclData><![CDATA[${new XMLSerializer().serializeToString(body.doc.documentElement)}]]></sds:SclData>
</sds:CreateRequest>`
}).catch(handleError)
<sds:Comment>${body.comment ?? ''}</sds:Comment>
<sds:SclData><![CDATA[${formatXml(
new XMLSerializer().serializeToString(
body.doc.documentElement
)
)}]]></sds:SclData>
</sds:CreateRequest>`,
})
.catch(handleError)
.then(handleResponse)
.then(parseXml)
.then(extractSclFromResponse);
},

updateSclDocument(type: string, id: string, body: UpdateRequestBody): Promise<Document> {
const sclUrl = getCompasSettings().sclDataServiceUrl + '/scl/v1/' + type + '/' + id;
updateSclDocument(
type: string,
id: string,
body: UpdateRequestBody
): Promise<Document> {
const sclUrl =
getCompasSettings().sclDataServiceUrl + '/scl/v1/' + type + '/' + id;
return fetch(sclUrl, {
method: 'PUT',
headers: {
'Content-Type': 'application/xml'
'Content-Type': 'application/xml',
},
body: `<?xml version="1.0" encoding="UTF-8"?>
<sds:UpdateRequest xmlns:sds="${SDS_NAMESPACE}">
<sds:ChangeSet>${body.changeSet}</sds:ChangeSet>
<sds:Comment>${body.comment}</sds:Comment>
<sds:SclData><![CDATA[${new XMLSerializer().serializeToString(body.doc.documentElement)}]]></sds:SclData>
</sds:UpdateRequest>`
}).catch(handleError)
<sds:Comment>${body.comment ?? ''}</sds:Comment>
<sds:SclData><![CDATA[${formatXml(
new XMLSerializer().serializeToString(
body.doc.documentElement
)
)}]]></sds:SclData>
</sds:UpdateRequest>`,
})
.catch(handleError)
.then(handleResponse)
.then(parseXml)
.then(extractSclFromResponse);
}
}
},
};
}
89 changes: 61 additions & 28 deletions src/compas-wizards/scl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,84 @@ import { get, translate } from 'lit-translate';

import {
cloneElement,
ComplexAction,
EditorAction,
getValue,
Wizard,
WizardActor,
WizardInputElement,
} from '../foundation.js';
import { COMPAS_SCL_PRIVATE_TYPE } from "../compas/private.js";

import '../compas/CompasLabelsField.js';

import {
COMPAS_SCL_PRIVATE_TYPE,
createCompasSclName,
createPrivate,
getCompasSclName,
getPrivate
} from "../compas/private.js";
getPrivate,
} from '../compas/private.js';
import { CompasLabelsFieldElement } from '../compas/CompasLabelsField.js';

export function updateSCL(sclElement: Element): WizardActor {
return (inputs: WizardInputElement[]): EditorAction[] => {
return (inputs: WizardInputElement[], wizard: Element): EditorAction[] => {
const newValue = getValue(inputs.find(i => i.label === 'filename')!)!;
const oldSclNameElement = getCompasSclName(sclElement);
const labelsField = <CompasLabelsFieldElement>(
wizard.shadowRoot!.querySelector('compas-labels-field')
);

const privateElement = getPrivate(sclElement, COMPAS_SCL_PRIVATE_TYPE)!;
const oldSclNameElement = getCompasSclName(privateElement);

const complexAction: ComplexAction = {
actions: [],
title: get('compas.scl.updateAction'),
};

if (oldSclNameElement) {
// Update the value in the existing SclName Element by cloning.
const oldValue = oldSclNameElement.textContent;
if (newValue === oldValue) {
return [];
if (newValue !== oldValue) {
const newSclNameElement = cloneElement(oldSclNameElement, {});
newSclNameElement.textContent = newValue;
complexAction.actions.push({
old: { element: oldSclNameElement },
new: { element: newSclNameElement },
});
}

const newSclNameElement = cloneElement(oldSclNameElement, {});
newSclNameElement.textContent = newValue;
return [{ old: { element: oldSclNameElement }, new: { element: newSclNameElement } }];
} else {
let privateElement = getPrivate(sclElement, COMPAS_SCL_PRIVATE_TYPE);
if (!privateElement) {
// No Private Element under SCL, so create both Private and SclName Element to be added to the SCL Element.
const newSclNameElement = createCompasSclName(sclElement, newValue);
privateElement = createPrivate(sclElement, COMPAS_SCL_PRIVATE_TYPE);
privateElement.prepend(newSclNameElement);
return [{ new: { parent: sclElement, element: privateElement } }];
}

// There is a Private Element, but no SclName Element, so only create a new SclName Element.
const newSclNameElement = createCompasSclName(sclElement, newValue);
return [{ new: { parent: privateElement, element: newSclNameElement } }];
complexAction.actions.push({
new: { parent: privateElement, element: newSclNameElement },
});
}

// We will replace the full Labels Element, so remove the original one and add the cloned/updated version.
if (labelsField.originalLabelsElement) {
complexAction.actions.push({
old: {
parent: privateElement,
element: labelsField.originalLabelsElement,
},
});
}
complexAction.actions.push({
new: { parent: privateElement, element: labelsField.newLabelsElement },
});

return [complexAction];
};
}

export function renderCompasSCL(
sclElement: Element
): TemplateResult[] {
const privateFilenameElement = getCompasSclName(sclElement);
const filename = privateFilenameElement ? privateFilenameElement.textContent : '';
export function renderCompasSCL(sclElement: Element): TemplateResult[] {
let privateElement = getPrivate(sclElement, COMPAS_SCL_PRIVATE_TYPE);
if (!privateElement) {
privateElement = createPrivate(sclElement, COMPAS_SCL_PRIVATE_TYPE);
sclElement.prepend(privateElement);
}
const privateFilenameElement = getCompasSclName(privateElement);
const filename = privateFilenameElement?.textContent ?? '';

return [
html`<wizard-textfield
Expand All @@ -62,12 +89,18 @@ export function renderCompasSCL(
helper="${translate('compas.scl.filenameHelper')}"
required
validationMessage="${translate('textfield.required')}"
dialogInitialFocus>
dialogInitialFocus
>
</wizard-textfield>`,
html`<h3 style="color: var(--mdc-theme-on-surface);">
${translate('compas.scl.labelsTitle')}
</h3>
<compas-labels-field
.privateElement="${privateElement}"
></compas-labels-field>`,
];
}


export function editCompasSCLWizard(sclElement: Element): Wizard {
return [
{
Expand Down
Loading

0 comments on commit 29519c5

Please sign in to comment.