Trail: Adding a Decorator

A decorator is a feature that adds rendering and behavior to slots and components. Learn how to create a decorator.

To avoid affecting the original storefront, you create the decorator in an overlay. All changes are restricted to the overlay and you can easily remove the overlay.

Prerequisites

Before starting this trail, you must have successfully completed the following.

Scenario

In this trail, you’ll learn how to create a decorator called trailDecorator and add it to the contextual menu around simple responsive banner components.

Procedure

  1. Add the following code to the <HYBRIS_BIN_DIR>/custom/smartedittrail/apps/smartedittrail/src/smartedittrail.ts file in your smartedittrail extension:

    /*
     * Copyright (c) 2022 SAP SE or an SAP affiliate company. All rights reserved.
     */
    // eslint-disable-next-line max-classes-per-file
    import {
        NgModule,
        Component // Added code
    } from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    import {
        SeDecorator, // Added code
        AbstractDecorator, // Added code
        IDecoratorService,
        IFeatureService,
        IPerspectiveService,
        moduleUtils,
        SeEntryModule
    } from 'smarteditcommons';
    import { AbAnalyticsModule } from 'smartedittrailcommons';
    import { AbAnalyticsDecoratorModule } from './abAnalyticsDecorator';
    
    // Added code start
    @SeDecorator()
    @Component({
        selector: 'trail-span-decorator',
        template: `
            <div class="row trail-decorator">
                <span style="background-color: #FFFF00; padding: 2em">
                    <ng-content></ng-content>
                </span>
            </div>
        `
    })
    export class TrailSpanDecorator extends AbstractDecorator {}
    // Added code end
    
    @SeEntryModule('smartedittrail')
    @NgModule({
        imports: [BrowserModule, AbAnalyticsModule, AbAnalyticsDecoratorModule],
        declarations: [TrailSpanDecorator], // Added code
        entryComponents: [TrailSpanDecorator], // Added code
        providers: [
            moduleUtils.bootstrap(
                (
                    decoratorService: IDecoratorService,
                    featureService: IFeatureService,
                    perspectiveService: IPerspectiveService
                ) => {
                    /// /////////////////////////////////////////////////
                    // Create Decorator
                    /// /////////////////////////////////////////////////
    
                    // Use the decoratorService.addMappings() method to associate decorators
                    // The keys may be given as strings or as regex
                    decoratorService.addMappings({
                        SimpleResponsiveBannerComponent: ['abAnalyticsDecorator'],
                        CMSParagraphComponent: ['abAnalyticsDecorator']
                    });
    
                    // Register new decorators the the featureService
                    // The key MUST be the same name as the component selector in camelCase
                    featureService.addDecorator({
                        key: 'abAnalyticsDecorator',
                        nameI18nKey: 'ab.analytics.feature.name'
                    });
    
                    // Added code start
                    decoratorService.addMappings({
                        SimpleResponsiveBannerComponent: ['trailSpanDecorator']
                    });
    
                    decoratorService.enable('trailSpanDecorator');
    
                    featureService.addDecorator({
                        key: 'trailSpanDecorator',
                        nameI18nKey: 'trailSpanDecorator.feature.name'
                    });
                    // Added code end
    
                    /// /////////////////////////////////////////////////
                    // Create  Perspective and assign features.
                    /// /////////////////////////////////////////////////
                    // Group the features created above in a perspective. This will enable the features once the user selects this new perspective.
                    perspectiveService.register({
                        key: 'abAnalyticsPerspective',
                        nameI18nKey: 'ab.analytics.perspective.name',
                        descriptionI18nKey: 'ab.analytics.perspective.description',
                        features: ['abAnalyticsToolbarItem', 'abAnalyticsDecorator'],
                        perspectives: []
                    });
    
                    perspectiveService.register({
                        key: 'trailPerspective',
                        nameI18nKey: 'Trail Mode',
                        descriptionI18nKey: 'Enable Trail Mode',
                        features: [
                            'trailSpanDecorator',
                            'se.contextualMenu',
                            'se.cms.componentMenuTemplate',
                            'se.cms.PageInfoMenu'
                        ],
                        perspectives: []
                    });
                },
                [IDecoratorService, IFeatureService, IPerspectiveService]
            )
        ]
    })
    export class SmartedittrailModule {}

    The code does the following:

    • Creates an Angular component called trailDecorator.

    • Specifies that the trailDecorator component contains the markup to render the decorator.

    • Specifies the attributes used as inputs to the Angular component.

    • Specifies that the TrailDecorator is added to simple responsive banner component.

    • Specifies that trailDecorator is a feature of mode Trail Mode.

  2. Build smartedit and start the server.

Result

Access any storefront homepage from https://127.0.0.1:9002/smartedit/ and switch mode to Trail Mode, you should see the span area in the decorator around simple responsive banner component.