This commit is contained in:
Richard Bowman 2023-11-02 20:30:54 +00:00
parent 79752a2a4c
commit c5c02eb029
9 changed files with 235 additions and 227 deletions

View file

@ -3,9 +3,9 @@
<label class="uk-form-label">{{ label }}</label>
<div class="input-and-buttons-container">
<input
v-model="value"
class="uk-form-small numeric-setting-line-input"
type="number"
v-model="value"
@focusin="focusIn"
@focusout="focusOut"
@keydown="keyDown"
@ -16,87 +16,89 @@
</div>
</div>
</template>
<script>
import axios from "axios";
export default {
name: "NumericSettingLine",
name: "NumericSettingLine",
props: {
props: {
label: {
type: String,
required: true
type: String,
required: true
},
propertyUrl: {
type: String,
required: true
type: String,
required: true
},
readBackDelay: {
type: Number,
default: undefined,
required: false
type: Number,
default: undefined,
required: false
}
},
},
data: () => {
data: () => {
return {
value: undefined,
valueOnEnter: undefined
}
},
value: undefined,
valueOnEnter: undefined
};
},
computed: {
computed: {
readBack: function() {
return this.readBackDelay !== undefined;
return this.readBackDelay !== undefined;
}
},
},
mounted() {
mounted() {
this.readProperty();
},
},
methods: {
methods: {
readProperty: async function() {
let response = await axios.get(this.propertyUrl)
this.value = response.data
console.log("Read property", this.propertyUrl, response.data)
return response.data
let response = await axios.get(this.propertyUrl);
this.value = response.data;
console.log("Read property", this.propertyUrl, response.data);
return response.data;
},
writeProperty: async function() {
try {
let requestedValue = Number(this.value)
await axios.post(this.propertyUrl, requestedValue)
if(this.readBack) {
await new Promise(r => setTimeout(r, this.readBackDelay))
let newVal = await this.readProperty()
if(newVal == requestedValue) {
await this.modalNotify(`Set ${this.label} to ${newVal}.`)
} else {
await this.modalNotify(`Set ${this.label} to ${newVal} (requested ${requestedValue}).`)
}
} else {
await this.modalNotify(`Set ${this.label} to ${this.value}.`);
}
} catch(error) {
this.modalError(error); // Let mixin handle error
try {
let requestedValue = Number(this.value);
await axios.post(this.propertyUrl, requestedValue);
if (this.readBack) {
await new Promise(r => setTimeout(r, this.readBackDelay));
let newVal = await this.readProperty();
if (newVal == requestedValue) {
await this.modalNotify(`Set ${this.label} to ${newVal}.`);
} else {
await this.modalNotify(
`Set ${this.label} to ${newVal} (requested ${requestedValue}).`
);
}
} else {
await this.modalNotify(`Set ${this.label} to ${this.value}.`);
}
} catch (error) {
this.modalError(error); // Let mixin handle error
}
},
focusIn: function(event) {
this.valueOnEnter = event.target.value;
this.valueOnEnter = event.target.value;
},
focusOut: function(event) {
if (this.valueOnEnter != event.target.value) {
this.writeProperty(event.target.value);
}
if (this.valueOnEnter != event.target.value) {
this.writeProperty(event.target.value);
}
},
keyDown: function(event) {
// Pressing enter should set the property, whether or not we think it's changed.
if (event.keyCode == 13) {
this.writeProperty();
}
// Pressing enter should set the property, whether or not we think it's changed.
if (event.keyCode == 13) {
this.writeProperty();
}
}
}
}
};
</script>
@ -110,10 +112,10 @@ methods: {
width: 100%;
}
.numeric-setting-line-input {
flex-grow: 1;
margin-left: 5px;
margin-right: 5px;
width: 6em;
flex-grow: 1;
margin-left: 5px;
margin-right: 5px;
width: 6em;
}
.button-next-to-input {
flex-grow: 0;