From 9d7efa89e4de9064961ee4fa0876e93ebeeda29b Mon Sep 17 00:00:00 2001 From: Tim Stallard Date: Mon, 27 Feb 2017 18:37:20 +0000 Subject: [PATCH] Added custom code block --- src/blocks/custom.js | 29 +++++++++++++++++++++++++++++ src/blocks/index.js | 1 + src/outputCalculation.js | 5 +---- 3 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 src/blocks/custom.js diff --git a/src/blocks/custom.js b/src/blocks/custom.js new file mode 100644 index 0000000..f38cd0e --- /dev/null +++ b/src/blocks/custom.js @@ -0,0 +1,29 @@ +var events = require("../events.js"); + +module.exports = { + name: "Custom Code", + inputs: { + inp1: "1", + inp2: "2", + inp3: "3", + inp4: "4", + inp5: "5" + }, + output: true, + execute: function(inputs, block){ + eval("customFunc = function(inp1, inp2, inp3, inp4, inp5){" + block.properties.code + "}"); + return customFunc(inputs.inp1, inputs.inp2, inputs.inp3, inputs.inp4, inputs.inp5); + }, + pageBlock: { + html: "To access inputs, use vars inp1, inp2, etc
", + js: function(block){ + if(block.properties.code){ + block.elem.find("textarea").val(block.properties.code); + } + $(block.elem).find("textarea").keyup(function(){ + block.properties.code = block.elem.find("textarea").val(); + events.emit("inputChanged"); + }); + } + } +} diff --git a/src/blocks/index.js b/src/blocks/index.js index 7bb7577..5b1b788 100644 --- a/src/blocks/index.js +++ b/src/blocks/index.js @@ -18,6 +18,7 @@ var blocks = [ "transpositionReverse", "substitution", "frequency", + "custom", ]; module.exports = blocks.reduce((blocks, block)=>{ diff --git a/src/outputCalculation.js b/src/outputCalculation.js index 35d710b..2cb7d04 100644 --- a/src/outputCalculation.js +++ b/src/outputCalculation.js @@ -14,10 +14,7 @@ function resolveOutput(block, cache){ } } - var output = ""; - if(Object.keys(blocks[block.type].inputs).length == Object.keys(inputValues).length){ //only execute if all inputs are present - output = blocks[block.type].execute(inputValues, block); - } + var output = blocks[block.type].execute(inputValues, block); cache[block.id] = output; return output;