Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[blockly] Add dictionary loop #1898

Merged
merged 2 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ export default function (f7, isGraalJs) {

/*
* Allows retrieving parameters provided by a rule
* Either a map can be directly used and an intermediate variable is created or a variable is directly provided
* Code part
*/
javascriptGenerator['dicts_get'] = function (block) {
Expand All @@ -208,4 +209,68 @@ export default function (f7, isGraalJs) {
let code = `${varName}[${key}]`
return [code, 0]
}

/*
* creates a loop for dictionaries
*
* Block type definition
*/
Blockly.Blocks['dicts_for'] = {
init: function () {
this.appendDummyInput()
.appendField('foreach')
this.appendDummyInput()
.appendField(new Blockly.FieldVariable('dictValue'), 'loopVar')
.appendField('in map')
this.appendValueInput('dict')
// allow Dictionary or Variable (type=null)
// in case of a variable we need to trust that it is a Dictionary
.setCheck(['Dictionary', null])

this.setInputsInline(true)
this.setColour('%{BKY_LOOPS_HUE}')
this.appendStatementInput('dictForCode')
.setCheck(null)
this.setPreviousStatement(true, null)
this.setNextStatement(true, null)
this.setTooltip('Create a named timer')
this.setHelpUrl('https://www.openhab.org/docs/configuration/blockly/rules-blockly-timers-and-delays.html#after-period-of-time-do-with-timer')
}
}

/*
* creates a loop for dictionaries
* Code part
*/
javascriptGenerator['dicts_for'] = function (block) {
const loopVar = block.getField('loopVar').getVariable().name
const dict = javascriptGenerator.valueToCode(block, 'dict', javascriptGenerator.ORDER_ATOMIC)
const dictForCode = javascriptGenerator.statementToCode(block, 'dictForCode')

const dictCheck = block.getInput('dict').connection.targetBlock().outputConnection.getCheck()
const dictType = (dictCheck) ? block.getInput('dict').connection.targetBlock().outputConnection.getCheck()[0] : ''

let code = ''
let dictVar

if (dictType === 'Dictionary') {
// Dictionary used directly, so we create an intermediate var
dictVar = addDict()
code += `${dictVar}=${dict};\n`
} else {
dictVar = dict
}

code += `for (var ${loopVar}Key in ${dictVar}) {\n`
code += ` ${loopVar} = ${dictVar}[${loopVar}Key];\n`
code += dictForCode
code += '}\n'
return code
}
}

function addDict () {
return javascriptGenerator.provideFunction_(
'dictionary',
['var ' + javascriptGenerator.FUNCTION_NAME_PLACEHOLDER_ + ';'])
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
</value>
</block>
<block type="controls_forEach" />
<block type="dicts_for" />
<block type="controls_flow_statements" />
</category>

Expand Down