device-properties.js 3.08 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, document, $, i18n, dcc) {
    'use strict';

    var PROPERTIES_DIALOG = '#screens-dcc-actions-deviceproperties-dialog';
    var PROPERTIES_SELECTOR = '.screens-dashboard-DeviceInfoTile';
    var BACKEND_ENDPOINT = '/profile_screens.json';

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

    function startAsyncOperation(el) {
        return Promise.resolve();
    }

    function updateProperties(endpoint, properties) {
        return function() {
            return $.ajax(endpoint, {
                    type: 'POST',
                    dataType: 'json',
                    data: properties
                });
        };
    }

    function reportError(err) {
        ui.notify(null, err, 'error');
        return Promise.resolve();
    }

    function hideDialog() {
        $('.screens-dcc-actions-deviceproperties-dialog').get(0).hide();
        return Promise.resolve();
    }

    function reloadUI() {
        var content = $('.foundation-content').adaptTo('foundation-content');
        content.refresh();
    }

    var handlePropertiesSubmit = function(ev) {
        var $device = $('[data-device-path]');
        var devicePath = $device.data('devicePath');

        var props = $(PROPERTIES_DIALOG).serializeArray().reduce(function(res, param) {
            if (!param.name.match(/^[_:]/)) {
                res[param.name.split('/').pop()] = param.value;
            }
            return res;
        }, {});

        startAsyncOperation($(PROPERTIES_SELECTOR).get(0))
            .then(updateProperties(devicePath + BACKEND_ENDPOINT, props))
            .then(hideDialog)
            .catch(reportError)
            .then(reloadUI);
        return false;
    };

    $(document).on('submit', PROPERTIES_DIALOG, handlePropertiesSubmit);

    // Add cache busting parameter to the dialog src to make sure its data is updated after a modification
    // Remove once GRANITE-14378 has been tackled
    $(document).on('foundation-contentloaded', function(ev) {
        if ($('.screens-DeviceDashboard').length) {
            var $el = $('.screens-dcc-actions-Device-properties-activator');
            if ($el.length) {
                var action = $el.data('foundationCollectionAction');
                action.data.src = action.data.src.replace(/\?.*/, '') + '?t=' + Date.now();
                $el.data('foundationCollectionAction', action);
            }
        }
    });

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