AEM 6 - Showing Asset Metadata in Touch UI Asset Finder

Goal


Extend Asset Finder to show asset metadata in a tooltip in Touch UI.

Demo | Package Install

A similar extension for Classic UI is here




Solution


1) Login to CRXDE Lite (http://localhost:4502/crx/de) and create folder /apps/asset-meta-data

2) Create node /apps/asset-meta-data/clientlib of type cq:ClientLibraryFolder and add a String property categories with value cq.authoring.editor.hook.assetfinder

3) Create file (nt:file) /apps/asset-meta-data/clientlib/js.txt and add

                       metadata.js

4) Create file (nt:file) /apps/asset-meta-data/clientlib/metadata.js and add the following code

(function (document, $, assetFinder) {
    // class assetfinder-content-container is defined in
    // libs/wcm/core/content/editor/jcr:content/sidepanels/edit/items/assetsTab/items/contentPanel/items/assetfinder
    var aContainer = assetFinder.$el.find('.assetfinder-content-container');

    // id assetfinder-filter and class assetfilter.type are defined in
    // libs/wcm/core/content/editor/jcr:content/sidepanels/edit/items/assetsTab/items/filterPanel/items/views/items/search/items/searchpanel
    var $assetFinderFilter = $('#assetfinder-filter');
    var $assetFinderType = $assetFinderFilter.find(".assetfilter.type");

    var showMeta = { "dam:FileFormat": "File format", "dam:MIMEtype": "Mime type",
                        "dam:size": "Size in bytes", "jcr:lastModified": "Last Modified",
                        "tiff:ImageLength": "Length", "tiff:ImageWidth": "Width" };

    var c$cardView = CUI.CardView.get(aContainer);

    c$cardView.$element.on("change:insertitem", function (event) {
        if (event.moreItems) {
            return;
        }

        var type = $assetFinderType.find("select").find("option:selected").val();

        if (type !== "Images") {
            return;
        }

        var $cards = $(event.target).find(".card-asset");

        $.each($cards, function (i, card) {
            addToolTip(card);
        });
    });

    var addToolTip = function (card) {
        var $card = $(card), nodeValue = "";

        var addHtml = function (key, value) {
            return "<div style='display: block;'>" +
                        "<span style='font-family:adobe-clean; font-weight: bold'>" + key + "</span>: "
                        + value +
                    "</div>";
        };

        $.ajax({
            url: $card.data("path") + "/jcr:content/metadata.json",
            dataType: "json"
        }).done(function (data) {
            nodeValue = addHtml("Name", $card.find("h4").text());

            for (var x in data) {
                if (!data.hasOwnProperty(x) || !showMeta.hasOwnProperty(x)) {
                    continue;
                }

                if (data[x]) {
                    nodeValue = nodeValue + addHtml(showMeta[x], data[x]);
                }
            }

            nodeValue = nodeValue + "</div>";

            $card.mouseenter(function () {
                var tooltip = new CUI.Tooltip({
                    type: "info",
                    target: $(card),
                    content: nodeValue,
                    arrow: "bottom",
                    interactive: true
                });
            });
        });
    }
}(document, jQuery, Granite.author.ui.assetFinder));

5) Tooltip widget can be attached using this simple JS code 

var tooltip = new CUI.Tooltip({
                    type: "info",
                    target: $(card),
                    content: nodeValue,
                    arrow: "bottom",
                    interactive: true
                });

1 comment:

  1. hey,

    I've created reference components and in the content finder, I've customized it.(Reference : https://helpx.adobe.com/experience-manager/kb/CustomCFTab.html)

    But, the same isn't working with Asset finder in TOUCH UI. One way, I found is /libs/cq/gui/components/authoring/clientlibs/assetfinder/js....Inside it, create a new folder and copy paste js from any of images, pages etc. Then, the new tab will get display. Then, what I did was change the servlet URL to my custom servlet URL and others param changes I did which was required. Then, using AJAX call, I printed the data on console and it's getting printing in successfully but I don't know the way how to render it on GUI so I can display reference components in Asset Finder tab like it's getting displayed in content finder.

    ReplyDelete