Toolbar Service

The toolbar service allows developers to add actions to the SmartEdit header, experience, and perspective toolbar in the SmartEdit UI from modules developed for the SmartEdit container and SmartEdit application layers.

SmartEdit uses an iframe to separate the inner storefront from the outer SmartEdit container, which adds complexity to the use and configuration of toolbars. API requirements stipulate that other extensions must be able to add actions to a toolbar through the toolbar service that is exposed by the SmartEdit application, that is, the inner and outer applications that make up the SmartEdit application. SmartEdit requires multiple toolbars and these toolbars must not interfere with one another, therefore a toolbar directive that is reusable and distinct from other toolbars is required.

A reusable toolbar directive was developed on top of the underlying gateway factory. The directive is HTML markup compiled by AngularJS using a template and bound to a controller. The toolbar directive makes it possible for toolbar actions to be configured from the interface exposed by the outer application's (the SmartEdit container) and the inner application's (the SmartEdit application) toolbar services.

Requirements of the Toolbar Directive

The SmartEdit toolbar directive has the following requirements:

  • The toolbar template and controller functionality must be encapsulated within a directive.
  • A service or factory must be used to generate the outer toolbar service; the controller of the directive can use this service to fetch the current set of actions.
  • A mirroring inner toolbar service must be available on the Angular application on the inner iframe; this service must be able to add actions to the toolbar (through delegation using the gateway factory).
  • The inner toolbar service can add actions with messages, callbacks, and icons. The outer toolbar service can add actions, but it can also add HTML templates and hybrid actions that consist of an action and a collapsible template.
  • Users who want to add actions to the toolbar can use either the inner or outer service's addItems() function.

Implementing the Toolbar Directive

Implementing the toolbar service affects the following:

  • Outer toolbar
  • Outer toolbar service
  • Inner toolbar service

Outer Toolbar

The SmartEdit toolbar is an AngularJS directive that exists on the SmartEdit container. It can be placed in an HTML page by using the <toolbar> HTML tag with two mandatory attributes:

  • css-class: Specifies the class of the toolbar to provide styling.
  • toolbar-name: Sets a unique gateway ID to be used by the messaging service.

The directive's controller uses the unique gateway ID (provided to reference the outer toolbar service) to attach to the gateway messaging protocol. The controller registers with the toolbar service so that it is automatically updated when new actions become available.

The directive <toolbar> markup compiles into the following HTML div as shown in the following code example from the toolbarTemplate.html file:

<div class='{{cssClass}}'>

    <div data-ng-repeat="item in actions | filter:{type:'TEMPLATE'}" class="yTemplateToolbar" data-ng-include="item.include" data-ng-if="item.include"></div>

    <div class='{{actionsClasses}}'>

        <ul class="nav nav-pills group yPageTB pull-right">
            <li class="yToolbarBtn text-center" data-ng-repeat="item in actions | filter:{type:'!TEMPLATE'}" data-ng-switch="item.type">
                <div data-ng-switch-when="ACTION" class="btn-group ">
                    <button type="button" dropdown-toggle="true" data-toggle="button" class="btn" aria-pressed="false" data-ng-click="triggerAction(item, $event)">
                        <span>{{item.name | translate}}</span>
                        <img id="{{toolbarName}}_option_{{$index}}" data-ng-src="{{imageRoot}}{{item.icons.default}} " class="file" title="{{item.name | translate}}" alt="{{item.name | translate}}" />
                    </button>
                </div>

                <div data-ng-switch-when="HYBRID_ACTION" class="btn-group ySEHybridAction" dropdown is-open="status.isopen ">
                    <button data-ng-show="item.icons.default" type="button" class="btn btn-default yHybridAction yDddlbResolution" dropdown-toggle ng-disabled="disabled " aria-pressed="false " data-ng-click="triggerAction(item, $event)">
                        <span>{{item.name | translate}}</span>
                        <img id="{{toolbarName}}_option_{{$index}}" data-ng-src="{{imageRoot}}{{item.icons.default}}" class="file" title="{{item.name | translate}}" alt="{{item.name | translate}}" />
                    </button>

                    <div data-ng-include="item.include" data-ng-if="item.include"></div>

                </div>
                <div data-ng-switch-default></div>
            </li>
        </ul>
    </div>
