Adding Decorators to Components
A decorator is a feature that adds rendering and behavior to components. You can design modules that contain a set of decorators, which in turn can be wired to different components.
A decorator attaches to a component on a page. It provides HTML-rendering or DOM manipulation around the component. A decorator has behavior that can call a backend API if necessary. Components are elements in storefronts that perform different functions, such as the paragraph component that contains simple text and the simple responsive banner component that contains images for different inflection points. To perform different operations on components, you require decorators that wrap around the components.
Adding Simple Decorators
Create a simple decorator named sample that you’ll wire to a component type named sampleComponent. The function of the sample is to display the text decorator wired to the component when the pointer is over the component, and to the hide the text when the pointer isn't over the component.
To create a simple decorator and wire it to a module, do the following:
- Create a module for the decorator.
- Create a template.
- Add the decorator to the module.
- Wire the decorators to the components.
- Create a module for the decorator template.
- Create a JavaScript file to expose the decorator.
- Expose and wire the module to the SmartEdit application.
Creating an AngularJS Module for the Decorator
To create an AngularJS module for the decorator, do the following:
- Create an AngularJS module for the decorator.
- Name the decorator module sampleDecorator.
- Give the decorator module a directive/decorator called sample.
- Save the file in the web/features/sampleDecorator folder.
The following code excerpt shows how to do this:
angular.module('sampleDecorator', ['myModuleTemplates'])
.directive('sample', ['$timeout', function($timeout) {
return {
templateUrl: 'web/features/sampleDecorator/sampleDecoratorTemplate.html',
restrict: 'C',
transclude: true,
replace: false,
scope: {
smarteditComponentId: '@',
smarteditComponentType: '@',
active: '='
},
link: function($scope, element, attrs) {
$scope.visible = false;
$scope.mouseleave = function() {
$timeout(function() {
$scope.visible = false;
}, 1000);
};
$scope.mouseenter = function() {
$scope.visible = true;
};
}
};
}]);
In the code excerpt above, the following has been done:
- Set the value for the templateURL property to the path of the folder where the decorative template is located; this is triggered when the directive is compiled.
- Set the value for the restrict property to C in order to restrict the directives to the CSS class type. We do this because we want the decorator to be triggered on finding the respective class attribute.
- Set the value for the replace property to false to instruct the directive to append the template content to the original DOM element and not to replace the whole element.
- Set the value for the transclude property to true to instruct the directive to inject the original DOM content into a predetermined location within the template.
-
Defined the values for the scope; they must be set as follows:
- smarteditComponentType: The type of the component extracted from the original DOM element in the storefront.
- smarteditComponentId: The ID of the component extracted from the original DOM element in the storefront.
- active: A boolean passed on by the SmartEdit wrapper that will be used in the template to hide the decorator.
- Set the link function. This function is used to attach event listeners to DOM elements, to watch model properties for changes, and to update the DOM. It’s executed after the directive has been compiled and it acts as a controller to call APIs. The following values are defined for the link function:
- Set the value for the visible property to false.
- Enable the visibility on triggering the mouseenter event.
- Disable the visibility on triggering the mouseleave event.
When creating a module for a decorator, you must include the tag <div data-ng-transclude> in all templates. The tag instructs the directive where to inject the transcluded content.
Creating a Template
You must create a decorator template named sampleDecoratorTemplate.html in web/features/sampleDecorator/sampleDecoratorTemplate.html. The template must define the mouseenter and mouseleave event handlers that control the visibility of the decorator.
The following code excerpt shows how to create a template named sampleDecoratorTemplate.html:
<div> <div data-ng-mouseleave="mouseleave()" data-ng-if="active"> <div data-ng-mouseenter="mouseenter()"> <div data-ng-transclude></div> <span id='decoratorText'>decorator wired</span> </div> </div> <div data-ng-mouseleave="mouseleave()" data-ng-if="!active"> <div data-ng-transclude></div> </div> </div>
In the code excerpt above, the ng-mouseleave and ng-mouseenter are the core AngularJS directive components. They specify the custom behavior to be performed on mouse-leave and mouse-enter actions. When a mouse-enter action is performed on a sampleComponent, the decorator displays the text decorator wired.
Adding Decorators to Modules
Once you design the decorator, you must wire it to the module that you created. You can wire multiple decorators to a single module and then add the module to SmartEdit.
To add your decorator to your module, do the following:
-
Create a module named myModule.
-
Add the decorator as a dependency to the module.
-
Add the decoratorServiceModule as a dependency to the module.
Adding the decoratorServiceModule decorator to the module allows you to wire the decorator to the component type. For information on wiring the decorator, see Wiring Decorators to Components.
- Save your module to the web/features/ folder, for example: web/features/myModule.js.
The following code excerpt shows how to create a module named myModule that includes the decorator as a dependency:
angular.module('myModule', [
'sampleDecorator',
'decoratorServiceModule'
])
Wiring Decorators to Components
Once you wire the decorator to the module, you must specify which components can be wired to the decorator. To do this, use the addMappings method of the decoratorService (which is part of the SmartEdit framework) in the run block of the myModule module.
The following excerpt shows how to specify which components can be wired to the decorator:
.run(function(decoratorService) {
decoratorService.addMappings({
'sampleComponent': ['sample']
});
});
As shown in the code excerpt above, the addMappings method of the decoratorService is called. It passes a map of the component-to-decorator mapping as the argument. The component names are the keys in the mapping. The component name can be a full name of the componentType or an ant-like wildcard such as *middle*Suffix, or any valid regex.
The value in the mapping is an array of decorators that you want to activate for the particular component-type match. The following code excerpt shows the use of a regex for component mapping:
'componentType1': ['decorator1'] --> will wire decorator1 to componentType1 (exact matching). 'componentType1': ['decorator1', 'decorator2'] --> will wire both decorator1 and decorator2 to componentType1 (exact matching). '.*Type1': ['decorator1'] --> will wire decorator1 to all components that end with "Type1". '^((?!Type1).)*$': ['decorator1'] --> will wire decorator1 to all components except those which contain "Type1" in their name.
Creating a Module for the Decorator Templates
You must generate an AngularJS module that contains template caches so that HTML files are converted into JavaScript modules. You can do this in the Grunt file, Gruntfile.js, using an existing Grunt build task to concatenate and register your AngularJS templates in $templateCache. The following code excerpt of the Gruntfile.js file shows how to do this:
ngtemplates: {
options: {
standalone: true, //to declare a module as opposed to binding to an existing one
module: 'myModuleTemplates'
},
run: {
src: ['web/features/**/*Template.html'],
dest: 'jsTarget/web/features/templates.js'
}
}
As shown in the code excerpt above, when this task is executed, it finds all templates that end with Template.html and load them into the given destination file templates.js as an AngularJS module named myModuleTemplates. If you add the newly created module, myModule.js, to all decorator modules, you’ll make the templates available without having to execute Ajax calls in the browser.
Creating a Single JavaScript File to Expose
You must concatenate the JavaScript files required for the module, including the template.js file, into a single JavaScript file so that it can be exposed to the SmartEdit application. Your build must save the file to the webroot directory of the project, web/webroot/js. You can concatenate the files using the concat task of the Grunt build mechanism. The following code excerpt shows how to do this:
concat: {
options: {
separator: ';'
},
dist: {
src: ['web/features/features/**/*.js', 'some other pattern'],
dest: 'jsTarget/myModule.js'
},
},
As shown in the code excerpt above, all the JavaScript files and the generated templates.js file are concatenated and placed into a single file in web/webroot/js/myModule.js.
Exposing and Wiring the Module to SmartEdit Application
Once you’ve created the module, you must expose it through an HTTPS interface. You can do this by creating an extension for the module in the Platform. Refer to the following documentation:
- To create an extension for the module, see the Exposing the Module to SmartEdit section of Setting Up Your SmartEdit Development Environment.
- To wire the module to SmartEdit, see Configuring Modules for SmartEdit and the SmartEdit Container section of Configuration Editor.
Known Constraints When Creating Decorators
Naming Constraints
- A decorator must have a unique name and the name should represent the business purpose. You must avoid using simple or standard HTML class names as they won’t be wrapped correctly.
- The name of the AngularJS module of a decorator must always end with Decorator.
- The name of the template of a decorator must always end with Template.html.
Constraints on the Directive Controller of the Decorator
- The directive of the decorator must have C as its restriction type, as it’s triggered only if the class name (CSS) matches the directive name.
- The decorator directive's replace property must be set to false because you don't want to replace entire element that the directive is declared on.
- The decorator directive's transclude property must be set to true because it appends the inner content of the directive declaration.
- Don't add or remove any elements to the scope property.
- The scope of smarteditComponentId and smarteditComponentType properties must be set to @ to allow two-way binding.
- The scope of active must be set to =.