{#
/**
 * Copyright (C) 2020 Xibo Signage Ltd
 *
 * Xibo - Digital Signage - http://www.xibo.org.uk
 *
 * This file is part of Xibo.
 *
 * Xibo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * Xibo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Xibo.  If not, see <http://www.gnu.org/licenses/>.
 */
#}
<!DOCTYPE html>
<html>
    <head>
        <title>{% trans "Preview for Layout" %} {{ layout.layoutId }}</title>

        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="public-path" content="{{ theme.rootUri() }}"/>
        
        <link rel="shortcut icon" href="{{ theme.uri("img/favicon.ico") }}" />
    </head>
    <body>
        {# Import JS bundle from dist #}
        <script src="{{ theme.rootUri() }}dist/preview.bundle.min.js?v={{ version }}&rev={{revision}}" nonce="{{ cspNonce }}"></script>
        <script type="text/javascript" nonce="{{ cspNonce }}">
            var previewTranslations = {};
            // Translations we want always available
            {% autoescape "js" %}
                previewTranslations.actionControllerTitle = "{{ "Webhook Controller"|trans }}";
                previewTranslations.navigateToLayout = "{{ "Navigate to layout with code [layoutTag]?"|trans }}";
                previewTranslations.emptyRegionMessage = "{{ "Empty region!"|trans }}";
                previewTranslations.next = "{{ "Next Item"|trans }}";
                previewTranslations.previous = "{{ "Previous Item"|trans }}";
                previewTranslations.navWidget = "{{ "Navigate to Widget"|trans }}";
                previewTranslations.navLayout = "{{ "Navigate to Layout"|trans }}";
                previewTranslations.widgetId = "{{ "Widget ID"|trans }}";
                previewTranslations.layoutCode = "{{ "Layout Code"|trans }}";
                previewTranslations.target = "{{ "Target"|trans }}";
            {% endautoescape %}

            {% set layoutObj = [{layoutId: layout.layoutId }] %}
            {% set xlrOptions = previewOptions|merge({inPreview: true, previewJwt: previewJwt, platform: "CMS"}) %}
            (function($){
                $(document).ready(function(){
                    var xiboLayoutRenderer = new XiboLayoutRenderer({{ layoutObj|json_encode()|raw }}, [], {{ xlrOptions|json_encode()|raw }});
    
                    xiboLayoutRenderer.init().then(async function(xlr) {
                      await xlr.playSchedules(xlr);
                    });

                    window.xlr = xiboLayoutRenderer;

                  // Listen to post messages from the host document.
                  // BroadcastChannel cannot cross distinct opaque origins in sandboxed iframes,
                  // so we forward xiboDC messages to child widget iframes via postMessage instead.
                  window.onmessage = function(e) {
                    if (e.data.method === 'xiboDC') {
                      document.querySelectorAll('iframe').forEach(function(iframe) {
                        iframe.contentWindow.postMessage(e.data.options.data, '*');
                      });
                    }
                  };
                });
            }(window.jQuery));
        </script>
    </body>
</html>