</div>

Outer Toolbar Service

The outer toolbar service, ToolbarService, is used to add toolbar items to the SmartEdit application or the SmartEdit container. The addItems() function passes items to the ToolbarService (for more information, see the Outer ToolbarService Interface). The items are then forwarded to the toolbar directive as aliases. An alias is a key-name mapping that maps to a specific callback; the alias models the action as it is meant to be displayed in the toolbar. The directive is responsible for mapping the actions to the view displayed to the user.

The toolbar service factory generates instances of the ToolbarService based on the gateway ID (toolbar-name) provided. Only one ToolbarService instance can exist for each gateway ID, that is, the instance is a singleton with respect to the gateway ID.

Outer ToolbarService Interface

The interface of the Outer ToolbarService consists of the following functions and methods:

  • addAliases(aliases): Concatenates an array of aliases on the current set of aliases. It then triggers the directive callback (if provided), which updates the actions on the directives scope.

  • addItems(actions): Internally maps an array of actions so that the internal callback key can display and trigger them. The item variable provided in the template exposes the action properties. The following properties are exposed for each action:

    • callback: Callback triggered when this toolbar action item is used.
    • descriptionI18nkey: Description translation key.
    • icons: List of image URLs for the icon images (only for ACTION and HYBRID_ACTION).
    • iconClassName: List of classes used to display icons from fonts.
    • include: HTML template URL (only for TEMPLATE and HYBRID_ACTION).
    • keepAliveOnClose: Keeps the dropdown content in the DOM on close. (default = false)
    • key: Unique identifier of the toolbar action item.
    • nameI18nkey: Name translation key.
    • type: TEMPLATE, ACTION or HYBRID_ACTION.
  • addItemsStyling('class1 class2 ....'): Adds styling to the <ul> tag by wrapping all items ACTION and HYBRID_ACTION items.

  • getAliases(): Returns the current array of aliases on the toolbar service.

  • getItems(): Returns the current set of actions attached to the toolbar. It is a key-callback mapping.

  • setOnAliasesChanges(callback): Ties a directive callback to the toolbar service. Each time an alias is changed, the scope is updated.

  • triggerAction(alias): Checks if the key exists on the internal map of actions. If the key exists, the action callback is triggered. If the key does not exist, the action is delegated to the triggerActionOnInner(alias) proxy function.

  • triggerActionOnInner(alias): Proxy function that delegates triggering an action to the inner toolbar service; it triggers a callback on the inner application which results in an action on the inner application scope).

Use Cases and Sequence Diagrams

Diagram 1: Add Actions

The user can add actions through the outer toolbar service, which results in the immediate update of the toolbar view.

Diagram 2: Click an Action

The user can click on an action in the view and the corresponding action must be triggered on the scope to which the action is attached: either the inner or outer application.

Inner Toolbar Service

The inner toolbar service is used to do the following:

  • Add toolbar actions that affect the inner application.
  • Publish aliases to the outer application through the proxy.
  • Store and manage callbacks made by private key.

The service is a managed singleton; the factory provides a getToolbarService function that returns a single instance of the ToolbarService for the gateway ID (toolbar-name), provided by the outer toolbar.

Inner ToolbarService Interface

The interface of the inner ToolbarService consists of the following functions and methods:

  • addAliases(aliases): Delegates the addition of aliases to the outer toolbar service. It is proxy function.

  • addItems(actions): Internally maps an array of actions so that an internal callback key displays and triggers them. Action properties are made available through the item variable in the included template. The properties of each action are:

    • callback: Callback triggered when this toolbar action item is used.
    • i18nKey: Name translation key.
    • type: ACTION
  • addItemsStyling('class1 class2 ....'): Adds styling to the <ul> tag by wrapping all ACTION and HYBRID_ACTION items.
  • getItems(): Returns the current set of actions attached to the toolbar service; it is akey-callback mapping.
  • triggerActionOnInner(action): Checks if the key exists on the internal map of actions. If the key exists, the action callback is triggered. If the key does not exist, an error is logged.

Use Cases and Sequence Diagrams for the Inner Toolbar Service

Diagram 3: Add Actions

The user can add actions using the inner toolbar service, which results in the immediate update of the toolbar view.