File

src/lib/fs/cms/page/navigation-message-handler.service.ts

Description

This service handles message events which are fired by fs-tpp-api/snap and initiates actions accordingly.

Index

Methods

Constructor

constructor(previewPageService: PreviewPageService, winRef: WindowRef)
Parameters :
Name Type Optional
previewPageService PreviewPageService No
winRef WindowRef No

Methods

destroy
destroy()

This method destroys the EventListener attached during initialize().

Returns : void
Private handleNavigationMessage
handleNavigationMessage(event: MessageEvent)
Parameters :
Name Type Optional
event MessageEvent No
Returns : void
initialize
initialize()

This method initializes the message EventListener and handles the navigation of the preview to a SAP Commerce page.

Returns : void
Private Async navigateToHybrisPage
navigateToHybrisPage(hybrisPageId: string)
Parameters :
Name Type Optional
hybrisPageId string No
Returns : Promise<void>
import { Injectable } from '@angular/core';
import { PreviewPageService } from './preview/preview-page.service';
import { WindowRef } from '@spartacus/core';

/**
 * This service handles message events which are fired by fs-tpp-api/snap and initiates actions accordingly.
 *
 * @export
 * @class NavigationMessageHandlerService
 */
@Injectable({
  providedIn: 'root',
})
export class NavigationMessageHandlerService {
  constructor(private previewPageService: PreviewPageService, private winRef: WindowRef) {}

  /**
   * This method initializes the message EventListener and handles the navigation of the preview to a SAP Commerce page.
   *
   */
  initialize(): void {
    this.winRef.nativeWindow?.addEventListener(
      'message',
      (event) => {
        this.handleNavigationMessage(event);
      },
      false
    );
  }

  /**
   * This method destroys the EventListener attached during initialize().
   */
  destroy() {
    this.winRef.nativeWindow?.removeEventListener('message', this.handleNavigationMessage, false);
  }

  private handleNavigationMessage(event: MessageEvent) {
    if (typeof event?.data === 'string' && event.data.startsWith('navigate:')) {
      const hybrisPageId = event.data.substring(9);
      this.navigateToHybrisPage(hybrisPageId);
    }
  }

  private async navigateToHybrisPage(hybrisPageId: string): Promise<void> {
    if (typeof hybrisPageId === 'string' && hybrisPageId.trim().length > 0) {
      if ((await this.previewPageService.navigateTo(hybrisPageId)) !== true) {
        console.error(`Could not navigate to the element with hybrisPageId: ${hybrisPageId}`);
      }
    } else console.error(`Could not navigate to empty hybrisPageId`);
  }
}

results matching ""

    No results matching ""