Permit single field disabling (#4932) (#4941)

* Permit single field disabling (https://github.com/google/blockly/issues/4932)

* Permit single field disabling (#4932)

* Fixed lint error (moved && to end of line and adjusted line breaks accordingly)

* Added XML Field (De)Serialization

* Call parent method in FieldDropdown's fromXml
* Added protected helper methods to handle serialization/deserialization of enabled property/attribute of fields
* Minor changes to annotations to account for field disabling and 4 spaces per line break per style guide

* Revert "Added XML Field (De)Serialization"

This reverts commit 1964e866b6.

* Comment style changes

* Comment reversions

* Indentation fix

* Indentation reversion
This commit is contained in:
jschanker
2021-07-19 21:02:20 -04:00
committed by Monica Kozbial
parent 3aa6b476aa
commit 50a66c024d

View File

@@ -203,6 +203,13 @@ Blockly.Field.prototype.isDirty_ = true;
*/
Blockly.Field.prototype.visible_ = true;
/**
* Can the field value be changed using the editor on an editable block?
* @type {boolean}
* @protected
*/
Blockly.Field.prototype.enabled_ = true;
/**
* The element the click handler is bound to.
* @type {Element}
@@ -393,8 +400,8 @@ Blockly.Field.prototype.bindEvents_ = function() {
};
/**
* Sets the field's value based on the given XML element. Should only be
* called by Blockly.Xml.
* Sets the field's value based on the given XML element. Should only be called
* by Blockly.Xml.
* @param {!Element} fieldElement The element containing info about the
* field's state.
* @package
@@ -441,7 +448,7 @@ Blockly.Field.prototype.updateEditable = function() {
if (!this.EDITABLE || !group) {
return;
}
if (this.sourceBlock_.isEditable()) {
if (this.enabled_ && this.sourceBlock_.isEditable()) {
Blockly.utils.dom.addClass(group, 'blocklyEditableText');
Blockly.utils.dom.removeClass(group, 'blocklyNonEditableText');
group.style.cursor = this.CURSOR;
@@ -452,23 +459,45 @@ Blockly.Field.prototype.updateEditable = function() {
}
};
/**
* Set whether this field's value can be changed using the editor when the
* source block is editable.
* @param {boolean} enabled True if enabled.
*/
Blockly.Field.prototype.setEnabled = function(enabled) {
this.enabled_ = enabled;
this.updateEditable();
};
/**
* Check whether this field's value can be changed using the editor when the
* source block is editable.
* @return {boolean} Whether this field is enabled.
*/
Blockly.Field.prototype.isEnabled = function() {
return this.enabled_;
};
/**
* Check whether this field defines the showEditor_ function.
* @return {boolean} Whether this field is clickable.
*/
Blockly.Field.prototype.isClickable = function() {
return !!this.sourceBlock_ && this.sourceBlock_.isEditable() &&
!!this.showEditor_ && (typeof this.showEditor_ === 'function');
return this.enabled_ && !!this.sourceBlock_ &&
this.sourceBlock_.isEditable() && !!this.showEditor_ &&
(typeof this.showEditor_ === 'function');
};
/**
* Check whether this field is currently editable. Some fields are never
* EDITABLE (e.g. text labels). Other fields may be EDITABLE but may exist on
* non-editable blocks.
* @return {boolean} Whether this field is editable and on an editable block
* non-editable blocks or be currently disabled.
* @return {boolean} Whether this field is currently enabled, editable and on
* an editable block.
*/
Blockly.Field.prototype.isCurrentlyEditable = function() {
return this.EDITABLE && !!this.sourceBlock_ && this.sourceBlock_.isEditable();
return this.enabled_ && this.EDITABLE && !!this.sourceBlock_ &&
this.sourceBlock_.isEditable();
};
/**