From 32d07c52730706b4192666010a56ce919977e2fb Mon Sep 17 00:00:00 2001 From: jojohoch <joachim.hoch@iqb.hu-berlin.de> Date: Sat, 22 Jan 2022 09:51:09 +0100 Subject: [PATCH] [player] Refactor UnitStateElementMapperService - Refactor findNestedUIElement as static class method --- .../unit-state-element-mapper.service.ts | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/projects/player/src/app/services/unit-state-element-mapper.service.ts b/projects/player/src/app/services/unit-state-element-mapper.service.ts index d255b81df..4d9521d86 100644 --- a/projects/player/src/app/services/unit-state-element-mapper.service.ts +++ b/projects/player/src/app/services/unit-state-element-mapper.service.ts @@ -17,32 +17,30 @@ export class UnitStateElementMapperService { dropListValueIds!: DragNDropValueObject[]; registerDropListValueIds(unitDefinition: Unit): void { - const dropListElements: UIElement[] = []; - this.findNestedDropLists(unitDefinition.pages, dropListElements); - this.dropListValueIds = dropListElements + this.dropListValueIds = UnitStateElementMapperService.findNestedUIElement(unitDefinition.pages, 'drop-list') .reduce( (accumulator: DragNDropValueObject[], currentValue: UIElement) => ( (currentValue.value && currentValue.value.length) ? accumulator.concat(currentValue.value) : accumulator), [] ); } - private findNestedDropLists(value: any | unknown[], dropListElements: UIElement[]): void { - if (this.isObject(value)) { - if (value.type === 'drop-list') { - dropListElements.push(value); + static findNestedUIElement(value: any | unknown[], type: string): UIElement[] { + const elements: UIElement[] = []; + if (value && typeof value === 'object') { + if (Array.isArray(value)) { + value.forEach((node: unknown) => elements + .push(...UnitStateElementMapperService.findNestedUIElement(node, type))); + } else if (value.type === type) { + elements.push(value); } else { const keys = (Object.keys(value)); - keys.forEach((key: string) => this.findNestedDropLists(value[key], dropListElements)); + keys.forEach((key: string) => elements + .push(...UnitStateElementMapperService.findNestedUIElement(value[key], type))); } - } else if (this.isArray(value)) { - value.forEach((element: unknown) => this.findNestedDropLists(element, dropListElements)); } + return elements; } - private isObject = (value: unknown) => !!(value && typeof value === 'object' && !Array.isArray(value)); - - private isArray = (value: unknown) => !!(value && typeof value === 'object' && Array.isArray(value)); - mapToElementValue( elementModel: UIElement, unitStateElement: UnitStateElementCode | undefined, -- GitLab