File

src/lib/fs/cms/page/processing/fs-cms-page-component-injector.ts

Description

This injector handles the adding of the FirstSpirit components to the configured slots.

Index

Methods

Constructor

constructor(typeCode: string)
Parameters :
Name Type Optional
typeCode string No

Methods

Private adaptPageComponents
adaptPageComponents(cmsStructureModel: CmsStructureModel, relevantSlots: string[])
Parameters :
Name Type Optional
cmsStructureModel CmsStructureModel No
relevantSlots string[] No
Returns : any
Private adaptSlots
adaptSlots(cmsStructureModel: CmsStructureModel, slots: string[])
Parameters :
Name Type Optional
cmsStructureModel CmsStructureModel No
slots string[] No
Returns : any
addCmsPageComponents
addCmsPageComponents(cmsPage: CmsStructureModel, slots: string[])

This method adds the FirstSpirit components to the provided cms page and slots list.

Parameters :
Name Type Optional Description
cmsPage CmsStructureModel No

The page to which the components will be added.

slots string[] No

The array of slots to which the components will be added.

Returns : CmsStructureModel

The merged page.

Private addContentSlotComponent
addContentSlotComponent(slot: ContentSlotData, component: ContentSlotComponentData)
Parameters :
Name Type Optional
slot ContentSlotData No
component ContentSlotComponentData No
Returns : any
Private addSlotComponents
addSlotComponents(cmsStructureModel: CmsStructureModel, slots: string[])
Parameters :
Name Type Optional
cmsStructureModel CmsStructureModel No
slots string[] No
Returns : any
Private createEmptyObjectWithProperty
createEmptyObjectWithProperty(propertyName: string, propertyValue: T)
Type parameters :
  • T
Parameters :
Name Type Optional
propertyName string No
propertyValue T No
Returns : literal type
Private createFsComponentUid
createFsComponentUid(slotName: string)
Parameters :
Name Type Optional
slotName string No
Returns : string
Private createSlotComponentData
createSlotComponentData(slotName: string)
Parameters :
Name Type Optional
slotName string No
Private getFsPageComponents
getFsPageComponents(cmsStructureModel: CmsStructureModel, firstSpiritManagedSlot: string[])
Parameters :
Name Type Optional
cmsStructureModel CmsStructureModel No
firstSpiritManagedSlot string[] No
Returns : CmsComponent[]
Private joinComponentUids
joinComponentUids(data: ContentSlotComponentData[])
Parameters :
Name Type Optional
data ContentSlotComponentData[] No
Returns : any
import { CmsStructureModel, ContentSlotComponentData, ContentSlotData, CmsComponent } from '@spartacus/core';
import { merge, setIn } from 'immutable';

/**
 * This injector handles the adding of the FirstSpirit components to the configured slots.
 *
 * @export
 * @class FsCmsPageComponentInjector
 */
export class FsCmsPageComponentInjector {
  constructor(private typeCode: string) {}

  /**
   * This method adds the FirstSpirit components to the provided cms page and slots list.
   *
   * @param {CmsStructureModel} cmsPage The page to which the components will be added.
   * @param {string[]} slots The array of slots to which the components will be added.
   * @return {CmsStructureModel} The merged page.
   * @memberof FsCmsPageComponentInjector
   */
  addCmsPageComponents(cmsPage: CmsStructureModel, slots: string[]): CmsStructureModel {
    if (cmsPage) {
      const adaptedSlots = this.adaptSlots(cmsPage, slots);
      const adaptedPageComponents = this.adaptPageComponents(cmsPage, slots);

      return merge(cmsPage, {
        page: setIn(cmsPage.page, ['slots'], adaptedSlots),
        components: adaptedPageComponents,
      } as CmsStructureModel);
    } else {
      return cmsPage;
    }
  }

  private adaptSlots(cmsStructureModel: CmsStructureModel, slots: string[]) {
    const updatedSlots = this.addSlotComponents(cmsStructureModel, slots);
    return merge(cmsStructureModel.page.slots, updatedSlots);
  }

  private adaptPageComponents(cmsStructureModel: CmsStructureModel, relevantSlots: string[]) {
    const updatedPageComponents = this.getFsPageComponents(cmsStructureModel, relevantSlots);
    return merge(cmsStructureModel.components || [], updatedPageComponents);
  }

  private addSlotComponents(cmsStructureModel: CmsStructureModel, slots: string[]) {
    return slots
      .map((slotName) => {
        const fsEditingAreaSlotComponent = this.createSlotComponentData(slotName);
        const updatedSlot = this.addContentSlotComponent(cmsStructureModel.page.slots[slotName], fsEditingAreaSlotComponent);
        return this.createEmptyObjectWithProperty(slotName, updatedSlot);
      })
      .reduce((accumulatedSlotsProperties, slotProperty) => Object.assign(accumulatedSlotsProperties, slotProperty), {});
  }

  private createSlotComponentData(slotName: string) {
    return {
      uid: this.createFsComponentUid(slotName),
      typeCode: this.typeCode,
      flexType: this.typeCode,
    };
  }

  private createFsComponentUid(slotName: string) {
    return `${this.typeCode}_${slotName}`;
  }

  private addContentSlotComponent(slot: ContentSlotData, component: ContentSlotComponentData) {
    return merge(slot || { components: [] }, {
      components: [component],
    } as ContentSlotData);
  }

  private createEmptyObjectWithProperty<T>(propertyName: string, propertyValue: T): { [key: string]: T } {
    const object = {};
    object[propertyName] = propertyValue;
    return object;
  }

  private getFsPageComponents(cmsStructureModel: CmsStructureModel, firstSpiritManagedSlot: string[]): CmsComponent[] {
    return firstSpiritManagedSlot.map((slot) => {
      const componentUid = this.createFsComponentUid(slot);
      const slotData = cmsStructureModel.page.slots[slot] || { components: [] };
      return {
        typeCode: this.typeCode,
        uid: componentUid,
        name: componentUid,
        components: this.joinComponentUids(slotData.components),
        slotName: slot,
      };
    });
  }

  private joinComponentUids(data: ContentSlotComponentData[]) {
    return data ? data.map((comp) => comp.uid).join(',') : '';
  }
}

results matching ""

    No results matching ""