From 974e28e9babf865d92297980db7e4ff144ffa566 Mon Sep 17 00:00:00 2001 From: Florian Hotze Date: Tue, 26 Mar 2024 12:12:23 +0100 Subject: [PATCH] Script edit: Fix dirty handling (#2508) Signed-off-by: Florian Hotze --- .../settings/rules/script/script-edit.vue | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/bundles/org.openhab.ui/web/src/pages/settings/rules/script/script-edit.vue b/bundles/org.openhab.ui/web/src/pages/settings/rules/script/script-edit.vue index 0d332b9913..0a855c8046 100644 --- a/bundles/org.openhab.ui/web/src/pages/settings/rules/script/script-edit.vue +++ b/bundles/org.openhab.ui/web/src/pages/settings/rules/script/script-edit.vue @@ -269,12 +269,29 @@ export default { } }, methods: { + /** + * Calculates the value of `this.dirty` from the individual dirty states. + */ calculateDirty () { this.dirty = this.scriptDirty || this.modeDirty || this.ruleDirty || this.currentModuleDirty }, + /** + * Calls {@link initDirty} and resets `this.dirty` and all individual dirty states to `false`. + */ resetDirty () { - this.scriptDirty = this.modeDirty = this.ruleDirty = this.currentModuleDirty = false - this.calculateDirty() + this.initDirty() + this.dirty = this.scriptDirty = this.modeDirty = this.ruleDirty = this.currentModuleDirty = false + }, + /** + * Stores the current state of tracked objects as the saved state of those, e.g. `this.rule` is cloned to `this.savedRule`. + */ + initDirty () { + this.savedRule = cloneDeep(this.rule) + if (this.currentModule) { + this.savedCurrentModule = cloneDeep(this.currentModule) + this.savedMode = this.mode = this.currentModule.configuration.type + this.savedScript = this.script = this.currentModule.configuration.script || '' + } }, onPageAfterIn () { if (this.ready) return @@ -379,7 +396,6 @@ export default { Promise.all([this.$oh.api.get('/rest/module-types?type=trigger'), this.$oh.api.get('/rest/rules/' + this.ruleId)]).then((data) => { this.$set(this.moduleTypes, 'triggers', data[0]) this.$set(this, 'rule', data[1]) - this.savedRule = cloneDeep(this.rule) if (this.moduleId) { this.$set(this, 'currentModule', this.rule.actions.concat(this.rule.conditions).find((m) => m.id === this.moduleId)) @@ -388,11 +404,7 @@ export default { this.isScriptRule = true } - if (this.currentModule) { - this.savedCurrentModule = cloneDeep(this.currentModule) - this.savedMode = this.mode = this.currentModule.configuration.type - this.savedScript = this.script = this.currentModule.configuration.script || '' - } + this.initDirty() if (!this.rule.editable) { const commentChar = (this.mode === 'application/x-ruby' ? '#' : '//')