From 1e6dc0efc5a9b5f64844ebe80efdb53ba2f1d004 Mon Sep 17 00:00:00 2001
From: rhenck <richard.henck@iqb.hu-berlin.de>
Date: Fri, 22 Oct 2021 12:51:47 +0200
Subject: [PATCH] Refactor elements to set their changed properties themselves

There is now a default method working the same as before. But elements
(likert for now) can overwrite it and add some logic for specific
properties.
---
 projects/common/models/uI-element.ts    |  6 ++++++
 projects/editor/src/app/unit.service.ts | 15 +++++++--------
 2 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/projects/common/models/uI-element.ts b/projects/common/models/uI-element.ts
index de8368f03..0a86d38ed 100644
--- a/projects/common/models/uI-element.ts
+++ b/projects/common/models/uI-element.ts
@@ -41,6 +41,12 @@ export abstract class UIElement {
       this.yPosition = coordinates.y;
     }
   }
+
+  // This can be overwritten by elements if they need to handle some property specifics. Likert does.
+  setProperty(property: string,
+              value: string | number | boolean | string[] | AnswerOption[] | LikertRow[] | null): void {
+    this[property] = value;
+  }
 }
 
 export abstract class InputElement extends UIElement {
diff --git a/projects/editor/src/app/unit.service.ts b/projects/editor/src/app/unit.service.ts
index 7570ac645..34ea72d84 100644
--- a/projects/editor/src/app/unit.service.ts
+++ b/projects/editor/src/app/unit.service.ts
@@ -166,8 +166,9 @@ export class UnitService {
   }
 
   updateElementProperty(elements: UIElement[], property: string,
-                        value: string | number | boolean | string[] | null): void {
-    elements.forEach((element: UIElement) => {
+                        value: string | number | boolean | string[] |
+                        AnswerOption[] | LikertElementRow[] | null): boolean {
+    for (const element of elements) {
       if (property === 'id') {
         if (!IdService.getInstance().isIdAvailable((value as string))) { // prohibit existing IDs
           this.messageService.showError('ID ist bereits vergeben');
@@ -176,15 +177,13 @@ export class UnitService {
         IdService.getInstance().removeId(element[property]);
         IdService.getInstance().addId(<string>value);
       }
-      if (Array.isArray(value)) {
-        element[property] = [...value];
-      } else {
-        element[property] = value;
-      }
+      element.setProperty(property, value);
       this.elementPropertyUpdated.next();
       return true;
-    });
+    }
     this.veronaApiService.sendVoeDefinitionChangedNotification();
+    return true;
+  }
 
   async editQuestion(question: LikertElementRow): Promise<void> {
     await this.dialogService.showLikertQuestionEditDialog(question)
-- 
GitLab