AEM 62 - Add Start Workflow Select to Assets Console Action Bar

Goal


Add Coral Select - granite/ui/components/coral/foundation/form/select to Assets Console Action bar to start workflow on selected assets

For adding a button to 61 Assets action bar check this post

Demo | Package Install


Product



Extension



Solution


1) Login to CRX DE Lite http://localhost:4502/crx/de/index.jsp and create nt:folder /apps/eaem-assets-action-bar-start-workflow

2) Create sling:Folder /apps/eaem-assets-action-bar-start-workflow/button and nt:unstructured /apps/eaem-assets-action-bar-start-workflow/button/model for rendering start workflow coral select, with the following structure

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
    jcr:primaryType="nt:unstructured"
    sling:resourceType="granite/ui/components/coral/foundation/form/select"
    emptyText="Select a Workflow Model"
    workflowtags="[dam]">
    <datasource
        jcr:primaryType="nt:unstructured"
        sling:resourceType="cq/gui/components/coral/common/admin/timeline/events/workflow/datasources/models"/>
</jcr:root>

3) Create a clientlib (type cq:ClientLibraryFolder) /apps/eaem-assets-action-bar-start-workflow/clientlib with categories - dam.gui.admin.util and dependencies - [underscore]

4) Create file /apps/eaem-assets-action-bar-start-workflow/clientlib/js.txt with the following content

                                                start-workflow.js

5) Create file /apps/eaem-assets-action-bar-start-workflow/clientlib/start-workflow.js, add the following code

(function ($, $document) {
    // cq-damadmin-admin-actions-share-activator
    // defined here /libs/dam/gui/content/commons/sidepanels/search/items/searchpanel/result/header/items/selection/items/editasset
    var SHARE_ACTIVATOR = "cq-damadmin-admin-actions-share-activator",
        BUTTON_HTML_URL = "/apps/eaem-assets-action-bar-start-workflow/button/model.html",
        added = false;

    $document.on("foundation-mode-change", function(e, mode){
        if(added || (mode !== "selection") ){
            return;
        }

        added = true;

        $.ajax(BUTTON_HTML_URL).done(addButton);
    });

    function addButton(html){
        var $eActivator = $("." + SHARE_ACTIVATOR);

        if ($eActivator.length == 0) {
            return;
        }

        var $startWorkflow = $(html).css("margin-left", "20px").insertBefore( $eActivator );

        CUI.Select.init($startWorkflow, $document);

        var cuiSelect = $startWorkflow.data("select");

        cuiSelect.on('coral-select:change', handleSelect);
    }

    function handleSelect(){
        var selection = this._getSelection();

        if(_.isEmpty(selection)){
            return;
        }

        var wSelect = this, model = selection.value,
            wTitle = $(selection).html(),
            paths = [], $items = $(".foundation-collection").find(".foundation-selections-item");

        $items.each(function(index, item) {
            paths.push($(item).data("foundation-collection-item-id"));
        });

        function startWorkflow(){
            var data = [{name: "_charset_", value: "utf-8"},
                {name: ":status", value: "browser"},
                {name: "payloadType", value: "JCR_PATH"},
                {name: "model", value: model}];

            _.each(paths, function(path){
                data.push( { name: "payload", value: path} )
            });

            $.ajax( { url: "/etc/workflow/instances" , type: "post", data: data}).done(function(){
                showMessage("Success", "Workflow initiated");
                wSelect.clear();
            })
        }

        showConfirmation("Workflow", "Run workflow '" + wTitle + "' on selected items?", startWorkflow);
    }

    function showMessage(title, message){
        $(window).adaptTo('foundation-ui').prompt(title, message, "notice", [{
            primary: true,
            text: Granite.I18n.get('OK')
        }]);
    }

    function showConfirmation(title, message, handler){
        var cancel = {
            text: Granite.I18n.get('Cancel')
        };

        var ok = {
            text: Granite.I18n.get('OK'),
            primary: true,
            handler: handler
        };

        $(window).adaptTo('foundation-ui').prompt(title, message, 'warning', [cancel, ok ]);
    }
})(jQuery, jQuery(document));

No comments:

Post a Comment