Files
blockly/blocks/variables_dynamic.js
Rachel Fenichel 6dc0f90959 chore: replace var with const and let in blocks directory (#5626)
* chore: use const and let in blocks/lists.js

* chore: use const and let in blocks/logic.js

* chore: use const and let in blocks/loops.js

* chore: use const and let in blocks/math.js

* chore: use const and let in blocks/procedures.js

* chore: use const and let in blocks/text.js

* chore: use const and let in blocks/variables_dynamic.js

* chore: use const and let in blocks/variables.js

* fix: updateShape_ variable scoping

* fix: declarations in switch clauses

* other: change while loops to for loops

* fix: fix violation of no-cond-assign
2021-10-21 14:25:37 -07:00

179 lines
6.1 KiB
JavaScript

/**
* @license
* Copyright 2017 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Variable blocks for Blockly.
* This file is scraped to extract a .json file of block definitions. The array
* passed to defineBlocksWithJsonArray(..) must be strict JSON: double quotes
* only, no outside references, no functions, no trailing commas, etc. The one
* exception is end-of-line comments, which the scraper will remove.
*/
'use strict';
goog.provide('Blockly.Constants.VariablesDynamic');
goog.require('Blockly');
goog.require('Blockly.FieldLabel');
goog.require('Blockly.FieldVariable');
/**
* Unused constant for the common HSV hue for all blocks in this category.
* @deprecated Use Blockly.Msg['VARIABLES_DYNAMIC_HUE']. (2018 April 5)
*/
Blockly.Constants.VariablesDynamic.HUE = 310;
Blockly.defineBlocksWithJsonArray([ // BEGIN JSON EXTRACT
// Block for variable getter.
{
"type": "variables_get_dynamic",
"message0": "%1",
"args0": [{
"type": "field_variable",
"name": "VAR",
"variable": "%{BKY_VARIABLES_DEFAULT_NAME}",
}],
"output": null,
"style": "variable_dynamic_blocks",
"helpUrl": "%{BKY_VARIABLES_GET_HELPURL}",
"tooltip": "%{BKY_VARIABLES_GET_TOOLTIP}",
"extensions": ["contextMenu_variableDynamicSetterGetter"],
},
// Block for variable setter.
{
"type": "variables_set_dynamic",
"message0": "%{BKY_VARIABLES_SET}",
"args0": [{
"type": "field_variable",
"name": "VAR",
"variable": "%{BKY_VARIABLES_DEFAULT_NAME}",
},
{
"type": "input_value",
"name": "VALUE",
},
],
"previousStatement": null,
"nextStatement": null,
"style": "variable_dynamic_blocks",
"tooltip": "%{BKY_VARIABLES_SET_TOOLTIP}",
"helpUrl": "%{BKY_VARIABLES_SET_HELPURL}",
"extensions": ["contextMenu_variableDynamicSetterGetter"],
},
]); // END JSON EXTRACT (Do not delete this comment.)
/**
* Mixin to add context menu items to create getter/setter blocks for this
* setter/getter.
* Used by blocks 'variables_set_dynamic' and 'variables_get_dynamic'.
* @mixin
* @augments Blockly.Block
* @package
* @readonly
*/
Blockly.Constants.VariablesDynamic.CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN = {
/**
* Add menu option to create getter/setter block for this setter/getter.
* @param {!Array} options List of menu options to add to.
* @this {Blockly.Block}
*/
customContextMenu: function(options) {
// Getter blocks have the option to create a setter block, and vice versa.
if (!this.isInFlyout) {
let opposite_type;
let contextMenuMsg;
const id = this.getFieldValue('VAR');
const variableModel = this.workspace.getVariableById(id);
const varType = variableModel.type;
if (this.type === 'variables_get_dynamic') {
opposite_type = 'variables_set_dynamic';
contextMenuMsg = Blockly.Msg['VARIABLES_GET_CREATE_SET'];
} else {
opposite_type = 'variables_get_dynamic';
contextMenuMsg = Blockly.Msg['VARIABLES_SET_CREATE_GET'];
}
const option = {enabled: this.workspace.remainingCapacity() > 0};
const name = this.getField('VAR').getText();
option.text = contextMenuMsg.replace('%1', name);
const xmlField = Blockly.utils.xml.createElement('field');
xmlField.setAttribute('name', 'VAR');
xmlField.setAttribute('variabletype', varType);
xmlField.appendChild(Blockly.utils.xml.createTextNode(name));
const xmlBlock = Blockly.utils.xml.createElement('block');
xmlBlock.setAttribute('type', opposite_type);
xmlBlock.appendChild(xmlField);
option.callback = Blockly.ContextMenu.callbackFactory(this, xmlBlock);
options.push(option);
} else {
if (this.type === 'variables_get_dynamic' ||
this.type === 'variables_get_reporter_dynamic') {
const renameOption = {
text: Blockly.Msg.RENAME_VARIABLE,
enabled: true,
callback: Blockly.Constants.Variables.RENAME_OPTION_CALLBACK_FACTORY(this),
};
const name = this.getField('VAR').getText();
const deleteOption = {
text: Blockly.Msg.DELETE_VARIABLE.replace('%1', name),
enabled: true,
callback: Blockly.Constants.Variables.DELETE_OPTION_CALLBACK_FACTORY(this),
};
options.unshift(renameOption);
options.unshift(deleteOption);
}
}
},
/**
* Called whenever anything on the workspace changes.
* Set the connection type for this block.
* @param {!Blockly.Events.Abstract} _e Change event.
* @this {Blockly.Block}
*/
onchange: function(_e) {
const id = this.getFieldValue('VAR');
const variableModel = Blockly.Variables.getVariable(this.workspace, id);
if (this.type === 'variables_get_dynamic') {
this.outputConnection.setCheck(variableModel.type);
} else {
this.getInput('VALUE').connection.setCheck(variableModel.type);
}
},
};
/**
* Callback for rename variable dropdown menu option associated with a
* variable getter block.
* @param {!Blockly.Block} block The block with the variable to rename.
* @return {!function()} A function that renames the variable.
*/
Blockly.Constants.VariablesDynamic.RENAME_OPTION_CALLBACK_FACTORY = function(block) {
return function() {
const workspace = block.workspace;
const variable = block.getField('VAR').getVariable();
Blockly.Variables.renameVariable(workspace, variable);
};
};
/**
* Callback for delete variable dropdown menu option associated with a
* variable getter block.
* @param {!Blockly.Block} block The block with the variable to delete.
* @return {!function()} A function that deletes the variable.
*/
Blockly.Constants.VariablesDynamic.DELETE_OPTION_CALLBACK_FACTORY = function(block) {
return function() {
const workspace = block.workspace;
const variable = block.getField('VAR').getVariable();
workspace.deleteVariableById(variable.getId());
workspace.refreshToolboxSelection();
};
};
Blockly.Extensions.registerMixin('contextMenu_variableDynamicSetterGetter',
Blockly.Constants.VariablesDynamic.CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN);