From 6bda6d38833dba0d2fc9b37d1e9b6908a0823967 Mon Sep 17 00:00:00 2001 From: Tim Stallard Date: Tue, 21 Feb 2017 16:58:48 +0000 Subject: [PATCH] Working joining + events system --- client/src/events.js | 17 +++++++++++++++++ client/src/pageInteraction/dragDrop.js | 17 ++++++++++++++--- client/src/pageInteraction/joining.js | 10 +++++++--- client/src/styles.scss | 1 + 4 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 client/src/events.js diff --git a/client/src/events.js b/client/src/events.js new file mode 100644 index 0000000..6403f48 --- /dev/null +++ b/client/src/events.js @@ -0,0 +1,17 @@ +var subscriptions = {}; + +module.exports = { + subscribe: function(event, callback){ + if(!subscriptions[event]){ + subscriptions[event] = []; + } + subscriptions[event].push(callback); + }, + emit: function(event, data){ + if(subscriptions[event]){ + for(var callback of subscriptions[event]){ + callback(data); + } + } + } +}; diff --git a/client/src/pageInteraction/dragDrop.js b/client/src/pageInteraction/dragDrop.js index c773119..8c4abf9 100644 --- a/client/src/pageInteraction/dragDrop.js +++ b/client/src/pageInteraction/dragDrop.js @@ -1,5 +1,6 @@ var blocks = require("../blocks"); var diagram = require("../diagram.js"); +var events = require("../events.js"); var $ = require("jquery"); var uuid = require("node-uuid"); @@ -13,6 +14,8 @@ function blockPositionChange(event){ left: block.position.x, top: block.position.y, }); + + events.emit("blockMove"); } } @@ -61,15 +64,23 @@ $(document).on("mousemove", function(event){ blockPositionChange(event); }); -$("#workspace").on("mousedown", ".block", function(event){ +$("#workspace").on("mousedown", ".block>.main", function(event){ if(event.which == 3){ //right mouse button, delete event.preventDefault(); for(var i in diagram.state){ - if(diagram.state[i].id == $(this).attr("id")){ + if(diagram.state[i].id == $(this).parent().attr("id")){ diagram.state.splice(i, 1); } } - $(this).remove(); + for(var block of diagram.state){ + for(var input in block.inputs){ + if(block.inputs[input] == $(this).parent().attr("id")){ + delete block.inputs[input]; + } + } + } + $(this).parent().remove(); + events.emit("blockDelete"); } }); diff --git a/client/src/pageInteraction/joining.js b/client/src/pageInteraction/joining.js index 29d641b..2bf54c3 100644 --- a/client/src/pageInteraction/joining.js +++ b/client/src/pageInteraction/joining.js @@ -1,5 +1,6 @@ var blocks = require("../blocks"); var diagram = require("../diagram.js"); +var events = require("../events.js"); var $ = require("jquery"); function moveLine(elem, a, b, c, d){ @@ -11,7 +12,7 @@ function moveLine(elem, a, b, c, d){ $(elem).css({ left: x, top: y, - width: l - 2, + width: l, transform: "rotate(" + theta + "rad)" }); } @@ -33,7 +34,7 @@ $("#workspace").on("mousedown", ".block>.output", function(event){ $(document).on("mousemove", function(event){ if(dragging){ lineEnd = [event.pageX - $("#workspace").offset().left, event.pageY - $("#workspace").offset().top]; - moveLine($(".line"), lineStart[0], lineStart[1], lineEnd[0], lineEnd[1]); + moveLine($("#joiningLine"), lineStart[0], lineStart[1], lineEnd[0], lineEnd[1]); } }); @@ -48,14 +49,17 @@ $("#workspace").on("mouseup", ".block>.inputs>div", function(event){ if(dragging){ endBlock = $(this).parent().parent().attr("id"); endInput = $(this).attr("id"); - console.log(startBlock, endBlock, endInput); var endBlockInstance = diagram.state.filter((block)=>(block.id == endBlock))[0]; endBlockInstance.inputs[endInput] = startBlock; drawJoiningLines(); } }); +events.subscribe("blockMove", drawJoiningLines); +events.subscribe("blockDelete", drawJoiningLines); + function drawJoiningLines(){ + $(".line").remove(); for(var endBlock of diagram.state){ for(var input in endBlock.inputs){ startBlockId = endBlock.inputs[input]; diff --git a/client/src/styles.scss b/client/src/styles.scss index e89be10..f033ab8 100644 --- a/client/src/styles.scss +++ b/client/src/styles.scss @@ -64,5 +64,6 @@ body{ border-top: 2px solid black; position: absolute; z-index: 1; + pointer-events: none; } }