Adding Contextual Menu Items to Components

A contextual menu is a core decorator that wraps around a component. It can contain a set of icons, each of which performs a different action. You can add contextual menu items to the contextual menu decorator in the SmartEdit web application using SmartEdit modules.

Adding Items to the Contextual Menu

You add contextual menu items to components using the contextual menu service of the contextual menu service module. To add items to the contextual menu, you must call the Add Items method of the contextual menu service and pass a mapping of component type to array of contextual menu items. The key in the mapping is the name of the component type. The component name can be the full name of the component type, an ant-like wildcard (such as *middle*Suffix), or a valid regex that includes or excludes a set of component types. The value in the mapping is an array of contextual menu items that you want to activate for the component type match.

The following code excerpt shows the use of a regex for component mapping:

'componentType1': ['menuItem1'] --> will wire decorator1 to menuItem1 (exact matching).
'componentType1': ['menuItem1', 'menuItem2'] --> will wire both menuItem1 and menuItem2 to componentType1 (exact matching).
'*Type1': ['menuItem1'] --> will wire menuItem1 to all components that end with "Type1".
'^((?!Type1).)*$': ['menuItem1'] --> will wire menuItem1 to all components except those which contain "Type1" in their name.

The contextual menu items can have the following properties:

  • i18nKey: Is the message key of the contextual menu item to be translated.
  • condition: Is an optional entry that holds the condition required to activate the menu item based on the component type and the component ID.
  • callback: Is the action to be performed when a user clicks on the menu item.
  • icon: Contains the location of the icon of the contextual menu item to be displayed.
  • smallicon: Contains the location of the smaller version of the icon; this is the icon that is displayed when the menu item is part of the More... menu option.

Once you have added the contextual menu items, you must activate the contextual menu decorator for the component types. You do this by adding the contextual menu entry using the addMappings method of the decorator service. We recommend that you add the code to a run block of an AngularJS module.

The following code excerpt shows how to add contextual menu items:

angular.module('CMSApp', [
        'decoratorServiceModule',
        'contextualMenuServiceModule'
    ])
    .run(function(decoratorService, contextualMenuService) {

            decoratorService.addMappings({
                '^((?!Slot).)*$': ['contextualMenu'],
            });

            contextualMenuService.addItems({
                'SimpleResponsiveBannerComponent': [{
                    i18nKey: 'CLONE',
                    condition: function(componentType, componentId) {
                        return componentId === 'ElectronicsHompageSplashBannerComponent';
                    },
                    callback: function(componentType, componentId) {
                        alert('cloning_' + componentType + "_" + componentId);
                    },
                    icon: '.../icons/clone.png'
                }],
 			
				'.*FooterNavigationComponent': [{
                    i18nKey: 'INFO',
                    condition: function(componentType, componentId) {
                        return true;
                    },
                    callback: function() {
                        alert('clicked on menu item INFO');
                    },
                    icon: '.../icons/info.png',
                    smallicon: '.../icons/info_small.png'
                }, {
                    i18nKey: 'ROLLBACK',
                    condition: function(componentType, componentId) {
                        return true;
                    },
                    callback: function() {
                        alert('clicked on menu item ROLLBACK');
                    },
                    icon: '.../icons/rollback.png',
                    smallicon: '.../icons/rollback_small.png'
                }]
            });
        });

The above code example shows the following:

  • A contextual menu item name CLONE has been added for simple responsive banner component type
  • The contextual menu items INFO and ROLLBACK have been added for all component types that contain footer navigation component in their name.
  • The contextual menu decorator has been activated for all component types except for those that contain slot in their name.