File size: 4,798 Bytes
71174bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<template>
    <span>
        <div :class="'container-fluid ' + cls" :style="styl">
            <div class="row">
                <div :class="'col px-0' + (axisIdx === 1 ? ' px-1' : '')" v-for="axisIdx in axesIdxs" :key="axisIdx">
                    <input :ref="`inputElem${axes[axisIdx]}`" :data-idx="axisIdx" type="number"
                        class="form-control form-control-sm" :readonly="readonly" :id="axes[axisIdx] + '-' + id"
                        :placeholder="axes[axisIdx].toUpperCase() + ' value...'" :disabled="disabled"
                        @input="handleInput" @keydown="onKeyDown" :value="modelValue[axisIdx]"
                        :delayBetweenChangesDetected="delayBetweenChangesDetected
                            " />
                </div>
            </div>
        </div>
        <FormElementDescription :description="description" :warning="warning"></FormElementDescription>
    </span>
</template>

<script lang="ts">
/* eslint-disable @typescript-eslint/ban-types */

import { randomID } from "@/Core/Utils/MiscUtils";
import { Options, Vue } from "vue-class-component";
import { Prop } from "vue-property-decorator";
import FormElementDescription from "@/UI/Forms/FormElementDescription.vue";
import { formInputDelayUpdate } from "@/Core/GlobalVars";

/**
 * FormVector3D component
 */
@Options({
    components: {
        FormElementDescription,
    },
})
export default class FormVector3D extends Vue {
    @Prop({ required: true }) modelValue!: [number, number, number];
    @Prop({ default: randomID() }) id!: string;
    @Prop({ default: "placeholder" }) placeHolder!: string;
    @Prop({ default: false }) disabled!: boolean;
    @Prop({}) description!: string;
    @Prop({ default: false }) readonly!: boolean;
    @Prop({ required: false }) filterFunc!: Function;
    @Prop({ required: false }) warningFunc!: (val: any) => string;
    @Prop({ default: "" }) cls!: string;
    @Prop({ default: "" }) styl!: string;
    @Prop({ default: formInputDelayUpdate })
    delayBetweenChangesDetected!: number;

    axesIdxs = [0, 1, 2];
    axes = ["x", "y", "z"];

    /**
     * Runs when user presses a key.
     *
     * @param {KeyboardEvent} _e  The key event. Not used.
     */
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    onKeyDown(_e: KeyboardEvent) {
        this.$emit("onKeyDown");
    }

    /**
     * The warning message.
     *
     * @returns {string} The warning message.
     */
    get warning(): string {
        if (this.warningFunc) {
            return this.warningFunc(this.modelValue);
        }
        return "";
    }

    lastHandleInputTimeStamp = 0;
    handleInputTimeout: any = null;

    /**
     * Let the parent component know of any changes, after user has not interacted
     * for a bit (to prevent rapid updates).
     *
     * @param {any} e  The value.
     */
    handleInput(e: any): void {
        // It's important not to handle the input too rapidly. Good to give the
        // user time to fix any temporarily wrong values.

        if (e.data === ".") {
            // Not done typing in the whole number yet. Don't update.
            return;
        }

        const now = Date.now();

        if (
            now - this.lastHandleInputTimeStamp <
            this.delayBetweenChangesDetected
        ) {
            // Too soon. Timeout below will handle.
            return;
        }

        // Clear previous timeout
        clearTimeout(this.handleInputTimeout);

        this.lastHandleInputTimeStamp = now;
        this.handleInputTimeout = setTimeout(() => {
            if (["", "-"].indexOf(e.target.value) !== -1) {
                // User has likely not yet finished typing a number.
                return;
            }

            // Get "idx" data from target
            const idx = parseInt(e.target.dataset.idx);
            const newVals = JSON.parse(JSON.stringify(this.modelValue));

            let newVal = parseFloat(e.target.value);

            if (isNaN(newVal)) {
                // If not a number, set to 0.
                newVal = 0;
            }

            if (this.filterFunc) {
                // If there's a filter funciton, update everything.
                newVal = this.filterFunc(newVal);
            }

            newVals[idx] = newVal;

            this.$emit("update:modelValue", newVals);

            // In some circumstances (e.g., changing values in an object), not reactive.
            // Emit also "onChange" to signal the value has changed.
            this.$emit("onChange");
        }, this.delayBetweenChangesDetected);
    }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
// Input of type color
// .form-control-color {
//     width: 100%;
// }</style>