cq.screens.dcc.delete.js 5 KB
/*
 * ADOBE CONFIDENTIAL
 *
 * Copyright 2016 Adobe Systems Incorporated
 * All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and may be covered by U.S. and Foreign Patents,
 * patents in process, and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 */
(function(window, $, i18n) {
    'use strict';

    // global actions for GraniteUI foundation

    var ui = $(window).adaptTo('foundation-ui');

    // deletes entries of a collection with the sling post servlet
    $(window).adaptTo('foundation-registry').register('foundation.collection.action.action', {
        name: 'cq.screens.dcc.delete',
        handler: function(name, el, config, collection, selections) {
            var paths = selections.map(function(v) {
                return $(v).data('foundationCollectionItemId');
            });

            if (!paths.length) {
                return;
            }

            var JCR_CONTENT_PATTERN = '/jcr:content';

            function pathThatContainsJcrContent(path) {
                return path.indexOf(JCR_CONTENT_PATTERN) > -1;
            }
            function substringJcrContentPathOnly(path) {
                var index = path.indexOf(JCR_CONTENT_PATTERN);
                return path.substring(0, index + JCR_CONTENT_PATTERN.length);
            }
            function removeDuplicates(value, index, array) {
                return array.indexOf(value) === index;
            }

            // touch (i.e. update lastModified and lastModifiedBy properties) of the provided path to jcr:content content path
            function touchPageAsyncNoWait(pathToJcrContent) {
                // "touch" the page, no need to wait for the response
                $.post(pathToJcrContent, {
                    'jcr:lastModified': '',
                    'jcr:lastModifiedBy': ''
                });
            }

            // the paths refer to nodes in pages, the touchContainingPages config allows to touch the containing pages
            // i.e. update the last modified date and by on those pages                                                        ¯
            function handleTouchContainingPages(pathArray) {
                pathArray
                    .filter(pathThatContainsJcrContent)
                    .map(substringJcrContentPathOnly)
                    .filter(removeDuplicates)
                    .forEach(touchPageAsyncNoWait);
            }

            function handlePrompt() {
                ui.wait();
                var promises = paths.map(function(path) {
                    return $.ajax({
                        'method': 'DELETE',
                        'url': '/api/screens' + path
                    });
                });
                $.when.apply(null, promises)
                    .then(function() { // eslint-disable-line max-nested-callbacks
                        if (config.data && !!config.data.touchContainingPages) {
                            // touch pages (async), no need to wait for responses
                            handleTouchContainingPages(paths);
                        }
                        ui.clearWait();
                        $(collection).adaptTo('foundation-collection').reload();
                    })
                    .fail(function(jqXHR, textStatus, error) { // eslint-disable-line max-nested-callbacks
                        ui.clearWait();
                        ui.notify(i18n.get('Could not delete items.'), error, 'error');
                    });
            }

            var message = $('<div/>');

            var intro = $('<p/>').appendTo(message);
            if (selections.length === 1) {
                intro.text(Granite.I18n.get('You are going to delete the following item:'));
            } else {
                intro.text(Granite.I18n.get('You are going to delete the following {0} items:', selections.length));
            }

            var list = [];
            var maxCount = Math.min(selections.length, 12);
            for (var i = 0, ln = maxCount; i < ln; i++) {
                var title = $(selections[i]).find('.foundation-collection-item-title').text();
                list.push($('<b/>').text(title).prop('outerHTML'));
            }
            if (selections.length > maxCount) {
                list.push('&#8230;'); // &#8230; is ellipsis
            }

            $('<p/>').html(list.join('<br>')).appendTo(message);

            // Let the user choose what to do next
            ui.prompt(i18n.get('Delete'), message.html(), 'notice', [
                {text: i18n.get('Cancel')},
                {text: i18n.get('Delete'), warning: true, handler: handlePrompt}
            ]);
        }
    });

}(window, Granite.$, Granite.I18n));