/** * Visual Blocks Editor * * Copyright 2012 Google Inc. * http://blockly.googlecode.com/ * * 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. */ /** * @fileoverview Text blocks for Blockly. * @author fraser@google.com (Neil Fraser) */ 'use strict'; goog.provide('Blockly.Blocks.text'); goog.require('Blockly.Blocks'); Blockly.Blocks['text'] = { // Text value. init: function() { this.setHelpUrl(Blockly.Msg.TEXT_TEXT_HELPURL); this.setColour(160); this.appendDummyInput() .appendField(new Blockly.FieldImage(Blockly.pathToBlockly + 'media/quote0.png', 12, 12, '"')) .appendField(new Blockly.FieldTextInput(''), 'TEXT') .appendField(new Blockly.FieldImage(Blockly.pathToBlockly + 'media/quote1.png', 12, 12, '"')); this.setOutput(true, 'String'); this.setTooltip(Blockly.Msg.TEXT_TEXT_TOOLTIP); } }; Blockly.Blocks['text_join'] = { // Create a string made up of any number of elements of any type. init: function() { this.setHelpUrl(Blockly.Msg.TEXT_JOIN_HELPURL); this.setColour(160); this.appendValueInput('ADD0') .appendField(Blockly.Msg.TEXT_JOIN_TITLE_CREATEWITH); this.appendValueInput('ADD1'); this.setOutput(true, 'String'); this.setMutator(new Blockly.Mutator(['text_create_join_item'])); this.setTooltip(Blockly.Msg.TEXT_JOIN_TOOLTIP); this.itemCount_ = 2; }, mutationToDom: function() { var container = document.createElement('mutation'); container.setAttribute('items', this.itemCount_); return container; }, domToMutation: function(xmlElement) { for (var x = 0; x < this.itemCount_; x++) { this.removeInput('ADD' + x); } this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10); for (var x = 0; x < this.itemCount_; x++) { var input = this.appendValueInput('ADD' + x); if (x == 0) { input.appendField(Blockly.Msg.TEXT_JOIN_TITLE_CREATEWITH); } } if (this.itemCount_ == 0) { this.appendDummyInput('EMPTY') .appendField(new Blockly.FieldImage(Blockly.pathToBlockly + 'media/quote0.png', 12, 12, '"')) .appendField(new Blockly.FieldImage(Blockly.pathToBlockly + 'media/quote1.png', 12, 12, '"')); } }, decompose: function(workspace) { var containerBlock = new Blockly.Block(workspace, 'text_create_join_container'); containerBlock.initSvg(); var connection = containerBlock.getInput('STACK').connection; for (var x = 0; x < this.itemCount_; x++) { var itemBlock = new Blockly.Block(workspace, 'text_create_join_item'); itemBlock.initSvg(); connection.connect(itemBlock.previousConnection); connection = itemBlock.nextConnection; } return containerBlock; }, compose: function(containerBlock) { // Disconnect all input blocks and remove all inputs. if (this.itemCount_ == 0) { this.removeInput('EMPTY'); } else { for (var x = this.itemCount_ - 1; x >= 0; x--) { this.removeInput('ADD' + x); } } this.itemCount_ = 0; // Rebuild the block's inputs. var itemBlock = containerBlock.getInputTargetBlock('STACK'); while (itemBlock) { var input = this.appendValueInput('ADD' + this.itemCount_); if (this.itemCount_ == 0) { input.appendField(Blockly.Msg.TEXT_JOIN_TITLE_CREATEWITH); } // Reconnect any child blocks. if (itemBlock.valueConnection_) { input.connection.connect(itemBlock.valueConnection_); } this.itemCount_++; itemBlock = itemBlock.nextConnection && itemBlock.nextConnection.targetBlock(); } if (this.itemCount_ == 0) { this.appendDummyInput('EMPTY') .appendField(new Blockly.FieldImage(Blockly.pathToBlockly + 'media/quote0.png', 12, 12, '"')) .appendField(new Blockly.FieldImage(Blockly.pathToBlockly + 'media/quote1.png', 12, 12, '"')); } }, saveConnections: function(containerBlock) { // Store a pointer to any connected child blocks. var itemBlock = containerBlock.getInputTargetBlock('STACK'); var x = 0; while (itemBlock) { var input = this.getInput('ADD' + x); itemBlock.valueConnection_ = input && input.connection.targetConnection; x++; itemBlock = itemBlock.nextConnection && itemBlock.nextConnection.targetBlock(); } } }; Blockly.Blocks['text_create_join_container'] = { // Container. init: function() { this.setColour(160); this.appendDummyInput() .appendField(Blockly.Msg.TEXT_CREATE_JOIN_TITLE_JOIN); this.appendStatementInput('STACK'); this.setTooltip(Blockly.Msg.TEXT_CREATE_JOIN_TOOLTIP); this.contextMenu = false; } }; Blockly.Blocks['text_create_join_item'] = { // Add items. init: function() { this.setColour(160); this.appendDummyInput() .appendField(Blockly.Msg.TEXT_CREATE_JOIN_ITEM_TITLE_ITEM); this.setPreviousStatement(true); this.setNextStatement(true); this.setTooltip(Blockly.Msg.TEXT_CREATE_JOIN_ITEM_TOOLTIP); this.contextMenu = false; } }; Blockly.Blocks['text_append'] = { // Append to a variable in place. init: function() { this.setHelpUrl(Blockly.Msg.TEXT_APPEND_HELPURL); this.setColour(160); this.appendValueInput('TEXT') .appendField(Blockly.Msg.TEXT_APPEND_TO) .appendField(new Blockly.FieldVariable( Blockly.Msg.TEXT_APPEND_VARIABLE), 'VAR') .appendField(Blockly.Msg.TEXT_APPEND_APPENDTEXT); this.setPreviousStatement(true); this.setNextStatement(true); // Assign 'this' to a variable for use in the tooltip closure below. var thisBlock = this; this.setTooltip(function() { return Blockly.Msg.TEXT_APPEND_TOOLTIP.replace('%1', thisBlock.getFieldValue('VAR')); }); }, getVars: function() { return [this.getFieldValue('VAR')]; }, renameVar: function(oldName, newName) { if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) { this.setFieldValue(newName, 'VAR'); } } }; Blockly.Blocks['text_length'] = { // String length. init: function() { this.setHelpUrl(Blockly.Msg.TEXT_LENGTH_HELPURL); this.setColour(160); this.interpolateMsg(Blockly.Msg.TEXT_LENGTH_TITLE, ['VALUE', ['String', 'Array'], Blockly.ALIGN_RIGHT], Blockly.ALIGN_RIGHT); this.setOutput(true, 'Number'); this.setTooltip(Blockly.Msg.TEXT_LENGTH_TOOLTIP); } }; Blockly.Blocks['text_isEmpty'] = { // Is the string null? init: function() { this.setHelpUrl(Blockly.Msg.TEXT_ISEMPTY_HELPURL); this.setColour(160); this.interpolateMsg(Blockly.Msg.TEXT_ISEMPTY_TITLE, ['VALUE', ['String', 'Array'], Blockly.ALIGN_RIGHT], Blockly.ALIGN_RIGHT); this.setOutput(true, 'Boolean'); this.setTooltip(Blockly.Msg.TEXT_ISEMPTY_TOOLTIP); } }; Blockly.Blocks['text_indexOf'] = { // Find a substring in the text. init: function() { var OPERATORS = [[Blockly.Msg.TEXT_INDEXOF_OPERATOR_FIRST, 'FIRST'], [Blockly.Msg.TEXT_INDEXOF_OPERATOR_LAST, 'LAST']]; this.setHelpUrl(Blockly.Msg.TEXT_INDEXOF_HELPURL); this.setColour(160); this.setOutput(true, 'Number'); this.appendValueInput('VALUE') .setCheck('String') .appendField(Blockly.Msg.TEXT_INDEXOF_INPUT_INTEXT); this.appendValueInput('FIND') .setCheck('String') .appendField(new Blockly.FieldDropdown(OPERATORS), 'END'); if (Blockly.Msg.TEXT_INDEXOF_TAIL) { this.appendDummyInput().appendField(Blockly.Msg.TEXT_INDEXOF_TAIL); } this.setInputsInline(true); this.setTooltip(Blockly.Msg.TEXT_INDEXOF_TOOLTIP); } }; Blockly.Blocks['text_charAt'] = { // Get a character from the string. init: function() { this.WHERE_OPTIONS = [[Blockly.Msg.TEXT_CHARAT_FROM_START, 'FROM_START'], [Blockly.Msg.TEXT_CHARAT_FROM_END, 'FROM_END'], [Blockly.Msg.TEXT_CHARAT_FIRST, 'FIRST'], [Blockly.Msg.TEXT_CHARAT_LAST, 'LAST'], [Blockly.Msg.TEXT_CHARAT_RANDOM, 'RANDOM']]; this.setHelpUrl(Blockly.Msg.TEXT_CHARAT_HELPURL); this.setColour(160); this.setOutput(true, 'String'); this.appendValueInput('VALUE') .setCheck('String') .appendField(Blockly.Msg.TEXT_CHARAT_INPUT_INTEXT); this.appendDummyInput('AT'); this.setInputsInline(true); this.updateAt(true); this.setTooltip(Blockly.Msg.TEXT_CHARAT_TOOLTIP); }, mutationToDom: function() { // Save whether there is an 'AT' input. var container = document.createElement('mutation'); var isAt = this.getInput('AT').type == Blockly.INPUT_VALUE; container.setAttribute('at', isAt); return container; }, domToMutation: function(xmlElement) { // Restore the 'AT' input. // Note: Until January 2013 this block did not have mutations, // so 'at' defaults to true. var isAt = (xmlElement.getAttribute('at') != 'false'); this.updateAt(isAt); }, updateAt: function(isAt) { // Create or delete an input for the numeric index. // Destroy old 'AT' and 'ORDINAL' inputs. this.removeInput('AT'); this.removeInput('ORDINAL', true); // Create either a value 'AT' input or a dummy input. if (isAt) { this.appendValueInput('AT').setCheck('Number'); if (Blockly.Msg.ORDINAL_NUMBER_SUFFIX) { this.appendDummyInput('ORDINAL') .appendField(Blockly.Msg.ORDINAL_NUMBER_SUFFIX); } } else { this.appendDummyInput('AT'); } if (Blockly.Msg.TEXT_CHARAT_TAIL) { this.removeInput('TAIL', true); this.appendDummyInput('TAIL') .appendField(Blockly.Msg.TEXT_CHARAT_TAIL); } var menu = new Blockly.FieldDropdown(this.WHERE_OPTIONS, function(value) { var newAt = (value == 'FROM_START') || (value == 'FROM_END'); // The 'isAt' variable is available due to this function being a closure. if (newAt != isAt) { var block = this.sourceBlock_; block.updateAt(newAt); // This menu has been destroyed and replaced. Update the replacement. block.setFieldValue(value, 'WHERE'); return null; } return undefined; }); this.getInput('AT').appendField(menu, 'WHERE'); } }; Blockly.Blocks['text_getSubstring'] = { // Get substring. init: function() { this.WHERE_OPTIONS_1 = [[Blockly.Msg.TEXT_GET_SUBSTRING_START_FROM_START, 'FROM_START'], [Blockly.Msg.TEXT_GET_SUBSTRING_START_FROM_END, 'FROM_END'], [Blockly.Msg.TEXT_GET_SUBSTRING_START_FIRST, 'FIRST']]; this.WHERE_OPTIONS_2 = [[Blockly.Msg.TEXT_GET_SUBSTRING_END_FROM_START, 'FROM_START'], [Blockly.Msg.TEXT_GET_SUBSTRING_END_FROM_END, 'FROM_END'], [Blockly.Msg.TEXT_GET_SUBSTRING_END_LAST, 'LAST']]; this.setHelpUrl(Blockly.Msg.TEXT_GET_SUBSTRING_HELPURL); this.setColour(160); this.appendValueInput('STRING') .setCheck('String') .appendField(Blockly.Msg.TEXT_GET_SUBSTRING_INPUT_IN_TEXT); this.appendDummyInput('AT1'); this.appendDummyInput('AT2'); if (Blockly.Msg.TEXT_GET_SUBSTRING_TAIL) { this.appendDummyInput('TAIL') .appendField(Blockly.Msg.TEXT_GET_SUBSTRING_TAIL); } this.setInputsInline(true); this.setOutput(true, 'String'); this.updateAt(1, true); this.updateAt(2, true); this.setTooltip(Blockly.Msg.TEXT_GET_SUBSTRING_TOOLTIP); }, mutationToDom: function() { // Save whether there are 'AT' inputs. var container = document.createElement('mutation'); var isAt1 = this.getInput('AT1').type == Blockly.INPUT_VALUE; container.setAttribute('at1', isAt1); var isAt2 = this.getInput('AT2').type == Blockly.INPUT_VALUE; container.setAttribute('at2', isAt2); return container; }, domToMutation: function(xmlElement) { // Restore the block shape. var isAt1 = (xmlElement.getAttribute('at1') == 'true'); var isAt2 = (xmlElement.getAttribute('at2') == 'true'); this.updateAt(1, isAt1); this.updateAt(2, isAt2); }, updateAt: function(n, isAt) { // Create or delete an input for the numeric index. // Destroy old 'AT' and 'ORDINAL' inputs. this.removeInput('AT' + n); this.removeInput('ORDINAL' + n, true); // Create either a value 'AT' input or a dummy input. if (isAt) { this.appendValueInput('AT' + n).setCheck('Number'); if (Blockly.Msg.ORDINAL_NUMBER_SUFFIX) { this.appendDummyInput('ORDINAL' + n) .appendField(Blockly.Msg.ORDINAL_NUMBER_SUFFIX); } } else { this.appendDummyInput('AT' + n); } // Move tail, if present, to end of block. if (n == 2 && Blockly.Msg.TEXT_GET_SUBSTRING_TAIL) { this.removeInput('TAIL', true); this.appendDummyInput('TAIL') .appendField(Blockly.Msg.TEXT_GET_SUBSTRING_TAIL); } var menu = new Blockly.FieldDropdown(this['WHERE_OPTIONS_' + n], function(value) { var newAt = (value == 'FROM_START') || (value == 'FROM_END'); // The 'isAt' variable is available due to this function being a closure. if (newAt != isAt) { var block = this.sourceBlock_; block.updateAt(n, newAt); // This menu has been destroyed and replaced. Update the replacement. block.setFieldValue(value, 'WHERE' + n); return null; } return undefined; }); this.getInput('AT' + n) .appendField(menu, 'WHERE' + n); if (n == 1) { this.moveInputBefore('AT1', 'AT2'); } } }; Blockly.Blocks['text_changeCase'] = { // Change capitalization. init: function() { var OPERATORS = [[Blockly.Msg.TEXT_CHANGECASE_OPERATOR_UPPERCASE, 'UPPERCASE'], [Blockly.Msg.TEXT_CHANGECASE_OPERATOR_LOWERCASE, 'LOWERCASE'], [Blockly.Msg.TEXT_CHANGECASE_OPERATOR_TITLECASE, 'TITLECASE']]; this.setHelpUrl(Blockly.Msg.TEXT_CHANGECASE_HELPURL); this.setColour(160); this.appendValueInput('TEXT') .setCheck('String') .appendField(new Blockly.FieldDropdown(OPERATORS), 'CASE'); this.setOutput(true, 'String'); this.setTooltip(Blockly.Msg.TEXT_CHANGECASE_TOOLTIP); } }; Blockly.Blocks['text_trim'] = { // Trim spaces. init: function() { var OPERATORS = [[Blockly.Msg.TEXT_TRIM_OPERATOR_BOTH, 'BOTH'], [Blockly.Msg.TEXT_TRIM_OPERATOR_LEFT, 'LEFT'], [Blockly.Msg.TEXT_TRIM_OPERATOR_RIGHT, 'RIGHT']]; this.setHelpUrl(Blockly.Msg.TEXT_TRIM_HELPURL); this.setColour(160); this.appendValueInput('TEXT') .setCheck('String') .appendField(new Blockly.FieldDropdown(OPERATORS), 'MODE'); this.setOutput(true, 'String'); this.setTooltip(Blockly.Msg.TEXT_TRIM_TOOLTIP); } }; Blockly.Blocks['text_print'] = { // Print statement. init: function() { this.setHelpUrl(Blockly.Msg.TEXT_PRINT_HELPURL); this.setColour(160); this.interpolateMsg(Blockly.Msg.TEXT_PRINT_TITLE, ['TEXT', null, Blockly.ALIGN_RIGHT], Blockly.ALIGN_RIGHT); this.setPreviousStatement(true); this.setNextStatement(true); this.setTooltip(Blockly.Msg.TEXT_PRINT_TOOLTIP); } }; Blockly.Blocks['text_prompt'] = { // Prompt function. init: function() { var TYPES = [[Blockly.Msg.TEXT_PROMPT_TYPE_TEXT, 'TEXT'], [Blockly.Msg.TEXT_PROMPT_TYPE_NUMBER, 'NUMBER']]; // Assign 'this' to a variable for use in the closure below. var thisBlock = this; this.setHelpUrl(Blockly.Msg.TEXT_PROMPT_HELPURL); this.setColour(160); var dropdown = new Blockly.FieldDropdown(TYPES, function(newOp) { if (newOp == 'NUMBER') { thisBlock.changeOutput('Number'); } else { thisBlock.changeOutput('String'); } }); this.appendDummyInput() .appendField(dropdown, 'TYPE') .appendField(new Blockly.FieldImage(Blockly.pathToBlockly + 'media/quote0.png', 12, 12, '"')) .appendField(new Blockly.FieldTextInput(''), 'TEXT') .appendField(new Blockly.FieldImage(Blockly.pathToBlockly + 'media/quote1.png', 12, 12, '"')); this.setOutput(true, 'String'); // Assign 'this' to a variable for use in the tooltip closure below. var thisBlock = this; this.setTooltip(function() { return (thisBlock.getFieldValue('TYPE') == 'TEXT') ? Blockly.Msg.TEXT_PROMPT_TOOLTIP_TEXT : Blockly.Msg.TEXT_PROMPT_TOOLTIP_NUMBER; }); } };