diff --git a/core/field.js b/core/field.js index 4b18ca08e1a..2861bd36ba5 100644 --- a/core/field.js +++ b/core/field.js @@ -399,14 +399,39 @@ Blockly.Field.prototype.bindEvents_ = function() { }; /** - * Sets the field's value based on the given XML element. Should only be - * called by Blockly.Xml. + * Disables field if the given XML element specifies this. * @param {!Element} fieldElement The element containing info about the - * field's state. + * field's state. + * @protected + */ +Blockly.Field.prototype.setEnabledFromXml_ = function(fieldElement) { + if (fieldElement.getAttribute('enabled') === 'false') { + this.setEnabled(false); + } +}; + +/** + * Sets the field's value and possibly disables field 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 */ Blockly.Field.prototype.fromXml = function(fieldElement) { this.setValue(fieldElement.textContent); + this.setEnabledFromXml_(fieldElement); +}; + +/** + * Adds false enabled attribute to the given XML element if field is disabled. + * @param {!Element} fieldElement The element containing info about the + * field's state. + * @protected + */ +Blockly.Field.prototype.addDisabledToXml_ = function(fieldElement) { + if (!this.enabled_) { + fieldElement.setAttribute('enabled', 'false'); + } }; /** @@ -418,6 +443,7 @@ Blockly.Field.prototype.fromXml = function(fieldElement) { */ Blockly.Field.prototype.toXml = function(fieldElement) { fieldElement.textContent = this.getValue(); + this.addDisabledToXml_(fieldElement); return fieldElement; }; @@ -460,7 +486,7 @@ Blockly.Field.prototype.updateEditable = function() { /** * Set whether this field's value can be changed using the editor when the - * source block is editable. + * source block is editable. * @param {boolean} enabled True if enabled. */ Blockly.Field.prototype.setEnabled = function(enabled) { @@ -470,7 +496,7 @@ Blockly.Field.prototype.setEnabled = function(enabled) { /** * Check whether this field's value can be changed using the editor when the - * source block is editable. + * source block is editable. * @return {boolean} Whether this field is enabled. */ Blockly.Field.prototype.isEnabled = function() { diff --git a/core/field_dropdown.js b/core/field_dropdown.js index 66c7d11b12e..51eae11290f 100644 --- a/core/field_dropdown.js +++ b/core/field_dropdown.js @@ -153,17 +153,17 @@ Blockly.FieldDropdown.fromJson = function(options) { }; /** - * Sets the field's value based on the given XML element. Should only be - * called by Blockly.Xml. + * Sets the field's value and possibly disables field 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. + * field's state. * @package */ Blockly.FieldDropdown.prototype.fromXml = function(fieldElement) { if (this.isOptionListDynamic()) { this.getOptions(false); } - this.setValue(fieldElement.textContent); + Blockly.FieldDropdown.superClass_.fromXml.call(this, fieldElement); }; /** diff --git a/core/field_multilineinput.js b/core/field_multilineinput.js index c6260e4fd81..362c6a97908 100644 --- a/core/field_multilineinput.js +++ b/core/field_multilineinput.js @@ -105,18 +105,20 @@ Blockly.FieldMultilineInput.prototype.toXml = function(fieldElement) { // `Blockly.Xml.domToText` will appear on a single line (this is a limitation // of the plain-text format). fieldElement.textContent = this.getValue().replace(/\n/g, ' '); + this.addDisabledToXml_(fieldElement); return fieldElement; }; /** - * Sets the field's value based on the given XML element. Should only be - * called by Blockly.Xml. + * Sets the field's value and possibly disables field 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 */ Blockly.FieldMultilineInput.prototype.fromXml = function(fieldElement) { this.setValue(fieldElement.textContent.replace(/ /g, '\n')); + this.setEnabledFromXml_(fieldElement); }; /** diff --git a/core/field_variable.js b/core/field_variable.js index efdf08bfa92..6125d290e51 100644 --- a/core/field_variable.js +++ b/core/field_variable.js @@ -173,6 +173,7 @@ Blockly.FieldVariable.prototype.fromXml = function(fieldElement) { } this.setValue(variable.getId()); + this.setEnabledFromXml_(fieldElement); }; /** @@ -190,6 +191,7 @@ Blockly.FieldVariable.prototype.toXml = function(fieldElement) { if (this.variable_.type) { fieldElement.setAttribute('variabletype', this.variable_.type); } + this.addDisabledToXml_(fieldElement); return fieldElement; };