cq.screens.dcc.copy.js 5.78 KB
/*
 * ADOBE CONFIDENTIAL
 *
 * Copyright 2017 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.
 */
/* eslint max-nested-callbacks: [2, 5] */
(function(window, $, i18n) {
    'use strict';

    var CLIPBOARD_KEY = 'cq-wcm-copy-items';
    var COMMAND_URL = Granite.HTTP.externalize('/content/screens/svc/copy');
    var ACTIVATOR_SELECTOR = '.screens-dcc-actions-paste-activator';

    var clipboard = $(window).adaptTo('foundation-clipboard');
    var ids = null;

    function closeBrowserWarning() {
        return Granite.I18n.get('Paste operation is in progress. Refreshing page may cause unexpected results.');
    }

    function updatePasteButton(context) {
        context = context || document;
        var pasteButton = $(ACTIVATOR_SELECTOR, context);

        ids = clipboard.get(CLIPBOARD_KEY);

        if (!ids) {
            pasteButton.attr('hidden', 'hidden');
            return;
        }

        pasteButton.removeAttr('hidden');
    }

    function getDestPath(activator) {
        var dest = activator.data('cqWcmPasteActivatorDest');

        if (dest) {
            return dest;
        }

        var target = activator.data('cqWcmPasteActivatorTarget');

        if (!target) {
            return null;
        }

        var collection = $(target);

        if (!collection.hasClass('foundation-collection')) {
            return null;
        }

        return collection.data('foundationCollectionId');
    }

    function triggerPasteCompletionEvent(args, params) {
        var destPath;
        if ($.isArray(args[0])) {
            // Multiple items pasted
            for (var i = 0; i < args.length; i++) {
                destPath = $(args[i][0]).find('#Message').html();
                params[i].destPath = destPath;
            }
        } else {
            // Single item pasted
            destPath = $(args[0]).find('#Message').html();
            params[0].destPath = destPath;
        }

        $(document).trigger('cq-wcm-paste-completed', [params]);
    }

    $(window).adaptTo('foundation-registry').register('foundation.collection.action.action', {
        name: 'cq.screens.dcc.copy',
        handler: function(name, el, config, collection, selections) {
            ids = selections.map(function(v) {
                return $(v).data('foundationCollectionItemId');
            });

            if (!ids.length) {
                return;
            }

            clipboard.set(CLIPBOARD_KEY, ids);
            updatePasteButton();

            $(collection).adaptTo('foundation-selections').clear();
        }
    });

    $(document).on('click', ACTIVATOR_SELECTOR, function(e) {
        e.preventDefault();

        var activator = $(this);
        var destParentPath = getDestPath(activator);
        var outputParams = [];

        if (!destParentPath) {
            return;
        }

        var ui = $(window).adaptTo('foundation-ui');
        ui.wait();
        $(window).on('beforeunload', closeBrowserWarning);

        var promises = ids.map(function(v) {
            // check if we are copying a template which is stored in /conf
            var relativeTemplateParent = 'settings/wcm/templates';
            if (v.indexOf('/conf') === 0
                && v.indexOf(relativeTemplateParent) > 0
                && destParentPath.indexOf(relativeTemplateParent) === -1) {
                destParentPath += '/' + relativeTemplateParent;
            }

            var outputData = {
                srcPath: v,
                destParentPath: destParentPath,
                before: '',
                shallow: false
            };
            outputParams.push(outputData);

            return $.ajax({
                url: COMMAND_URL,
                type: 'POST',
                data: {
                    _charset_: 'UTF-8',
                    srcPath: v,
                    destParentPath: destParentPath,
                    before: '',
                    shallow: false
                }
            });
        });

        $.when.apply(null, promises)
            .always(function() {
                $(window).off('beforeunload', closeBrowserWarning);

                ui.clearWait();
            })
            .done(function() {

                triggerPasteCompletionEvent(arguments, outputParams);

                var target = activator.data('cqWcmPasteActivatorTarget');

                if (target) {
                    var api = $(target).adaptTo('foundation-collection');
                    if (api && 'reload' in api) {
                        api.reload();
                        return;
                    }
                }

                var contentApi = $('.foundation-content').adaptTo('foundation-content');
                if (contentApi) {
                    contentApi.refresh();
                }
            }).fail(function(xhr) {
                if (xhr.status === 0 || xhr.readyState === 0) {
                    // premature reload, do nothing
                    return;
                }

                var title = Granite.I18n.get('Error');
                var message = Granite.I18n.getVar(xhr.responseText);
                ui.alert(title, message, 'error');
            });
    });

    $(document).on('foundation-contentloaded', function(e) {
        updatePasteButton(e.target);
    });

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