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');
|
2017-06-15 19:13:35 -07:00
|
|
|
goog.require('Blockly.ScratchBlocks.VerticalExtensions');
|
2016-07-11 11:00:33 -04:00
|
|
|
|
2017-06-19 16:31:13 -07: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",
|
2017-08-01 00:14:08 -04:00
|
|
|
"lastDummyAlign0": "CENTRE",
|
2016-07-11 11:00:33 -04:00
|
|
|
"args0": [
|
|
|
|
{
|
2017-02-22 13:49:39 -08:00
|
|
|
"type": "field_variable_getter",
|
|
|
|
"text": "",
|
2018-01-11 14:27:59 -05:00
|
|
|
"name": "VARIABLE",
|
|
|
|
"variableType": ""
|
2016-07-11 11:00:33 -04:00
|
|
|
}
|
|
|
|
],
|
2016-09-30 14:42:39 -06:00
|
|
|
"category": Blockly.Categories.data,
|
2017-02-22 13:49:39 -08:00
|
|
|
"checkboxInFlyout": true,
|
2017-06-19 16:31:13 -07:00
|
|
|
"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({
|
2018-04-23 16:11:05 -04:00
|
|
|
"message0": Blockly.Msg.DATA_SETVARIABLETO,
|
2016-07-11 11:00:33 -04:00
|
|
|
"args0": [
|
|
|
|
{
|
2017-06-14 16:30:07 -04:00
|
|
|
"type": "field_variable",
|
2016-07-11 11:00:33 -04:00
|
|
|
"name": "VARIABLE"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "input_value",
|
|
|
|
"name": "VALUE"
|
|
|
|
}
|
|
|
|
],
|
2016-09-30 14:42:39 -06:00
|
|
|
"category": Blockly.Categories.data,
|
2017-06-15 19:13:35 -07:00
|
|
|
"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({
|
2018-04-23 16:11:05 -04:00
|
|
|
"message0": Blockly.Msg.DATA_CHANGEVARIABLEBY,
|
2016-07-11 11:00:33 -04:00
|
|
|
"args0": [
|
|
|
|
{
|
2017-06-14 16:30:07 -04:00
|
|
|
"type": "field_variable",
|
2016-07-11 11:00:33 -04:00
|
|
|
"name": "VARIABLE"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "input_value",
|
|
|
|
"name": "VALUE"
|
|
|
|
}
|
|
|
|
],
|
2016-09-30 14:42:39 -06:00
|
|
|
"category": Blockly.Categories.data,
|
2017-06-15 19:13:35 -07:00
|
|
|
"extensions": ["colours_data", "shape_statement"]
|
2016-07-11 11:00:33 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-01 11:41:07 -05:00
|
|
|
Blockly.Blocks['data_showvariable'] = {
|
|
|
|
/**
|
|
|
|
* Block to show a variable
|
|
|
|
* @this Blockly.Block
|
|
|
|
*/
|
|
|
|
init: function() {
|
|
|
|
this.jsonInit({
|
2018-04-23 16:11:05 -04:00
|
|
|
"message0": Blockly.Msg.DATA_SHOWVARIABLE,
|
2017-12-01 11:41:07 -05:00
|
|
|
"args0": [
|
|
|
|
{
|
|
|
|
"type": "field_variable",
|
|
|
|
"name": "VARIABLE"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"previousStatement": null,
|
|
|
|
"nextStatement": null,
|
|
|
|
"category": Blockly.Categories.data,
|
2023-03-02 13:46:07 -05:00
|
|
|
"extensions": ["colours_data"]
|
2017-12-01 11:41:07 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Blockly.Blocks['data_hidevariable'] = {
|
|
|
|
/**
|
|
|
|
* Block to hide a variable
|
|
|
|
* @this Blockly.Block
|
|
|
|
*/
|
|
|
|
init: function() {
|
|
|
|
this.jsonInit({
|
2018-04-23 16:11:05 -04:00
|
|
|
"message0": Blockly.Msg.DATA_HIDEVARIABLE,
|
2017-12-01 11:41:07 -05:00
|
|
|
"args0": [
|
|
|
|
{
|
|
|
|
"type": "field_variable",
|
|
|
|
"name": "VARIABLE"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"previousStatement": null,
|
|
|
|
"nextStatement": null,
|
|
|
|
"category": Blockly.Categories.data,
|
2023-03-02 13:46:07 -05:00
|
|
|
"extensions": ["colours_data"]
|
2017-12-01 11:41:07 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2016-09-15 16:45:45 -04:00
|
|
|
|
2016-09-27 12:01:36 -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",
|
2018-01-11 14:27:59 -05:00
|
|
|
"variableType": Blockly.LIST_VARIABLE_TYPE
|
2016-09-15 16:45:45 -04:00
|
|
|
}
|
|
|
|
],
|
2017-12-16 22:30:25 -07:00
|
|
|
"category": Blockly.Categories.dataLists,
|
2018-10-24 12:28:57 -07:00
|
|
|
"extensions": ["contextMenu_getListBlock", "colours_data_lists", "output_string"],
|
2016-09-15 16:45:45 -04:00
|
|
|
"checkboxInFlyout": true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-05 17:14:33 -04:00
|
|
|
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"],
|
2018-05-13 13:34:33 -04:00
|
|
|
[Blockly.Msg.DATA_INDEX_LAST, "last"],
|
|
|
|
[Blockly.Msg.DATA_INDEX_ALL, "all"]
|
2016-10-05 17:14:33 -04:00
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"category": Blockly.Categories.data,
|
2017-06-22 14:42:03 -07:00
|
|
|
"extensions": ["colours_textfield", "output_string"]
|
2016-10-05 17:14:33 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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"],
|
2018-05-13 13:34:33 -04:00
|
|
|
[Blockly.Msg.DATA_INDEX_LAST, "last"],
|
|
|
|
[Blockly.Msg.DATA_INDEX_RANDOM, "random"]
|
2016-10-05 17:14:33 -04:00
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"category": Blockly.Categories.data,
|
2017-06-22 14:42:03 -07:00
|
|
|
"extensions": ["colours_textfield", "output_string"]
|
2016-10-05 17:14:33 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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({
|
2018-04-23 16:11:05 -04:00
|
|
|
"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",
|
2018-01-11 14:27:59 -05:00
|
|
|
"variableTypes": [Blockly.LIST_VARIABLE_TYPE]
|
2016-09-15 16:45:45 -04:00
|
|
|
}
|
|
|
|
],
|
2017-12-16 22:30:25 -07:00
|
|
|
"category": Blockly.Categories.dataLists,
|
2017-11-29 11:57:55 -05:00
|
|
|
"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({
|
2018-04-23 16:11:05 -04:00
|
|
|
"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",
|
2018-01-11 14:27:59 -05:00
|
|
|
"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
|
|
|
}
|
|
|
|
],
|
2017-12-16 22:30:25 -07:00
|
|
|
"category": Blockly.Categories.dataLists,
|
2017-11-29 11:57:55 -05:00
|
|
|
"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({
|
2018-04-23 16:11:05 -04:00
|
|
|
"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",
|
2018-01-11 14:27:59 -05:00
|
|
|
"variableTypes": [Blockly.LIST_VARIABLE_TYPE]
|
2016-09-15 16:45:45 -04:00
|
|
|
}
|
|
|
|
],
|
2017-12-16 22:30:25 -07:00
|
|
|
"category": Blockly.Categories.dataLists,
|
2017-11-29 11:57:55 -05:00
|
|
|
"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({
|
2018-04-23 16:11:05 -04:00
|
|
|
"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",
|
2018-01-11 14:27:59 -05:00
|
|
|
"variableTypes": [Blockly.LIST_VARIABLE_TYPE]
|
2016-09-15 16:45:45 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "input_value",
|
|
|
|
"name": "ITEM"
|
|
|
|
}
|
|
|
|
],
|
2017-12-16 22:30:25 -07:00
|
|
|
"category": Blockly.Categories.dataLists,
|
2017-11-29 11:57:55 -05:00
|
|
|
"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({
|
2018-04-23 16:11:05 -04:00
|
|
|
"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",
|
2018-01-11 14:27:59 -05:00
|
|
|
"variableTypes": [Blockly.LIST_VARIABLE_TYPE]
|
2016-09-15 16:45:45 -04:00
|
|
|
}
|
|
|
|
],
|
|
|
|
"output": null,
|
2017-12-16 22:30:25 -07:00
|
|
|
"category": Blockly.Categories.dataLists,
|
2017-11-29 11:57:55 -05:00
|
|
|
"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({
|
2018-09-12 19:40:48 -03:00
|
|
|
"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({
|
2018-04-23 16:11:05 -04:00
|
|
|
"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",
|
2018-01-11 14:27:59 -05:00
|
|
|
"variableTypes": [Blockly.LIST_VARIABLE_TYPE]
|
2016-09-15 16:45:45 -04:00
|
|
|
}
|
|
|
|
],
|
2017-12-16 22:30:25 -07:00
|
|
|
"category": Blockly.Categories.dataLists,
|
2017-11-29 11:57:55 -05:00
|
|
|
"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({
|
2018-04-23 16:11:05 -04:00
|
|
|
"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",
|
2018-01-11 14:27:59 -05:00
|
|
|
"variableTypes": [Blockly.LIST_VARIABLE_TYPE]
|
2016-09-15 16:45:45 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "input_value",
|
|
|
|
"name": "ITEM"
|
|
|
|
}
|
|
|
|
],
|
2017-12-16 22:30:25 -07:00
|
|
|
"category": Blockly.Categories.dataLists,
|
2017-11-29 11:57:55 -05:00
|
|
|
"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({
|
2018-04-23 16:11:05 -04:00
|
|
|
"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",
|
2018-01-11 14:27:59 -05:00
|
|
|
"variableTypes": [Blockly.LIST_VARIABLE_TYPE]
|
2016-09-15 16:45:45 -04:00
|
|
|
}
|
|
|
|
],
|
2017-12-16 22:30:25 -07:00
|
|
|
"category": Blockly.Categories.dataLists,
|
2017-11-29 11:57:55 -05:00
|
|
|
"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({
|
2018-04-23 16:11:05 -04:00
|
|
|
"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",
|
2018-01-11 14:27:59 -05:00
|
|
|
"variableTypes": [Blockly.LIST_VARIABLE_TYPE]
|
2016-09-15 16:45:45 -04:00
|
|
|
}
|
|
|
|
],
|
2017-12-16 22:30:25 -07:00
|
|
|
"category": Blockly.Categories.dataLists,
|
2017-11-29 11:57:55 -05:00
|
|
|
"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 = {
|
|
|
|
/**
|
2018-10-24 12:28:57 -07:00
|
|
|
* 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) {
|
2018-10-24 12:28:57 -07:00
|
|
|
var fieldName = 'VARIABLE';
|
2017-06-29 09:57:59 -07:00
|
|
|
if (this.isCollapsed()) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-12 14:01:03 -05:00
|
|
|
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++) {
|
2018-12-12 14:01:03 -05:00
|
|
|
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};
|
2018-12-12 14:01:03 -05:00
|
|
|
option.text = varName;
|
2017-02-22 13:49:39 -08:00
|
|
|
|
|
|
|
option.callback =
|
|
|
|
Blockly.Constants.Data.VARIABLE_OPTION_CALLBACK_FACTORY(this,
|
2018-12-04 15:54:51 -05:00
|
|
|
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,
|
2018-10-24 12:28:57 -07:00
|
|
|
callback: Blockly.Constants.Data.RENAME_OPTION_CALLBACK_FACTORY(this,
|
|
|
|
fieldName)
|
2017-06-29 09:57:59 -07:00
|
|
|
};
|
|
|
|
var deleteOption = {
|
2018-12-12 14:01:03 -05:00
|
|
|
text: Blockly.Msg.DELETE_VARIABLE.replace('%1', currentVarName),
|
2017-06-29 09:57:59 -07:00
|
|
|
enabled: true,
|
2018-10-24 12:28:57 -07:00
|
|
|
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
|
|
|
|
2018-10-24 12:28:57 -07: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;
|
|
|
|
}
|
2018-12-12 14:01:03 -05:00
|
|
|
var currentVarName = this.getField(fieldName).text_;
|
2018-10-24 12:28:57 -07:00
|
|
|
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);
|
|
|
|
});
|
2018-10-24 12:28:57 -07:00
|
|
|
for (var i = 0; i < variablesList.length; i++) {
|
2018-12-12 14:01:03 -05:00
|
|
|
var varName = variablesList[i].name;
|
|
|
|
if (varName == currentVarName) continue;
|
|
|
|
|
2018-10-24 12:28:57 -07:00
|
|
|
var option = {enabled: true};
|
2018-12-12 14:01:03 -05:00
|
|
|
option.text = varName;
|
2018-10-24 12:28:57 -07:00
|
|
|
|
|
|
|
option.callback =
|
|
|
|
Blockly.Constants.Data.VARIABLE_OPTION_CALLBACK_FACTORY(this,
|
2018-12-04 15:54:51 -05:00
|
|
|
variablesList[i].getId(), fieldName);
|
2018-10-24 12:28:57 -07:00
|
|
|
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 = {
|
2018-12-12 14:01:03 -05:00
|
|
|
text: Blockly.Msg.DELETE_LIST.replace('%1', currentVarName),
|
2018-10-24 12:28:57 -07:00
|
|
|
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.
|
2018-12-04 15:54:51 -05:00
|
|
|
* @param {string} id The id of the variable to set on this block.
|
2018-10-24 12:28:57 -07:00
|
|
|
* @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.
|
|
|
|
*/
|
2018-10-24 12:28:57 -07:00
|
|
|
Blockly.Constants.Data.VARIABLE_OPTION_CALLBACK_FACTORY = function(block,
|
2018-12-04 15:54:51 -05:00
|
|
|
id, fieldName) {
|
2017-02-22 13:49:39 -08:00
|
|
|
return function() {
|
2018-10-24 12:28:57 -07:00
|
|
|
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.");
|
|
|
|
}
|
2018-12-04 15:54:51 -05:00
|
|
|
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.
|
2018-10-24 12:28:57 -07:00
|
|
|
* @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.
|
|
|
|
*/
|
2018-10-24 12:28:57 -07:00
|
|
|
Blockly.Constants.Data.RENAME_OPTION_CALLBACK_FACTORY = function(block,
|
|
|
|
fieldName) {
|
2017-06-29 09:57:59 -07:00
|
|
|
return function() {
|
|
|
|
var workspace = block.workspace;
|
2018-10-24 12:28:57 -07:00
|
|
|
var variable = block.getField(fieldName).getVariable();
|
2017-11-02 10:41:59 -04:00
|
|
|
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.
|
2018-10-24 12:28:57 -07:00
|
|
|
* @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.
|
|
|
|
*/
|
2018-10-24 12:28:57 -07:00
|
|
|
Blockly.Constants.Data.DELETE_OPTION_CALLBACK_FACTORY = function(block,
|
|
|
|
fieldName) {
|
2017-06-29 09:57:59 -07:00
|
|
|
return function() {
|
|
|
|
var workspace = block.workspace;
|
2018-10-24 12:28:57 -07:00
|
|
|
var variable = block.getField(fieldName).getVariable();
|
2018-01-12 12:21:46 -05:00
|
|
|
workspace.deleteVariableById(variable.getId());
|
2017-06-29 09:57:59 -07:00
|
|
|
};
|
|
|
|
};
|