{#
/**
 * 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/>.
 */
#}
<script type="text/javascript">

    // Runs after form opens
    function regionFormEditOpen() {
        
        // Transition type affects the transition-group visibility
        formHelpers.setupObjectValueInputFields($(this).find('form'), '#transitionType', ['.transition-group'], null, true);

        // Handle set region as fullscreen button
        // Get layout object
        var app = lD;
        var layout = lD.layout;
        var form = $(this).find('form');
        var buttonText = form.find('#setFullScreenButton').html();

        // Replace button text tags
        if(buttonText) {
            form.find('#setFullScreenButton').html(buttonText.replace('[width]', layout.width).replace('[height]', layout.height));
        }

        // Handle click action
        form.find('#setFullScreenButton').click(function() {
            // Set position values to 0
            form.find('[name="top"]').val(0);
            form.find('[name="left"]').val(0);

            // Set dimensions values to be the same as the layout's
            form.find('[name="width"]').val(layout.width);
            form.find('[name="height"]').val(layout.height);

            formChangesRegion({
                'width': layout.width,
                'height': layout.height,
                'top': 0,
                'left': 0 
            });
        });
        
        form.find('[name="top"], [name="left"], [name="width"], [name="height"], [name="zIndex"]').on('change', _.debounce(function(){
            formChangesRegion();
        }, 200));

        regionChangesForm();

        app.viewer.updateMoveable();
    }

    function formChangesRegion() {
        var app = lD;
        var layout = app.layout;
        var regionId = app.selectedObject.id;
        var form = $(app.propertiesPanel.DOMObject).find('form');

        layout.regions[regionId].transform({
            'width': form.find('[name="width"]').val(),
            'height': form.find('[name="height"]').val(),
            'top': form.find('[name="top"]').val(),
            'left': form.find('[name="left"]').val(),
            'zIndex': form.find('[name="zIndex"]').val(),
        }, true);

        app.viewer.updateRegion(layout.regions[regionId]);

        app.viewer.updateMoveable();
    }

    function regionChangesForm() {
        var app = lD;
        var form = $(app.propertiesPanel.DOMObject).find('form')
        
        // If form not loaded, prevent changes
        if(form.length == 0 || app.layout.regions[app.selectedObject.id] == undefined) {
            return;
        }
        
        var regionDimensions = app.layout.regions[app.selectedObject.id].dimensions;

        form.find('[name="top"]').val(parseInt(regionDimensions.top));
        form.find('[name="left"]').val(parseInt(regionDimensions.left));

        // Set dimensions values to be the same as the layout's
        form.find('[name="width"]').val(parseInt(regionDimensions.width));
        form.find('[name="height"]').val(parseInt(regionDimensions.height));
    }
</script>