diff --git a/bundles/org.openhab.ui/web/src/assets/definitions/transformations.js b/bundles/org.openhab.ui/web/src/assets/definitions/transformations.js index d89e305985..bb191e3b90 100644 --- a/bundles/org.openhab.ui/web/src/assets/definitions/transformations.js +++ b/bundles/org.openhab.ui/web/src/assets/definitions/transformations.js @@ -1,14 +1,25 @@ export default { SNIPPETS: { - SCRIPT: { - 'application/vnd.openhab.dsl.rule': 'var returnValue = "String has " + input.length + " characters"\n\nreturnValue', - 'application/javascript': '(function(data) {\n var returnValue = "String has " + data.length + " characters"\n return returnValue\n})(input)', - 'application/javascript;version=ECMAScript-5.1': '(function(data) {\n var returnValue = "String has " + data.length + " characters"\n return returnValue\n})(input)', - 'application/x-ruby': '"String has #{input.length} characters"' - } + DSL: 'var returnValue = "String has " + input.length + " characters"\n\nreturnValue', + JINJA: '{# Available values:\nvalue - The incoming value.\nvalue_json - The incoming value parsed as JSON. #}\n{{value_json[\'AM2301\'].Temperature}}', + JS: '(function(data) {\n var returnValue = "String has " + data.length + " characters"\n return returnValue\n})(input)', + NASHORNJS: '(function(data) {\n var returnValue = "String has " + data.length + " characters"\n return returnValue\n})(input)', + MAP: 'key=value\n=default', + RB: '"String has #{input.length} characters"', + XSLT: '\n' + + '\n' + + ' \n' + + ' \n' + + ' world\n' + + ' \n' + + '' }, EDITOR_MODES: { + DSL: 'application/vnd.openhab.dsl.rule', + EXEC: 'application/x-sh', MAP: 'text/x-properties', - SCALE: 'text/x-properties' + NASHORNJS: 'application/javascript;version=ECMAScript-5.1', + SCALE: 'text/x-properties', + XSLT: 'application/xml' } } diff --git a/bundles/org.openhab.ui/web/src/components/config/controls/script-editor.vue b/bundles/org.openhab.ui/web/src/components/config/controls/script-editor.vue index aa30d1ecfd..9253bbe22c 100644 --- a/bundles/org.openhab.ui/web/src/components/config/controls/script-editor.vue +++ b/bundles/org.openhab.ui/web/src/components/config/controls/script-editor.vue @@ -46,13 +46,16 @@ import _CodeMirror from 'codemirror' import 'codemirror/lib/codemirror.css' // language js -import 'codemirror/mode/javascript/javascript.js' import 'codemirror/mode/clike/clike.js' import 'codemirror/mode/groovy/groovy.js' +import 'codemirror/mode/jinja2/jinja2.js' +import 'codemirror/mode/javascript/javascript.js' +import 'codemirror/mode/properties/properties.js' import 'codemirror/mode/python/python.js' import 'codemirror/mode/ruby/ruby.js' +import 'codemirror/mode/shell/shell.js' +import 'codemirror/mode/xml/xml.js' import 'codemirror/mode/yaml/yaml.js' -import 'codemirror/mode/properties/properties.js' // theme css import 'codemirror/theme/gruvbox-dark.css' @@ -168,15 +171,16 @@ export default { }, methods: { translateMode (mode) { - if (this.mode && this.mode.indexOf('yaml') >= 0) return 'text/x-yaml' - if (this.mode && this.mode.indexOf('application/javascript') === 0) return 'text/javascript' - if (this.mode && this.mode === 'application/vnd.openhab.dsl.rule') return 'text/x-java' - if (this.mode && this.mode.indexOf('python') >= 0) return 'text/x-python' - if (this.mode && this.mode.indexOf('ruby') >= 0) return 'text/x-ruby' - if (this.mode && this.mode.indexOf('groovy') >= 0) return 'text/x-groovy' - if (this.mode && this.mode === 'rb') return 'text/x-ruby' - if (this.mode && this.mode === 'properties') return 'text/x-properties' - return this.mode + // Translations required for some special modes used in MainUI + // See https://codemirror.net/5/mode/index.html for supported language names & MIME types + if (!mode) return mode + if (mode.indexOf('yaml') >= 0) return 'text/x-yaml' + if (mode.indexOf('application/javascript') === 0 || mode === 'js') return 'text/javascript' + if (mode === 'application/vnd.openhab.dsl.rule') return 'text/x-java' + if (mode === 'py') return 'text/x-python' + if (mode === 'rb') return 'text/x-ruby' + if (mode.indexOf('jinja') >= 0) return 'text/jinja2' + return mode }, ternComplete (file, query) { let pos = tern.resolvePos(file, query.end) diff --git a/bundles/org.openhab.ui/web/src/pages/settings/transformations/transformation-edit.vue b/bundles/org.openhab.ui/web/src/pages/settings/transformations/transformation-edit.vue index fcaef1e706..d8e5232aad 100644 --- a/bundles/org.openhab.ui/web/src/pages/settings/transformations/transformation-edit.vue +++ b/bundles/org.openhab.ui/web/src/pages/settings/transformations/transformation-edit.vue @@ -15,7 +15,7 @@ - +
@@ -56,12 +56,15 @@ - - + + Remove Transformation +

