actions.js 5.41 KB
/*
 * ADOBE CONFIDENTIAL
 *
 * Copyright 2015 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, dcc) {
    'use strict';

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

    $(window).adaptTo('foundation-registry').register('foundation.collection.action.action', {
        name: 'cq.screens.dcc.device.delete',
        handler: function(name, el, config, collection, selections) {
            var $title = $(selections[0]).find('.foundation-collection-item-title');
            var deviceName = $title.attr('value') || $title.attr('title');

            if (!deviceName) {
                console.error('Fatal: Could not delete Device because Device Name could not be resolved.');
                return;
            }

            function handlePrompt() {
                ui.wait();
                dcc.device.delete(deviceName).then(function() {
                    ui.clearWait();
                    $(collection).adaptTo('foundation-collection').reload();
                }).catch(function(error) {
                    ui.clearWait();
                    ui.notify(i18n.get('Could not delete Device.'), error, 'error');
                });
            }

            var PROMPT_HTML = '<p>' + i18n.get('You are going to delete the Device:') + '</p>' +
            '<p><strong>' + deviceName + '</strong></p>';

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

    $(window).adaptTo('foundation-registry').register('foundation.collection.action.action', {
        name: 'cq.screens.dcc.device.updateconfig',
        handler: function(name, el, config, collection, selections) {

            // Handle case when there is a selection
            if (selections.length) {
                var getDeviceId = function(tr) {
                    return tr.querySelector('.screens-Device-id').getAttribute('value');
                };
                var updateConfig = function(deviceId) {
                    return dcc.device.updateConfig(deviceId);
                };
                Promise.all(selections.map(getDeviceId).map(updateConfig))
                    .then(function() {
                        ui.notify(null, i18n.get('Update command sent to all devices.'), 'success');
                    })
                    .catch(function(error) {
                        ui.notify(null, error.message, 'error');
                    });
                return;
            }

            // Handle case when there is no selection and we use the collection instead
            var collectionId = collection.dataset.foundationCollectionId;
            if (collectionId) {
                dcc.device.bulkUpdateConfig(collectionId)
                    .then(function() {
                        ui.notify(null, i18n.get('Update command sent to all devices.'), 'success');
                    })
                    .catch(function(error) {
                        ui.notify(null, error.message, 'error');
                    });
            }
        }
    });

    $(window).adaptTo('foundation-registry').register('foundation.collection.action.action', {
        name: 'cq.screens.dcc.device.unassign',
        handler: function(name, el, config, collection, selections) {

            function handlePrompt() {
                ui.wait();
                var devices = selections.map(function(item) {
                    return $(item).data('deviceId');
                });
                dcc.device.unassign(devices).then(function() {
                    ui.clearWait();
                    $(collection).adaptTo('foundation-collection').reload();
                }).catch(function(error) {
                    ui.clearWait();
                    ui.notify(i18n.get('Could not unassign devices.'), error, 'error');
                });
            }

            var message = '<p>';
            if (selections.length === 1) {
                message += i18n.get('You are going to remove the assignment for the following device:');
            }
            else {
                message += i18n.get('You are going to remove the assignment for the following {0} devices:', selections.length);
            }
            message += '</p><p>';
            selections.forEach(function(item) {
                message += '<strong>' + $(item).find('.screens-Device-id').attr('title') + '</strong><br/>';
            });
            message + '</p>';

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

}(window, Granite.$, Granite.I18n, window.CQ.screens.dcc));