-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtrainrun-section-card.component.ts
More file actions
220 lines (194 loc) · 8.02 KB
/
trainrun-section-card.component.ts
File metadata and controls
220 lines (194 loc) · 8.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import {AfterViewInit, ChangeDetectorRef, Component, Input, OnDestroy, OnInit} from "@angular/core";
import {TrainrunSectionService} from "../../../../services/data/trainrunsection.service";
import {TrainrunService} from "../../../../services/data/trainrun.service";
import {TrainrunsectionHelper} from "../../../../services/util/trainrunsection.helper";
import {takeUntil} from "rxjs/operators";
import {Subject} from "rxjs";
import {Node} from "../../../../models/node.model";
import {Direction, LinePatternRefs} from "../../../../data-structures/business.data.structures";
import {StaticDomTags} from "../../../editor-main-view/data-views/static.dom.tags";
import {ColorRefType} from "../../../../data-structures/technical.data.structures";
import {
TrainrunSectionTimesService,
LeftAndRightTimeStructure,
} from "../../../../services/data/trainrun-section-times.service";
@Component({
selector: "sbb-trainrunsection-card",
templateUrl: "./trainrun-section-card.component.html",
styleUrls: ["./trainrun-section-card.component.scss"],
providers: [TrainrunSectionTimesService],
})
export class TrainrunSectionCardComponent implements OnInit, AfterViewInit, OnDestroy {
@Input() innerContentScaleFactor = "1.0";
public leftNode: Node;
public rightNode: Node;
public frequencyLinePattern: LinePatternRefs;
public categoryColorRef: ColorRefType;
public timeCategoryLinePattern: LinePatternRefs;
public chosenCard: "top" | "bottom";
private destroyed = new Subject<void>();
public get nodesOrdered(): Node[] {
return this.trainrunSectionTimesService.getNodesOrdered();
}
constructor(
private trainrunService: TrainrunService,
private trainrunSectionService: TrainrunSectionService,
private changeDetection: ChangeDetectorRef,
public trainrunSectionTimesService: TrainrunSectionTimesService,
) {
this.trainrunSectionTimesService.setOffset(0);
this.trainrunService.trainruns.pipe(takeUntil(this.destroyed)).subscribe(() => {
this.updateAllValues();
});
this.trainrunSectionService.trainrunSections.pipe(takeUntil(this.destroyed)).subscribe(() => {
this.updateAllValues();
});
}
ngOnInit() {
this.updateAllValues();
}
updateAllValues() {
const selectedTrainrun = this.trainrunService.getSelectedTrainrun();
if (!selectedTrainrun) {
return;
}
const trainrunSection = this.trainrunService.getFirstTrainrunSection(selectedTrainrun);
this.leftNode = this.trainrunService.getLeftOrTopNodeWithTrainrunId(
trainrunSection.getTrainrunId(),
);
this.rightNode = this.trainrunService.getRightOrBottomNodeWithTrainrunId(
trainrunSection.getTrainrunId(),
);
this.trainrunSectionTimesService.setOffset(0);
if (selectedTrainrun.isRoundTrip()) {
// Initialize round trip trainrun with top card
this.onTrainrunSectionCardClick("top");
} else {
this.chosenCard = this.trainrunService.isTrainrunTargetRightOrBottom() ? "top" : "bottom";
}
this.trainrunSectionTimesService.setTrainrunSection(trainrunSection);
this.frequencyLinePattern = trainrunSection.getFrequencyLinePatternRef();
this.categoryColorRef = selectedTrainrun.getCategoryColorRef();
this.timeCategoryLinePattern = selectedTrainrun.getTimeCategoryLinePatternRef();
this.trainrunSectionTimesService.setHighlightTravelTimeElement(false);
this.trainrunSectionTimesService.applyOffsetAndTransformTimeStructure();
}
ngOnDestroy() {
this.destroyed.next();
this.destroyed.complete();
}
ngAfterViewInit(): void {
this.updateAllValues();
this.changeDetection.detectChanges();
}
getInnerContentStyleTag(): string {
return "transform: scale(" + this.innerContentScaleFactor + ");";
}
getEdgeLineClassAttrString(layer: number, cardPosition: string) {
return (
StaticDomTags.EDGE_LINE_CLASS +
StaticDomTags.makeClassTag(StaticDomTags.LINE_LAYER, "" + layer) +
StaticDomTags.makeClassTag(StaticDomTags.FREQ_LINE_PATTERN, this.frequencyLinePattern) +
" " +
StaticDomTags.TAG_UI_DIALOG +
" " +
StaticDomTags.makeClassTag(StaticDomTags.TAG_COLOR_REF, this.getColorRefTag(cardPosition)) +
StaticDomTags.makeClassTag(StaticDomTags.TAG_LINEPATTERN_REF, this.timeCategoryLinePattern)
);
}
getColorRefTag(cardPosition: string) {
return (
this.categoryColorRef +
" " +
(this.chosenCard === cardPosition
? StaticDomTags.TAG_FOCUS
: this.chosenCard === null
? ""
: StaticDomTags.TAG_MUTED)
);
}
getOneWayCardBetriebspunktClassTag(cardPosition: string) {
return "OneWayCardBetriebspunkt " + this.getColorRefTag(cardPosition);
}
getEdgeLineTextClass(cardPosition: string) {
return (
StaticDomTags.EDGE_LINE_TEXT_CLASS +
" " +
StaticDomTags.makeClassTag(StaticDomTags.TAG_COLOR_REF, this.getColorRefTag(cardPosition))
);
}
getEdgeLineArrowClass(cardPosition: string) {
return (
StaticDomTags.EDGE_LINE_ARROW_CLASS +
" " +
StaticDomTags.makeClassTag(StaticDomTags.TAG_COLOR_REF, this.getColorRefTag(cardPosition))
);
}
onTrainrunSectionCardClick(position: "top" | "bottom") {
if (this.chosenCard === position) {
return;
}
const selectedTrainrun = this.trainrunService.getSelectedTrainrun();
if (!selectedTrainrun) {
return;
}
let trainrunSection = this.trainrunService.getFirstTrainrunSection(selectedTrainrun);
if (selectedTrainrun.isRoundTrip()) {
// For a round trip trainrun, we want to choose the most top/left
// section as reference when switching to a one-way trainrun
trainrunSection = this.trainrunService.getLeftOrTopExtremitySection();
}
const referenceNode = position === "top" ? this.leftNode : this.rightNode;
let isTrainInverted = false;
if (referenceNode !== trainrunSection.getSourceNode()) {
isTrainInverted = true;
this.trainrunSectionService.invertTrainrunSectionsSourceAndTarget(
trainrunSection.getTrainrunId(),
);
}
this.chosenCard = position;
this.trainrunService.updateDirection(selectedTrainrun, Direction.ONE_WAY, isTrainInverted);
}
getTrainrunTimeStructure(): Omit<LeftAndRightTimeStructure, "travelTime"> {
const selectedTrainrun = this.trainrunService.getSelectedTrainrun();
if (!selectedTrainrun) {
return undefined;
}
const selectedTrainrunId = selectedTrainrun.getId();
const trainrunSections =
this.trainrunSectionService.getAllTrainrunSectionsForTrainrun(selectedTrainrunId);
const [startNode, endNode] = [
this.trainrunService.getLeftOrTopNodeWithTrainrunId(selectedTrainrunId),
this.trainrunService.getRightOrBottomNodeWithTrainrunId(selectedTrainrunId),
];
// Try to find startNode → endNode
let firstTrainrunSection = trainrunSections.find(
(ts) => ts.getSourceNodeId() === startNode.getId(),
);
let lastTrainrunSection = [...trainrunSections]
.reverse()
.find((ts) => ts.getTargetNodeId() === endNode.getId());
// If not found, swap first and last sections (and source and target nodes)
if (!firstTrainrunSection && !lastTrainrunSection) {
firstTrainrunSection = trainrunSections.find(
(ts) => ts.getSourceNodeId() === endNode.getId(),
);
lastTrainrunSection = [...trainrunSections]
.reverse()
.find((ts) => ts.getTargetNodeId() === startNode.getId());
[firstTrainrunSection, lastTrainrunSection] = [lastTrainrunSection, firstTrainrunSection];
return {
leftDepartureTime: firstTrainrunSection.getTargetDeparture(),
leftArrivalTime: firstTrainrunSection.getTargetArrival(),
rightDepartureTime: lastTrainrunSection.getSourceDeparture(),
rightArrivalTime: lastTrainrunSection.getSourceArrival(),
};
}
return {
leftDepartureTime: firstTrainrunSection.getSourceDeparture(),
leftArrivalTime: firstTrainrunSection.getSourceArrival(),
rightDepartureTime: lastTrainrunSection.getTargetDeparture(),
rightArrivalTime: lastTrainrunSection.getTargetArrival(),
};
}
}