From 03cdaf57b65d9fe813ac04e1171248eb35df5672 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20H=C3=B6hn?= Date: Mon, 1 May 2023 21:42:55 +0200 Subject: [PATCH] [blockly] add new mutating math-round block with toFixed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stefan Höhn --- .../assets/definitions/blockly/blocks-math.js | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/bundles/org.openhab.ui/web/src/assets/definitions/blockly/blocks-math.js b/bundles/org.openhab.ui/web/src/assets/definitions/blockly/blocks-math.js index 01541533db..0fb0ad15f3 100644 --- a/bundles/org.openhab.ui/web/src/assets/definitions/blockly/blocks-math.js +++ b/bundles/org.openhab.ui/web/src/assets/definitions/blockly/blocks-math.js @@ -79,4 +79,61 @@ export default function (f7, isGraalJs) { } return [`${first} ${operand} ${second}`, parentheses] } + + Blockly.Blocks['math_round'] = { + init: function () { + let thisBlock = this + let dropDown = new Blockly.FieldDropdown([['round', 'ROUND'], ['round up', 'ROUNDUP'], ['round down', 'ROUNDDOWN'], ['round →', 'toFixed']], + function (operation) { + thisBlock.updateType_(operation) + }) + this.appendValueInput('NUM') + .setCheck('Number') + .appendField(dropDown, 'op') + this.setColour('%{BKY_MATH_HUE}') + this.setInputsInline(false) + this.setTooltip('Round a number up or down') + this.setHelpUrl('https://www.openhab.org/docs/configuration/blockly/rules-blockly-math.html#round') + this.setOutput(true, 'Number') + }, + updateType_: function (type) { + if (type === 'toFixed') { + this.appendValueInput('decimals') + .setCheck('Number') + .appendField('by') + this.appendDummyInput('declabel') + .appendField('decimals') + this.setInputsInline(true) + } else if (this.getInput('decimals')) { + this.removeInput('decimals') + this.removeInput('declabel') + this.setInputsInline(false) + } + } + } + + javascriptGenerator['math_round'] = function (block) { + const math_number = javascriptGenerator.valueToCode(block, 'NUM', javascriptGenerator.ORDER_FUNCTION_CALL) + const decimals = javascriptGenerator.valueToCode(block, 'decimals', javascriptGenerator.ORDER_NONE) + const operand = block.getFieldValue('op') + let code = '' + if (operand !== 'toFixed') { + let method = '' + switch (operand) { + case 'ROUND': + method = 'Math.round' + break + case 'ROUNDUP': + method = 'Math.ceil' + break + case 'ROUNDDOWN': + method = 'Math.floor' + break + } + code = `${method}(${math_number})` + } else { + code = `(${math_number}).toFixed(${decimals})` + } + return [code, 0] + } }