From 0fc6752e7f4691dec359f78a938922df6e18c47d Mon Sep 17 00:00:00 2001
From: rhenck <richard.henck@iqb.hu-berlin.de>
Date: Mon, 12 Sep 2022 17:03:00 +0200
Subject: [PATCH] Fix IDs in drop-list-simple

- Do ID checks only in the class, not in unit service (or elsewhere)
- Fix repaired value list to get new objects instead of references to
the existing.
---
 .../cloze-child-elements/drop-list-simple.ts  | 26 +++++++++----------
 .../editor/src/app/services/unit.service.ts   |  6 -----
 2 files changed, 12 insertions(+), 20 deletions(-)

diff --git a/projects/common/models/elements/compound-elements/cloze/cloze-child-elements/drop-list-simple.ts b/projects/common/models/elements/compound-elements/cloze/cloze-child-elements/drop-list-simple.ts
index 18f4cc92d..222f603a8 100644
--- a/projects/common/models/elements/compound-elements/cloze/cloze-child-elements/drop-list-simple.ts
+++ b/projects/common/models/elements/compound-elements/cloze/cloze-child-elements/drop-list-simple.ts
@@ -21,10 +21,10 @@ export class DropListSimpleElement extends InputElement {
     itemBackgroundColor: string;
   };
 
-  constructor(element: Partial<DropListSimpleElement>, ...args: unknown[]) {
-    super({ width: 150, height: 30, ...element }, ...args);
-    if (element.value) {
-      this.value = DropListSimpleElement.checkAndRepairValueIDs(element.value);
+  constructor(element: Partial<DropListSimpleElement>, idManager?: IDManager) {
+    super({ width: 150, height: 30, ...element }, idManager);
+    if (element.value && idManager) { // IDManager should not be present in player, therefore no ID checks necessary
+      this.value = DropListSimpleElement.checkAndRepairValueIDs(element.value, idManager);
     }
     if (element.connectedTo) this.connectedTo = element.connectedTo;
     if (element.copyOnDrop) this.copyOnDrop = element.copyOnDrop;
@@ -65,23 +65,21 @@ export class DropListSimpleElement extends InputElement {
       .map(option => ({ value: option.id, label: option.text as string }));
   }
 
-  setProperty(property: string, value: UIElementValue) {
-    if (property === 'value') {
-      this.value = DropListSimpleElement.checkAndRepairValueIDs(value as DragNDropValueObject[]);
-    } else {
-      super.setProperty(property, value);
+  private static checkAndRepairValueIDs(valueList: DragNDropValueObject[],
+                                        idManager?: IDManager): DragNDropValueObject[] {
+    if (!idManager) {
+      return valueList;
     }
-  }
-
-  private static checkAndRepairValueIDs(valueList: DragNDropValueObject[]): DragNDropValueObject[] {
+    const newValueList: DragNDropValueObject[] = [];
     valueList.forEach(valueObject => {
       if (IDManager.getInstance().isIdAvailable(valueObject.id)) {
         IDManager.getInstance().addID(valueObject.id);
+        newValueList.push(valueObject);
       } else {
-        valueObject.id = IDManager.getInstance().getNewID('value');
+        newValueList.push({ ...valueObject, id: IDManager.getInstance().getNewID('value') });
       }
     });
-    return valueList;
+    return newValueList;
   }
 
   getElementComponent(): Type<ElementComponent> {
diff --git a/projects/editor/src/app/services/unit.service.ts b/projects/editor/src/app/services/unit.service.ts
index b030dbed9..b5e08307e 100644
--- a/projects/editor/src/app/services/unit.service.ts
+++ b/projects/editor/src/app/services/unit.service.ts
@@ -182,12 +182,6 @@ export class UnitService {
       newElement.position.yPosition += 10;
     }
 
-    if ('value' in newElement && newElement.value instanceof Object) { // replace value Ids with fresh ones (dropList)
-      (newElement.value as DragNDropValueObject[]).forEach((valueObject: { id: string }) => {
-        valueObject.id = this.idManager.getNewID('value');
-      });
-    }
-
     if ('row' in newElement && newElement.rows instanceof Object) { // replace row Ids with fresh ones (likert)
       (newElement.rows as LikertRowElement[]).forEach((rowObject: { id: string }) => {
         rowObject.id = this.idManager.getNewID('likert_row');
-- 
GitLab