Line data Source code
1 : // @ts-check 2 : 3 : import Vue from "vue"; 4 : import { every } from "lodash"; 5 : import PropertyMixin from "./PropertyMixin"; 6 : import BadgeButton from "../BadgeButton.vue"; 7 : import { BaseInput } from "@cern/base-vue"; 8 : 9 : /** 10 : * @typedef {{ value: V.Instance<BaseInput> }} Refs 11 : */ 12 : 13 : export default /** @type {V.Constructor<any, Refs>} */(Vue).extend({ 14 : name: "PropertyAuto", 15 : components: { BaseInput, BadgeButton }, 16 : mixins: [ PropertyMixin ], 17 : watch: { 18 : value() { 19 5 : if (!this.$refs.value.hasFocus) { 20 4 : this.$refs.value.editValue = JSON.stringify(this.value); 21 : } 22 : } 23 : }, 24 : mounted() { 25 3 : this.$refs.value.editValue = this.value; 26 : }, 27 : methods: { 28 : onEdit() { 29 8 : if (!this.$refs.value.hasFocus) { return; } 30 : /** @type {string|number|null} */ 31 1 : let value = this.$refs?.value?.editValue; 32 1 : try { 33 : /* 34 : special case, empty string and several zeroes are considered as zero 35 : this just makes the input a bit more convenient 36 : */ 37 1 : if (every(value, (v) => (v === "0"))) { 38 0 : value = 0; 39 : } 40 : else { 41 1 : value = JSON.parse(/** @type {string} */(value)); 42 : } 43 : } 44 : catch { /* noop */ } 45 1 : this.$store.commit("engine/updateState", { [this.name]: value }); 46 : } 47 : } 48 : });