Creating an AngularJS Widget

In Backoffice Framework, the ZK framework is used by default, however it is not obligatory and you can use any other JavaScript framework. In this document, you can see an example of creating an HTML widget using AngularJS directives.

Context

The widget you are going create in this tutorial should be able to communicate with the Advanced Search Widget and the Advanced Search Engine Widget. Let's call the widget Angular Sample Widget and define it in the extension called angularCNG.

Creating Extension

To create AngularJS widget, you need a backoffice-based extension.

Context

At first, we need to create our custom backoffice-based extension:

Procedure

  1. In the command prompt, navigate to the <HYBRIS_BIN_DIR>/platform directory and run ant extgen.
  2. Following the instructions that appear on the screen, create an extension with the following configuration:
    • Template: ybackoffice
    • Extension name: angularCNG
    • Package name: example.cng.angular
    • Sample widget: false
    • Sample style sheets: false

    The new extension is generated in the following directory: <HYBRIS_BIN_DIR>/custom

Defining Widget Dependencies

As it was decided to write this widget using AngularJS, you need to define a proper dependency in your extension.

Procedure

Create the js-libraries.xml file in the following directory: backoffice/resources/cng/js. This is the obligatory path inside your extension.

angularCNG/backoffice/resources/cng/js/js-libraries.xml

<libraries xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="http://www.hybris.com/schema/cockpitng/libraries.xsd">
 
    <library>
        <name>angular</name>
    </library>
 
</libraries>

Creating Widget Definition

Start creating an AngularJS widget with its definition.

Procedure

Create the widget's definition.xml file in any sub-directory of the backoffice/resources/widgets. The most important thing here is that we do not define a widget controller, but only the HTML view file:

angularCNG/backoffice/resources/widgets/angularwidget/definition.xml

<widget-definition xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:noNamespaceSchemaLocation="http://www.hybris.com/schema/cockpitng/widget-definition.xsd"
   id="example.cng.angular.widgets.angularwidget">
	<name>Angular Sample Widget</name>
	<description>Sample widget using AngularJS</description>
	<defaultTitle>AngularJS Sample Widget</defaultTitle>
	<author>hybris</author>
	<version>1.0</version>
	<view src="angularwidget.html" />
	<keywords>
		<keyword>AngularJS</keyword>
	</keywords>
	<sockets>
		<input id="input" type="java.lang.Object" />
		<output id="output" type="&lt;T&gt;" />
	</sockets>
</widget-definition>

Defining Widget View

AngularJS widget needs to have a view defined.

Context

The widget is supposed to receive an object through the input socket, display all its properties and then pass them through to the output socket. To separate JavaScript from HTML, define:
  • a view file, including HTML code:

    angularCNG/backoffice/resources/widgets/angularwidget/angularwidget.html
  • a script file, including some general-purpose methods, a controller, and a socket listener:

    angularCNG/backoffice/resources/widgets/angularwidget/angularwidget.js

Procedure

  1. Define HTML file in the angularCNG/backoffice/resources/widgets/angularwidget directory.

    angularCNG/backoffice/resources/widgets/angularwidget/angularwidget.html

    <widget ng-controller="SampleAngularController">
     
    	<h1 class="caption">Hello! This example shows how to write widgets using 3rd party js libraries</h1>
     
    	<h3 class="caption">Below, you may see what has been received through an input socket</h3>
    	<div ng-if="!input">Nothing done yet. Waiting for some socket input...</div>
    	<table ng-if="input">
    		<tr ng-repeat="(key, value) in input" ng-class="{true:'even', false:'odd'}[$even]">
    			<td class="label">{{key}}</td>
    			<td class="value">{{value}}</td>
    		</tr>
    	</table>
     
    	<cng-event type="socket" id="input" handler="socketInput(socket, data)" />
     
    </widget>
  2. Define JavaScript file in the angularCNG/backoffice/resources/widgets/angularwidget directory.

    angularCNG/backoffice/resources/widgets/angularwidget/angularwidget.js

    var LEVEL_ERROR = "ERROR";
    var LEVEL_WARNING = "Warning";
    var LEVEL_INFO = null;
     
    showMessage = function (level, message) {
        try {
            if (level === undefined || level == null) {
                alert(message);
            } else {
                alert(level + ": " + message);
            }
        } catch(e) {}
    };
     
    AngularCNG.init = function(widgetName, controller, services) {
     
        return new AngularCNG( "widgets." + widgetName ).controller(controller, services);
     
    };
     
    SampleAngularController = function($scope) {
     
        $scope.socketInput = function(socketId, data) {
            showMessage(LEVEL_INFO, "New socket message received");
     
            $scope.input = data;
            $scope.adapter.cng().socketEvent("output", data);
        };
     
    };
     
    AngularCNG.init("angularwidget", SampleAngularController);
  3. Optionally, you can add a CSS file to make the widget look better.

    For example: angularCNG/backoffice/resources/widgets/angularwidget/angularwidget.css

    .yw-labelstyle
    {
        color: red;
    }
      
    .caption {
        font-weight: bold;
        color: #13275a;
    }
      
    table {
        border: none;
        margin: 0;
    }
      
    .caption {
        font-weight: bold;
        color: #13275a;
    }
      
    table .header {
        text-align: center;
        font-weight: bold;
        background-color: #13275a;
        color: #c2e1ef;
    }
      
    table .label {
        text-align: right;
    }
      
    table .value {
        text-align: left;
    }
      
    table .even {
        background-color: #c4c4c4;
    }
      
    table .odd {
        background-color: #6699cc;
    }
      
    table .commit {
        text-align: center;
    }
      
    widget a {
        text-decoration: none;
        color: #303030;
    }
      
    widget a:hover {
        text-decoration: underline;
        color: #303030;
    }

Testing

To test the newly created widget, you need to deploy it to the application.

Procedure

  1. Append the new extension into the localextensions.xml and run ant all in the <HYBRIS_BIN_DIR>/ platform directory in the command prompt. Then start the server (by calling hybrisserver.bat or hybrisserver.sh).
  2. To test whether our new widget works as intended, open Backoffice in the Application Orchestrator mode (by pressing F4 on Windows or fn + F4 on Unix system).
  3. Open widgets settings of the Border Layout widget and enable the east-slot (set the eastDisabled configuration property to false):
  4. Add the newly created widget to the right slot of the Border Layout widget by pressing icon and choosing the Angular Sample Widget from the list.
  5. Exit Application Orchestrator mode to see the result is as follows:
  6. In the symbolic widgets view, change the connection between Advanced Search widget and the Advanced Search Engine widget - it should be passed through the Angular Sample Widget (the simplest way to achieve that is to remove the existing connections between the Advanced Search and the Advanced Search Engine and to create a new one):

Results

In the symbolic widgets view, we change the connection between Advanced Search widget and the Advanced Search Engine widget - it should be passed through the Angular Sample Widget (the simplest way to achieve that is to remove the existing connections between the Advanced Search and the Advanced Search Engine and to create a new one):