From d9efbf4c4393e7024a2f7dcc635af05d5882711b Mon Sep 17 00:00:00 2001 From: Tim Stallard Date: Mon, 3 Apr 2017 09:24:31 +0100 Subject: [PATCH] Did stuff --- package.json | 1 - src/blocks/affineEncrypt.js | 2 +- src/blocks/output.js | 3 +- src/outputCalculation.js | 19 +++++--- src/pageInteraction/addBlockToPage.js | 1 + src/pageInteraction/dragDrop.js | 70 +++++++++++++++++++++++++-- src/typeConversion.js | 14 ++++++ 7 files changed, 97 insertions(+), 13 deletions(-) create mode 100644 src/typeConversion.js diff --git a/package.json b/package.json index 04c9ed8..0bff4e6 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,6 @@ "node-uuid": "^1.4.7", "sass-loader": "^6.0.1", "style-loader": "^0.13.1", - "uuid": "^3.0.1", "webpack": "^2.2.1", "webpack-dev-server": "^2.3.0" } diff --git a/src/blocks/affineEncrypt.js b/src/blocks/affineEncrypt.js index a79f15a..490d477 100644 --- a/src/blocks/affineEncrypt.js +++ b/src/blocks/affineEncrypt.js @@ -33,8 +33,8 @@ module.exports = { } if(!require("./util/coPrime.js")(a, 26)){ + throw "a and 26 must be coprime"; return ""; - console.log(a, 26, "not coprime"); } var lookupTable = []; diff --git a/src/blocks/output.js b/src/blocks/output.js index 77a831f..e6f4f5c 100644 --- a/src/blocks/output.js +++ b/src/blocks/output.js @@ -2,7 +2,8 @@ module.exports = { name: "Output", inputs: { input: { - name: "Input" + name: "Input", + type: "text" } }, output: false, diff --git a/src/outputCalculation.js b/src/outputCalculation.js index e651b6c..560dd6d 100644 --- a/src/outputCalculation.js +++ b/src/outputCalculation.js @@ -1,6 +1,7 @@ var diagram = require("./diagram"); var events = require("./events.js"); -var blocks = require("./blocks"); +var blockModels = require("./blocks"); +var typeConversion = require("./typeConversion.js"); function resolveOutput(block, cache){ try{ @@ -19,17 +20,21 @@ function resolveOutput(block, cache){ else if(block.inputs[input].value){ //if value is already set, just save that inputValues[input] = block.inputs[input].value; } - if(!inputValues[input]){ //if currently missing/blank - if(blocks[block.type].inputs[input].default){ //if a default is present, use that - inputValues[input] = blocks[block.type].inputs[input].default; - } - else if(blocks[block.type].inputs[input].required){ //otherwise, throw an error + + if(inputValues[input]){ //if input is present, check and convert into type + inputValues[input] = typeConversion[blockModels[block.type].inputs[input].type](inputValues[input]); + } + else{ //currently missing/blank + if(blockModels[block.type].inputs[input].required){ //if required, throw an error throw "A required input is missing"; } + else if(blockModels[block.type].inputs[input].default){ //otherwise, if a default is present, use that + inputValues[input] = blockModels[block.type].inputs[input].default; + } } } - var output = blocks[block.type].execute(inputValues, block); + var output = blockModels[block.type].execute(inputValues, block); cache[block.id] = output; return output; } diff --git a/src/pageInteraction/addBlockToPage.js b/src/pageInteraction/addBlockToPage.js index 3733671..51ea7a3 100644 --- a/src/pageInteraction/addBlockToPage.js +++ b/src/pageInteraction/addBlockToPage.js @@ -1,5 +1,6 @@ var blocks = require("../blocks"); var events = require("../events"); +var $ = require("jquery"); module.exports = function(newBlock){ var newBlockElement = $( diff --git a/src/pageInteraction/dragDrop.js b/src/pageInteraction/dragDrop.js index 5156120..84cebf1 100644 --- a/src/pageInteraction/dragDrop.js +++ b/src/pageInteraction/dragDrop.js @@ -8,8 +8,18 @@ function blockPositionChange(event){ //fired when a block is moved or added var block = diagram.state.filter((block)=>(block.dragging))[0]; if(block){ //change position to the event coordinates plus the offset, factoring in the relative positioning of the items on the workspace - block.position.x = event.pageX - $("#workspace").position().left - block.offset.x; - block.position.y = event.pageY - $("#workspace").position().top - block.offset.y; + block.position.x = window.scrollX + event.clientX - $("#workspace").position().left - block.offset.x; + block.position.y = window.scrollY + event.clientY - $("#workspace").position().top - block.offset.y; + + if(offY){ + //when off y-axis, move to sit edge of block on axis + block.position.y = window.scrollY + window.innerHeight - $(block.elem).height() - $("#workspace").position().top; + } + + if(offX){ + //when off y-axis, move to sit edge of block on axis + block.position.x = window.scrollX + window.innerWidth - $(block.elem).width() - $("#workspace").position().left; + } $("#workspace>.block#" + block.id).css({ //apply new position to element left: block.position.x, @@ -61,9 +71,10 @@ $("#workspace").on("mousedown", ".block>.main,.block>.inputs", function(event){ $("#workspace").on("mouseup", ".block>.main,.block>.inputs", function(event){ diagram.state.filter((block)=>(block.id == $(this).parent().attr("id")))[0].dragging = false; + offX = false; + offY = false; }); - $(document).on("mousemove", function(event){ event.preventDefault(); blockPositionChange(event); @@ -89,3 +100,56 @@ $(document).on("contextmenu", function(event){ event.preventDefault(); return false; }); + +var timer; +var lastevent; +var startevent; +var offY = false; +var offX = false; +$(document).on("mousemove", ".block", function(e){ + if(diagram.state.filter((block)=>(block.id == $(this).attr("id"))).filter((block)=>(block.dragging)).length){ + //block is being dragged + lastevent = e; + if(!offY){ + if(($(this).offset().top + $(this).height() - window.scrollY) > window.innerHeight){ + //block has just moved off the page, start scrolling + startevent = e; + offY = true; + } + } + else{ + if(e.clientY < startevent.clientY){ + //user has moved the block back from the boundary, stop + offY = false; + blockPositionChange(lastevent); + } + } + + if(!offX){ + if(($(this).offset().left + $(this).width() - window.scrollX) > window.innerWidth){ + //block has just moved off the page, start scrolling + startevent = e; + offX = true; + } + } + else{ + if(e.clientX < startevent.clientX){ + //user has moved the block back from the boundary, stop + offX = false; + blockPositionChange(lastevent); + } + } + } +}); + +timer = setInterval(function(){ + if(offY){ //scroll in y-direction and update blocks if block is moved off in that direction + window.scrollBy(0, 2); + } + if(offX){ //scroll in x-direction and update blocks if block is moved off in that direction + window.scrollBy(2, 0); + } + if(offX || offY){ //if off in either direction, move the block on the page + blockPositionChange(lastevent); + } +}, 3); diff --git a/src/typeConversion.js b/src/typeConversion.js new file mode 100644 index 0000000..222e21a --- /dev/null +++ b/src/typeConversion.js @@ -0,0 +1,14 @@ +module.exports = { + text: function(input){ + if(typeof input != "string"){ + throw "Invalid input type - should be a string."; + } + return input; + }, + number: function(input){ + if(isNaN(parseInt(input))){ + throw "Invalid input type - should be a string."; + } + return parseInt(input); + } +}