Untitled

 avatar
user_8078570
plain_text
a month ago
3.3 kB
1
Indexable
    .subscribe((data) => this.handleSectionSubscription(data, section, node, layout, nodeIsItemChild));
  }
  private handleSectionSubscription(
    data: any,
    section: PageSection,
    node: any,
    layout: any,
    nodeIsItemChild: boolean
  ) {
    if (data === undefined) {
      section.messages = [
        ...(section.messages ?? []),
        {
          severity: 'error',
          detail: this.i18nService.translate(
            'editor.EDITORPAGE.MESSAGE.ERRORITEMEXISTS',
            {},
            this.i18nService.getActiveLang()
          ),
        },
      ];
      section.header = node.label;
      section.key = <string>node.key;
      section.expandedOnce = true;
    } else {
      const { relItemData: relatedItem, pageSection } = data;
      section.header = pageSection.header;
      section.key = <string>node.key;
      section.content = <EditorContent[]>pageSection.content;

      if (relatedItem !== undefined) {
        const parentSection = layout.sections.find((section) =>
          section.content.some((content) => content.nodeName === node.key)
        );
        const itemHasChildSections = this.flattenedTreeNodes?.some((treeNode) =>
          treeNode.key?.includes(`${node.key}-`)
        );

        if (!nodeIsItemChild) {
          section.metadata = [
            ...ITEM_METADATA_NODES.map(
              (itemNode) =>
                ({
                  ...itemNode,
                  value: relatedItem
                    ? relatedItem[`${itemNode.nodeName}` as keyof ItemModel]
                    : undefined,
                  itemId: relatedItem.id,
                  type: EditorFieldInputType.InputText,
                } as EditorContent)
            ),
          ];
        }

        section.header = pageSection.header;
        section.itemName = relatedItem.name;
        section.parentKey = parentSection?.key;
        section.valueName = <string>relatedItem.name;
        section.isItem = true;
        section.expandedOnce = true;
        section.content = section.content.map((content) => ({
          itemId: relatedItem.id,
          ...content,
        }));

        if (itemHasChildSections) {
          section.content = [];
        } else {
          section.content.forEach((content) => {
            const itemValue = relatedItem.nodes?.find(
              (val) => val.nodeName === content.nodeName
            );
            const setupValue = this.setupValues?.find(
              (val) => val.nodeName === content.nodeName
            );

            if (itemValue && setupValue) {
              setupValue.value = itemValue.value;
            }
          });
        }

        const treeNode = [
          ...(this.flattenedTreeNodes?.map((treeNode) => treeNode) ?? []),
        ].find((tNode) => tNode.key === section.key);

        if (treeNode && !nodeIsItemChild) {
          treeNode.label = `${treeNode.label?.split(':')[0]}: ${
            relatedItem.name ?? ''
          }`;
          section.header = treeNode.label;
          if (treeNode.selectable === false) {
            treeNode.selectable = true;
            treeNode.children = [
              ...(treeNode.children?.map((cNode) => ({
                ...cNode,
                selectable: true,
              })) ?? []),
            ];
          }
        }
      }
    }
Leave a Comment