Skip to content

Commit f0cea11

Browse files
feat: add AppComponent inputs/outputs
Add inputs and outputs so that a custom element user can interact with NGE. A netzgrafikDto input is introduced to get/set the data service's current DTO. A operation output is introduced to notify about DTO changes. The changes are described by a new Operation class. Co-authored-by: Simon Ser <contact@emersion.fr>
1 parent cc4c56b commit f0cea11

File tree

4 files changed

+79
-5
lines changed

4 files changed

+79
-5
lines changed

src/app/app.component.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
import {Component} from "@angular/core";
1+
import {Component, Input, Output} from "@angular/core";
22
import {AuthService} from "./services/auth/auth.service";
3+
import {TrainrunService} from "./services/data/trainrun.service";
4+
import {TrainrunSectionService} from "./services/data/trainrunsection.service";
5+
import {DataService} from "./services/data/data.service";
36
import {environment} from "../environments/environment";
47
import packageJson from "../../package.json";
5-
import {Observable} from "rxjs";
8+
import {Observable, merge} from "rxjs";
69
import {ProjectDto} from "./api/generated";
10+
import {NetzgrafikDto} from "./data-structures/business.data.structures";
11+
import {Operation} from "./models/operation.model";
712

813
@Component({
914
selector: "sbb-root",
@@ -33,7 +38,7 @@ export class AppComponent {
3338
return this.authService.claims?.email;
3439
}
3540

36-
constructor(private authService: AuthService) {
41+
constructor(private authService: AuthService, private dataService: DataService, private trainrunService: TrainrunService, private trainrunSectionService: TrainrunSectionService) {
3742
if (!this.disableBackend) {
3843
this.authenticated = authService.initialized;
3944
}
@@ -44,4 +49,15 @@ export class AppComponent {
4449
this.authService.logOut();
4550
}
4651
}
52+
53+
@Input()
54+
get netzgrafikDto() {
55+
return this.dataService.getNetzgrafikDto();
56+
}
57+
set netzgrafikDto(netzgrafikDto: NetzgrafikDto) {
58+
this.dataService.loadNetzgrafikDto(netzgrafikDto);
59+
}
60+
61+
@Output()
62+
operation: Observable<Operation> = merge(this.trainrunService.operation, this.trainrunSectionService.operation);
4763
}

src/app/models/operation.model.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {Trainrun} from "./trainrun.model";
2+
import {TrainrunSection} from "./trainrunsection.model";
3+
4+
enum OperationType {
5+
create = "create",
6+
update = "update",
7+
delete = "delete"
8+
}
9+
10+
export abstract class Operation {
11+
readonly type: OperationType;
12+
13+
constructor(type: OperationType) {
14+
this.type = type;
15+
}
16+
}
17+
18+
export class CreateTrainrunOperation extends Operation {
19+
readonly trainrunSection: TrainrunSection;
20+
21+
constructor(trainrunSection: TrainrunSection) {
22+
super(OperationType.create);
23+
this.trainrunSection = trainrunSection;
24+
}
25+
}
26+
27+
export class UpdateTrainrunSectionsOperation extends Operation {
28+
readonly trainrunSections: TrainrunSection[];
29+
30+
constructor(trainrunSections: TrainrunSection[]) {
31+
super(OperationType.update);
32+
this.trainrunSections = trainrunSections;
33+
}
34+
}
35+
36+
export class DeleteTrainrunOperation extends Operation {
37+
readonly trainrun: Trainrun;
38+
39+
constructor(trainrun: Trainrun) {
40+
super(OperationType.delete);
41+
this.trainrun = trainrun;
42+
}
43+
}

src/app/services/data/trainrun.service.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
TrainrunFrequency,
99
TrainrunTimeCategory,
1010
} from "../../data-structures/business.data.structures";
11-
import {Injectable} from "@angular/core";
11+
import {EventEmitter, Injectable} from "@angular/core";
1212
import {BehaviorSubject} from "rxjs";
1313
import {NodeService} from "./node.service";
1414
import {TrainrunSectionService} from "./trainrunsection.service";
@@ -23,6 +23,7 @@ import {FilterService} from "../ui/filter.service";
2323
import {Transition} from "../../models/transition.model";
2424
import {Port} from "../../models/port.model";
2525
import {Connection} from "../../models/connection.model";
26+
import {DeleteTrainrunOperation, Operation} from "../../models/operation.model";
2627

2728
@Injectable({
2829
providedIn: "root",
@@ -34,6 +35,8 @@ export class TrainrunService {
3435

3536
trainrunsStore: { trainruns: Trainrun[] } = {trainruns: []}; // store the data in memory
3637

38+
readonly operation = new EventEmitter<Operation>();
39+
3740
private dataService: DataService = null;
3841
private nodeService: NodeService = null;
3942
private trainrunSectionService: TrainrunSectionService = null;
@@ -180,6 +183,7 @@ export class TrainrunService {
180183
if (enforceUpdate) {
181184
this.trainrunsUpdated();
182185
}
186+
this.operation.emit(new DeleteTrainrunOperation(trainrun));
183187
}
184188

185189
getSelectedTrainrun(): Trainrun {

src/app/services/data/trainrunsection.service.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
TrainrunSectionDto,
66
} from "../../data-structures/business.data.structures";
77
import {Node} from "../../models/node.model";
8-
import {Injectable, OnDestroy} from "@angular/core";
8+
import {Injectable, OnDestroy, EventEmitter} from "@angular/core";
99
import {BehaviorSubject, Subject} from "rxjs";
1010
import {TrainrunService} from "./trainrun.service";
1111
import {NodeService} from "./node.service";
@@ -22,6 +22,7 @@ import {Transition} from "../../models/transition.model";
2222
import {takeUntil} from "rxjs/operators";
2323
import {FilterService} from "../ui/filter.service";
2424
import {TrainrunSectionNodePair} from "../util/trainrun.iterator";
25+
import {CreateTrainrunOperation, Operation, UpdateTrainrunSectionsOperation} from "../../models/operation.model";
2526

2627
interface DepartureAndArrivalTimes {
2728
nodeFromDepartureTime: number;
@@ -46,6 +47,8 @@ export class TrainrunSectionService implements OnDestroy {
4647
trainrunSections: [],
4748
}; // store the data in memory
4849

50+
readonly operation = new EventEmitter<Operation>();
51+
4952
informSelectedTrainrunClickSubject =
5053
new BehaviorSubject<InformSelectedTrainrunClick>({
5154
trainrunSectionId: undefined,
@@ -668,6 +671,8 @@ export class TrainrunSectionService implements OnDestroy {
668671

669672
createTrainrunSection(sourceNodeId: number, targetNodeId: number, retrieveTravelTimeFromEdge: boolean = false) {
670673
const trainrunSection: TrainrunSection = new TrainrunSection();
674+
const initialTrainrunsLength = this.trainrunService.trainrunsStore.trainruns.length;
675+
671676
trainrunSection.setTrainrun(
672677
this.trainrunService.getSelectedOrNewTrainrun(),
673678
);
@@ -698,6 +703,12 @@ export class TrainrunSectionService implements OnDestroy {
698703
this.propagateTimesForNewTrainrunSection(trainrunSection);
699704
//this.trainrunSectionsUpdated();
700705
this.trainrunService.trainrunsUpdated();
706+
707+
if (initialTrainrunsLength !== this.trainrunService.trainrunsStore.trainruns.length) {
708+
this.operation.emit(new CreateTrainrunOperation(trainrunSection));
709+
} else {
710+
this.operation.emit(new UpdateTrainrunSectionsOperation(this.getAllTrainrunSectionsForTrainrun(trainrunSection.getTrainrunId())));
711+
}
701712
}
702713

703714
reconnectTrainrunSection(

0 commit comments

Comments
 (0)