<script>
import Moveable from "vue-moveable";

export default {
    components: { Moveable },
    data() {
        const resizable = "$preview_resizable";
        const keepRatio = "$preview_keepRatio";
        const throttleResize = "$preview_throttleResize";
        const renderDirections = "$preview_renderDirections";
        const minWidth = "$preview_minWidth";
        const minHeight = "$preview_minHeight";
        const maxWidth = "$preview_maxWidth";
        const maxHeight = "$preview_maxHeight";
        const widthInputRef = null;
        const heightInputRef = null;
        const moveableRef = null;
        const onInput = (e) => {
            const ev = (e.nativeEvent || e);
            const horizontal = JSON.parse(ev.target.getAttribute("data-horizontal"));
            if (typeof ev.data === "undefined") {
                moveableRef.request("resizable", {
                    offsetWidth: parseFloat(widthInputRef.value),
                    offsetHeight: parseFloat(heightInputRef.value),
                    horizontal
                }, true);
            }
        };
        const onKeyUp = (e) => {
            const ev = (e.nativeEvent || e);
            const horizontal = JSON.parse(ev.target.getAttribute("data-horizontal"));
            e.stopPropagation();
            if (e.keyCode === 13) {
                moveableRef.request("resizable", {
                    offsetWidth: parseFloat(widthInputRef.value),
                    offsetHeight: parseFloat(heightInputRef.value),
                    horizontal
                }, true);
            }
        };
        return {
            resizable,
            keepRatio,
            throttleResize,
            renderDirections,
            minWidth,
            minHeight,
            maxWidth,
            maxHeight,
            widthInputRef,
            heightInputRef,
            moveableRef,
            onInput,
            onKeyUp
        };
    },
    methods: {
        onResizeGroupStart({ setMin, setMax }) {
            setMin([this.minWidth, this.minHeight]);
            setMax([this.maxWidth, this.maxHeight]);
        },
        onResizeGroup({ events }) {
            events.forEach(ev => {
                ev.target.style.width = `${ev.boundingWidth}px`;
                ev.target.style.height = `${ev.boundingHeight}px`;
                ev.target.style.transform = `translate(${ev.drag.beforeTranslate[0]}px, ${ev.drag.beforeTranslate[1]}px)`;
            });
        },
        onResizeGroupEnd(e) {
            requestAnimationFrame(() => {
                const rect = e.moveable.getRect();
                this.$refs.widthInputRef.value = `${rect.offsetWidth}`;
                this.$refs.heightInputRef.value = `${rect.offsetHeight}`;
            });
        },
        onDragGroup({ events }) {
            events.forEach(ev => {
                ev.target.style.transform = ev.transform;
            });
        }
    },
    mounted() {
        this.widthInputRef = this.$refs.widthInputRef;
        this.heightInputRef = this.$refs.heightInputRef;
        this.moveableRef = this.$refs.moveableRef;
    }
};
</script>
<template>
    <div class="root">
        <div>
            width:
            <input
                ref="widthInputRef"
                type="number"
                defaultValue="280"
                @input="onInput"
                @keyUp="onKeyUp"
                data-horizontal="true"/>
            &nbsp;
                        height:
            <input
                ref="heightInputRef"
                type="number"
                defaultValue="230"
                @input="onInput"
                @keyUp="onKeyUp"
                data-horizontal="false"/>
        </div>
        <div class="container">
            <div class="target target1">Target1</div>
            <div class="target target2">Target2</div>
            <div class="target target3">Target3</div>
            <Moveable
                ref="moveableRef"
                :target="'.target'"
                :draggable="true"
                :resizable="resizable"
                :keepRatio="keepRatio"
                :throttleResize="throttleResize"
                :renderDirections="renderDirections"
                @resizeGroupStart="onResizeGroupStart"
                @resizeGroup="onResizeGroup"
                @resizeGroupEnd="onResizeGroupEnd"
                @dragGroup="onDragGroup"/>
        </div>
    </div>
</template>