Skip to content

Commit

Permalink
transformation services dropdown
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
  • Loading branch information
mherwege committed Sep 6, 2024
1 parent bba8d32 commit 41a03e4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Blockly from 'blockly'
import { javascriptGenerator } from 'blockly/javascript.js'
import { addOSGiService } from './utils.js'

export default function defineOHBlocks_Scripts (f7, isGraalJs, scripts) {
export default function defineOHBlocks_Scripts (f7, isGraalJs, transformationServices, scripts) {
/*
* Calls a script that is provided in openHABs scripts folder
* Blockly part
Expand Down Expand Up @@ -111,8 +111,9 @@ export default function defineOHBlocks_Scripts (f7, isGraalJs, scripts) {
.appendField('transform')
this.appendValueInput('function')
.appendField('apply')
.appendField(new Blockly.FieldTextInput('MAP'), 'type')
.appendField(new Blockly.FieldDropdown(transformationOptions()), 'type')
.appendField('with')
.setCheck(null)

this.setInputsInline(false)
this.setOutput(true, null)
Expand All @@ -122,12 +123,14 @@ export default function defineOHBlocks_Scripts (f7, isGraalJs, scripts) {
this.setTooltip(function () {
const type = thisBlock.getFieldValue('type')
switch (type) {
case '':
return 'select from the installed transformations. The list is empty if no transformation addons have been installed.'
case 'MAP':
return 'transforms an input via a map file. Specify the file as the function.\nREGEX and JSONPATH are also valid.'
return 'transforms an input via a map file. Specify the file as the function.'
case 'REGEX':
return 'transforms / filters an input by applying the provided regular expression.\nMAP and JSONPATH are also valid.'
return 'transforms / filters an input by applying the provided regular expression.'
case 'JSONPATH':
return 'transforms / filters a JSON input by executing the provided JSONPath query.\nMAP and REGEX are also valid.'
return 'transforms / filters a JSON input by executing the provided JSONPath query.'
default:
return 'transforms the input with the ' + type + ' transformation.'
}
Expand All @@ -139,6 +142,17 @@ export default function defineOHBlocks_Scripts (f7, isGraalJs, scripts) {
}
}

function transformationOptions () {
let options = []
if (transformationServices && transformationServices.length > 0) {
for (let key in transformationServices) {
let transformationOption = transformationServices[key]
options.push([transformationOption, transformationOption])
}
}
return options.length > 0 ? options : [['', '']]
}

/*
* Allow transformations via different methods
* Code part
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function (f7, libraryDefinitions, data, isGraalJs) {
defineTimerBlocks(f7, isGraalJs)
defineValueStorageBlocks(f7, isGraalJs)
defineEphemerisBlocks(f7, isGraalJs)
defineScriptsBlocks(f7, isGraalJs)
defineScriptsBlocks(f7, isGraalJs, data.transformationServices)
definePersistenceBlocks(f7, isGraalJs, data.persistenceServices)
defineColorBlocks(f7, isGraalJs)
defineTextBlocks(f7, isGraalJs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,7 @@ export default {
scripts: [],
rules: [],
persistenceServices: [],
transformationServices: [],
loading: true,
ready: false
}
Expand Down Expand Up @@ -1229,7 +1230,8 @@ export default {
this.$oh.api.get('/rest/audio/sinks'),
this.$oh.api.get('/rest/voice/voices'),
this.libraryDefinitions ? Promise.resolve(this.libraryDefinitions) : this.$oh.api.get('/rest/ui/components/ui:blocks'),
this.$oh.api.get('/rest/persistence')
this.$oh.api.get('/rest/persistence'),
this.$oh.api.get('/rest/transformations/services')
]
Promise.all(dataPromises)
.then((data) => {
Expand Down Expand Up @@ -1266,6 +1268,12 @@ export default {
return labelA.localeCompare(labelB)
})
this.transformationServices = data[5].sort((a, b) => {
const labelA = a
const labelB = b
return labelA.localeCompare(labelB)
})
this.initBlockly(this.blockLibraries)
})
.catch((err, status) => {
Expand All @@ -1276,7 +1284,8 @@ export default {
defineOHBlocks(this.$f7, libraryDefinitions, {
sinks: this.sinks,
voices: this.voices,
persistenceServices: this.persistenceServices
persistenceServices: this.persistenceServices,
transformationServices: this.transformationServices
}, this.isGraalJs)
this.addLibraryToToolbox(libraryDefinitions || [])
Expand Down

0 comments on commit 41a03e4

Please sign in to comment.