Skip to content

Commit

Permalink
Rule & Scene edit: Use mixin for shared code
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Hotze <[email protected]>
  • Loading branch information
florian-h05 committed Sep 22, 2023
1 parent 98d4262 commit ed88ed6
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 204 deletions.
110 changes: 110 additions & 0 deletions bundles/org.openhab.ui/web/src/pages/settings/rules/rule-edit-mixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import YAML from 'yaml'

export default {
data () {
return {
showModuleControls: false,
eventSource: null,
keyHandler: null
}
},
computed: {
isEditable () {
return this.rule && this.rule.editable !== false
},
yamlError () {
if (this.currentTab !== 'code') return null
try {
YAML.parse(this.ruleYaml, { prettyErrors: true })
return 'OK'
} catch (e) {
return e
}
}
},
methods: {
onPageAfterIn () {
if (window) {
window.addEventListener('keydown', this.keyDown)
}
this.load()
},
onPageAfterOut () {
this.stopEventSource()
if (window) {
window.removeEventListener('keydown', this.keyDown)
}
},
onEditorInput (value) {
this.ruleYaml = value
this.dirty = true
},
toggleDisabled () {
if (this.createMode) return
const enable = (this.rule.status.statusDetail === 'DISABLED')
this.$oh.api.postPlain('/rest/rules/' + this.rule.uid + '/enable', enable.toString()).then((data) => {
this.$f7.toast.create({
text: (enable) ? 'Enabled' : 'Disabled',
destroyOnClose: true,
closeTimeout: 2000
}).open()
}).catch((err) => {
this.$f7.toast.create({
text: 'Error while disabling or enabling: ' + err,
destroyOnClose: true,
closeTimeout: 2000
}).open()
})
},
keyDown (ev) {
if ((ev.ctrlKey || ev.metaKey) && !(ev.altKey || ev.shiftKey)) {
if (this.currentModule) return
switch (ev.keyCode) {
case 68:
this.toggleDisabled()
ev.stopPropagation()
ev.preventDefault()
break
case 82:
this.runNow()
ev.stopPropagation()
ev.preventDefault()
break
case 83:
this.save()
ev.stopPropagation()
ev.preventDefault()
break
}
}
},
toggleModuleControls () {
this.showModuleControls = !this.showModuleControls
},
showSwipeout (ev) {
let swipeoutElement = ev.target
ev.cancelBubble = true
while (!swipeoutElement.classList.contains('swipeout')) {
swipeoutElement = swipeoutElement.parentElement
}

if (swipeoutElement) {
this.$f7.swipeout.open(swipeoutElement)
}
},
startEventSource () {
this.eventSource = this.$oh.sse.connect('/rest/events?topics=openhab/rules/' + this.ruleId + '/*', null, (event) => {
const topicParts = event.topic.split('/')
switch (topicParts[3]) {
case 'state':
this.$set(this.rule, 'status', JSON.parse(event.payload)) // e.g. {"status":"RUNNING","statusDetail":"NONE"}
break
}
})
},
stopEventSource () {
this.$oh.sse.close(this.eventSource)
this.eventSource = null
}
}
}
106 changes: 3 additions & 103 deletions bundles/org.openhab.ui/web/src/pages/settings/rules/rule-edit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,9 @@ import YAML from 'yaml'
import cloneDeep from 'lodash/cloneDeep'
import fastDeepEqual from 'fast-deep-equal/es6'
import SemanticsPicker from '@/components/tags/semantics-picker.vue'
import TagInput from '@/components/tags/tag-input.vue'
import RuleModulePopup from './rule-module-popup.vue'
import RuleMixin from './rule-edit-mixin'
import ModuleDescriptionSuggestions from './module-description-suggestions'
import RuleStatus from '@/components/rule/rule-status-mixin'
import DirtyMixin from '../dirty-mixin'
Expand All @@ -190,7 +188,7 @@ import ConfigSheet from '@/components/config/config-sheet.vue'
import RuleGeneralSettings from '@/components/rule/rule-general-settings.vue'
export default {
mixins: [ModuleDescriptionSuggestions, RuleStatus, DirtyMixin],
mixins: [RuleMixin, ModuleDescriptionSuggestions, RuleStatus, DirtyMixin],
components: {
RuleGeneralSettings,
ConfigSheet,
Expand All @@ -208,7 +206,6 @@ export default {
conditions: [],
triggers: []
},
showModuleControls: false,
currentSection: 'actions',
currentTab: 'design',
currentModuleType: null,
Expand All @@ -219,8 +216,6 @@ export default {
actions: ['Then', 'Add Action'],
conditions: ['But only if', 'Add Condition']
},
eventSource: null,
keyHandler: null,
codeEditorOpened: false,
cronPopupOpened: false,
Expand All @@ -247,22 +242,6 @@ export default {
}
},
methods: {
onPageAfterIn () {
if (window) {
window.addEventListener('keydown', this.keyDown)
}
this.load()
},
onPageAfterOut () {
this.stopEventSource()
if (window) {
window.removeEventListener('keydown', this.keyDown)
}
},
onEditorInput (value) {
this.ruleYaml = value
this.dirty = true
},
load () {
if (this.loading) return
this.loading = true
Expand Down Expand Up @@ -359,28 +338,11 @@ export default {
}).open()
})
},
toggleDisabled () {
if (this.createMode) return
const enable = (this.rule.status.statusDetail === 'DISABLED')
this.$oh.api.postPlain('/rest/rules/' + this.rule.uid + '/enable', enable.toString()).then((data) => {
this.$f7.toast.create({
text: (enable) ? 'Rule enabled' : 'Rule disabled',
destroyOnClose: true,
closeTimeout: 2000
}).open()
}).catch((err) => {
this.$f7.toast.create({
text: 'Error while disabling or enabling: ' + err,
destroyOnClose: true,
closeTimeout: 2000
}).open()
})
},
runNow () {
if (this.createMode) return
if (this.rule.status.status === 'RUNNING' || this.rule.status.status === 'UNINITIALIZED') {
return this.$f7.toast.create({
text: `Rule cannot be run ${(this.rule.status.status === 'RUNNING') ? 'while already running, please wait' : 'if it is uninitialized'}!`,
text: `Rule cannot be ^${(this.rule.status.status === 'RUNNING') ? 'while already running, please wait' : 'if it is uninitialized'}!`,
destroyOnClose: true,
closeTimeout: 2000
}).open()
Expand Down Expand Up @@ -414,20 +376,6 @@ export default {
}
)
},
startEventSource () {
this.eventSource = this.$oh.sse.connect('/rest/events?topics=openhab/rules/' + this.ruleId + '/*', null, (event) => {
const topicParts = event.topic.split('/')
switch (topicParts[3]) {
case 'state':
this.$set(this.rule, 'status', JSON.parse(event.payload))
break
}
})
},
stopEventSource () {
this.$oh.sse.close(this.eventSource)
this.eventSource = null
},
selectTemplate (uid) {
this.$set(this.rule, 'configuration', {})
this.$set(this.rule, 'triggers', [])
Expand All @@ -440,42 +388,6 @@ export default {
this.$set(this, 'currentTemplate', this.templates.find((t) => t.uid === uid))
this.rule.templateUID = uid
},
keyDown (ev) {
if ((ev.ctrlKey || ev.metaKey) && !(ev.altKey || ev.shiftKey)) {
if (this.currentModule) return
switch (ev.keyCode) {
case 68:
this.toggleDisabled()
ev.stopPropagation()
ev.preventDefault()
break
case 82:
this.runNow()
ev.stopPropagation()
ev.preventDefault()
break
case 83:
this.save()
ev.stopPropagation()
ev.preventDefault()
break
}
}
},
toggleModuleControls () {
this.showModuleControls = !this.showModuleControls
},
showSwipeout (ev) {
let swipeoutElement = ev.target
ev.cancelBubble = true
while (!swipeoutElement.classList.contains('swipeout')) {
swipeoutElement = swipeoutElement.parentElement
}
if (swipeoutElement) {
this.$f7.swipeout.open(swipeoutElement)
}
},
editModule (ev, section, mod, force) {
if (this.showModuleControls) return
if (!this.isEditable) return
Expand Down Expand Up @@ -677,9 +589,6 @@ export default {
}
},
computed: {
isEditable () {
return this.rule && this.rule.editable !== false
},
hasTemplate () {
return this.rule && this.currentTemplate !== null
},
Expand All @@ -689,15 +598,6 @@ export default {
const marketplaceTag = this.currentTemplate.tags.find((t) => t.indexOf('marketplace:') === 0)
if (marketplaceTag) return 'https://community.openhab.org/t/' + marketplaceTag.replace('marketplace:', '')
return null
},
yamlError () {
if (this.currentTab !== 'code') return null
try {
YAML.parse(this.ruleYaml, { prettyErrors: true })
return 'OK'
} catch (e) {
return e
}
}
}
}
Expand Down
Loading

0 comments on commit ed88ed6

Please sign in to comment.