-
-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rule & Scene edit: Use mixin for shared code
Signed-off-by: Florian Hotze <[email protected]>
- Loading branch information
1 parent
98d4262
commit ed88ed6
Showing
3 changed files
with
115 additions
and
204 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
bundles/org.openhab.ui/web/src/pages/settings/rules/rule-edit-mixin.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.