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 f2c5f053e3479dd0867c7058578673344dd790e3..1394cbf23b86a1830a5645c16f3ed9032acecd9e 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 @@ -1,12 +1,14 @@ import { ElementFactory } from 'common/util/element.factory'; -import { BasicStyles, InputElement } from 'common/models/elements/element'; +import { BasicStyles, DragNDropValueObject, InputElement, UIElementValue } from 'common/models/elements/element'; import { Type } from '@angular/core'; import { ElementComponent } from 'common/directives/element-component.directive'; import { DropListSimpleComponent } from 'common/components/compound-elements/cloze/cloze-child-elements/drop-list-simple.component'; +import { IDManager } from 'common/util/id-manager'; export class DropListSimpleElement extends InputElement { + value: DragNDropValueObject[] = []; connectedTo: string[] = []; copyOnDrop: boolean = false; highlightReceivingDropList: boolean = false; @@ -17,7 +19,9 @@ export class DropListSimpleElement extends InputElement { constructor(element: Partial<DropListSimpleElement>, ...args: unknown[]) { super({ width: 150, height: 30, ...element }, ...args); - this.value = element.value || []; + if (element.value) { + this.value = DropListSimpleElement.checkAndRepairValueIDs(element.value); + } if (element.connectedTo) this.connectedTo = element.connectedTo; if (element.copyOnDrop) this.copyOnDrop = element.copyOnDrop; if (element.highlightReceivingDropList) this.highlightReceivingDropList = element.highlightReceivingDropList; @@ -30,6 +34,25 @@ export class DropListSimpleElement extends InputElement { }; } + 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[]): DragNDropValueObject[] { + valueList.forEach(valueObject => { + if (IDManager.getInstance().isIdAvailable(valueObject.id)) { + IDManager.getInstance().addID(valueObject.id); + } else { + valueObject.id = IDManager.getInstance().getNewID('value'); + } + }); + return valueList; + } + getComponentFactory(): Type<ElementComponent> { return DropListSimpleComponent; }