From a053714ac2498cc57f32f9e4f8a665d8a204246e Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Wed, 31 Jul 2024 14:12:34 +0200 Subject: [PATCH 1/9] front: add NGE DTO types --- .../components/MacroEditor/types.ts | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 front/src/applications/operationalStudies/components/MacroEditor/types.ts diff --git a/front/src/applications/operationalStudies/components/MacroEditor/types.ts b/front/src/applications/operationalStudies/components/MacroEditor/types.ts new file mode 100644 index 00000000000..b81a51da887 --- /dev/null +++ b/front/src/applications/operationalStudies/components/MacroEditor/types.ts @@ -0,0 +1,95 @@ +// NGE DTO types, see: +// https://github.com/SchweizerischeBundesbahnen/netzgrafik-editor-frontend/blob/main/src/app/data-structures/business.data.structures.ts + +export type Haltezeit = { + haltezeit: number; + no_halt: boolean; +}; + +export type Node = { + id: number | string; // TODO: in NGE this is a number + /** Trigram */ + betriebspunktName: string; + fullName: string; + positionX: number; + positionY: number; + ports: unknown[]; + transitions: unknown[]; + connections: unknown[]; + resourceId: number; + /** Number of tracks where train can stop */ + perronkanten: number; + /** Time needed to change train in minutes */ + connectionTime: number; + trainrunCategoryHaltezeiten: { [category: string]: Haltezeit }; + symmetryAxis: number; + warnings: unknown[]; + labelIds: number[]; +}; + +export type Trainrun = { + id: number; + name: string; + categoryId: number; + frequencyId: number; + trainrunTimeCategoryId: number; + labelIds: number[]; +}; + +export type TimeLock = { + time: number; + consecutiveTime: number; + lock: boolean; + warning: null; + timeFormatter: null; +}; + +export type TrainrunSection = { + id: number; + sourceNodeId: number | string; + sourcePortId: number; + targetNodeId: number | string; + targetPortId: number; + + sourceArrival: TimeLock; + sourceDeparture: TimeLock; + targetArrival: TimeLock; + targetDeparture: TimeLock; + travelTime: TimeLock; + + numberOfStops: number; + + trainrunId: number; + resourceId: number; + + specificTrainrunSectionFrequencyId: number; + path: null; + warnings: unknown[]; +}; + +export type Resource = { + id: number; + capacity: number; +}; + +/** + * The DTO contains the entire NGE state. + */ +export type NetzgrafikDto = { + nodes: Node[]; + trainrunSections: TrainrunSection[]; + trainruns: Trainrun[]; + resources: Resource[]; + metadata: { + netzgrafikColors: unknown[]; + trainrunCategories: unknown[]; + trainrunFrequencies: unknown[]; + trainrunTimeCategories: unknown[]; + }; + freeFloatingTexts: unknown[]; + labels: unknown[]; + labelGroups: unknown[]; + filterData: { + filterSettings: unknown[]; + }; +}; From 77ba3a4e173475a9ae878959ad14a9cab3475a53 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Wed, 31 Jul 2024 14:12:51 +0200 Subject: [PATCH 2/9] front: add dto property to NGE component --- .../components/MacroEditor/NGE.tsx | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/front/src/applications/operationalStudies/components/MacroEditor/NGE.tsx b/front/src/applications/operationalStudies/components/MacroEditor/NGE.tsx index e641b762ee5..737747da858 100644 --- a/front/src/applications/operationalStudies/components/MacroEditor/NGE.tsx +++ b/front/src/applications/operationalStudies/components/MacroEditor/NGE.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useRef } from 'react'; +import React, { useEffect, useRef, useState } from 'react'; /* eslint-disable import/extensions, import/no-unresolved */ import ngeMain from '@osrd-project/netzgrafik-frontend/dist/netzgrafik-frontend/en/main.js?url'; @@ -8,6 +8,12 @@ import ngeStyles from '@osrd-project/netzgrafik-frontend/dist/netzgrafik-fronten import ngeVendor from '@osrd-project/netzgrafik-frontend/dist/netzgrafik-frontend/en/vendor.js?url'; /* eslint-enable import/extensions, import/no-unresolved */ +import type { NetzgrafikDto } from './types'; + +interface NGEElement extends HTMLElement { + netzgrafikDto: NetzgrafikDto; +} + const frameSrc = ` @@ -22,29 +28,24 @@ const frameSrc = ` `; -const NGE = () => { +const NGE = ({ dto }: { dto?: NetzgrafikDto }) => { const frameRef = useRef(null); + const [ngeRootElement, setNgeRootElement] = useState(null); useEffect(() => { const frame = frameRef.current!; const handleFrameLoad = () => { frame.removeEventListener('load', handleFrameLoad); - const ngeRoot = frame.contentDocument!.createElement('sbb-root'); + const ngeRoot = frame.contentDocument!.createElement('sbb-root') as NGEElement; frame.contentDocument!.body.appendChild(ngeRoot); + setNgeRootElement(ngeRoot); // listens to create, update and delete operations // ngeRoot.addEventListener('operation', (event: Event) => { // console.log('Operation received', (event as CustomEvent).detail); // }); - - // get netzgrafik model from NGE - // let netzgrafikDto = ngeRoot.netzgrafikDto; - - // // set new netzgrafik model with new nodes - // netzgrafikDto.nodes = testNodesDto; - // ngeRoot.netzgrafikDto = netzgrafikDto; }; frame.addEventListener('load', handleFrameLoad); @@ -54,6 +55,12 @@ const NGE = () => { }; }, []); + useEffect(() => { + if (ngeRootElement && dto) { + ngeRootElement.netzgrafikDto = dto; + } + }, [dto, ngeRootElement]); + return