init.js 4.23 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.
 */
/* globals require */
(function($) {
    'use strict';

    var SEQUENCE_ITEMS_SELECTOR = '[data-strategy]';

    window.ScreensPlayer = window.ScreensPlayer || {
        strategies: {},
        transitions: {}
    };

    /**
     * Initialize the sequences
     *
     * @param {WindowIO}    wio             The window.io object
     * @param {boolean}     immediateStart  true to start the sequence immediately
     * @returns {Function} a function for the array iterator
     */
    function initSequence(wio, immediateStart) {
        return function(i, el) {
            var newSequence = new window.ScreensPlayer.Sequence(wio, el);
            wio.on('channel-show', function(payload, origin) {
                console.log('[Sequence#init] Reveiced channel-show for origin ' + origin);
                if (origin === 'single') { // message from firmware
                    newSequence.start();
                }
            });
            wio.on('channel-hide', function(payload, origin) {
                console.log('[Sequence#init] Reveiced channel-hide for origin ' + origin);
                if (origin === 'single') { // message from firmware
                    newSequence.stop();
                }
            });
            if (immediateStart) {
                newSequence.start();
            }
        };
    }

    if (window === window.top) {
        require(['window.io'], function(WindowIO) {
            $(document).ready(function() {
                // only autostart the sequence in the authoring case (no orchestrator that triggers the plaback)
                $(SEQUENCE_ITEMS_SELECTOR).each(initSequence(new WindowIO(), true));
            });
        });
        return;
    }

    // The firmware is supposed to start the sequence
    // with a channel-start event through Window.IO.
    // In preview mode we will need to cover the following cases with autostart
    // * Preview in editor
    // * Preview of the channel
    // * Preview of the display
    // * Preview of the device
    require(['util/ScreensDisplay', 'window.io'], function(ScreensDisplay, WindowIO) {
        var wio = ScreensDisplay.getWindowIO();

        // To start the preview in editor
        // we check if the channel is loaded in the editor
        var isInAuthoring = false;

        try {
            isInAuthoring = !!(window.parent !== window && window.parent.Granite && window.parent.Granite.author && window.parent.Granite.author.layerManager);
        }
        catch (securityException) {
            // If a security exception happens, the parent window is on a different domain / protocol, this is most likely not the authoring mode.
        }

        if (!wio) { // is not executed in firmware
            wio = new WindowIO();
        }

        if (Object.values(wio.targetWindows).indexOf(window.parent) === -1 || wio.targetWindows.single === window.parent) {
            // Post message to parent window only if current sequence is a subsequence.
            // Root sequence does need to post to parent firmware twice (already in wio)
            // Chrome case: window might already be there as single if WindowIO is mapped to top channel frame (second part of the test)
            wio.addWindow('parentSequence', window.parent);
        }

        $(document).ready(function() {

            // only autostart the sequence in the authoring case (no orchestrator that triggers the plaback)
            $(SEQUENCE_ITEMS_SELECTOR).each(initSequence(wio, isInAuthoring));

            // to be sure the channel has not missed any event
            wio.postMessage('channel-status-ready');
        });
    });

}(window.jQuery));