+ Tip: Use {{ itemStateTransformationCode }} for Item state transformations. +

@@ -81,10 +84,12 @@ import DirtyMixin from '../dirty-mixin' import TransformationGeneralSettings from '@/pages/settings/transformations/transformation-general-settings' import TransformationDefinitions from '@/assets/definitions/transformations.js' +import ClipboardIcon from '@/components/util/clipboard-icon.vue' export default { mixins: [DirtyMixin], components: { + ClipboardIcon, TransformationGeneralSettings, 'editor': () => import(/* webpackChunkName: "script-editor" */ '@/components/config/controls/script-editor.vue'), 'blockly-editor': () => import(/* webpackChunkName: "blockly-editor" */ '@/components/config/controls/blockly-editor.vue') @@ -92,12 +97,11 @@ export default { props: ['transformationId', 'createMode'], data () { return { - newTransformation: this.createMode, + newTransformation: this.createMode || false, ready: false, loading: false, transformation: {}, types: [], - scriptLanguages: [], languages: [], language: '', detailsOpened: false, @@ -113,6 +117,9 @@ export default { // TODO: Enable Blockly after blocks have been adjusted // return this.transformation.configuration && this.transformation.configuration.blockSource return false + }, + itemStateTransformationCode () { + return `${this.transformation.type.toUpperCase()}(${this.transformationId})` } }, methods: { @@ -143,20 +150,10 @@ export default { }, editable: true } - Promise.all([this.$oh.api.get('/rest/transformations/services'), this.$oh.api.get('/rest/module-types/script.ScriptAction'), this.$oh.api.get('/rest/config-descriptions/system:i18n')]).then((data) => { + Promise.all([this.$oh.api.get('/rest/transformations/services'), this.$oh.api.get('/rest/config-descriptions/system:i18n')]).then((data) => { this.$set(this, 'types', data[0]) - this.$set(this, 'scriptLanguages', data[1].configDescriptions - .find((c) => c.name === 'type').options - .map((l) => { - return { - contentType: l.value, - name: l.label.split(' (')[0], - version: l.label.split(' (')[1].replace(')', '') - } - })) - - this.$set(this, 'languages', data[2].parameters.find(p => p.name === 'language').options) + this.$set(this, 'languages', data[1].parameters.find(p => p.name === 'language').options) }) this.language = '' this.ready = true @@ -182,10 +179,8 @@ export default { this.transformation.uid += ':' + this.language } - // Insert code examples for SCRIPT - if (this.transformation.type === 'SCRIPT') { - this.transformation.configuration.function = TransformationDefinitions.SNIPPETS.SCRIPT[this.transformation.configuration.mode] - } + // Insert code example if available + if (TransformationDefinitions.SNIPPETS[this.transformation.type.toUpperCase()]) this.transformation.configuration.function = TransformationDefinitions.SNIPPETS[this.transformation.type.toUpperCase()] this.$oh.api.put('/rest/transformations/' + this.transformation.uid, this.transformation).then(() => { this.$f7.toast.create({ @@ -194,12 +189,6 @@ export default { closeTimeout: 2000 }).open() this.$f7router.navigate(this.$f7route.url.replace('/add', '/' + this.transformation.uid), { reloadCurrent: true }) - this.newTransformation = false - this.ready = false - if (window) { - window.addEventListener('keydown', this.keyDown) - } - this.load() }) }, load () { @@ -208,7 +197,7 @@ export default { this.$oh.api.get('/rest/transformations/' + this.transformationId).then((data) => { this.$set(this, 'transformation', data) - this.editorMode = (this.transformation.configuration.mode) ? this.transformation.configuration.mode : TransformationDefinitions.EDITOR_MODES[this.transformation.type.toUpperCase()] + this.editorMode = TransformationDefinitions.EDITOR_MODES[this.transformation.type.toUpperCase()] || this.transformation.type this.loading = false this.ready = true }) diff --git a/bundles/org.openhab.ui/web/src/pages/settings/transformations/transformation-general-settings.vue b/bundles/org.openhab.ui/web/src/pages/settings/transformations/transformation-general-settings.vue index 72232665f3..75da56bd16 100644 --- a/bundles/org.openhab.ui/web/src/pages/settings/transformations/transformation-general-settings.vue +++ b/bundles/org.openhab.ui/web/src/pages/settings/transformations/transformation-general-settings.vue @@ -2,12 +2,17 @@ - - + + +
+ {{ transformation.uid }} + +
+
+