cq.screens.dcc.unassignChannel.js 3.2 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.unassignChannel',
        handler: function(name, el, config, collection, selections) {
            var displayPaths = selections.map(function(v) {
                return v.dataset.foundationCollectionItemId;
            });

            if (!displayPaths.length) {
                return;
            }

            var channelPath = collection.dataset.foundationCollectionId;
            if (!channelPath) {
                return;
            }

            function handlePrompt() {
                ui.wait();

                window.CQ.screens.dcc.channel.unassignFromDisplays(channelPath, displayPaths).then(function() {
                    ui.clearWait();
                    $(collection).adaptTo('foundation-collection').reload();
                }, function(err) {
                    ui.clearWait();
                    ui.notify(i18n.get('Deletion failed'), err, 'error');
                });
            }

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

            var intro = $('<p/>').appendTo(message);
            if (selections.length === 1) {
                intro.text(Granite.I18n.get('You are going to remove the channel assignment from the following display:'));
            } else {
                intro.text(Granite.I18n.get('You are going to remove the channel assignment from the following {0} displays:', selections.length));
            }

            // Re-using the algorithm from Sites' delete operation
            var list = [];
            var maxCount = Math.min(selections.length, 12);
            for (var i = 0; i < maxCount; 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('Remove Channel Assignments'), message.html(), 'notice', [
                {text: i18n.get('Cancel')},
                {text: i18n.get('Remove'), warning: true, handler: handlePrompt}
            ]);
        }
    });

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