{#
/*
 * Spring Signage Ltd - http://www.springsignage.com
 * Copyright (C) 2017 Spring Signage Ltd
 * (notification-designer-javascript.twig)
 */
#}
<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
        const layout = getXiboApp().layout;
        const form = $(this).find('form');
        let buttonText = form.find('#setFullScreenButton').html();

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

        // Handle click action
        form.find('#setFullScreenButton').click(() => {
            // Set position values to 0
            form.find('#top').val(0);
            form.find('#left').val(0);

            // Set dimensions values to be the same as the layout's
            form.find('#width').val(layout.width);
            form.find('#height').val(layout.height);

            formChangesRegion({
                'width': layout.width,
                'height': layout.height,
                'top': 0,
                'left': 0 
            });
        });
        
        form.find('#top, #left, #width, #height').on('change', function(){
            formChangesRegion();
        });

        form.find('#zIndex').on('change', function(){
            formChangesRegionZIndex();
        });

        regionChangesForm();
    }

    function formChangesRegion() {
        const app = getXiboApp();
        const layout = app.layout;
        const regionId = app.selectedObject.id;
        const form = $(app.navigatorEdit.regionPropertiesPanel.DOMObject).find('form');

        layout.regions[regionId].transform({
            'width': form.find('#width').val(),
            'height': form.find('#height').val(),
            'top': form.find('#top').val(),
            'left': form.find('#left').val()
        }, false);

        app.renderContainer(app.navigatorEdit);
    }

    function formChangesRegionZIndex() {
        const app = getXiboApp();
        const form = $(app.navigatorEdit.regionPropertiesPanel.DOMObject).find('form');
        const newZIndex = parseInt(form.find('#zIndex').val());

        app.navigatorEdit.DOMObject.find('#' + app.selectedObject.id).css('zIndex', newZIndex);
        app.selectedObject.zIndex = newZIndex;
    }

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

        form.find('#top').val(regionDimensions.top);
        form.find('#left').val(regionDimensions.left);

        // Set dimensions values to be the same as the layout's
        form.find('#width').val(regionDimensions.width);
        form.find('#height').val(regionDimensions.height);
    }
</script>