<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/** @var \Magento\Backend\Block\Page $block */
?>
<div id="preview" class="cms-revision-preview">
    <div class="toolbar">
        <?php if (!$block->isSingleStoreMode()) :?>
        <div class="store-switcher">
            <?= $block->getChildHtml('store_switcher') ?>
        </div>
        <?php endif;?>
    </div>
    <iframe
            name="preview_iframe"
            id="preview_iframe"
            frameborder="0"
            title="<?= $block->escapeHtmlAttr(__('Preview')) ?>"
            width="100%"
            sandbox="allow-forms allow-pointer-lock allow-scripts"
    >

    </iframe>
    <?= $block->getChildHtml('preview_form') ?>
</div>

<script>
require(['jquery', 'loadingPopup', 'prototype'], function(jQuery){

//<![CDATA[
var previewForm = $('preview_form');
var previewIframe = $('preview_iframe');

function preview() {
    previewForm.writeAttribute('target', previewIframe.readAttribute('id'));
    blockPreview();
    previewForm.submit();
}

function blockPreview() {
    jQuery('body').loadingPopup({
        timeout: false
    });
}

function unBlockPreview() {
    jQuery('body').trigger('hideLoadingPopup');
}

jQuery(document).ready(preview);
jQuery(previewIframe).ready(unBlockPreview);

jQuery("#preview_iframe").load(function() {
    jQuery(this).height(jQuery(this).contents().find("html").height() );
});

//]]>

});
</script>
