JSONify variable blocks

This commit is contained in:
Rachel Fenichel
2017-02-10 17:05:02 -08:00
parent 3af6238457
commit 74ff24e323

View File

@@ -20,81 +20,108 @@
/** /**
* @fileoverview Variable blocks for Blockly. * @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.
* @author fraser@google.com (Neil Fraser) * @author fraser@google.com (Neil Fraser)
*/ */
'use strict'; 'use strict';
goog.provide('Blockly.Blocks.variables'); goog.provide('Blockly.Blocks.variables'); // Deprecated.
goog.provide('Blockly.Constants.Variables');
goog.require('Blockly.Blocks'); goog.require('Blockly.Blocks');
/** /**
* Common HSV hue for all blocks in this category. * Common HSV hue for all blocks in this category.
* Should be the same as Blockly.Msg.VARIABLES_HUE.
* @readonly
*/ */
Blockly.Blocks.variables.HUE = 330; Blockly.Constants.Variables.HUE = 330;
/** @deprecated Use Blockly.Constants.Variables.HUE */
Blockly.Blocks.variables.HUE = Blockly.Constants.Variables.HUE;
Blockly.Blocks['variables_get'] = { Blockly.defineBlocksWithJsonArray([ // BEGIN JSON EXTRACT
/** // Block for variable getter.
* Block for variable getter. {
* @this Blockly.Block "type": "variables_get",
*/ "message0": "%1",
init: function() { "args0": [
this.setHelpUrl(Blockly.Msg.VARIABLES_GET_HELPURL); {
this.setColour(Blockly.Blocks.variables.HUE); "type": "field_variable",
this.appendDummyInput() "name": "VAR",
.appendField(new Blockly.FieldVariable( "variable": "%{BKY_VARIABLES_DEFAULT_NAME}"
Blockly.Msg.VARIABLES_DEFAULT_NAME), 'VAR'); }
this.setOutput(true); ],
this.setTooltip(Blockly.Msg.VARIABLES_GET_TOOLTIP); "output": null,
this.contextMenuMsg_ = Blockly.Msg.VARIABLES_GET_CREATE_SET; "colour": "%{BKY_VARIABLES_HUE}",
"helpUrl": "%{BKY_VARIABLES_GET_HELPURL}",
"tooltip": "%{BKY_VARIABLES_GET_TOOLTIP}",
"extensions": ["contextMenu_variableSetterGetter"]
}, },
contextMenuType_: 'variables_set', // Block for variable setter.
{
"type": "variables_set",
"message0": "%{BKY_VARIABLES_SET}",
"args0": [
{
"type": "field_variable",
"name": "VAR",
"variable": "%{BKY_VARIABLES_DEFAULT_NAME}"
},
{
"type": "input_value",
"name": "VALUE"
}
],
"previousStatement": null,
"nextStatement": null,
"colour": "%{BKY_VARIABLES_HUE}",
"tooltip": "%{BKY_VARIABLES_SET_TOOLTIP}",
"helpUrl": "%{BKY_VARIABLES_SET_HELPURL}",
"extensions": ["contextMenu_variableSetterGetter"]
}
]); // 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' and 'variables_get'.
* @mixin
* @augments Blockly.Block
* @package
* @readonly
*/
Blockly.Constants.Variables.CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN = {
/** /**
* Add menu option to create getter/setter block for this setter/getter. * Add menu option to create getter/setter block for this setter/getter.
* @param {!Array} options List of menu options to add to. * @param {!Array} options List of menu options to add to.
* @this Blockly.Block * @this Blockly.Block
*/ */
customContextMenu: function(options) { customContextMenu: function(options) {
// Getter blocks have the option to create a setter block, and vice versa.
if (this.type == 'variables_get') {
var opposite_type = 'variables_set';
var contextMenuMsg = Blockly.Msg.VARIABLES_GET_CREATE_SET;
} else {
var opposite_type = 'variables_get';
var contextMenuMsg = Blockly.Msg.VARIABLES_SET_CREATE_GET;
}
var option = {enabled: true}; var option = {enabled: true};
var name = this.getFieldValue('VAR'); var name = this.getFieldValue('VAR');
option.text = this.contextMenuMsg_.replace('%1', name); option.text = contextMenuMsg.replace('%1', name);
var xmlField = goog.dom.createDom('field', null, name); var xmlField = goog.dom.createDom('field', null, name);
xmlField.setAttribute('name', 'VAR'); xmlField.setAttribute('name', 'VAR');
var xmlBlock = goog.dom.createDom('block', null, xmlField); var xmlBlock = goog.dom.createDom('block', null, xmlField);
xmlBlock.setAttribute('type', this.contextMenuType_); xmlBlock.setAttribute('type', opposite_type);
option.callback = Blockly.ContextMenu.callbackFactory(this, xmlBlock); option.callback = Blockly.ContextMenu.callbackFactory(this, xmlBlock);
options.push(option); options.push(option);
} }
}; };
Blockly.Blocks['variables_set'] = { Blockly.Extensions.registerMixin('contextMenu_variableSetterGetter',
/** Blockly.Constants.Variables.CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN);
* Block for variable setter.
* @this Blockly.Block
*/
init: function() {
this.jsonInit({
"message0": Blockly.Msg.VARIABLES_SET,
"args0": [
{
"type": "field_variable",
"name": "VAR",
"variable": Blockly.Msg.VARIABLES_DEFAULT_NAME
},
{
"type": "input_value",
"name": "VALUE"
}
],
"previousStatement": null,
"nextStatement": null,
"colour": Blockly.Blocks.variables.HUE,
"tooltip": Blockly.Msg.VARIABLES_SET_TOOLTIP,
"helpUrl": Blockly.Msg.VARIABLES_SET_HELPURL
});
this.contextMenuMsg_ = Blockly.Msg.VARIABLES_SET_CREATE_GET;
},
contextMenuType_: 'variables_get',
customContextMenu: Blockly.Blocks['variables_get'].customContextMenu
};