Skip to content

Commit 621bcb2

Browse files
committed
bootstrap customElement + emit output create/update/delete operations
1 parent 43a0b02 commit 621bcb2

File tree

8 files changed

+77
-39
lines changed

8 files changed

+77
-39
lines changed

src/app/app.component.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,7 @@ export class AppComponent {
3737
return this.authService.claims?.email;
3838
}
3939

40-
constructor(private authService: AuthService, private trainrunService: TrainrunService, private trainrunSectionService: TrainrunSectionService, private dataService: DataService) {
41-
42-
/*trainrunService.trainruns.subscribe((value) => {
43-
console.log('trainrunService', value);
44-
});
45-
46-
trainrunSectionService.trainrunSections.subscribe((value) => {
47-
console.log('trainrunSectionService', value);
48-
});*/
49-
50-
/*trainrunSectionService.trainrunSectionCreated.subscribe((trainrunSection) => {
51-
console.log('trainrunSectionCreated', trainrunSection);
52-
});*/
40+
constructor(private authService: AuthService, private dataService: DataService, private trainrunService: TrainrunService, private trainrunSectionService: TrainrunSectionService) {
5341

5442
if (!this.disableBackend) {
5543
this.authenticated = authService.initialized;
@@ -63,13 +51,16 @@ export class AppComponent {
6351
}
6452

6553
@Input()
66-
get dto() {
54+
get netzgrafikDto() {
6755
return this.dataService.getNetzgrafikDto();
6856
}
69-
set dto(dto: NetzgrafikDto) {
70-
this.dataService.loadNetzgrafikDto(dto);
57+
set netzgrafikDto(netzgrafikDto: NetzgrafikDto) {
58+
this.dataService.loadNetzgrafikDto(netzgrafikDto);
7159
}
7260

7361
@Output()
74-
trainrunSectionOperation = this.trainrunSectionService.trainrunSectionOperation;
62+
trainrunOperation = this.trainrunService.operation;
63+
64+
@Output()
65+
trainrunSectionOperation = this.trainrunSectionService.operation;
7566
}

src/app/app.module.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {NgModule, Injector} from "@angular/core";
1+
import {NgModule, Injector, ApplicationRef} from "@angular/core";
22
import {NgxEditorModule} from "ngx-editor";
33
import {BrowserModule} from "@angular/platform-browser";
44
import {createCustomElement} from "@angular/elements";
@@ -231,17 +231,21 @@ import {ActionMenuComponent} from "./view/action-menu/action-menu/action-menu.co
231231
SbbBreadcrumbModule,
232232
SbbAutocompleteModule,
233233
],
234-
//bootstrap: [AppComponent],
235234
providers: [
236235
{provide: BASE_PATH, useValue: environment.backendUrl},
237236
{provide: HTTP_INTERCEPTORS, useClass: HttpErrorInterceptor, multi: true},
238237
],
239238
})
239+
240240
export class AppModule {
241241
constructor(private injector: Injector) {}
242242

243-
ngDoBootstrap() {
244-
const element = createCustomElement(AppComponent, { injector: this.injector });
245-
customElements.define("sbb-root", element);
243+
ngDoBootstrap(appRef: ApplicationRef) {
244+
if (environment.customElement) {
245+
const element = createCustomElement(AppComponent, { injector: this.injector });
246+
customElements.define("sbb-root", element);
247+
} else {
248+
appRef.bootstrap(AppComponent);
249+
}
246250
}
247251
}

src/app/models/operation.model.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
type: String;
12+
}
13+
14+
export class CreateTrainrunOperation extends Operation {
15+
readonly trainrunSection: TrainrunSection;
16+
17+
constructor(trainrunSection: TrainrunSection){
18+
super();
19+
this.type = OperationType.create;
20+
this.trainrunSection = trainrunSection;
21+
}
22+
}
23+
24+
export class UpdateTrainrunSectionsOperation extends Operation {
25+
readonly trainrunSections: TrainrunSection[];
26+
27+
constructor(trainrunSections: TrainrunSection[]){
28+
super();
29+
this.type = OperationType.update;
30+
this.trainrunSections = trainrunSections;
31+
}
32+
}
33+
34+
export class DeleteTrainrunOperation extends Operation {
35+
readonly trainrun: Trainrun;
36+
37+
constructor(trainrun: Trainrun){
38+
super();
39+
this.type = OperationType.delete;
40+
this.trainrun = trainrun;
41+
}
42+
}

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: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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;
@@ -35,11 +36,6 @@ export interface InformSelectedTrainrunClick {
3536
open: boolean;
3637
}
3738

38-
export class TrainrunSectionOperation {
39-
type: 'create' | 'update' | 'delete';
40-
trainrunSection: TrainrunSection;
41-
}
42-
4339
@Injectable({
4440
providedIn: "root",
4541
})
@@ -51,7 +47,7 @@ export class TrainrunSectionService implements OnDestroy {
5147
trainrunSections: [],
5248
}; // store the data in memory
5349

54-
trainrunSectionOperation = new EventEmitter<TrainrunSectionOperation>();
50+
readonly operation = new EventEmitter<Operation>();
5551

5652
informSelectedTrainrunClickSubject =
5753
new BehaviorSubject<InformSelectedTrainrunClick>({
@@ -675,6 +671,8 @@ export class TrainrunSectionService implements OnDestroy {
675671

676672
createTrainrunSection(sourceNodeId: number, targetNodeId: number, retrieveTravelTimeFromEdge: boolean = false) {
677673
const trainrunSection: TrainrunSection = new TrainrunSection();
674+
const initialTrainrunsLength = this.trainrunService.trainrunsStore.trainruns.length;
675+
678676
trainrunSection.setTrainrun(
679677
this.trainrunService.getSelectedOrNewTrainrun(),
680678
);
@@ -689,11 +687,6 @@ export class TrainrunSectionService implements OnDestroy {
689687
const targetNode = this.nodeService.getNodeFromId(targetNodeId);
690688
trainrunSection.setSourceAndTargetNodeReference(sourceNode, targetNode);
691689
this.trainrunSectionsStore.trainrunSections.push(trainrunSection);
692-
this.logger.log(
693-
"create new trainrunSection between nodes",
694-
sourceNode.getBetriebspunktName(),
695-
targetNode.getBetriebspunktName(),
696-
);
697690

698691
this.handleNodeAndTrainrunSectionDetails(
699692
sourceNode,
@@ -706,10 +699,11 @@ export class TrainrunSectionService implements OnDestroy {
706699
//this.trainrunSectionsUpdated();
707700
this.trainrunService.trainrunsUpdated();
708701

709-
this.trainrunSectionOperation.emit({
710-
type: 'create',
711-
trainrunSection: trainrunSection,
712-
});
702+
if (initialTrainrunsLength != this.trainrunService.trainrunsStore.trainruns.length) {
703+
this.operation.emit(new CreateTrainrunOperation(trainrunSection));
704+
} else {
705+
this.operation.emit(new UpdateTrainrunSectionsOperation(this.getAllTrainrunSectionsForTrainrun(trainrunSection.getTrainrunId())));
706+
}
713707
}
714708

715709
reconnectTrainrunSection(

src/environments/environment.model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ export interface Environment {
66
backendUrl: string;
77
authConfig: AuthConfig;
88
disableBackend: boolean;
9+
customElement: boolean;
910
}

src/environments/environment.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export const environment: Environment = {
2323
label: "local",
2424
backendUrl: "http://localhost:8080",
2525
authConfig,
26-
disableBackend: false
26+
disableBackend: false,
27+
customElement: false,
2728
};
2829

2930
/*

src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ if (environment.production) {
88
}
99

1010
environment.disableBackend = true;
11+
environment.customElement = true;
1112

1213
platformBrowserDynamic()
1314
.bootstrapModule(AppModule)

0 commit comments

Comments
 (0)