Skip to content
Snippets Groups Projects
Commit 6662a438 authored by jojohoch's avatar jojohoch
Browse files

[player] Adopt the order of `UnitPages` for form

* Since always visible pages are removed from the order, form
arrays must be rearranged
* Refactor getter as properties in `PlayerStateComponent`. Getter
work like functions and are executed every time Angular change
detection runs.
parent 133c2db7
No related branches found
No related tags found
No related merge requests found
...@@ -21,4 +21,5 @@ export interface ChildFormGroup { ...@@ -21,4 +21,5 @@ export interface ChildFormGroup {
formGroup: FormGroup; formGroup: FormGroup;
parentForm: FormGroup; parentForm: FormGroup;
parentArray: 'pages' | 'sections' | 'elements' parentArray: 'pages' | 'sections' | 'elements'
parentArrayIndex: number;
} }
...@@ -27,6 +27,7 @@ import { FormService } from '../../../../common/form.service'; ...@@ -27,6 +27,7 @@ import { FormService } from '../../../../common/form.service';
export class ElementOverlayComponent implements OnInit, OnDestroy { export class ElementOverlayComponent implements OnInit, OnDestroy {
@Input() elementModel!: UnitUIElement; @Input() elementModel!: UnitUIElement;
@Input() parentForm!: FormGroup; @Input() parentForm!: FormGroup;
@Input() parentArrayIndex!: number;
private elementForm!: FormGroup; private elementForm!: FormGroup;
@ViewChild('elementComponentContainer', @ViewChild('elementComponentContainer',
...@@ -72,7 +73,8 @@ export class ElementOverlayComponent implements OnInit, OnDestroy { ...@@ -72,7 +73,8 @@ export class ElementOverlayComponent implements OnInit, OnDestroy {
this.formService.registerFormGroup({ this.formService.registerFormGroup({
formGroup: this.elementForm, formGroup: this.elementForm,
parentForm: this.parentForm, parentForm: this.parentForm,
parentArray: 'elements' parentArray: 'elements',
parentArrayIndex: this.parentArrayIndex
}); });
} }
......
...@@ -84,7 +84,11 @@ export class FormComponent implements OnDestroy { ...@@ -84,7 +84,11 @@ export class FormComponent implements OnDestroy {
private addGroup = (group: ChildFormGroup): void => { private addGroup = (group: ChildFormGroup): void => {
const formArray: FormArray = group.parentForm.get(group.parentArray) as FormArray; const formArray: FormArray = group.parentForm.get(group.parentArray) as FormArray;
formArray.push(group.formGroup); if (group.parentArrayIndex < formArray.length) {
formArray.insert(group.parentArrayIndex, group.formGroup);
} else {
formArray.push(group.formGroup);
}
}; };
private onElementValueChanges = (value: ValueChangeElement): void => { private onElementValueChanges = (value: ValueChangeElement): void => {
......
...@@ -10,6 +10,7 @@ import { FormService } from '../../../../common/form.service'; ...@@ -10,6 +10,7 @@ import { FormService } from '../../../../common/form.service';
template: ` template: `
<app-section *ngFor="let section of page.sections" <app-section *ngFor="let section of page.sections"
[parentForm]="pageForm" [parentForm]="pageForm"
[parentArrayIndex]="parentArrayIndex"
[section]="section" [section]="section"
[ngStyle]="{ [ngStyle]="{
position: 'relative', position: 'relative',
...@@ -25,6 +26,7 @@ import { FormService } from '../../../../common/form.service'; ...@@ -25,6 +26,7 @@ import { FormService } from '../../../../common/form.service';
export class PageComponent implements OnInit { export class PageComponent implements OnInit {
@Input() page!: UnitPage; @Input() page!: UnitPage;
@Input() parentForm!: FormGroup; @Input() parentForm!: FormGroup;
@Input() parentArrayIndex!: number;
pageForm!: FormGroup; pageForm!: FormGroup;
constructor(private formService: FormService, private formBuilder: FormBuilder) {} constructor(private formService: FormService, private formBuilder: FormBuilder) {}
...@@ -37,7 +39,8 @@ export class PageComponent implements OnInit { ...@@ -37,7 +39,8 @@ export class PageComponent implements OnInit {
this.formService.registerFormGroup({ this.formService.registerFormGroup({
formGroup: this.pageForm, formGroup: this.pageForm,
parentForm: this.parentForm, parentForm: this.parentForm,
parentArray: 'pages' parentArray: 'pages',
parentArrayIndex: this.parentArrayIndex
}); });
} }
} }
<div *ngIf="!running" class='stopped-overlay'></div> <div *ngIf="!running" class='stopped-overlay'></div>
<div fxLayout="row"> <div fxLayout="row">
<mat-tab-group [fxFlex]="pageWidth" [(selectedIndex)]="currentIndex" <mat-tab-group *ngIf="hasScrollPages" [fxFlex]="pageWidth" [(selectedIndex)]="currentIndex"
(selectedIndexChange)="onSelectedIndexChange()" (selectedIndexChange)="onSelectedIndexChange()"
mat-align-tabs="start"> mat-align-tabs="start">
<mat-tab *ngFor="let page of scrollPages; let i = index" label="{{'pageIndication' | translate: {index: i + 1} }}"> <ng-container *ngFor="let page of pages; let i = index">
<app-page class="page" [parentForm]="parenForm" [page]="page"></app-page> <mat-tab *ngIf="!page.alwaysVisible" label="{{'pageIndication' | translate: {index: pageIndices[i] + 1} }}">
</mat-tab> <app-page class="page" [parentArrayIndex]="i" [parentForm]="parenForm" [page]="page"></app-page>
</mat-tab>
</ng-container>
</mat-tab-group> </mat-tab-group>
<div *ngIf="alwaysVisiblePage" fxFlex="50"> <div *ngIf="alwaysVisiblePage" [fxFlex]="pageWidth">
<div class="mat-tab-label">{{'alwaysVisiblePage' | translate}}</div> <div class="mat-tab-label">{{'alwaysVisiblePage' | translate}}</div>
<app-page class="page" [parentForm]="parenForm" [page]="alwaysVisiblePage"></app-page> <app-page class="page" [parentArrayIndex]="alwaysVisiblePageIndex" [parentForm]="parenForm" [page]="alwaysVisiblePage"></app-page>
</div> </div>
</div> </div>
...@@ -21,8 +21,17 @@ import { VeronaPostService } from '../../services/verona-post.service'; ...@@ -21,8 +21,17 @@ import { VeronaPostService } from '../../services/verona-post.service';
export class PlayerStateComponent implements OnInit, OnDestroy { export class PlayerStateComponent implements OnInit, OnDestroy {
@Input() parenForm!: FormGroup; @Input() parenForm!: FormGroup;
@Input() pages!: UnitPage[]; @Input() pages!: UnitPage[];
currentIndex: number = 0; currentIndex: number = 0;
pageIndices!: number[];
scrollPages!: UnitPage[];
hasScrollPages!: boolean;
pageWidth!: number;
validPages!: Record<string, string> [];
alwaysVisiblePage!: UnitPage | undefined;
alwaysVisiblePageIndex!: number;
running: boolean = true; running: boolean = true;
private ngUnsubscribe = new Subject<void>(); private ngUnsubscribe = new Subject<void>();
constructor(private veronaSubscriptionService: VeronaSubscriptionService, constructor(private veronaSubscriptionService: VeronaSubscriptionService,
...@@ -30,29 +39,9 @@ export class PlayerStateComponent implements OnInit, OnDestroy { ...@@ -30,29 +39,9 @@ export class PlayerStateComponent implements OnInit, OnDestroy {
private translateService: TranslateService) { private translateService: TranslateService) {
} }
private get state(): RunningState {
return this.running ? 'running' : 'stopped';
}
get pageWidth(): number {
return this.alwaysVisiblePage ? 50 : 100;
}
get validPages():Record<string, string>[] {
return this.scrollPages.map((page: UnitPage, index: number): Record<string, string> => (
{ [page.id]: `${this.translateService.instant('pageIndication', { index: index + 1 })}` }));
}
get alwaysVisiblePage(): UnitPage | undefined {
return this.pages.find((page: UnitPage): boolean => page.alwaysVisible);
}
get scrollPages(): UnitPage[] {
return this.pages.filter((page: UnitPage): boolean => !page.alwaysVisible);
}
ngOnInit(): void { ngOnInit(): void {
this.initSubscriptions(); this.initSubscriptions();
this.initPageState();
this.sendVopStateChangedNotification(); this.sendVopStateChangedNotification();
} }
...@@ -71,6 +60,28 @@ export class PlayerStateComponent implements OnInit, OnDestroy { ...@@ -71,6 +60,28 @@ export class PlayerStateComponent implements OnInit, OnDestroy {
.subscribe((message: VopGetStateRequest): void => this.onGetStateRequest(message)); .subscribe((message: VopGetStateRequest): void => this.onGetStateRequest(message));
} }
private initPageState() {
this.alwaysVisiblePageIndex = this.pages.findIndex((page: UnitPage): boolean => page.alwaysVisible);
this.alwaysVisiblePage = this.pages[this.alwaysVisiblePageIndex];
this.scrollPages = this.pages.filter((page: UnitPage): boolean => !page.alwaysVisible);
this.hasScrollPages = this.scrollPages && this.scrollPages.length > 0;
this.pageWidth = !this.alwaysVisiblePage || !this.hasScrollPages ? 100 : 50;
this.validPages = this.scrollPages.map((page: UnitPage, index: number): Record<string, string> => (
{ [page.id]: `${this.translateService.instant('pageIndication', { index: index + 1 })}` }));
this.pageIndices = this.pages.map(
(page: UnitPage, index: number): number => {
if (index === this.alwaysVisiblePageIndex) {
return this.pages.length - 1;
}
return (this.alwaysVisiblePageIndex < 0 || index < this.alwaysVisiblePageIndex) ? index : index - 1;
}
);
}
private get state(): RunningState {
return this.running ? 'running' : 'stopped';
}
onSelectedIndexChange(): void { onSelectedIndexChange(): void {
this.sendVopStateChangedNotification(); this.sendVopStateChangedNotification();
} }
......
...@@ -11,7 +11,8 @@ import { FormService } from '../../../../common/form.service'; ...@@ -11,7 +11,8 @@ import { FormService } from '../../../../common/form.service';
<app-element-overlay <app-element-overlay
*ngFor="let element of section.elements" *ngFor="let element of section.elements"
[elementModel]="element" [elementModel]="element"
[parentForm]="sectionForm"> [parentForm]="sectionForm"
[parentArrayIndex]="parentArrayIndex">
</app-element-overlay> </app-element-overlay>
` `
}) })
...@@ -19,8 +20,10 @@ export class SectionComponent implements OnInit { ...@@ -19,8 +20,10 @@ export class SectionComponent implements OnInit {
@Input() parentForm!: FormGroup; @Input() parentForm!: FormGroup;
@Input() section!: UnitPageSection; @Input() section!: UnitPageSection;
@Input() sectionForm!: FormGroup; @Input() sectionForm!: FormGroup;
@Input() parentArrayIndex!: number;
constructor(private formService: FormService, private formBuilder: FormBuilder) {} constructor(private formService: FormService, private formBuilder: FormBuilder) {
}
ngOnInit(): void { ngOnInit(): void {
this.sectionForm = new FormGroup({ this.sectionForm = new FormGroup({
...@@ -29,7 +32,8 @@ export class SectionComponent implements OnInit { ...@@ -29,7 +32,8 @@ export class SectionComponent implements OnInit {
this.formService.registerFormGroup({ this.formService.registerFormGroup({
formGroup: this.sectionForm, formGroup: this.sectionForm,
parentForm: this.parentForm, parentForm: this.parentForm,
parentArray: 'sections' parentArray: 'sections',
parentArrayIndex: this.parentArrayIndex
}); });
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment