123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- 'use strict';
- goog.provide('Blockly.Blocks.loops');
- goog.require('Blockly.Blocks');
- Blockly.Blocks.loops.HUE = "#9d64fd";
- Blockly.Blocks['controls_repeat_ext'] = {
-
- init: function() {
- this.jsonInit({
- "message0": Blockly.Msg.CONTROLS_REPEAT_TITLE,
- "args0": [
- {
- "type": "input_value",
- "name": "TIMES",
- "check": "Number"
- }
- ],
- "previousStatement": null,
- "nextStatement": null,
- "colour": Blockly.Blocks.loops.HUE,
- "tooltip": Blockly.Msg.CONTROLS_REPEAT_TOOLTIP,
- "helpUrl": Blockly.Msg.CONTROLS_REPEAT_HELPURL
- });
- this.appendStatementInput('DO')
- .appendField(Blockly.Msg.CONTROLS_REPEAT_INPUT_DO);
- }
- };
- Blockly.Blocks['controls_repeat'] = {
-
- init: function() {
- this.jsonInit({
- "message0": Blockly.Msg.CONTROLS_REPEAT_TITLE,
- "args0": [
- {
- "type": "field_number",
- "name": "TIMES",
- "value": 10,
- "min": 0,
- "precision": 1
- }
- ],
- "previousStatement": null,
- "nextStatement": null,
- "colour": Blockly.Blocks.loops.HUE,
- "tooltip": Blockly.Msg.CONTROLS_REPEAT_TOOLTIP,
- "helpUrl": Blockly.Msg.CONTROLS_REPEAT_HELPURL
- });
- this.appendStatementInput('DO')
- .appendField(Blockly.Msg.CONTROLS_REPEAT_INPUT_DO);
- }
- };
- Blockly.Blocks['controls_whileUntil'] = {
-
- init: function() {
- var OPERATORS =
- [[Blockly.Msg.CONTROLS_WHILEUNTIL_OPERATOR_WHILE, 'WHILE'],
- [Blockly.Msg.CONTROLS_WHILEUNTIL_OPERATOR_UNTIL, 'UNTIL']];
- this.setHelpUrl(Blockly.Msg.CONTROLS_WHILEUNTIL_HELPURL);
- this.setColour(Blockly.Blocks.loops.HUE);
- this.appendValueInput('BOOL')
- .setCheck('Boolean')
- .appendField(new Blockly.FieldDropdown(OPERATORS), 'MODE');
- this.appendStatementInput('DO')
- .appendField(Blockly.Msg.CONTROLS_WHILEUNTIL_INPUT_DO);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
-
- var thisBlock = this;
- this.setTooltip(function() {
- var op = thisBlock.getFieldValue('MODE');
- var TOOLTIPS = {
- 'WHILE': Blockly.Msg.CONTROLS_WHILEUNTIL_TOOLTIP_WHILE,
- 'UNTIL': Blockly.Msg.CONTROLS_WHILEUNTIL_TOOLTIP_UNTIL
- };
- return TOOLTIPS[op];
- });
- }
- };
- Blockly.Blocks['controls_while'] = {
-
- init: function() {
- this.setHelpUrl(Blockly.Msg.CONTROLS_WHILEUNTIL_HELPURL);
- this.setColour(Blockly.Blocks.loops.HUE);
- this.appendValueInput('BOOL')
- .setCheck('Boolean')
- .appendField(Blockly.Msg.CONTROLS_WHILEUNTIL_OPERATOR_WHILE);
- this.appendStatementInput('DO')
- .appendField(Blockly.Msg.CONTROLS_WHILEUNTIL_INPUT_DO);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setTooltip(Blockly.Msg.CONTROLS_WHILEUNTIL_TOOLTIP_WHILE);
- }
- };
- Blockly.Blocks['controls_for'] = {
-
- init: function() {
- this.jsonInit({
- "message0": Blockly.Msg.CONTROLS_FOR_TITLE,
- "args0": [
- {
- "type": "field_variable",
- "name": "VAR",
- "variable": null
- },
- {
- "type": "input_value",
- "name": "FROM",
- "check": "Number",
- "align": "RIGHT"
- },
- {
- "type": "input_value",
- "name": "TO",
- "check": "Number",
- "align": "RIGHT"
- },
- {
- "type": "input_value",
- "name": "BY",
- "check": "Number",
- "align": "RIGHT"
- }
- ],
- "inputsInline": true,
- "previousStatement": null,
- "nextStatement": null,
- "colour": Blockly.Blocks.loops.HUE,
- "helpUrl": Blockly.Msg.CONTROLS_FOR_HELPURL
- });
- this.appendStatementInput('DO')
- .appendField(Blockly.Msg.CONTROLS_FOR_INPUT_DO);
-
- var thisBlock = this;
- this.setTooltip(function() {
- return Blockly.Msg.CONTROLS_FOR_TOOLTIP.replace('%1',
- thisBlock.getFieldValue('VAR'));
- });
- },
-
- customContextMenu: function(options) {
- if (!this.isCollapsed()) {
- var option = {enabled: true};
- var name = this.getFieldValue('VAR');
- option.text = Blockly.Msg.VARIABLES_SET_CREATE_GET.replace('%1', name);
- var xmlField = goog.dom.createDom('field', null, name);
- xmlField.setAttribute('name', 'VAR');
- var xmlBlock = goog.dom.createDom('block', null, xmlField);
- xmlBlock.setAttribute('type', 'variables_get');
- option.callback = Blockly.ContextMenu.callbackFactory(this, xmlBlock);
- options.push(option);
- }
- }
- };
- Blockly.Blocks['controls_forEach'] = {
-
- init: function() {
- this.jsonInit({
- "message0": Blockly.Msg.CONTROLS_FOREACH_TITLE,
- "args0": [
- {
- "type": "field_variable",
- "name": "VAR",
- "variable": null
- },
- {
- "type": "input_value",
- "name": "LIST",
- "check": "Array"
- }
- ],
- "previousStatement": null,
- "nextStatement": null,
- "colour": Blockly.Blocks.loops.HUE,
- "helpUrl": Blockly.Msg.CONTROLS_FOREACH_HELPURL
- });
- this.appendStatementInput('DO')
- .appendField(Blockly.Msg.CONTROLS_FOREACH_INPUT_DO);
-
- var thisBlock = this;
- this.setTooltip(function() {
- return Blockly.Msg.CONTROLS_FOREACH_TOOLTIP.replace('%1',
- thisBlock.getFieldValue('VAR'));
- });
- },
- customContextMenu: Blockly.Blocks['controls_for'].customContextMenu
- };
- Blockly.Blocks['controls_pass'] = {
- init: function() {
- this.setHelpUrl("");
- this.setColour(Blockly.Blocks.loops.HUE);
- this.appendDummyInput()
- .appendField("do nothing");
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setTooltip("This block does absolutely nothing.");
- }
- }
- Blockly.Blocks['controls_flow_statements'] = {
-
- init: function() {
- var OPERATORS =
- [[Blockly.Msg.CONTROLS_FLOW_STATEMENTS_OPERATOR_BREAK, 'BREAK'],
- [Blockly.Msg.CONTROLS_FLOW_STATEMENTS_OPERATOR_CONTINUE, 'CONTINUE']];
- this.setHelpUrl(Blockly.Msg.CONTROLS_FLOW_STATEMENTS_HELPURL);
- this.setColour(Blockly.Blocks.loops.HUE);
- this.appendDummyInput()
- .appendField(new Blockly.FieldDropdown(OPERATORS), 'FLOW');
- this.setPreviousStatement(true);
-
- var thisBlock = this;
- this.setTooltip(function() {
- var op = thisBlock.getFieldValue('FLOW');
- var TOOLTIPS = {
- 'BREAK': Blockly.Msg.CONTROLS_FLOW_STATEMENTS_TOOLTIP_BREAK,
- 'CONTINUE': Blockly.Msg.CONTROLS_FLOW_STATEMENTS_TOOLTIP_CONTINUE
- };
- return TOOLTIPS[op];
- });
- },
-
- onchange: function(e) {
- if (this.workspace.isDragging()) {
- return;
- }
- var legal = false;
-
- var block = this;
- do {
- if (this.LOOP_TYPES.indexOf(block.type) != -1) {
- legal = true;
- break;
- }
- block = block.getSurroundParent();
- } while (block);
- if (legal) {
- this.setWarningText(null);
- if (!this.isInFlyout) {
- this.setDisabled(false);
- }
- } else {
- this.setWarningText(Blockly.Msg.CONTROLS_FLOW_STATEMENTS_WARNING);
- if (!this.isInFlyout && !this.getInheritedDisabled()) {
- this.setDisabled(true);
- }
- }
- },
-
- LOOP_TYPES: ['controls_repeat', 'controls_repeat_ext', 'controls_forEach',
- 'controls_for', 'controls_whileUntil', 'controls_while']
- };
- Blockly.Blocks['loop_forEach'] = {
- init: function() {
- this.setColour(Blockly.Blocks.loops.HUE)
- this.appendValueInput("ITEM")
- .appendField(Blockly.Msg.LOOP_FOREACH_ITEM)
- this.appendValueInput("LIST")
- .appendField(Blockly.Msg.LOOP_FOREACH_LIST)
- this.setOutput(true);
- this.setInputsInline(true);
- }
- }
|