Skip to content

Commit

Permalink
Merge 03cdaf5 into 79fbc79
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-hoehn authored May 1, 2023
2 parents 79fbc79 + 03cdaf5 commit f464fbe
Showing 1 changed file with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
}

0 comments on commit f464fbe

Please sign in to comment.