<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
?>
<script>
require([
    "jquery",
    "tinymce",
    "Magento_Ui/js/modal/modal",
    "prototype",
    "mage/adminhtml/events"
], function(jQuery, tinyMCE, modal){

//<![CDATA[

Window.keepMultiModalWindow = true;
var catalogWysiwygEditor = {
    overlayShowEffectOptions : null,
    overlayHideEffectOptions : null,
    modal: null,
    open : function(editorUrl, elementId) {
        if (editorUrl && elementId) {
           jQuery.ajax({
                url: editorUrl,
                data: {
                    element_id: elementId + '_editor',
                    store_id: '<?= $block->escapeJs($block->getStoreId()) ?>'
                },
                showLoader: true,
                dataType: 'html',
                success: function(data, textStatus, transport) {
                    this.openDialogWindow(data, elementId);
                }.bind(this)
            });
        }
    },
    openDialogWindow : function(data, elementId) {
        var self = this;

        if (this.modal) {
            this.modal.html(jQuery(data).html());
            this.modal.modal('option', 'firedElementId', elementId);
        } else {
            this.modal = jQuery(data).modal({
                title: '<?= $block->escapeJs($block->escapeHtml(__('WYSIWYG Editor'))) ?>',
                modalClass: 'magento',
                type: 'slide',
                firedElementId: elementId,
                buttons: [{
                    text: jQuery.mage.__('Cancel'),
                    click: function () {
                        self.closeDialogWindow(this);
                    }
                },{
                    text: jQuery.mage.__('Submit'),
                    click: function () {
                        self.okDialogWindow(this);
                    }
                }],
                close: function () {
                    self.closeDialogWindow(this);
                }
            });
        }
        this.modal.modal('openModal');
        $(elementId + '_editor').value = $(elementId).value;
    },
    okDialogWindow : function(dialogWindow) {
        var $firedElement = jQuery('#' + dialogWindow.options.firedElementId),
            $editorElement = jQuery('#' + dialogWindow.options.firedElementId + '_editor'),
            firedElementId = $firedElement.attr('id'),
            wysiwygObj = window['wysiwyg' + firedElementId + '_editor'];

        if ($firedElement.length) {
            wysiwygObj.turnOff();
            if (tinyMCE.get(wysiwygObj.id)) {
                $firedElement.val(tinyMCE.get(wysiwygObj.id).getContent()).change();
            } else {
                $firedElement.val($editorElement.val()).change();
            }
            if (tinyMCE.get(firedElementId)) {
                tinyMCE.get(firedElementId).load();
            }
            if (typeof varienGlobalEvents != undefined) {
                varienGlobalEvents.fireEvent('tinymceChange');
            }
        }
        this.closeDialogWindow(dialogWindow);
    },
    closeDialogWindow : function(dialogWindow) {
        // remove form validation event after closing editor to prevent errors during save main form
        if (typeof varienGlobalEvents != undefined && editorFormValidationHandler) {
            varienGlobalEvents.removeEventHandler('formSubmit', editorFormValidationHandler);
        }

        //IE fix - blocked form fields after closing
        try {
            $(dialogWindow.options.firedElementId).focus();
        } catch (e) {
            //ie8 cannot focus hidden elements
        }

        //destroy the instance of editor
        wysiwygObj = eval('wysiwyg'+dialogWindow.options.firedElementId+'_editor');
        if (tinyMCE.get(wysiwygObj.id)) {
           tinyMCE.execCommand('mceRemoveControl', true, wysiwygObj.id);
        }

        dialogWindow.closeModal();
        Windows.overlayShowEffectOptions = this.overlayShowEffectOptions;
        Windows.overlayHideEffectOptions = this.overlayHideEffectOptions;
    }
};

window.catalogWysiwygEditor = catalogWysiwygEditor;
//]]>

});
</script>
