Skip to content

Commit dd8716d

Browse files
author
Dennis Labordus
authored
Merge pull request #176 from com-pas/ied-compare-from-compas
Use Compas Open Component to select Template Project in Compare IED plugin
2 parents be0886c + d628780 commit dd8716d

File tree

12 files changed

+235
-283
lines changed

12 files changed

+235
-283
lines changed

public/js/plugins.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,9 @@ export const officialPlugins = [
213213
},
214214
{
215215
name: 'Compare IED',
216-
src: '/src/menu/CompareIED.js',
216+
src: '/src/menu/CompasCompareIED.js',
217217
icon: 'compare_arrows',
218-
default: false,
218+
default: true,
219219
kind: 'menu',
220220
requireDoc: true,
221221
position: 'middle',

src/compas-editors/CompasVersions.ts

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,65 @@ import '@material/mwc-list';
1515
import '@material/mwc-list/mwc-list-item';
1616
import '@material/mwc-list/mwc-check-list-item';
1717

18+
import { MultiSelectedEvent } from '@material/mwc-list/mwc-list-foundation';
19+
1820
import {
1921
newLogEvent,
2022
newOpenDocEvent,
2123
newWizardEvent,
2224
Wizard,
2325
} from '../foundation.js';
24-
import { MultiSelectedEvent } from '@material/mwc-list/mwc-list-foundation';
26+
import { renderDiff } from '../foundation/compare.js';
2527

2628
import {
2729
CompasSclDataService,
2830
SDS_NAMESPACE,
2931
} from '../compas-services/CompasSclDataService.js';
3032
import { createLogEvent } from '../compas-services/foundation.js';
3133
import {
34+
compareVersions,
3235
getTypeFromDocName,
3336
updateDocumentInOpenSCD,
3437
} from '../compas/foundation.js';
3538
import { addVersionToCompasWizard } from '../compas/CompasUploadVersion.js';
36-
import { compareWizard } from '../compas/CompasCompareDialog.js';
3739
import { getElementByName, styles } from './foundation.js';
3840
import { wizards } from '../wizards/wizard-library.js';
3941

42+
interface CompareOptions {
43+
title: string;
44+
}
45+
46+
function compareWizard(
47+
plugin: Element,
48+
oldElement: Element,
49+
newElement: Element,
50+
options: CompareOptions
51+
): Wizard {
52+
function renderDialogContent(): TemplateResult {
53+
return html` ${renderDiff(newElement, oldElement) ??
54+
html`${translate('compas.compare.noDiff')}`}`;
55+
}
56+
57+
function close() {
58+
return function () {
59+
plugin.dispatchEvent(newWizardEvent());
60+
return [];
61+
};
62+
}
63+
64+
return [
65+
{
66+
title: options.title,
67+
secondary: {
68+
icon: '',
69+
label: get('close'),
70+
action: close(),
71+
},
72+
content: [renderDialogContent()],
73+
},
74+
];
75+
}
76+
4077
/** An editor [[`plugin`]] for selecting the `Substation` section. */
4178
export default class CompasVersionsPlugin extends LitElement {
4279
@property()
@@ -244,15 +281,15 @@ export default class CompasVersionsPlugin extends LitElement {
244281
const selectedVersions = this.getSelectedVersions();
245282
if (selectedVersions.length === 1) {
246283
const oldVersion = selectedVersions[0];
284+
247285
const oldScl = await this.getVersion(oldVersion);
248286
const newScl = this.doc.documentElement;
249287

250288
this.dispatchEvent(
251289
newWizardEvent(
252290
compareWizard(this, oldScl, newScl, {
253-
title: get('compas.compare.title', {
291+
title: get('compas.compare.titleCurrent', {
254292
oldVersion: oldVersion,
255-
newVersion: 'current',
256293
}),
257294
})
258295
)
@@ -274,8 +311,9 @@ export default class CompasVersionsPlugin extends LitElement {
274311
async compareVersions(): Promise<void> {
275312
const selectedVersions = this.getSelectedVersions();
276313
if (selectedVersions.length === 2) {
277-
const oldVersion = selectedVersions[0];
278-
const newVersion = selectedVersions[1];
314+
const sortedVersions = selectedVersions.slice().sort(compareVersions);
315+
const oldVersion = sortedVersions[0];
316+
const newVersion = sortedVersions[1];
279317

280318
const oldScl = await this.getVersion(oldVersion);
281319
const newScl = await this.getVersion(newVersion);

src/compas/CompasCompareDialog.ts

Lines changed: 0 additions & 204 deletions
This file was deleted.

src/compas/foundation.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,44 @@ export function updateDocumentInOpenSCD(
6565
newOpenDocEvent(doc, docName, { detail: { docId: id } })
6666
);
6767
}
68+
69+
export function compareVersions(
70+
leftVersion: string,
71+
rightVersion: string
72+
): number {
73+
// Function to compare parts of the version.
74+
function comparePart(leftPart: string, rightPart: string): number {
75+
// First make convert them to number and check if the strings are numbers.
76+
const leftNumber = parseInt(leftPart);
77+
const rightNumber = parseInt(rightPart);
78+
if (isNaN(leftNumber) || isNaN(rightNumber)) {
79+
return 0;
80+
}
81+
// Now compare the two numbers.
82+
return leftNumber < rightNumber ? -1 : leftNumber > rightNumber ? 1 : 0;
83+
}
84+
85+
// If the strings are the same, just return 0, because they are the same.
86+
if (leftVersion.localeCompare(rightVersion) == 0) {
87+
return 0;
88+
}
89+
90+
// Split the version into parts.
91+
const leftParts = leftVersion.split('.');
92+
const rightParts = rightVersion.split('.');
93+
94+
// Version should exist out of 3 parts, major, minor, patch
95+
if (leftParts.length != 3 && rightParts.length != 3) {
96+
return 0;
97+
}
98+
99+
// Now first compare the major version, if they are the same repeat for minor version and patch version.
100+
let result = comparePart(leftParts[0], rightParts[0]);
101+
if (result === 0) {
102+
result = comparePart(leftParts[1], rightParts[1]);
103+
if (result === 0) {
104+
result = comparePart(leftParts[2], rightParts[2]);
105+
}
106+
}
107+
return result;
108+
}

src/menu/CompasCompareIED.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { html, TemplateResult } from 'lit-element';
2+
3+
import '../compas/CompasOpen.js';
4+
5+
import { DocRetrievedEvent } from '../compas/CompasOpen.js';
6+
7+
import CompareIEDPlugin from './CompareIED.js';
8+
9+
export default class CompasCompareIEDPlugin extends CompareIEDPlugin {
10+
/**
11+
* Overwriting the render function for opening the template project.
12+
* Now it will also be possible to select the template project from the CoMPAS Data Service.
13+
*
14+
* @override
15+
*/
16+
protected renderSelectTemplateFile(): TemplateResult {
17+
return html`<compas-open
18+
@docRetrieved=${(evt: DocRetrievedEvent) => {
19+
this.templateDoc = evt.detail.doc;
20+
}}
21+
></compas-open>
22+
${this.renderCloseButton()}`;
23+
}
24+
}

src/translations/de.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,8 @@ export const de: Translations = {
854854
},
855855
compare: {
856856
title: '???',
857+
titleCurrent: '???',
858+
noDiff: '???',
857859
attributes: 'Attribute',
858860
children: 'Kindelemente',
859861
},

0 commit comments

Comments
 (0)