scratch-blocks/blocks_vertical/data.js

667 lines
17 KiB
JavaScript
Raw Permalink Normal View History

2016-07-11 11:00:33 -04:00
/**
* @license
* Visual Blocks Editor
*
* Copyright 2016 Massachusetts Institute of Technology
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
goog.provide('Blockly.Blocks.data');
2017-02-22 13:49:39 -08:00
goog.provide('Blockly.Constants.Data');
2016-07-11 11:00:33 -04:00
goog.require('Blockly.Blocks');
goog.require('Blockly.Colours');
goog.require('Blockly.constants');
goog.require('Blockly.ScratchBlocks.VerticalExtensions');
2016-07-11 11:00:33 -04:00
2016-07-11 11:00:33 -04:00
Blockly.Blocks['data_variable'] = {
/**
* Block of Variables
* @this Blockly.Block
*/
init: function() {
this.jsonInit({
"message0": "%1",
"lastDummyAlign0": "CENTRE",
2016-07-11 11:00:33 -04:00
"args0": [
{
2017-02-22 13:49:39 -08:00
"type": "field_variable_getter",
"text": "",
"name": "VARIABLE",
"variableType": ""
2016-07-11 11:00:33 -04:00
}
],
"category": Blockly.Categories.data,
2017-02-22 13:49:39 -08:00
"checkboxInFlyout": true,
"extensions": ["contextMenu_getVariableBlock", "colours_data", "output_string"]
2016-07-11 11:00:33 -04:00
});
}
};
Blockly.Blocks['data_setvariableto'] = {
/**
* Block to set variable to a certain value
* @this Blockly.Block
*/
init: function() {
this.jsonInit({
"message0": Blockly.Msg.DATA_SETVARIABLETO,
2016-07-11 11:00:33 -04:00
"args0": [
{
"type": "field_variable",
2016-07-11 11:00:33 -04:00
"name": "VARIABLE"
},
{
"type": "input_value",
"name": "VALUE"
}
],
"category": Blockly.Categories.data,
"extensions": ["colours_data", "shape_statement"]
2016-07-11 11:00:33 -04:00
});
}
};
Blockly.Blocks['data_changevariableby'] = {
/**
* Block to change variable by a certain value
* @this Blockly.Block
*/
init: function() {
this.jsonInit({
"message0": Blockly.Msg.DATA_CHANGEVARIABLEBY,
2016-07-11 11:00:33 -04:00
"args0": [
{
"type": "field_variable",
2016-07-11 11:00:33 -04:00
"name": "VARIABLE"
},
{
"type": "input_value",
"name": "VALUE"
}
],
"category": Blockly.Categories.data,
"extensions": ["colours_data", "shape_statement"]
2016-07-11 11:00:33 -04:00
});
}
};
Blockly.Blocks['data_showvariable'] = {
/**
* Block to show a variable
* @this Blockly.Block
*/
init: function() {
this.jsonInit({
"message0": Blockly.Msg.DATA_SHOWVARIABLE,
"args0": [
{
"type": "field_variable",
"name": "VARIABLE"
}
],
"previousStatement": null,
"nextStatement": null,
"category": Blockly.Categories.data,
"extensions": ["colours_data"]
});
}
};
Blockly.Blocks['data_hidevariable'] = {
/**
* Block to hide a variable
* @this Blockly.Block
*/
init: function() {
this.jsonInit({
"message0": Blockly.Msg.DATA_HIDEVARIABLE,
"args0": [
{
"type": "field_variable",
"name": "VARIABLE"
}
],
"previousStatement": null,
"nextStatement": null,
"category": Blockly.Categories.data,
"extensions": ["colours_data"]
});
}
};
2016-09-15 16:45:45 -04:00
Blockly.Blocks['data_listcontents'] = {
2016-09-15 16:45:45 -04:00
/**
* List reporter.
* @this Blockly.Block
*/
init: function() {
this.jsonInit({
"message0": "%1",
"args0": [
{
2017-02-22 13:49:39 -08:00
"type": "field_variable_getter",
"text": "",
2017-07-06 18:00:33 -07:00
"name": "LIST",
"variableType": Blockly.LIST_VARIABLE_TYPE
2016-09-15 16:45:45 -04:00
}
],
"category": Blockly.Categories.dataLists,
"extensions": ["contextMenu_getListBlock", "colours_data_lists", "output_string"],
2016-09-15 16:45:45 -04:00
"checkboxInFlyout": true
});
}
};
Blockly.Blocks['data_listindexall'] = {
/**
* List index menu, with all option.
* @this Blockly.Block
*/
init: function() {
this.jsonInit({
"message0": "%1",
"args0": [
{
"type": "field_numberdropdown",
"name": "INDEX",
"value": "1",
"min": 1,
"precision": 1,
"options": [
["1", "1"],
[Blockly.Msg.DATA_INDEX_LAST, "last"],
[Blockly.Msg.DATA_INDEX_ALL, "all"]
]
}
],
"category": Blockly.Categories.data,
2017-06-22 14:42:03 -07:00
"extensions": ["colours_textfield", "output_string"]
});
}
};
Blockly.Blocks['data_listindexrandom'] = {
/**
* List index menu, with random option.
* @this Blockly.Block
*/
init: function() {
this.jsonInit({
"message0": "%1",
"args0": [
{
"type": "field_numberdropdown",
"name": "INDEX",
"value": "1",
"min": 1,
"precision": 1,
"options": [
["1", "1"],
[Blockly.Msg.DATA_INDEX_LAST, "last"],
[Blockly.Msg.DATA_INDEX_RANDOM, "random"]
]
}
],
"category": Blockly.Categories.data,
2017-06-22 14:42:03 -07:00
"extensions": ["colours_textfield", "output_string"]
});
}
};
2016-09-15 16:45:45 -04:00
Blockly.Blocks['data_addtolist'] = {
/**
* Block to add item to list.
* @this Blockly.Block
*/
init: function() {
this.jsonInit({
"message0": Blockly.Msg.DATA_ADDTOLIST,
2016-09-15 16:45:45 -04:00
"args0": [
{
"type": "input_value",
"name": "ITEM"
},
{
"type": "field_variable",
2017-07-06 18:00:33 -07:00
"name": "LIST",
"variableTypes": [Blockly.LIST_VARIABLE_TYPE]
2016-09-15 16:45:45 -04:00
}
],
"category": Blockly.Categories.dataLists,
"extensions": ["colours_data_lists", "shape_statement"]
2016-09-15 16:45:45 -04:00
});
}
};
Blockly.Blocks['data_deleteoflist'] = {
/**
* Block to delete item from list.
* @this Blockly.Block
*/
init: function() {
this.jsonInit({
"message0": Blockly.Msg.DATA_DELETEOFLIST,
2016-09-15 16:45:45 -04:00
"args0": [
{
"type": "input_value",
"name": "INDEX"
},
{
"type": "field_variable",
2017-07-06 18:00:33 -07:00
"name": "LIST",
"variableTypes": [Blockly.LIST_VARIABLE_TYPE]
2018-07-11 13:15:00 -04:00
}
],
"category": Blockly.Categories.dataLists,
"extensions": ["colours_data_lists", "shape_statement"]
});
}
};
Blockly.Blocks['data_deletealloflist'] = {
/**
* Block to delete all items from list.
* @this Blockly.Block
*/
init: function() {
this.jsonInit({
"message0": Blockly.Msg.DATA_DELETEALLOFLIST,
"args0": [
{
"type": "field_variable",
"name": "LIST",
"variableTypes": [Blockly.LIST_VARIABLE_TYPE]
2016-09-15 16:45:45 -04:00
}
],
"category": Blockly.Categories.dataLists,
"extensions": ["colours_data_lists", "shape_statement"]
2016-09-15 16:45:45 -04:00
});
}
};
Blockly.Blocks['data_insertatlist'] = {
/**
* Block to insert item to list.
* @this Blockly.Block
*/
init: function() {
this.jsonInit({
"message0": Blockly.Msg.DATA_INSERTATLIST,
2016-09-15 16:45:45 -04:00
"args0": [
{
"type": "input_value",
"name": "ITEM"
},
{
"type": "input_value",
"name": "INDEX"
},
{
"type": "field_variable",
2017-07-06 18:00:33 -07:00
"name": "LIST",
"variableTypes": [Blockly.LIST_VARIABLE_TYPE]
2016-09-15 16:45:45 -04:00
}
],
"category": Blockly.Categories.dataLists,
"extensions": ["colours_data_lists", "shape_statement"]
2016-09-15 16:45:45 -04:00
});
}
};
Blockly.Blocks['data_replaceitemoflist'] = {
/**
* Block to insert item to list.
* @this Blockly.Block
*/
init: function() {
this.jsonInit({
"message0": Blockly.Msg.DATA_REPLACEITEMOFLIST,
2016-09-15 16:45:45 -04:00
"args0": [
{
"type": "input_value",
"name": "INDEX"
},
{
"type": "field_variable",
2017-07-06 18:00:33 -07:00
"name": "LIST",
"variableTypes": [Blockly.LIST_VARIABLE_TYPE]
2016-09-15 16:45:45 -04:00
},
{
"type": "input_value",
"name": "ITEM"
}
],
"category": Blockly.Categories.dataLists,
"extensions": ["colours_data_lists", "shape_statement"]
2016-09-15 16:45:45 -04:00
});
}
};
Blockly.Blocks['data_itemoflist'] = {
/**
* Block for reporting item of list.
* @this Blockly.Block
*/
init: function() {
this.jsonInit({
"message0": Blockly.Msg.DATA_ITEMOFLIST,
2016-09-15 16:45:45 -04:00
"args0": [
{
"type": "input_value",
"name": "INDEX"
},
{
"type": "field_variable",
2017-07-06 18:00:33 -07:00
"name": "LIST",
"variableTypes": [Blockly.LIST_VARIABLE_TYPE]
2016-09-15 16:45:45 -04:00
}
],
"output": null,
"category": Blockly.Categories.dataLists,
"extensions": ["colours_data_lists"],
2016-09-15 16:45:45 -04:00
"outputShape": Blockly.OUTPUT_SHAPE_ROUND
});
}
};
2018-03-31 10:35:42 -03:00
Blockly.Blocks['data_itemnumoflist'] = {
/**
* Block for reporting the item # of a string in a list.
* @this Blockly.Block
*/
init: function() {
this.jsonInit({
"message0": Blockly.Msg.DATA_ITEMNUMOFLIST,
2018-03-31 10:35:42 -03:00
"args0": [
{
"type": "input_value",
"name": "ITEM"
},
{
"type": "field_variable",
"name": "LIST",
"variableTypes": [Blockly.LIST_VARIABLE_TYPE]
}
],
"output": null,
"category": Blockly.Categories.dataLists,
"extensions": ["colours_data_lists"],
"outputShape": Blockly.OUTPUT_SHAPE_ROUND
});
}
};
2016-09-15 16:45:45 -04:00
Blockly.Blocks['data_lengthoflist'] = {
/**
* Block for reporting length of list.
* @this Blockly.Block
*/
init: function() {
this.jsonInit({
"message0": Blockly.Msg.DATA_LENGTHOFLIST,
2016-09-15 16:45:45 -04:00
"args0": [
{
"type": "field_variable",
2017-07-06 18:00:33 -07:00
"name": "LIST",
"variableTypes": [Blockly.LIST_VARIABLE_TYPE]
2016-09-15 16:45:45 -04:00
}
],
"category": Blockly.Categories.dataLists,
"extensions": ["colours_data_lists", "output_number"]
2016-09-15 16:45:45 -04:00
});
}
};
Blockly.Blocks['data_listcontainsitem'] = {
/**
* Block to report whether list contains item.
* @this Blockly.Block
*/
init: function() {
this.jsonInit({
"message0": Blockly.Msg.DATA_LISTCONTAINSITEM,
2016-09-15 16:45:45 -04:00
"args0": [
{
"type": "field_variable",
2017-07-06 18:00:33 -07:00
"name": "LIST",
"variableTypes": [Blockly.LIST_VARIABLE_TYPE]
2016-09-15 16:45:45 -04:00
},
{
"type": "input_value",
"name": "ITEM"
}
],
"category": Blockly.Categories.dataLists,
"extensions": ["colours_data_lists", "output_boolean"]
2016-09-15 16:45:45 -04:00
});
}
};
Blockly.Blocks['data_showlist'] = {
/**
* Block to show a list.
* @this Blockly.Block
*/
init: function() {
this.jsonInit({
"message0": Blockly.Msg.DATA_SHOWLIST,
2016-09-15 16:45:45 -04:00
"args0": [
{
"type": "field_variable",
2017-07-06 18:00:33 -07:00
"name": "LIST",
"variableTypes": [Blockly.LIST_VARIABLE_TYPE]
2016-09-15 16:45:45 -04:00
}
],
"category": Blockly.Categories.dataLists,
"extensions": ["colours_data_lists", "shape_statement"]
2016-09-15 16:45:45 -04:00
});
}
};
Blockly.Blocks['data_hidelist'] = {
/**
* Block to hide a list.
* @this Blockly.Block
*/
init: function() {
this.jsonInit({
"message0": Blockly.Msg.DATA_HIDELIST,
2016-09-15 16:45:45 -04:00
"args0": [
{
"type": "field_variable",
2017-07-06 18:00:33 -07:00
"name": "LIST",
"variableTypes": [Blockly.LIST_VARIABLE_TYPE]
2016-09-15 16:45:45 -04:00
}
],
"category": Blockly.Categories.dataLists,
"extensions": ["colours_data_lists", "shape_statement"]
2016-09-15 16:45:45 -04:00
});
}
};
2017-02-22 13:49:39 -08:00
/**
* Mixin to add a context menu for a data_variable block. It adds one item for
* each variable defined on the workspace.
* @mixin
* @augments Blockly.Block
* @package
* @readonly
*/
Blockly.Constants.Data.CUSTOM_CONTEXT_MENU_GET_VARIABLE_MIXIN = {
/**
* Add context menu option to change the selected variable.
2017-02-22 13:49:39 -08:00
* @param {!Array} options List of menu options to add to.
* @this Blockly.Block
*/
customContextMenu: function(options) {
var fieldName = 'VARIABLE';
2017-06-29 09:57:59 -07:00
if (this.isCollapsed()) {
return;
}
var currentVarName = this.getField(fieldName).text_;
2017-06-29 09:57:59 -07:00
if (!this.isInFlyout) {
2017-06-19 12:38:54 -07:00
var variablesList = this.workspace.getVariablesOfType('');
2019-02-04 16:19:52 -04:00
variablesList.sort(function(a, b) {
return Blockly.scratchBlocksUtils.compareStrings(a.name, b.name);
});
2017-02-22 13:49:39 -08:00
for (var i = 0; i < variablesList.length; i++) {
var varName = variablesList[i].name;
if (varName == currentVarName) continue;
2019-04-23 18:22:49 -03:00
2017-02-22 13:49:39 -08:00
var option = {enabled: true};
option.text = varName;
2017-02-22 13:49:39 -08:00
option.callback =
Blockly.Constants.Data.VARIABLE_OPTION_CALLBACK_FACTORY(this,
variablesList[i].getId(), fieldName);
2017-02-22 13:49:39 -08:00
options.push(option);
}
2017-06-29 09:57:59 -07:00
} else {
var renameOption = {
text: Blockly.Msg.RENAME_VARIABLE,
enabled: true,
callback: Blockly.Constants.Data.RENAME_OPTION_CALLBACK_FACTORY(this,
fieldName)
2017-06-29 09:57:59 -07:00
};
var deleteOption = {
text: Blockly.Msg.DELETE_VARIABLE.replace('%1', currentVarName),
2017-06-29 09:57:59 -07:00
enabled: true,
callback: Blockly.Constants.Data.DELETE_OPTION_CALLBACK_FACTORY(this,
fieldName)
2017-06-29 09:57:59 -07:00
};
options.push(renameOption);
options.push(deleteOption);
2017-02-22 13:49:39 -08:00
}
}
};
Blockly.Extensions.registerMixin('contextMenu_getVariableBlock',
2018-05-02 15:58:36 -07:00
Blockly.Constants.Data.CUSTOM_CONTEXT_MENU_GET_VARIABLE_MIXIN);
2017-02-22 13:49:39 -08:00
/**
* Mixin to add a context menu for a data_listcontents block. It adds one item for
* each list defined on the workspace.
* @mixin
* @augments Blockly.Block
* @package
* @readonly
*/
Blockly.Constants.Data.CUSTOM_CONTEXT_MENU_GET_LIST_MIXIN = {
/**
* Add context menu option to change the selected list.
* @param {!Array} options List of menu options to add to.
* @this Blockly.Block
*/
customContextMenu: function(options) {
var fieldName = 'LIST';
if (this.isCollapsed()) {
return;
}
var currentVarName = this.getField(fieldName).text_;
if (!this.isInFlyout) {
var variablesList = this.workspace.getVariablesOfType('list');
2019-04-23 18:22:49 -03:00
variablesList.sort(function(a, b) {
return Blockly.scratchBlocksUtils.compareStrings(a.name, b.name);
});
for (var i = 0; i < variablesList.length; i++) {
var varName = variablesList[i].name;
if (varName == currentVarName) continue;
var option = {enabled: true};
option.text = varName;
option.callback =
Blockly.Constants.Data.VARIABLE_OPTION_CALLBACK_FACTORY(this,
variablesList[i].getId(), fieldName);
options.push(option);
}
} else {
var renameOption = {
text: Blockly.Msg.RENAME_LIST,
enabled: true,
callback: Blockly.Constants.Data.RENAME_OPTION_CALLBACK_FACTORY(this,
fieldName)
};
var deleteOption = {
text: Blockly.Msg.DELETE_LIST.replace('%1', currentVarName),
enabled: true,
callback: Blockly.Constants.Data.DELETE_OPTION_CALLBACK_FACTORY(this,
fieldName)
};
options.push(renameOption);
options.push(deleteOption);
}
}
};
Blockly.Extensions.registerMixin('contextMenu_getListBlock',
Blockly.Constants.Data.CUSTOM_CONTEXT_MENU_GET_LIST_MIXIN);
2017-02-22 13:49:39 -08:00
/**
* Callback factory for dropdown menu options associated with a variable getter
* block. Each variable on the workspace gets its own item in the dropdown
* menu, and clicking on that item changes the text of the field on the source
* block.
* @param {!Blockly.Block} block The block to update.
* @param {string} id The id of the variable to set on this block.
* @param {string} fieldName The name of the field to update on the block.
2017-02-22 13:49:39 -08:00
* @return {!function()} A function that updates the block with the new name.
*/
Blockly.Constants.Data.VARIABLE_OPTION_CALLBACK_FACTORY = function(block,
id, fieldName) {
2017-02-22 13:49:39 -08:00
return function() {
var variableField = block.getField(fieldName);
2017-02-22 13:49:39 -08:00
if (!variableField) {
console.log("Tried to get a variable field on the wrong type of block.");
}
variableField.setValue(id);
2017-02-22 13:49:39 -08:00
};
};
2017-06-29 09:57:59 -07:00
/**
* Callback for rename variable dropdown menu option associated with a
* variable getter block.
* @param {!Blockly.Block} block The block with the variable to rename.
* @param {string} fieldName The name of the field to inspect on the block.
2017-06-29 09:57:59 -07:00
* @return {!function()} A function that renames the variable.
*/
Blockly.Constants.Data.RENAME_OPTION_CALLBACK_FACTORY = function(block,
fieldName) {
2017-06-29 09:57:59 -07:00
return function() {
var workspace = block.workspace;
var variable = block.getField(fieldName).getVariable();
Blockly.Variables.renameVariable(workspace, variable);
2017-06-29 09:57:59 -07:00
};
};
/**
* Callback for delete variable dropdown menu option associated with a
* variable getter block.
* @param {!Blockly.Block} block The block with the variable to delete.
* @param {string} fieldName The name of the field to inspect on the block.
2017-06-29 09:57:59 -07:00
* @return {!function()} A function that deletes the variable.
*/
Blockly.Constants.Data.DELETE_OPTION_CALLBACK_FACTORY = function(block,
fieldName) {
2017-06-29 09:57:59 -07:00
return function() {
var workspace = block.workspace;
var variable = block.getField(fieldName).getVariable();
workspace.deleteVariableById(variable.getId());
2017-06-29 09:57:59 -07:00
};
};