mirror of
https://github.com/google/blockly.git
synced 2026-01-04 15:40:08 +01:00
Make it easy to disable unconnected blocks.
This commit is contained in:
@@ -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();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -790,6 +790,7 @@ function init() {
|
||||
}
|
||||
mainWorkspace.clearUndo();
|
||||
|
||||
mainWorkspace.addChangeListener(Blockly.Events.disableOrphans);
|
||||
mainWorkspace.addChangeListener(updateLanguage);
|
||||
document.getElementById('direction')
|
||||
.addEventListener('change', updatePreview);
|
||||
|
||||
@@ -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();
|
||||
};
|
||||
|
||||
|
||||
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user