From c97e4c106b1da2f686711c91c38f882044221bb9 Mon Sep 17 00:00:00 2001
From: jojohoch <joachim.hoch@iqb.hu-berlin.de>
Date: Thu, 8 Sep 2022 13:23:28 +0200
Subject: [PATCH] [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
---
 projects/common/util/unit-utils.ts            | 22 -------
 .../src/app/components/unit/unit.component.ts | 59 ++++++-------------
 2 files changed, 19 insertions(+), 62 deletions(-)
 delete mode 100644 projects/common/util/unit-utils.ts

diff --git a/projects/common/util/unit-utils.ts b/projects/common/util/unit-utils.ts
deleted file mode 100644
index c5989685a..000000000
--- a/projects/common/util/unit-utils.ts
+++ /dev/null
@@ -1,22 +0,0 @@
-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;
-  }
-}
diff --git a/projects/player/src/app/components/unit/unit.component.ts b/projects/player/src/app/components/unit/unit.component.ts
index af3b0f2b8..49922cc77 100644
--- a/projects/player/src/app/components/unit/unit.component.ts
+++ b/projects/player/src/app/components/unit/unit.component.ts
@@ -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 {
-- 
GitLab