From 73ff710a4d113cf4c441ebc8eddaa3cdce179aa0 Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Wed, 29 May 2019 12:50:00 -0700 Subject: [PATCH] Added getSourceBlock function to field. (#2508) --- blocks/lists.js | 9 ++++----- blocks/math.js | 2 +- blocks/procedures.js | 8 ++++---- blocks/text.js | 4 ++-- core/dropdowndiv.js | 4 ++-- core/field.js | 8 ++++++++ core/procedures.js | 4 ++-- demos/blockfactory/blocks.js | 6 +++--- 8 files changed, 26 insertions(+), 19 deletions(-) diff --git a/blocks/lists.js b/blocks/lists.js index 6bfa42139..9e9217f12 100644 --- a/blocks/lists.js +++ b/blocks/lists.js @@ -334,7 +334,7 @@ Blockly.Blocks['lists_getIndex'] = { this.setStyle('list_blocks'); var modeMenu = new Blockly.FieldDropdown(MODE, function(value) { var isStatement = (value == 'REMOVE'); - this.sourceBlock_.updateStatement_(isStatement); + this.getSourceBlock().updateStatement_(isStatement); }); this.appendValueInput('VALUE') .setCheck('Array') @@ -480,7 +480,7 @@ Blockly.Blocks['lists_getIndex'] = { var newAt = (value == 'FROM_START') || (value == 'FROM_END'); // The 'isAt' variable is available due to this function being a closure. if (newAt != isAt) { - var block = this.sourceBlock_; + var block = this.getSourceBlock(); block.updateAt_(newAt); // This menu has been destroyed and replaced. Update the replacement. block.setFieldValue(value, 'WHERE'); @@ -618,7 +618,7 @@ Blockly.Blocks['lists_setIndex'] = { var newAt = (value == 'FROM_START') || (value == 'FROM_END'); // The 'isAt' variable is available due to this function being a closure. if (newAt != isAt) { - var block = this.sourceBlock_; + var block = this.getSourceBlock(); block.updateAt_(newAt); // This menu has been destroyed and replaced. Update the replacement. block.setFieldValue(value, 'WHERE'); @@ -723,14 +723,13 @@ Blockly.Blocks['lists_getSublist'] = { // The 'isAt' variable is available due to this function being a // closure. if (newAt != isAt) { - var block = this.sourceBlock_; + var block = this.getSourceBlock(); block.updateAt_(n, newAt); // This menu has been destroyed and replaced. // Update the replacement. block.setFieldValue(value, 'WHERE' + n); return null; } - return undefined; }); this.getInput('AT' + n) .appendField(menu, 'WHERE' + n); diff --git a/blocks/math.js b/blocks/math.js index 3d4f337ae..4bc690916 100644 --- a/blocks/math.js +++ b/blocks/math.js @@ -504,7 +504,7 @@ Blockly.Constants.Math.IS_DIVISIBLEBY_MUTATOR_MIXIN = { Blockly.Constants.Math.IS_DIVISIBLE_MUTATOR_EXTENSION = function() { this.getField('PROPERTY').setValidator(function(option) { var divisorInput = (option == 'DIVISIBLE_BY'); - this.sourceBlock_.updateShape_(divisorInput); + this.getSourceBlock().updateShape_(divisorInput); }); }; diff --git a/blocks/procedures.js b/blocks/procedures.js index 5188ac8b9..c679ce54d 100644 --- a/blocks/procedures.js +++ b/blocks/procedures.js @@ -572,15 +572,15 @@ Blockly.Blocks['procedures_mutatorarg'] = { * @this Blockly.FieldTextInput */ validator_: function(varName) { - var outerWs = Blockly.Mutator.findParentWs(this.sourceBlock_.workspace); + var outerWs = Blockly.Mutator.findParentWs(this.getSourceBlock().workspace); varName = varName.replace(/[\s\xa0]+/g, ' ').replace(/^ | $/g, ''); if (!varName) { return null; } // Prevents duplicate parameter names in functions - var blocks = this.sourceBlock_.workspace.getAllBlocks(); + var blocks = this.getSourceBlock().workspace.getAllBlocks(); for (var i = 0; i < blocks.length; i += 1) { - if (blocks[i].id == this.sourceBlock_.id) { + if (blocks[i].id == this.getSourceBlock().id) { continue; } if (blocks[i].getFieldValue('NAME') == varName) { @@ -609,7 +609,7 @@ Blockly.Blocks['procedures_mutatorarg'] = { * @this Blockly.FieldTextInput */ deleteIntermediateVars_: function(newText) { - var outerWs = Blockly.Mutator.findParentWs(this.sourceBlock_.workspace); + var outerWs = Blockly.Mutator.findParentWs(this.getSourceBlock().workspace); if (!outerWs) { return; } diff --git a/blocks/text.js b/blocks/text.js index 244889456..91ed6c1fa 100644 --- a/blocks/text.js +++ b/blocks/text.js @@ -296,7 +296,7 @@ Blockly.Blocks['text_getSubstring'] = { // The 'isAt' variable is available due to this function being a // closure. if (newAt != isAt) { - var block = this.sourceBlock_; + var block = this.getSourceBlock(); block.updateAt_(n, newAt); // This menu has been destroyed and replaced. // Update the replacement. @@ -862,7 +862,7 @@ Blockly.Constants.Text.TEXT_CHARAT_EXTENSION = function() { dropdown.setValidator(function(value) { var newAt = (value == 'FROM_START') || (value == 'FROM_END'); if (newAt != this.isAt_) { - var block = this.sourceBlock_; + var block = this.getSourceBlock(); block.updateAt_(newAt); } }); diff --git a/core/dropdowndiv.js b/core/dropdowndiv.js index 5af33b8c9..f5eefa06b 100644 --- a/core/dropdowndiv.js +++ b/core/dropdowndiv.js @@ -250,7 +250,7 @@ Blockly.DropDownDiv.showPositionedByField = function(owner, // Set bounds to workspace; show the drop-down. Blockly.DropDownDiv.positionToField_ = true; Blockly.DropDownDiv.setBoundsElement( - owner.sourceBlock_.workspace.getParentSvg().parentNode); + owner.getSourceBlock().workspace.getParentSvg().parentNode); return Blockly.DropDownDiv.show( owner, primaryX, primaryY, secondaryX, secondaryY, opt_onHide); }; @@ -514,7 +514,7 @@ Blockly.DropDownDiv.repositionForWindowResize = function() { // when a field is focused, the soft keyboard opens triggering a window resize // event and we want the dropdown div to stick around so users can type into it. if (Blockly.DropDownDiv.owner_) { - var block = Blockly.DropDownDiv.owner_.sourceBlock_; + var block = Blockly.DropDownDiv.owner_.getSourceBlock(); var scale = block.workspace.scale; var bBox = { width: Blockly.DropDownDiv.positionToField_ ? diff --git a/core/field.js b/core/field.js index 0e7929178..ef66659b6 100644 --- a/core/field.js +++ b/core/field.js @@ -211,6 +211,14 @@ Blockly.Field.prototype.setSourceBlock = function(block) { this.sourceBlock_ = block; }; +/** + * Get the block this field is attached to. + * @return {Blockly.Block} The block containing this field. + */ +Blockly.Field.prototype.getSourceBlock = function() { + return this.sourceBlock_; +}; + /** * Initialize everything to render this field. Override * methods initModel and initView rather than this method. diff --git a/core/procedures.js b/core/procedures.js index a226bc971..6923a7d3b 100644 --- a/core/procedures.js +++ b/core/procedures.js @@ -163,11 +163,11 @@ Blockly.Procedures.rename = function(name) { name = name.replace(/^[\s\xa0]+|[\s\xa0]+$/g, ''); // Ensure two identically-named procedures don't exist. - var legalName = Blockly.Procedures.findLegalName(name, this.sourceBlock_); + var legalName = Blockly.Procedures.findLegalName(name, this.getSourceBlock()); var oldName = this.text_; if (oldName != name && oldName != legalName) { // Rename any callers. - var blocks = this.sourceBlock_.workspace.getAllBlocks(false); + var blocks = this.getSourceBlock().workspace.getAllBlocks(false); for (var i = 0; i < blocks.length; i++) { if (blocks[i].renameProcedure) { blocks[i].renameProcedure(oldName, legalName); diff --git a/demos/blockfactory/blocks.js b/demos/blockfactory/blocks.js index 4ff1af61d..19f8a15e6 100644 --- a/demos/blockfactory/blocks.js +++ b/demos/blockfactory/blocks.js @@ -46,9 +46,9 @@ Blockly.Blocks['factory_base'] = { ['↑ top connection', 'TOP'], ['↓ bottom connection', 'BOTTOM']], function(option) { - this.sourceBlock_.updateShape_(option); + this.getSourceBlock().updateShape_(option); // Connect a shadow block to this new input. - this.sourceBlock_.spawnOutputShadow_(option); + this.getSourceBlock().spawnOutputShadow_(option); }); this.appendDummyInput() .appendField(dropdown, 'CONNECTIONS'); @@ -863,7 +863,7 @@ Blockly.Blocks['colour_hue'] = { // Update the current block's colour to match. var hue = parseInt(text, 10); if (!isNaN(hue)) { - this.sourceBlock_.setColour(hue); + this.getSourceBlock().setColour(hue); } }, mutationToDom: function(workspace) {