screenshot.js 4.04 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, $, dcc) {
    'use strict';

    var POLLING_INTERVAL = 1000; // ms

    var SCREENSHOT_CLASS = 'screens-DeviceScreenshot';

    var SELECTOR_SCREENSHOT = '.screens-dcc-actions-Device-screenshot-activator';

    var SELECTOR_FULLSCREEN = '.screens-dcc-actions-Device-fullscreen-activator';

    var SELECTOR_DIALOG = '.screens-dcc-actions-DeviceScreenshotDialog';

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

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

    function getTimestamp(html) {
        var timestamp = $('.' + SCREENSHOT_CLASS + '-timestamp time', html).attr('datetime');
        return timestamp ? new Date(parseInt(timestamp, 10)) : null;
    }

    function renderImage() {
        var $screenshotImg = $('img.' + SCREENSHOT_CLASS + '-image');
        if ($screenshotImg.length) {
            $screenshotImg.attr('src', $screenshotImg.data('src'));
        }
    }

    function readScreenshot(deviceProfilePath) {
        var url = Granite.HTTP.externalize(deviceProfilePath + '/screenshot/jcr:content.json');
        return $.ajax(url);
    }

    function pollScreenshot(deviceProfilePath, timestamp) {
        var screenshotPollTimeout;

        var deferred = $.Deferred();  // eslint-disable-line new-cap
        var handleScreenshot = function() {

            var checkScreenshot = function(data) {
                var newTimestamp = data['jcr:lastModified'] ? new Date(data['jcr:lastModified']) : null;
                if (newTimestamp && (timestamp ? timestamp.getTime() : 0) < newTimestamp.getTime()) {
                    console.log(timestamp, newTimestamp);
                    deferred.resolve(newTimestamp);
                }
                else {
                    handleScreenshot();
                }
            };

            window.clearTimeout(screenshotPollTimeout);
            screenshotPollTimeout = window.setTimeout(function() {
                readScreenshot(deviceProfilePath)
                    .then(checkScreenshot)
                    .fail(handleScreenshot);
            }, POLLING_INTERVAL);
        };
        handleScreenshot();

        return deferred.promise();
    }

    function updateScreenshot(deviceId) {
        ui.wait($('.screens-dashboard-DeviceScreenshotTile').get(0));
        dcc.playerCommands.sendCommandToDevice(deviceId, dcc.playerCommands.COMMANDS.SCREENSHOT)
            .then(function(deviceData) {
                return pollScreenshot(deviceData.deviceProfilePath, getTimestamp());
            })
            .then(reloadUI);
    }

    $(document).on('click', SELECTOR_SCREENSHOT, function(ev) {
        var $t = $(ev.currentTarget);

        var $device = $t.closest('.screens-Device');
        var deviceId = $device.find('[data-device-id]').data('deviceId');

        if (!deviceId) {
            return;
        }

        updateScreenshot(deviceId);
    });

    $(document).on('click', SELECTOR_FULLSCREEN, function(ev) {
        $(document).on('coral-overlay:beforeopen', SELECTOR_DIALOG, function() {
            $('.screens-dcc-actions-DeviceScreenshotDialog coral-dialog-content > div').empty().append($('.screens-DeviceScreenshot-image').clone());
        });
    });

    $(document).on('foundation-contentloaded', function() {
        renderImage();
    });

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