Overview of the SmartEdit Framework Architecture
The SmartEdit Framework is a UI-centric business tooling framework that allows users to edit changes in a visually-enhanced way. It is a lightweight AngularJS JavaScript application based on a SAP Commerce Cloud-furnished UI (for example, a website) and a set of modules that define additional functionality.
Overview of the SmartEdit Architectural Framework Components
The SmartEdit Framework is comprised of architectural components that perform different functions.
The SmartEdit Framework is comprised of the following architectural components:
-
Decorator: AngularJS module that contains a directive and an HTML template.
-
Gateway: A communication gateway between the SmartEdit container and the SmartEdit application.
-
Module: An AngularJS module.
-
Remote Web Services: Any server-side web service used by the various applications and modules for authentication, loading of modules, etc.
-
SmartEdit application: An AngularJS web application that is injected into an iframe alongside a website's storefront. It loads modules that modify the DOM of the website to provide editing and tooling functionality for the website's storefront.
-
SmartEdit bundle: A collection of links to SmartEdit modules, third-party libraries, CSS, and SmartEdit core JavaScript code to be bootstrapped.
-
SmartEdit commons: A module provided for extensions to consume sources at compile time and runtime.
-
SmartEdit container: An AngularJS web application that configures and loads the SmartEdit application and a website into an iframe
-
SmartEdit loader: A short-lived AngularJS web application that configures and loads the SmartEdit container.
-
SmartEdit module: AngularJS modules that have access to features of SmartEdit, such as decorators, toolbar actions, and contextual menus.
-
SmartEdit services: A set of web services, such as configuration, authentication, caching, etc, that are used by the SmartEdit application and the SmartEdit container.
-
Web Application Injector: A JavaScript library that pushes the SmartEdit application and other resources into the website within the SmartEdit container's iframe.
-
Website: The website to be enriched with SmartEdit functionality. The website is typically a storefront, such as Accelerator.
The following images shows the interaction between the components within the SmartEdit Framework.
How the SmartEdit Framework Architectural Components Interact
The SmartEdit Framework architectural components interact with each other as follows:
-
The SmartEdit loader retrieves the configuration (for example: modules) for the SmartEdit container and bootstraps an extended SmartEdit container into the parent frame. The SmartEdit container holds all the configured modules and dependencies that have been pulled from remote locations.
-
The SmartEdit container retrieves the configuration for the SmartEdit application and bundles it all as an extended SmartEdit application. The SmartEdit bundle contains the SmartEdit application, resources, and all the configured modules and dependencies that have been pulled from remote locations.
-
The SmartEdit container loads an experience of the storefront into an iframe. (The website must follow the HTML Markup Contract and include the Web Application Injector in its header. For more information, see SmartEdit Contract for Storefronts)
-
Once the storefront has been loaded, the storefront requires that the SmartEdit container sends the SmartEdit bundle through a W3C postMessage.
-
Once all JavaScript and CSS files of the SmartEdit bundle have been added to the storefront, the SmartEdit auto-bootstraps and sends a signal to the container when it is ready.
-
The SmartEdit container and the SmartEdit application communicate bidirectionally using the SmartEdit gateway (for more information, see Gateway Factory).
-
The SmartEdit container, the SmartEdit application, and their respective modules interact with each other through dependency injection and the gateway across frames.
-
The SmartEdit container, SmartEdit application, and their respective modules interact with the REST APIs.
SmartEdit Framework Architectural Components
The SmartEdit Framework consists of a number of architectural components such as the SmartEdit container, the SmartEdit application, the web application injector, the SmartEdit wrapper, and the SmartEdit module.
SmartEdit Container
The SmartEdit container is a AngularJS web application. Its primary function is to bootstrap the SmartEdit application into the page loaded through the iframe. In addition to this bootstrapping, the SmartEdit container provides configuration and UI features so that the SmartEdit container and SmartEdit application can interact.
A SmartEdit Configuration Editor is provided to manage the configuration of loading SmartEdit modules, websites, security, etc. The UI can create, read, update, and delete the configuration to other configured web services (URL). This makes it possible to configure SmartEdit to store the configuration in the platform persistence or any other custom web-facing service. Developers can create SmartEdit modules to extend the SmartEdit container's features. For more information, see Configuration Editor.
SmartEdit Application
The SmartEdit application is an AngularJS application that provides a robust framework and API to add functionality to existing web pages in a pluggable way. The SmartEdit application is injected and bootstrapped into a page in the SmartEdit container's iframe by the Web Application Injector. It provides the core SmartEdit functionality and installs any configured modules to extend the functionality. An API and a set of services are provided to accelerate development and provide access to core services to SmartEdit module developers.
Web Application Injector
The Web Application Injector is a JavaScript library that must be included in all pages so that they can be edited using the SmartEdit web application. You can include the JavaScript file in the header of the page to be edited, for example:
<script src="path/to/webApplicationInjector.js"></script>
The Web Application Injector's JavaScript library contains a W3C-standard window event handler that receives a SmartEdit bundle through a window.postMessage from the SmartEdit container. The bundle contains the SmartEdit application, URLs to HTML/CSS/JS resources, and other application configurations. The application configurations are saved to the window namespace. JavaScript resources are added to the storefront using $script.js, an asynchronous JavaScript loader bundled with the Web Application Injector, and CSS resources are added to the head of the storefront using JQuery. This completes the injection process. If bootstrapping fails or if the Web Application Injector is not found on the storefront, the SmartEdit container will detect this failure through heartbeat polling.
SmartEdit Wrapper and RenderService
The SmartEdit wrapper (SAK) and Render Service work together to provide a WYSIWYG editing experience within the storefront loaded in the SmartEdit application. When a user selects an editing perspective (called a mode in the SmartEdit web application), the Render Service responds to the event by creating an overlay layer. This results in the creation of an overlay for each content slot and component in the storefront viewport, which is the area of the page that is visible to users. Once the overlay is generated, the AngularJS directive of the SmartEdit wrapper triggers on each component in the overlay through an HTML class selector. Each overlay element is wrapped by decorators that correspond to the associated component's type, but only decorators enabled in the current perspective are applied.
In addition to the perspective change listener, the SmartEdit application observes DOM mutations (add, remove, mutate) and a viewport intersection of components in the storefront. It also observes the modification of the page identifier. The SmartEdit application adds positioning and resizing listeners to components. These intersection observers, DOM mutation observers and listeners are required to keep the overlay layer synchronized with the storefront. For more information, see the following:
- Intersection Observer API, see https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API.
- Mutation Observer, see https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver.
The decorators enhance a component with functionality, such as a contextual menu. SmartEdit developers can create their own decorators–or contextual menu items–in order to add functionality for generic CMS editing, personalization, or other features. For more information on how to add new decorators, see Adding Decorators to Components.
For more information, see the SmartEdit Bootstrap and Perspective Switching Sequence section below.
SmartEdit Module
A SmartEdit is a set of HTML/CSS/JavaScript fragments that follow a specific set of restrictions and can be plugged into SmartEdit to extend its functionality.
SmartEdit Commons
A library of TypeScript modules provided for extensions to consume for type checking at compile time and for reference modules in the smarteditcommons bundle to consume at runtime. When the Webpack bundle references the smarteditcommons module, the DDLReferencePlugin locates the manifest file to map its dependencies to the prebuilt smarteditcommons.js bundle. When the library is loaded into the browser, a global variable is exposed. The extensions use this global variable to access the module.
The @smart/utils application is an internal legacy application slated for migration to smarteditcommons in future. When developing custom smartedit extensions, you are recommended not to depend on @smart/utils. Instead, custom smartedit cextensions should exclusively rely on smarteditcommons, smartedit, and smarteditcontainer.
Architectural Services of the SmartEdit Framework
The SmartEdit Framework uses SAP Commerce Cloud-specific web services.
The following SAP Commerce Cloud services allow the SmartEdit Framework to enable its architecture:
- bootstrapService: Is responsible for preparing extended SmartEdit container and extended SmartEdit application bundles.
- featureService: Registers features and is responsible for enabling and disabling them.
- IFrameManager: Is a set of methods to load the storefront within the iframe.
- loadConfigManager: Is responsible for fetching configurations from remote APIs.
- perspectiveService: Registers perspectives and allows to switch from one to another by invoking the featureService
- renderService: Creates and refreshes the overlay as required, for example, when initially loading after bootstrapping or when rerendering CMS components or content slots.
For more information about these services, see API Documentation and YAML Files.
Authorization
The authorization service is used to verify if users have the permissions required to create, read, update, and remove component types when using the SmartEdit web application.
For more information, see the Authorization Service.
iFrame Manager
The iFrameManager service exposes methods to load an experience into an iframe.
Loading a Storefront into the Iframe using the iFrameManager
The iFrameManager loads storefronts into the iframe so that the storefronts can be edited using the SmartEdit web application. To load a storefront into the iframe, the iFrameManger requires a preview ticket. The user initiates the loading process by selecting a catalog version in the SmartEdit landing page or in the Preview Selector. This results in an update in the src attribute of the iframe tag.
The following steps are executed to load a storefront into the iframe:
- Get a preview ticket
- Load the preview
- Set up the iframe source
Getting a Preview Ticket
The initializeCatalogPreview API is responsible for getting a preview ticket and loading the corresponding storefront experience. A preview ticket contains the Accelerator session settings, such as the content catalog name, content catalog version, language, and date and time. A preview ticket is mandatory because it defines the context experience the storefront is loaded in.
The initializeCatalogPreview API first checks the sharedDataService to verify if an experience has been set. If an experience has not been set, the user is redirected to the landing page to select a catalog version. By selecting the catalog version, the user has also implicitly selected the associated site, catalog name, and the default language.
The application configuration is then retrieved through the loadConfigManagerService. The configuration specifies the location of the preview ticket REST API. If it was not overwritten in the configuration, the default preview ticket endpoint is provided by the previewwebservices extension. SmartEdit fetches a new preview ticket by invoking a POST call to this endpoint and sending the context details of the current storefront in the request body. The new preview ticket includes the preview ticket and the storefront URL. This preview information is stored in the sharedDataService.
Loading the Preview
Once the iFrameManager has received the preview ticket and the storefront URL, the loadPreview method loads the specified experience in the storefront iframe. The yacceleratorstorefront extension has a mechanism that loads a storefront experience using a ticket; It appends the /cx-preview suffix to the storefront URL and adds the preview ticket ID as a query parameter. This request is sent in the background to initialize the preview in the yacceleratorstorefront session on the back end.
Once the request has been completed, the requested page is opened in the SmartEdit application using the load method. The preview ticket no longer needs to be appended to the requested pages since it is stored in a back end session tied to a JSESSIONID persisted on the frontend. The storefront is opened in the Preview perspective by default.
Setting up the Iframe Source
To display the storefront experience context in the iframe, the src attribute must be updated with the URL of the requested storefront. The load method is responsible for providing this information. The DOM object is retrieved with jQuery. The src attribute is updated with the URL of the homepage in preview mode or another requested location that is not in preview mode.
The following URL is an example of the homepage in preview mode for the Apparel-Uk catalog:
https://<DOMAIN>/yacceleratorstorefront/?site=apparel-uk&cmsTicketId=64758455834596483412987b-dc51-4f5f-93ae-923268a0e5a9
Frontend Driven Storefronts
Frontend driven storefronts, which are single-page applications, dynamically update the content of the storefront without reloading the page.
If a perspective other than the default Preview perspective is loaded, the overlay div in the page is constantly updated to provide an actual representation of the current storefront components present in the viewport; the overlay div provides a representation of the visible components on the page, that is their number, sizes, and positions.
The SmartEdit application constantly listens for the following events in the storefront:
- Component added: Component has been added to the viewport.
- Component removed: Component has been removed from the viewport.
- Component attribute modified: A component attribute that belongs to the CMS Storefront Markup Contract has been modified within the viewport.
- Component size changed.
- Component position changed.
- Page identifier modified: A page identifier has been modified, that is, a deep link.
The SmartEdit application is agnostic of the library that performs these actions.
Contract Change Listener
When bootstrapped, the SmartEdit application initializes a contract change listener service that listens for each type of event that is observed. The following table lists the storefront events and the resulting actions in the overlay rerender:
|
Storefront Event |
Overlay Rerender |
|---|---|
|
Component added |
|
|
Component removed |
|
|
Component attribute modified |
|
|
Component size changed |
|
|
Component position changed |
|
|
Page identifier modified (deep link) |
|
In addition to the event listeners, the SmartEdit application consumes a renderComponent method. The storefront uses the renderComponent method in the SmartEdit namespace in order to execute an on-demand rerendering of a specified content slot based on its identifier. For more information, see SmartEdit Contract for Storefronts. If the renderComponent method is not exposed, the SmartEdit application fetches the storefront page and replaces the old content slot in the overly with the new content slot that is returned.
SmartEdit uses the native Intersection Observer API and the MutationObserver to detect changes in the storefront DOM.
Sequence Diagrams
This topic provides the loading sequences for the SmartEdit container, iframe injection, bootstrapping SmartEdit, and wrapping decorators.
SmartEdit Container Loading Sequence
- The user navigates to the SmartEdit extension served by the SAP Commerce Cloud Platform.
- The index.jsp is retrieved from the server that the SmartEdit extensions reside on.
- The SmartEdit loader is the entrypoint for bootstrapping SmartEdit. It retrieves configurations from the smarteditwebservices configurations REST API through the LoadConfigurationManager, retrieves all SmartEdit container module locations, and then bootstrapps the extended SmartEdit container AngularJS application. These optional modules augment the SmartEdit container. For more information on how to configure these modules, see Configuration Editor.
- If an authentication cookie is not found locally, the user is prompted to sign in.
- The user is then redirected by an AngularJS routing to the SmartEdit landing page. From there, the user selects a content catalog version to edit.
- Once the user has selected a catalog version, the user is redirected to the storefront page. The iFrameManager requests a preview ticket for the desired context from the server on which the preview ticket REST API resides. By default, this API is provided by the previewwebservices extension, but this may be overridden using the Configuration Editor.
- The iFrameManager loads the storefront homepage of the desired content catalog version with the new preview ticket in the iframe.
- The BootstrapService responds to the storefront load complete event by once again retrieving the configurations, resolving optional SmartEdit application module locations, and injecting the SmartEdit resource bundle into the iframe using a W3C postMessage.
- The Web Application Injector, which is included as part of the storefront by the smarteditaddon AddOn, hooks onto this postMessage event. This triggers the bootstrap process of the extended SmartEdit application with the optional SmartEdit application modules. This step is described in detail in the Iframe Injection Sequence.
Iframe Injection Sequence
The Iframe Injection Sequence is executed after the SmartEdit Loading Sequence has been executed. The Iframe Injection Sequence is as follows:
-
The Web Application Injector begins its execution by posting a loading message to the parent frame.
-
The SmartEdit container receives the message and displays a loading modal to inform the user that the web page is not ready.
-
The web application injector sets a watch on the HTML onReady event. The HTML triggers this event as soon as it finishes rendering its DOM.
-
Upon receiving the onReady event, the web application injector sends a bootstrap SmartEdit message to the SmartEdit container. This message informs the container that the web page is ready to be bootstrapped.
-
The SmartEdit container loads the SmartEdit configuration and sends it to the web application injector.
-
The web application injector uses the configuration it has received to retrieve and inject the necessary CSS, JS, and HTML files into the web page.
-
As part of the JavaScript loading process, the SmartEdit AngularJS module loads and starts its bootstrap process.
-
After SmartEdit finishes loading, it will post a SmartEditReady message.
-
The SmartEdit container receives the SmartEditReady message, which informs it that the application has finished loading. The container will then hide the loading message. SmartEdit is now ready to be used.
The following diagram shows the flow of the sequence to inject the iframe.
SmartEdit Bootstrap and Perspective Switching Sequence
The SmartEdit Bootstrap and Perspective Switching sequence is executed after the Iframe Injection Sequence has been executed. The SmartEdit Bootstrap and Perspective Switching sequence is as follows:
-
The browser sends a document-ready event that triggers the handler in the SmartEditBootstrap.js, which is part of the SmartEdit bundle.
-
The SmartEdit modules from the configuration are added as AngularJS dependencies of the SmartEdit application. The references of the SmartEdit module were kept in the window namespace after the injection sequence.
-
The SmartEdit application is bootstrapped.
-
At the end of the bootstraping process, AngularJS is triggered to recompile the DOM. Recompiling the DOM will process all AngularJS directives, including the process that instructs the container to load a perspective. The perspective that is loaded will be the default perspective, the Preview perspective, or the perspective that the user used when the user ended her/her last session (stored in a cookie).
-
Once the perspective has been loaded, the perspective service will publish a rerender event. The Render Service subscribes to this rerender event.
-
If a perspective other than the default Preview perspective is loaded, an overlay div is created and each visiblie SmartEdit component is cloned, wrapped in the SAK HTML markup, recompiled by AngularJS, and appended to the overlay.
-
For each component, recompiling will cause the SmartEdit (SAK) wrapper to wrap its clone with the decorators provided by the decorator service for the specified component type.
-
Each response from the decorator service contains only the decorators enabled for the loaded perspective.
-
When all SAK instances have been processed, SmartEdit is marked as ready by the render service, thereby ending the SmartEdit bootstrapping.
-
If the Preview perspective has been selected, the render service hides the overlay and marks SmartEdit as ready, thereby ending the SmartEdit bootstrapping.
When a perspective other than the Preview perspective is selected, the overlay is removed and steps 7 through 9 are repeated.
When partial rendering is required, such as when using drag and drop, adding, or removing a component, the dimensions and position of every clone in the overlay are recalculated to correspond to the new storefront layout.
The following diagram shows the compiling and module-wiring sequence.