Make it easy to disable unconnected blocks.

This commit is contained in:
Neil Fraser
2016-06-21 04:31:45 -07:00
parent 732e9b0659
commit 93125fd1e6
4 changed files with 35 additions and 0 deletions

View File

@@ -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();
}
};

View File

@@ -790,6 +790,7 @@ function init() {
}
mainWorkspace.clearUndo();
mainWorkspace.addChangeListener(Blockly.Events.disableOrphans);
mainWorkspace.addChangeListener(updateLanguage);
document.getElementById('direction')
.addEventListener('change', updatePreview);

View File

@@ -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();
};

View File

@@ -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');