From 93125fd1e6fd825a676c3f788cc94e0067ac7957 Mon Sep 17 00:00:00 2001 From: Neil Fraser Date: Tue, 21 Jun 2016 04:31:45 -0700 Subject: [PATCH] Make it easy to disable unconnected blocks. --- core/events.js | 31 +++++++++++++++++++++++++++++++ demos/blockfactory/factory.js | 1 + demos/graph/index.html | 2 ++ demos/plane/plane.js | 1 + 4 files changed, 35 insertions(+) diff --git a/core/events.js b/core/events.js index 67e210b55..f1971478d 100644 --- a/core/events.js +++ b/core/events.js @@ -784,3 +784,34 @@ Blockly.Events.Ui.prototype.fromJson = function(json) { this.element = json['element']; this.newValue = json['newValue']; }; + +/** + * Enable/disable a block depending on whether it is properly connected. + * Use this on applications where all blocks should be connected to a top block. + * Recommend setting the 'disable' option to 'false' in the config so that + * users don't try to reenable disabled orphan blocks. + * @param {!Blockly.Events.Abstract} event Custom data for event. + */ +Blockly.Events.disableOrphans = function(event) { + if (event.type == Blockly.Events.MOVE || + event.type == Blockly.Events.CREATE) { + Blockly.Events.disable(); + var workspace = Blockly.Workspace.getById(event.workspaceId); + var block = workspace.getBlockById(event.blockId); + if (block) { + if (block.getParent() && !block.getParent().disabled) { + do { + block.setDisabled(false); + block = block.getNextBlock(); + } while (block); + } else if ((block.outputConnection || block.previousConnection) && + Blockly.dragMode_ == Blockly.DRAG_NONE) { + do { + block.setDisabled(true); + block = block.getNextBlock(); + } while (block); + } + } + Blockly.Events.enable(); + } +}; diff --git a/demos/blockfactory/factory.js b/demos/blockfactory/factory.js index 2d229c316..3e1cf16a0 100644 --- a/demos/blockfactory/factory.js +++ b/demos/blockfactory/factory.js @@ -790,6 +790,7 @@ function init() { } mainWorkspace.clearUndo(); + mainWorkspace.addChangeListener(Blockly.Events.disableOrphans); mainWorkspace.addChangeListener(updateLanguage); document.getElementById('direction') .addEventListener('change', updatePreview); diff --git a/demos/graph/index.html b/demos/graph/index.html index c3ce000cc..4732070d9 100644 --- a/demos/graph/index.html +++ b/demos/graph/index.html @@ -337,6 +337,7 @@ Graph.resize = function() { Graph.init = function() { Graph.workspace = Blockly.inject('blocklyDiv', {collapse: false, + disable: false, media: '../../media/', toolbox: document.getElementById('toolbox')}); Blockly.Xml.domToWorkspace(document.getElementById('startBlocks'), @@ -345,6 +346,7 @@ Graph.init = function() { // When Blockly changes, update the graph. Graph.workspace.addChangeListener(Graph.drawVisualization); + Graph.workspace.addChangeListener(Blockly.Events.disableOrphans); Graph.resize(); }; diff --git a/demos/plane/plane.js b/demos/plane/plane.js index 25e350fb6..99e5c175c 100644 --- a/demos/plane/plane.js +++ b/demos/plane/plane.js @@ -276,6 +276,7 @@ Plane.init = function() { Plane.loadBlocks(defaultXml); Plane.workspace.addChangeListener(Plane.recalculate); + Plane.workspace.addChangeListener(Blockly.Events.disableOrphans); // Initialize the slider. var svg = document.getElementById('plane');