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

[player] Remove UnitUtils

- Use Unit's getAllElements method of Unit instead of
UnitUtils.findUIElements to get all DragNDropValueObjects
in UnitComponent
-Remove unnecessary timeout in UnitComponent
parent c9e64708
No related branches found
No related tags found
No related merge requests found
Pipeline #40777 passed
import { UIElement, UIElementType } from 'common/models/elements/element';
export abstract class UnitUtils { // TODO delete this. replce by unit method
static findUIElements(value: any | unknown[], type?: UIElementType): UIElement[] {
const elements: UIElement[] = [];
if (value && typeof value === 'object') {
if (type ? value.type === type : value instanceof UIElement) {
elements.push(value);
}
if (Array.isArray(value)) {
value.forEach((arrayItem: unknown) => {
elements.push(...UnitUtils.findUIElements(arrayItem, type));
});
} else {
Object.keys(value).forEach((key: string) => {
elements.push(...UnitUtils.findUIElements(value[key], type));
});
}
}
return elements;
}
}
......@@ -2,8 +2,7 @@ import { ChangeDetectorRef, Component, OnInit } from '@angular/core';
import { PlayerConfig, VopStartCommand } from 'player/modules/verona/models/verona';
import { Unit } from 'common/models/unit';
import { LogService } from 'player/modules/logging/services/log.service';
import { UnitUtils } from 'common/util/unit-utils';
import { DragNDropValueObject, UIElement } from 'common/models/elements/element';
import { DragNDropValueObject } from 'common/models/elements/element';
import { SanitizationService } from 'common/services/sanitization.service';
import { Page } from 'common/models/page';
import { UnitStateService } from 'player/src/app/services/unit-state.service';
......@@ -37,45 +36,25 @@ export class UnitComponent implements OnInit {
private configureUnit(message: VopStartCommand): void {
this.reset();
setTimeout(() => {
if (message.unitDefinition) {
const unitDefinition: Unit = new Unit(
this.sanitizationService.sanitizeUnitDefinition(JSON.parse(message.unitDefinition))
);
LogService.info('player: unitDefinition', unitDefinition);
this.configurePlayerAndPages(message, unitDefinition);
this.configureServices(message);
} else {
LogService.warn('player: message has no unitDefinition');
}
});
}
private configurePlayerAndPages(message: VopStartCommand, unitDefinition: Unit): void {
this.pages = unitDefinition.pages;
this.playerConfig = message.playerConfig || {};
LogService.info('player: unitStateElementCodes', this.unitStateService.elementCodes);
}
private configureServices(message: VopStartCommand): void {
this.veronaPostService.sessionId = message.sessionId;
this.veronaPostService.stateReportPolicy = message.playerConfig?.stateReportPolicy || 'none';
this.configureElementModelElementCodeMappingService();
this.unitStateService.elementCodes = message.unitState?.dataParts?.elementCodes ?
JSON.parse(message.unitState.dataParts.elementCodes) : [];
}
private configureElementModelElementCodeMappingService(): void {
this.elementModelElementCodeMappingService.dragNDropValueObjects =
UnitUtils
.findUIElements(this.pages, 'drop-list')
.concat(UnitUtils.findUIElements(this.pages, 'drop-list-simple'))
.reduce(
(accumulator: DragNDropValueObject[], currentValue: UIElement) => (
(currentValue.value && (currentValue.value as DragNDropValueObject[]).length) ?
accumulator.concat(currentValue.value as DragNDropValueObject) :
accumulator), []
);
if (message.unitDefinition) {
const unitDefinition: Unit = new Unit(
this.sanitizationService.sanitizeUnitDefinition(JSON.parse(message.unitDefinition))
);
LogService.info('player: unitDefinition', unitDefinition);
this.pages = unitDefinition.pages;
this.playerConfig = message.playerConfig || {};
LogService.info('player: unitStateElementCodes', this.unitStateService.elementCodes);
this.veronaPostService.sessionId = message.sessionId;
this.veronaPostService.stateReportPolicy = message.playerConfig?.stateReportPolicy || 'none';
this.unitStateService.elementCodes = message.unitState?.dataParts?.elementCodes ?
JSON.parse(message.unitState.dataParts.elementCodes) : [];
this.elementModelElementCodeMappingService.dragNDropValueObjects = [
...unitDefinition.getAllElements('drop-list'),
...unitDefinition.getAllElements('drop-list-simple')] as unknown as DragNDropValueObject[];
} else {
LogService.warn('player: message has no unitDefinition');
}
}
private reset(): void {
......
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