Add Block.setEnabled (#2386)

* Implement Block.setEnabled()

From issue #1593. This commit:
- add setEnabled
- deprecate setDisabled

* Update setDisabled calls to setEnabled

Add setEnabled and deprecate setDisabled in
- core/block_svg

Update calls in
- blocks/loops
- blocks/procedures
- core/block_events
- core/events
- core/flyout_base
- core/xml
- tests/workplace_svg/procedure_svg_test

* Implement changes from comments from RoboErikG

- Implement isEnabled()
- Make this.disabled @private
- Make setDisabled(disabled) call setEnabled(!disabled)
- Update setEnabled to use isEnabled()

* Utilize isEnabled() and fix typos

Fix missing parentheses
Implement isEnabled() more widely
Fix lint and parentheses errors

* Change prevDisabledState to prevEnabledState
This commit is contained in:
Jim Jiang
2019-04-23 11:49:07 -04:00
committed by RoboErikG
parent aa4d885cba
commit 1b10d134a5
9 changed files with 71 additions and 38 deletions

View File

@@ -328,12 +328,12 @@ Blockly.Constants.Loops.CONTROL_FLOW_IN_LOOP_CHECK_MIXIN = {
if (legal) {
this.setWarningText(null);
if (!this.isInFlyout) {
this.setDisabled(false);
this.setEnabled(true);
}
} else {
this.setWarningText(Blockly.Msg['CONTROLS_FLOW_STATEMENTS_WARNING']);
if (!this.isInFlyout && !this.getInheritedDisabled()) {
this.setDisabled(true);
this.setEnabled(false);
}
}
}

View File

@@ -639,7 +639,7 @@ Blockly.Blocks['procedures_callnoreturn'] = {
this.argumentVarModels_ = [];
this.quarkConnections_ = {};
this.quarkIds_ = null;
this.previousDisabledState_ = false;
this.previousEnabledState_ = true;
},
/**
@@ -933,10 +933,10 @@ Blockly.Blocks['procedures_callnoreturn'] = {
}
Blockly.Events.setGroup(event.group);
if (event.newValue) {
this.previousDisabledState_ = this.disabled;
this.setDisabled(true);
this.previousEnabledState_ = this.isEnabled();
this.setEnabled(false);
} else {
this.setDisabled(this.previousDisabledState_);
this.setEnabled(this.previousEnabledState_);
}
Blockly.Events.setGroup(oldGroup);
}
@@ -985,7 +985,7 @@ Blockly.Blocks['procedures_callreturn'] = {
this.arguments_ = [];
this.quarkConnections_ = {};
this.quarkIds_ = null;
this.previousDisabledState_ = false;
this.previousEnabledState_ = true;
},
getProcedureCall: Blockly.Blocks['procedures_callnoreturn'].getProcedureCall,
@@ -1081,12 +1081,12 @@ Blockly.Blocks['procedures_ifreturn'] = {
}
this.setWarningText(null);
if (!this.isInFlyout) {
this.setDisabled(false);
this.setEnabled(true);
}
} else {
this.setWarningText(Blockly.Msg['PROCEDURES_IFRETURN_WARNING']);
if (!this.isInFlyout && !this.getInheritedDisabled()) {
this.setDisabled(true);
this.setEnabled(false);
}
}
},

View File

@@ -75,7 +75,10 @@ Blockly.Block = function(workspace, prototypeName, opt_id) {
this.inputList = [];
/** @type {boolean|undefined} */
this.inputsInline = undefined;
/** @type {boolean} */
/**
* @type {boolean}
* @private
*/
this.disabled = false;
/** @type {string|!Function} */
this.tooltip = '';
@@ -1217,12 +1220,31 @@ Blockly.Block.prototype.getInputsInline = function() {
/**
* Set whether the block is disabled or not.
* @param {boolean} disabled True if disabled.
* @deprecated May 2019
*/
Blockly.Block.prototype.setDisabled = function(disabled) {
if (this.disabled != disabled) {
console.warn('Deprecated call to Blockly.Block.prototype.setDisabled, ' +
'use Blockly.Block.prototype.setEnabled instead.');
this.setEnabled(!disabled);
};
/**
* Get whether this block is enabled or not.
* @return {boolean} True if enabled.
*/
Blockly.Block.prototype.isEnabled = function() {
return !this.disabled;
};
/**
* Set whether the block is enabled or not.
* @param {boolean} enabled True if enabled.
*/
Blockly.Block.prototype.setEnabled = function(enabled) {
if (this.isEnabled() != enabled) {
Blockly.Events.fire(new Blockly.Events.BlockChange(
this, 'disabled', null, this.disabled, disabled));
this.disabled = disabled;
this, 'disabled', null, this.disabled, !enabled));
this.disabled = !enabled;
}
};

View File

@@ -186,7 +186,7 @@ Blockly.Events.Change.prototype.run = function(forward) {
block.setCollapsed(value);
break;
case 'disabled':
block.setDisabled(value);
block.setEnabled(!value);
break;
case 'inline':
block.setInputsInline(value);

View File

@@ -709,7 +709,7 @@ Blockly.BlockSvg.prototype.showContextMenu_ = function(e) {
if (!group) {
Blockly.Events.setGroup(true);
}
block.setDisabled(!block.disabled);
block.setEnabled(!block.isEnabled());
if (!group) {
Blockly.Events.setGroup(false);
}
@@ -1174,10 +1174,21 @@ Blockly.BlockSvg.prototype.setMutator = function(mutator) {
/**
* Set whether the block is disabled or not.
* @param {boolean} disabled True if disabled.
* @deprecated May 2019
*/
Blockly.BlockSvg.prototype.setDisabled = function(disabled) {
if (this.disabled != disabled) {
Blockly.BlockSvg.superClass_.setDisabled.call(this, disabled);
console.warn('Deprecated call to Blockly.BlockSvg.prototype.setDisabled, ' +
'use Blockly.BlockSvg.prototype.setEnabled instead.');
this.setEnabled(!disabled);
};
/**
* Set whether the block is enabled or not.
* @param {boolean} enabled True if enabled.
*/
Blockly.BlockSvg.prototype.setEnabled = function(enabled) {
if (this.isEnabled() != enabled) {
Blockly.BlockSvg.superClass_.setEnabled.call(this, enabled);
if (this.rendered) {
this.updateDisabled();
}

View File

@@ -414,12 +414,12 @@ Blockly.Events.disableOrphans = function(event) {
if (block.getParent() && !block.getParent().disabled) {
var children = block.getDescendants(false);
for (var i = 0, child; child = children[i]; i++) {
child.setDisabled(false);
child.setEnabled(true);
}
} else if ((block.outputConnection || block.previousConnection) &&
!workspace.isDragging()) {
do {
block.setDisabled(true);
block.setEnabled(false);
block = block.getNextBlock();
} while (block);
}

View File

@@ -750,10 +750,10 @@ Blockly.Flyout.prototype.filterForCapacity_ = function() {
var blocks = this.workspace_.getTopBlocks(false);
for (var i = 0, block; block = blocks[i]; i++) {
if (this.permanentlyDisabled_.indexOf(block) == -1) {
var disable = !this.targetWorkspace_
var enable = this.targetWorkspace_
.isCapacityAvailable(Blockly.utils.getBlockTypeCounts(block));
while (block) {
block.setDisabled(disable);
block.setEnabled(enable);
block = block.getNextBlock();
}
}

View File

@@ -754,7 +754,7 @@ Blockly.Xml.domToBlockHeadless_ = function(xmlBlock, workspace) {
}
var disabled = xmlBlock.getAttribute('disabled');
if (disabled) {
block.setDisabled(disabled == 'true' || disabled == 'disabled');
block.setEnabled(disabled != 'true' && disabled != 'disabled');
}
var deletable = xmlBlock.getAttribute('deletable');
if (deletable) {

View File

@@ -66,7 +66,7 @@ function test_procedureReturnSetDisabledUpdatesCallers() {
workspace.clearUndo();
Blockly.Events.setGroup('g1');
barDef.setDisabled(true);
barDef.setEnabled(false);
Blockly.Events.setGroup(false);
assertTrue('Callers are disabled when their definition is disabled.',
@@ -82,7 +82,7 @@ function test_procedureReturnSetDisabledUpdatesCallers() {
workspace.clearUndo();
Blockly.Events.setGroup('g2');
barDef.setDisabled(false);
barDef.setEnabled(true);
Blockly.Events.setGroup(false);
assertTrue('Callers are enabled when their definition is enabled.',
@@ -125,10 +125,10 @@ function test_procedureReturnEnablingRemembersOldCallerState() {
var barDef = workspace.getBlockById('bar-def');
var barCalls = [workspace.getBlockById('bar-c1'), workspace.getBlockById('bar-c2')];
barCalls[0].setDisabled(true);
barCalls[0].setEnabled(false);
workspace.clearUndo();
Blockly.Events.setGroup('g1');
barDef.setDisabled(true);
barDef.setEnabled(false);
Blockly.Events.setGroup(false);
assertTrue('Callers are disabled when their definition is disabled.',
@@ -143,7 +143,7 @@ function test_procedureReturnEnablingRemembersOldCallerState() {
workspace.clearUndo();
Blockly.Events.setGroup('g2');
barDef.setDisabled(false);
barDef.setEnabled(true);
Blockly.Events.setGroup(false);
@@ -184,7 +184,7 @@ function test_procedureNoReturnSetDisabledUpdatesCallers() {
workspace.clearUndo();
Blockly.Events.setGroup('g1');
barDef.setDisabled(true);
barDef.setEnabled(false);
Blockly.Events.setGroup(false);
assertTrue('Callers are disabled when their definition is disabled.',
@@ -200,7 +200,7 @@ function test_procedureNoReturnSetDisabledUpdatesCallers() {
workspace.clearUndo();
Blockly.Events.setGroup('g2');
barDef.setDisabled(false);
barDef.setEnabled(true);
Blockly.Events.setGroup(false);
assertTrue('Callers are enabled when their definition is enabled.',
@@ -241,10 +241,10 @@ function test_procedureNoReturnEnablingRemembersOldCallerState() {
var barDef = workspace.getBlockById('bar-def');
var barCalls = [workspace.getBlockById('bar-c1'), workspace.getBlockById('bar-c2')];
barCalls[0].setDisabled(true);
barCalls[0].setEnabled(false);
workspace.clearUndo();
Blockly.Events.setGroup('g1');
barDef.setDisabled(true);
barDef.setEnabled(false);
Blockly.Events.setGroup(false);
assertTrue('Callers are disabled when their definition is disabled.',
@@ -259,7 +259,7 @@ function test_procedureNoReturnEnablingRemembersOldCallerState() {
workspace.clearUndo();
Blockly.Events.setGroup('g2');
barDef.setDisabled(false);
barDef.setEnabled(true);
Blockly.Events.setGroup(false);
@@ -329,19 +329,19 @@ function test_procedureEnableDisableInteractions() {
var fooCalls = [workspace.getBlockById('foo-c1'), workspace.getBlockById('foo-c2')];
var bazCall = workspace.getBlockById('baz-c1');
barDef.setDisabled(true);
barDef.setEnabled(false);
assertTrue('Callers are disabled when their definition is disabled.',
barCalls[0].disabled && barCalls[1].disabled);
assertTrue('Callers in definitions are disabled by inheritence.',
!fooCalls[0].disabled && fooCalls[0].getInheritedDisabled());
fooDef.setDisabled(true);
fooDef.setEnabled(false);
assertTrue('Callers are disabled when their definition is disabled',
fooCalls[0].disabled && fooCalls[1].disabled);
barDef.setDisabled(false);
barDef.setEnabled(true);
assertTrue('Callers are reenabled with their definition',
!barCalls[0].disabled && !barCalls[0].disabled);
@@ -349,7 +349,7 @@ function test_procedureEnableDisableInteractions() {
assertTrue('Nested disabled callers remain disabled, not by inheritence.',
fooCalls[0].disabled && !fooCalls[0].getInheritedDisabled());
bazDef.setDisabled(true);
bazDef.setEnabled(false);
assertTrue('Caller is disabled with its definition',
bazCall.disabled);
@@ -357,11 +357,11 @@ function test_procedureEnableDisableInteractions() {
assertTrue('Caller in the return is disabled by inheritence.',
!barCalls[1].disabled && barCalls[1].getInheritedDisabled());
barDef.setDisabled(true);
barDef.setEnabled(false);
assertTrue('Callers are disabled when their definition is disabled.',
barCalls[0].disabled && barCalls[1].disabled);
bazDef.setDisabled(false);
bazDef.setEnabled(true);
assertTrue('Caller in the return remains disabled, not by inheritence.',
barCalls[1].disabled && !barCalls[1].getInheritedDisabled());