Colour propery box on input, flash green on update, only modal notify if value is not set exactly

This commit is contained in:
Julian Stirling 2025-08-28 14:44:34 +01:00
parent 80ee303ac9
commit 212b8fe062
3 changed files with 90 additions and 9 deletions

View file

@ -6,6 +6,7 @@
<input <input
v-model="internalValue" v-model="internalValue"
class="uk-form-small numeric-setting-line-input" class="uk-form-small numeric-setting-line-input"
:class="{ edited: isEdited, flash: animateUpdate }"
type="number" type="number"
@focusin="focusIn" @focusin="focusIn"
@focusout="focusOut" @focusout="focusOut"
@ -35,7 +36,9 @@
:key="i" :key="i"
v-model="internalValue[i - 1]" v-model="internalValue[i - 1]"
class="uk-form-small numeric-setting-line-input" class="uk-form-small numeric-setting-line-input"
:class="{ edited: isEdited, flash: animateUpdate }"
type="number" type="number"
@input="updateIsEdited"
@focusin="focusIn" @focusin="focusIn"
@focusout="focusOut" @focusout="focusOut"
@keydown="keyDown" @keydown="keyDown"
@ -51,7 +54,9 @@
<input <input
v-model="internalValue[key]" v-model="internalValue[key]"
class="uk-form-small numeric-setting-line-input" class="uk-form-small numeric-setting-line-input"
:class="{ edited: isEdited, flash: animateUpdate }"
type="number" type="number"
@input="updateIsEdited"
@focusin="focusIn" @focusin="focusIn"
@focusout="focusOut" @focusout="focusOut"
@keydown="keyDown" @keydown="keyDown"
@ -95,19 +100,50 @@ export default {
type: String, type: String,
default: "" default: ""
}, },
animate: {
type: Boolean,
default: false
}
}, },
data() { data() {
return { return {
internalValue: this.value, // Initialise with a copy. As this is faster than stringifying and parsing.
valueOnEnter: undefined, internalValue: Array.isArray(this.value)
focused: false ? [...this.value]
: typeof this.value === 'object'
? { ...this.value }
: this.value,
// Is edited can't be computed as we mutate internalValue
isEdited: false,
animateUpdate: false,
}; };
}, },
mounted() {
if (this.value !== undefined) {
this.resetInternalValue();
}
},
watch: { watch: {
value(newValue) { value() {
this.internalValue = newValue; // Fire updateIsEdited on both value and internal value change,
// as change in value may not causse internalValue to change.
this.updateIsEdited();
this.resetInternalValue();
},
internalValue() {
this.updateIsEdited();
},
animate(updated) {
if (updated) {
this.animateUpdate = true;
setTimeout(() => {
this.animateUpdate = false;
this.$emit('animationShown');
}, 700);
}
} }
}, },
computed: { computed: {
@ -170,6 +206,10 @@ export default {
}, },
methods: { methods: {
resetInternalValue: function() {
// stringify and parse to ensure no internal mutation for objects
this.internalValue = JSON.parse(JSON.stringify(this.value));
},
requestUpdate: async function() { requestUpdate: async function() {
this.$emit("requestUpdate") this.$emit("requestUpdate")
}, },
@ -195,7 +235,23 @@ export default {
if (event.keyCode == 13) { if (event.keyCode == 13) {
this.sendValue(); this.sendValue();
} }
},
updateIsEdited: function() {
this.isEdited = this.deepStringify(this.internalValue) !== this.deepStringify(this.value);
},
deepStringify: function(val) {
if (Array.isArray(val)) {
return JSON.stringify(val.map(String));
}
if (val && typeof val === 'object') {
const normalized = Object.fromEntries(
Object.entries(val).map(([k, v]) => [k, String(v)])
);
return JSON.stringify(normalized);
}
return JSON.stringify(String(val));
} }
} }
}; };
</script> </script>
@ -215,4 +271,14 @@ export default {
margin-right: 5px; margin-right: 5px;
width: 6em; width: 6em;
} }
.edited {
background-color: #fff3cd;
}
@keyframes green-flash {
0% { background-color: #3fda63; }
100% { background-color: white; }
}
.flash {
animation: green-flash 0.7s ease;
}
</style> </style>

View file

@ -3,8 +3,10 @@
v-model="value" v-model="value"
:data-schema="propertyDescription" :data-schema="propertyDescription"
:label="label" :label="label"
:animate="animate"
@requestUpdate="readProperty" @requestUpdate="readProperty"
@sendValue="writeProperty" @sendValue="writeProperty"
@animationShown="resetAnimate"
/> />
</template> </template>
@ -45,7 +47,8 @@ export default {
data() { data() {
return { return {
value: undefined value: undefined,
animate: false
}; };
}, },
@ -98,14 +101,15 @@ export default {
await new Promise(r => setTimeout(r, this.readBackDelay)); await new Promise(r => setTimeout(r, this.readBackDelay));
let newVal = await this.readProperty(); let newVal = await this.readProperty();
if (newVal == requestedValue) { if (newVal == requestedValue) {
await this.modalNotify(`Set ${this.label} to ${newVal}.`); this.animate = true;
} else { } else {
this.animate = true;
await this.modalNotify( await this.modalNotify(
`Set ${this.label} to ${newVal} (requested ${requestedValue}).` `Set ${this.label} to ${newVal} (closest valid value to requested ${requestedValue}).`
); );
} }
} else { } else {
await this.modalNotify(`Set ${this.label} to ${this.value}.`); this.animate = true;
} }
} catch (error) { } catch (error) {
// Use mixin to display error // Use mixin to display error
@ -113,6 +117,9 @@ export default {
// Re-read property to try to update to server value // Re-read property to try to update to server value
this.readProperty(); this.readProperty();
} }
},
resetAnimate: function() {
this.animate = false;
} }
} }
}; };

View file

@ -10,8 +10,10 @@
v-model="backgroundDetectorStatus.settings" v-model="backgroundDetectorStatus.settings"
:data-schema="backgroundDetectorStatus.settings_schema" :data-schema="backgroundDetectorStatus.settings_schema"
label="" label=""
:animate="animate"
@requestUpdate="readSettings" @requestUpdate="readSettings"
@sendValue="writeSettings" @sendValue="writeSettings"
@animationShown="resetAnimate"
/> />
</div> </div>
</li> </li>
@ -57,6 +59,7 @@ export default {
data() { data() {
return { return {
backgroundDetectorStatus: undefined, backgroundDetectorStatus: undefined,
animate: false
}; };
}, },
@ -87,6 +90,11 @@ export default {
"update_detector_settings", "update_detector_settings",
{"data": requestedValue} {"data": requestedValue}
); );
this.animate = true;
this.readSettings();
},
resetAnimate: function() {
this.animate = false;
} }
}, },
async created() { async created() {