Skip to content

Commit

Permalink
Permit single field disabling (#4932) (#4941)
Browse files Browse the repository at this point in the history
* Permit single field disabling (#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 1964e86.

* Comment style changes

* Comment reversions

* Indentation fix

* Indentation reversion
  • Loading branch information
jschanker authored Jul 20, 2021
1 parent 607c5e1 commit e8c4af3
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions core/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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();
};

/**
Expand Down

0 comments on commit e8c4af3

Please sign in to comment.