text.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. /**
  2. * @license
  3. * Visual Blocks Editor
  4. *
  5. * Copyright 2012 Google Inc.
  6. * https://developers.google.com/blockly/
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. /**
  21. * @fileoverview Text blocks for Blockly.
  22. * @author fraser@google.com (Neil Fraser)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.Blocks.texts');
  26. goog.require('Blockly.Blocks');
  27. /**
  28. * Common HSV hue for all blocks in this category.
  29. */
  30. Blockly.Blocks.texts.HUE = "#2cc58e";
  31. var UTILITY_HUE = "#2cc58e";
  32. Blockly.Blocks['text'] = {
  33. /**
  34. * Block for text value.
  35. * @this Blockly.Block
  36. */
  37. init: function() {
  38. this.setHelpUrl(Blockly.Msg.TEXT_TEXT_HELPURL);
  39. this.setColour(Blockly.Blocks.texts.HUE);
  40. this.appendDummyInput()
  41. .appendField(this.newQuote_(true))
  42. .appendField(new Blockly.FieldTextInput(''), 'TEXT')
  43. .appendField(this.newQuote_(false));
  44. this.setOutput(true, 'String');
  45. // Assign 'this' to a variable for use in the tooltip closure below.
  46. var thisBlock = this;
  47. // Text block is trivial. Use tooltip of parent block if it exists.
  48. this.setTooltip(function() {
  49. var parent = thisBlock.getParent();
  50. return (parent && parent.getInputsInline() && parent.tooltip) ||
  51. Blockly.Msg.TEXT_TEXT_TOOLTIP;
  52. });
  53. },
  54. /**
  55. * Create an image of an open or closed quote.
  56. * @param {boolean} open True if open quote, false if closed.
  57. * @return {!Blockly.FieldImage} The field image of the quote.
  58. * @this Blockly.Block
  59. * @private
  60. */
  61. newQuote_: function(open) {
  62. if (open == this.RTL) {
  63. var file = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAKCAQAAAAqJXdxAAAAqUlEQVQI1z3KvUpCcRiA8ef9E4JNHhI0aFEacm1o0BsI0Slx8wa8gLauoDnoBhq7DcfWhggONDmJJgqCPA7neJ7p934EOOKOnM8Q7PDElo/4x4lFb2DmuUjcUzS3URnGib9qaPNbuXvBO3sGPHJDRG6fGVdMSeWDP2q99FQdFrz26Gu5Tq7dFMzUvbXy8KXeAj57cOklgA+u1B5AoslLtGIHQMaCVnwDnADZIFIrXsoXrgAAAABJRU5ErkJggg==';
  64. } else {
  65. var file = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAKCAQAAAAqJXdxAAAAn0lEQVQI1z3OMa5BURSF4f/cQhAKjUQhuQmFNwGJEUi0RKN5rU7FHKhpjEH3TEMtkdBSCY1EIv8r7nFX9e29V7EBAOvu7RPjwmWGH/VuF8CyN9/OAdvqIXYLvtRaNjx9mMTDyo+NjAN1HNcl9ZQ5oQMM3dgDUqDo1l8DzvwmtZN7mnD+PkmLa+4mhrxVA9fRowBWmVBhFy5gYEjKMfz9AylsaRRgGzvZAAAAAElFTkSuQmCC';
  66. }
  67. return new Blockly.FieldImage(file, 12, 12, '"');
  68. }
  69. };
  70. Blockly.Blocks['text_join'] = {
  71. /**
  72. * Block for creating a string made up of any number of elements of any type.
  73. * @this Blockly.Block
  74. */
  75. init: function() {
  76. this.setHelpUrl(Blockly.Msg.TEXT_JOIN_HELPURL);
  77. this.setColour(Blockly.Blocks.texts.HUE);
  78. this.itemCount_ = 2;
  79. this.updateShape_();
  80. this.setOutput(true, 'String');
  81. this.setMutator(new Blockly.Mutator(['text_create_join_item']));
  82. this.setTooltip(Blockly.Msg.TEXT_JOIN_TOOLTIP);
  83. },
  84. /**
  85. * Create XML to represent number of text inputs.
  86. * @return {!Element} XML storage element.
  87. * @this Blockly.Block
  88. */
  89. mutationToDom: function() {
  90. var container = document.createElement('mutation');
  91. container.setAttribute('items', this.itemCount_);
  92. return container;
  93. },
  94. /**
  95. * Parse XML to restore the text inputs.
  96. * @param {!Element} xmlElement XML storage element.
  97. * @this Blockly.Block
  98. */
  99. domToMutation: function(xmlElement) {
  100. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  101. this.updateShape_();
  102. },
  103. /**
  104. * Populate the mutator's dialog with this block's components.
  105. * @param {!Blockly.Workspace} workspace Mutator's workspace.
  106. * @return {!Blockly.Block} Root block in mutator.
  107. * @this Blockly.Block
  108. */
  109. decompose: function(workspace) {
  110. var containerBlock = workspace.newBlock('text_create_join_container');
  111. containerBlock.initSvg();
  112. var connection = containerBlock.getInput('STACK').connection;
  113. for (var i = 0; i < this.itemCount_; i++) {
  114. var itemBlock = workspace.newBlock('text_create_join_item');
  115. itemBlock.initSvg();
  116. connection.connect(itemBlock.previousConnection);
  117. connection = itemBlock.nextConnection;
  118. }
  119. return containerBlock;
  120. },
  121. /**
  122. * Reconfigure this block based on the mutator dialog's components.
  123. * @param {!Blockly.Block} containerBlock Root block in mutator.
  124. * @this Blockly.Block
  125. */
  126. compose: function(containerBlock) {
  127. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  128. // Count number of inputs.
  129. var connections = [];
  130. while (itemBlock) {
  131. connections.push(itemBlock.valueConnection_);
  132. itemBlock = itemBlock.nextConnection &&
  133. itemBlock.nextConnection.targetBlock();
  134. }
  135. // Disconnect any children that don't belong.
  136. for (var i = 0; i < this.itemCount_; i++) {
  137. var connection = this.getInput('ADD' + i).connection.targetConnection;
  138. if (connection && connections.indexOf(connection) == -1) {
  139. connection.disconnect();
  140. }
  141. }
  142. this.itemCount_ = connections.length;
  143. this.updateShape_();
  144. // Reconnect any child blocks.
  145. for (var i = 0; i < this.itemCount_; i++) {
  146. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  147. }
  148. },
  149. /**
  150. * Store pointers to any connected child blocks.
  151. * @param {!Blockly.Block} containerBlock Root block in mutator.
  152. * @this Blockly.Block
  153. */
  154. saveConnections: function(containerBlock) {
  155. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  156. var i = 0;
  157. while (itemBlock) {
  158. var input = this.getInput('ADD' + i);
  159. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  160. i++;
  161. itemBlock = itemBlock.nextConnection &&
  162. itemBlock.nextConnection.targetBlock();
  163. }
  164. },
  165. /**
  166. * Modify this block to have the correct number of inputs.
  167. * @private
  168. * @this Blockly.Block
  169. */
  170. updateShape_: function() {
  171. if (this.itemCount_ && this.getInput('EMPTY')) {
  172. this.removeInput('EMPTY');
  173. } else if (!this.itemCount_ && !this.getInput('EMPTY')) {
  174. this.appendDummyInput('EMPTY')
  175. .appendField(this.newQuote_(true))
  176. .appendField(this.newQuote_(false));
  177. }
  178. // Add new inputs.
  179. for (var i = 0; i < this.itemCount_; i++) {
  180. if (!this.getInput('ADD' + i)) {
  181. var input = this.appendValueInput('ADD' + i);
  182. if (i == 0) {
  183. input.appendField(Blockly.Msg.TEXT_JOIN_TITLE_CREATEWITH);
  184. }
  185. }
  186. }
  187. // Remove deleted inputs.
  188. while (this.getInput('ADD' + i)) {
  189. this.removeInput('ADD' + i);
  190. i++;
  191. }
  192. },
  193. newQuote_: Blockly.Blocks['text'].newQuote_
  194. };
  195. Blockly.Blocks['text_create_join_container'] = {
  196. /**
  197. * Mutator block for container.
  198. * @this Blockly.Block
  199. */
  200. init: function() {
  201. this.setColour(Blockly.Blocks.texts.HUE);
  202. this.appendDummyInput()
  203. .appendField(Blockly.Msg.TEXT_CREATE_JOIN_TITLE_JOIN);
  204. this.appendStatementInput('STACK');
  205. this.setTooltip(Blockly.Msg.TEXT_CREATE_JOIN_TOOLTIP);
  206. this.contextMenu = false;
  207. }
  208. };
  209. Blockly.Blocks['text_create_join_item'] = {
  210. /**
  211. * Mutator block for add items.
  212. * @this Blockly.Block
  213. */
  214. init: function() {
  215. this.setColour(Blockly.Blocks.texts.HUE);
  216. this.appendDummyInput()
  217. .appendField(Blockly.Msg.TEXT_CREATE_JOIN_ITEM_TITLE_ITEM);
  218. this.setPreviousStatement(true);
  219. this.setNextStatement(true);
  220. this.setTooltip(Blockly.Msg.TEXT_CREATE_JOIN_ITEM_TOOLTIP);
  221. this.contextMenu = false;
  222. }
  223. };
  224. Blockly.Blocks['text_append'] = {
  225. /**
  226. * Block for appending to a variable in place.
  227. * @this Blockly.Block
  228. */
  229. init: function() {
  230. this.setHelpUrl(Blockly.Msg.TEXT_APPEND_HELPURL);
  231. this.setColour(Blockly.Blocks.texts.HUE);
  232. this.appendValueInput('TEXT')
  233. .appendField(Blockly.Msg.TEXT_APPEND_TO)
  234. .appendField(new Blockly.FieldVariable(
  235. Blockly.Msg.TEXT_APPEND_VARIABLE), 'VAR')
  236. .appendField(Blockly.Msg.TEXT_APPEND_APPENDTEXT);
  237. this.setPreviousStatement(true);
  238. this.setNextStatement(true);
  239. // Assign 'this' to a variable for use in the tooltip closure below.
  240. var thisBlock = this;
  241. this.setTooltip(function() {
  242. return Blockly.Msg.TEXT_APPEND_TOOLTIP.replace('%1',
  243. thisBlock.getFieldValue('VAR'));
  244. });
  245. }
  246. };
  247. Blockly.Blocks['text_length'] = {
  248. /**
  249. * Block for string length.
  250. * @this Blockly.Block
  251. */
  252. init: function() {
  253. this.jsonInit({
  254. "message0": Blockly.Msg.TEXT_LENGTH_TITLE,
  255. "args0": [
  256. {
  257. "type": "input_value",
  258. "name": "VALUE",
  259. "check": ['String', 'Array']
  260. }
  261. ],
  262. "output": 'Number',
  263. "colour": Blockly.Blocks.texts.HUE,
  264. "tooltip": Blockly.Msg.TEXT_LENGTH_TOOLTIP,
  265. "helpUrl": Blockly.Msg.TEXT_LENGTH_HELPURL
  266. });
  267. }
  268. };
  269. Blockly.Blocks['text_isEmpty'] = {
  270. /**
  271. * Block for is the string null?
  272. * @this Blockly.Block
  273. */
  274. init: function() {
  275. this.jsonInit({
  276. "message0": Blockly.Msg.TEXT_ISEMPTY_TITLE,
  277. "args0": [
  278. {
  279. "type": "input_value",
  280. "name": "VALUE",
  281. "check": ['String', 'Array']
  282. }
  283. ],
  284. "output": 'Boolean',
  285. "colour": Blockly.Blocks.texts.HUE,
  286. "tooltip": Blockly.Msg.TEXT_ISEMPTY_TOOLTIP,
  287. "helpUrl": Blockly.Msg.TEXT_ISEMPTY_HELPURL
  288. });
  289. }
  290. };
  291. Blockly.Blocks['text_indexOf'] = {
  292. /**
  293. * Block for finding a substring in the text.
  294. * @this Blockly.Block
  295. */
  296. init: function() {
  297. var OPERATORS =
  298. [[Blockly.Msg.TEXT_INDEXOF_OPERATOR_FIRST, 'FIRST'],
  299. [Blockly.Msg.TEXT_INDEXOF_OPERATOR_LAST, 'LAST']];
  300. this.setHelpUrl(Blockly.Msg.TEXT_INDEXOF_HELPURL);
  301. this.setColour(Blockly.Blocks.texts.HUE);
  302. this.setOutput(true, 'Number');
  303. this.appendValueInput('VALUE')
  304. .setCheck('String')
  305. .appendField(Blockly.Msg.TEXT_INDEXOF_INPUT_INTEXT);
  306. this.appendValueInput('FIND')
  307. .setCheck('String')
  308. .appendField(new Blockly.FieldDropdown(OPERATORS), 'END');
  309. if (Blockly.Msg.TEXT_INDEXOF_TAIL) {
  310. this.appendDummyInput().appendField(Blockly.Msg.LED_POSITION);
  311. }
  312. this.setInputsInline(true);
  313. // Assign 'this' to a variable for use in the tooltip closure below.
  314. var thisBlock = this;
  315. this.setTooltip(function() {
  316. return Blockly.Msg.TEXT_INDEXOF_TOOLTIP.replace('%1',
  317. thisBlock.workspace.options.oneBasedIndex ? '0' : '-1');
  318. });
  319. }
  320. };
  321. Blockly.Blocks['text_charAt'] = {
  322. /**
  323. * Block for getting a character from the string.
  324. * @this Blockly.Block
  325. */
  326. init: function() {
  327. this.WHERE_OPTIONS =
  328. [[Blockly.Msg.TEXT_CHARAT_FROM_START, 'FROM_START'],
  329. [Blockly.Msg.TEXT_CHARAT_FROM_END, 'FROM_END'],
  330. [Blockly.Msg.TEXT_CHARAT_FIRST, 'FIRST'],
  331. [Blockly.Msg.TEXT_CHARAT_LAST, 'LAST'],
  332. [Blockly.Msg.TEXT_CHARAT_RANDOM, 'RANDOM']];
  333. this.setHelpUrl(Blockly.Msg.TEXT_CHARAT_HELPURL);
  334. this.setColour(Blockly.Blocks.texts.HUE);
  335. this.setOutput(true, 'String');
  336. this.appendValueInput('VALUE')
  337. .setCheck('String')
  338. .appendField(Blockly.Msg.TEXT_CHARAT_INPUT_INTEXT);
  339. this.appendDummyInput('AT');
  340. this.setInputsInline(true);
  341. this.updateAt_(true);
  342. // Assign 'this' to a variable for use in the tooltip closure below.
  343. var thisBlock = this;
  344. this.setTooltip(function() {
  345. var where = thisBlock.getFieldValue('WHERE');
  346. var tooltip = Blockly.Msg.TEXT_CHARAT_TOOLTIP;
  347. if (where == 'FROM_START' || where == 'FROM_END') {
  348. var msg = (where == 'FROM_START') ?
  349. Blockly.Msg.LISTS_INDEX_FROM_START_TOOLTIP :
  350. Blockly.Msg.LISTS_INDEX_FROM_END_TOOLTIP;
  351. tooltip += ' ' + msg.replace('%1',
  352. thisBlock.workspace.options.oneBasedIndex ? '#1' : '#0');
  353. }
  354. return tooltip;
  355. });
  356. },
  357. /**
  358. * Create XML to represent whether there is an 'AT' input.
  359. * @return {!Element} XML storage element.
  360. * @this Blockly.Block
  361. */
  362. mutationToDom: function() {
  363. var container = document.createElement('mutation');
  364. var isAt = this.getInput('AT').type == Blockly.INPUT_VALUE;
  365. container.setAttribute('at', isAt);
  366. return container;
  367. },
  368. /**
  369. * Parse XML to restore the 'AT' input.
  370. * @param {!Element} xmlElement XML storage element.
  371. * @this Blockly.Block
  372. */
  373. domToMutation: function(xmlElement) {
  374. // Note: Until January 2013 this block did not have mutations,
  375. // so 'at' defaults to true.
  376. var isAt = (xmlElement.getAttribute('at') != 'false');
  377. this.updateAt_(isAt);
  378. },
  379. /**
  380. * Create or delete an input for the numeric index.
  381. * @param {boolean} isAt True if the input should exist.
  382. * @private
  383. * @this Blockly.Block
  384. */
  385. updateAt_: function(isAt) {
  386. // Destroy old 'AT' and 'ORDINAL' inputs.
  387. this.removeInput('AT');
  388. this.removeInput('ORDINAL', true);
  389. // Create either a value 'AT' input or a dummy input.
  390. if (isAt) {
  391. this.appendValueInput('AT').setCheck('Number');
  392. // if (Blockly.Msg.ORDINAL_NUMBER_SUFFIX) {
  393. // this.appendDummyInput('ORDINAL')
  394. // .appendField(Blockly.Msg.ARD_SERIAL_RECEIVE_DATASET_TYPE_STRING);
  395. // }
  396. } else {
  397. this.appendDummyInput('AT');
  398. }
  399. if (Blockly.Msg.TEXT_CHARAT_TAIL) {
  400. this.removeInput('TAIL', true);
  401. this.appendDummyInput('TAIL')
  402. .appendField(Blockly.Msg.TEXT_CHARAT_TAIL);
  403. }
  404. var menu = new Blockly.FieldDropdown(this.WHERE_OPTIONS, function(value) {
  405. var newAt = (value == 'FROM_START') || (value == 'FROM_END');
  406. // The 'isAt' variable is available due to this function being a closure.
  407. if (newAt != isAt) {
  408. var block = this.sourceBlock_;
  409. block.updateAt_(newAt);
  410. // This menu has been destroyed and replaced. Update the replacement.
  411. block.setFieldValue(value, 'WHERE');
  412. return null;
  413. }
  414. return undefined;
  415. });
  416. this.getInput('AT').appendField(menu, 'WHERE');
  417. }
  418. };
  419. Blockly.Blocks['text_getSubstring'] = {
  420. /**
  421. * Block for getting substring.
  422. * @this Blockly.Block
  423. */
  424. init: function() {
  425. this['WHERE_OPTIONS_1'] =
  426. [[Blockly.Msg.TEXT_GET_SUBSTRING_START_FROM_START, 'FROM_START'],
  427. [Blockly.Msg.TEXT_GET_SUBSTRING_START_FROM_END, 'FROM_END'],
  428. [Blockly.Msg.TEXT_GET_SUBSTRING_START_FIRST, 'FIRST']];
  429. this['WHERE_OPTIONS_2'] =
  430. [[Blockly.Msg.TEXT_GET_SUBSTRING_END_FROM_START, 'FROM_START'],
  431. [Blockly.Msg.TEXT_GET_SUBSTRING_END_FROM_END, 'FROM_END'],
  432. [Blockly.Msg.TEXT_GET_SUBSTRING_END_LAST, 'LAST']];
  433. this.setHelpUrl(Blockly.Msg.TEXT_GET_SUBSTRING_HELPURL);
  434. this.setColour(Blockly.Blocks.texts.HUE);
  435. this.appendValueInput('STRING')
  436. .setCheck('String')
  437. .appendField(Blockly.Msg.TEXT_GET_SUBSTRING_INPUT_IN_TEXT);
  438. this.appendDummyInput('AT1');
  439. this.appendDummyInput('AT2');
  440. if (Blockly.Msg.TEXT_GET_SUBSTRING_TAIL) {
  441. this.appendDummyInput('TAIL')
  442. .appendField(Blockly.Msg.TEXT_GET_SUBSTRING_TAIL);
  443. }
  444. this.setInputsInline(true);
  445. this.setOutput(true, 'String');
  446. this.updateAt_(1, true);
  447. this.updateAt_(2, true);
  448. this.setTooltip(Blockly.Msg.TEXT_GET_SUBSTRING_TOOLTIP);
  449. },
  450. /**
  451. * Create XML to represent whether there are 'AT' inputs.
  452. * @return {!Element} XML storage element.
  453. * @this Blockly.Block
  454. */
  455. mutationToDom: function() {
  456. var container = document.createElement('mutation');
  457. var isAt1 = this.getInput('AT1').type == Blockly.INPUT_VALUE;
  458. container.setAttribute('at1', isAt1);
  459. var isAt2 = this.getInput('AT2').type == Blockly.INPUT_VALUE;
  460. container.setAttribute('at2', isAt2);
  461. return container;
  462. },
  463. /**
  464. * Parse XML to restore the 'AT' inputs.
  465. * @param {!Element} xmlElement XML storage element.
  466. * @this Blockly.Block
  467. */
  468. domToMutation: function(xmlElement) {
  469. var isAt1 = (xmlElement.getAttribute('at1') == 'true');
  470. var isAt2 = (xmlElement.getAttribute('at2') == 'true');
  471. this.updateAt_(1, isAt1);
  472. this.updateAt_(2, isAt2);
  473. },
  474. /**
  475. * Create or delete an input for a numeric index.
  476. * This block has two such inputs, independant of each other.
  477. * @param {number} n Specify first or second input (1 or 2).
  478. * @param {boolean} isAt True if the input should exist.
  479. * @private
  480. * @this Blockly.Block
  481. */
  482. updateAt_: function(n, isAt) {
  483. // Create or delete an input for the numeric index.
  484. // Destroy old 'AT' and 'ORDINAL' inputs.
  485. this.removeInput('AT' + n);
  486. this.removeInput('ORDINAL' + n, true);
  487. // Create either a value 'AT' input or a dummy input.
  488. if (isAt) {
  489. this.appendValueInput('AT' + n).setCheck('Number');
  490. if (Blockly.Msg.ORDINAL_NUMBER_SUFFIX) {
  491. this.appendDummyInput('ORDINAL' + n)
  492. .appendField(Blockly.Msg.ORDINAL_NUMBER_SUFFIX);
  493. }
  494. } else {
  495. this.appendDummyInput('AT' + n);
  496. }
  497. // Move tail, if present, to end of block.
  498. if (n == 2 && Blockly.Msg.TEXT_GET_SUBSTRING_TAIL) {
  499. this.removeInput('TAIL', true);
  500. this.appendDummyInput('TAIL')
  501. .appendField(Blockly.Msg.TEXT_GET_SUBSTRING_TAIL);
  502. }
  503. var menu = new Blockly.FieldDropdown(this['WHERE_OPTIONS_' + n],
  504. function(value) {
  505. var newAt = (value == 'FROM_START') || (value == 'FROM_END');
  506. // The 'isAt' variable is available due to this function being a
  507. // closure.
  508. if (newAt != isAt) {
  509. var block = this.sourceBlock_;
  510. block.updateAt_(n, newAt);
  511. // This menu has been destroyed and replaced.
  512. // Update the replacement.
  513. block.setFieldValue(value, 'WHERE' + n);
  514. return null;
  515. }
  516. return undefined;
  517. });
  518. this.getInput('AT' + n)
  519. .appendField(menu, 'WHERE' + n);
  520. if (n == 1) {
  521. this.moveInputBefore('AT1', 'AT2');
  522. }
  523. }
  524. };
  525. Blockly.Blocks['text_changeCase'] = {
  526. /**
  527. * Block for changing capitalization.
  528. * @this Blockly.Block
  529. */
  530. init: function() {
  531. var OPERATORS =
  532. [[Blockly.Msg.TEXT_CHANGECASE_OPERATOR_UPPERCASE, 'UPPERCASE'],
  533. [Blockly.Msg.TEXT_CHANGECASE_OPERATOR_LOWERCASE, 'LOWERCASE'],
  534. [Blockly.Msg.TEXT_CHANGECASE_OPERATOR_TITLECASE, 'TITLECASE']];
  535. this.setHelpUrl(Blockly.Msg.TEXT_CHANGECASE_HELPURL);
  536. this.setColour(Blockly.Blocks.texts.HUE);
  537. this.appendValueInput('TEXT')
  538. .setCheck('String')
  539. .appendField(new Blockly.FieldDropdown(OPERATORS), 'CASE');
  540. this.setOutput(true, 'String');
  541. this.setTooltip(Blockly.Msg.TEXT_CHANGECASE_TOOLTIP);
  542. }
  543. };
  544. Blockly.Blocks['text_trim'] = {
  545. /**
  546. * Block for trimming spaces.
  547. * @this Blockly.Block
  548. */
  549. init: function() {
  550. var OPERATORS =
  551. [[Blockly.Msg.TEXT_TRIM_OPERATOR_BOTH, 'BOTH'],
  552. [Blockly.Msg.TEXT_TRIM_OPERATOR_LEFT, 'LEFT'],
  553. [Blockly.Msg.TEXT_TRIM_OPERATOR_RIGHT, 'RIGHT']];
  554. this.setHelpUrl(Blockly.Msg.TEXT_TRIM_HELPURL);
  555. this.setColour(Blockly.Blocks.texts.HUE);
  556. this.appendValueInput('TEXT')
  557. .setCheck('String')
  558. .appendField(new Blockly.FieldDropdown(OPERATORS), 'MODE');
  559. this.setOutput(true, 'String');
  560. this.setTooltip(Blockly.Msg.TEXT_TRIM_TOOLTIP);
  561. }
  562. };
  563. Blockly.Blocks['text_print'] = {
  564. /**
  565. * Block for print statement.
  566. * @this Blockly.Block
  567. */
  568. init: function() {
  569. this.jsonInit({
  570. "message0": Blockly.Msg.TEXT_PRINT_TITLE,
  571. "args0": [
  572. {
  573. "type": "input_value",
  574. "name": "TEXT"
  575. }
  576. ],
  577. "previousStatement": null,
  578. "nextStatement": null,
  579. "colour": UTILITY_HUE,
  580. "tooltip": Blockly.Msg.TEXT_PRINT_TOOLTIP,
  581. "helpUrl": Blockly.Msg.TEXT_PRINT_HELPURL
  582. });
  583. }
  584. };
  585. Blockly.Blocks['text_prompt_ext'] = {
  586. /**
  587. * Block for prompt function (external message).
  588. * @this Blockly.Block
  589. */
  590. init: function() {
  591. var TYPES =
  592. [[Blockly.Msg.TEXT_PROMPT_TYPE_TEXT, 'TEXT'],
  593. [Blockly.Msg.TEXT_PROMPT_TYPE_NUMBER, 'NUMBER']];
  594. this.setHelpUrl(Blockly.Msg.TEXT_PROMPT_HELPURL);
  595. this.setColour(Blockly.Blocks.texts.HUE);
  596. // Assign 'this' to a variable for use in the closures below.
  597. var thisBlock = this;
  598. var dropdown = new Blockly.FieldDropdown(TYPES, function(newOp) {
  599. thisBlock.updateType_(newOp);
  600. });
  601. this.appendValueInput('TEXT')
  602. .appendField(dropdown, 'TYPE');
  603. this.setOutput(true, 'String');
  604. this.setTooltip(function() {
  605. return (thisBlock.getFieldValue('TYPE') == 'TEXT') ?
  606. Blockly.Msg.TEXT_PROMPT_TOOLTIP_TEXT :
  607. Blockly.Msg.TEXT_PROMPT_TOOLTIP_NUMBER;
  608. });
  609. },
  610. /**
  611. * Modify this block to have the correct output type.
  612. * @param {string} newOp Either 'TEXT' or 'NUMBER'.
  613. * @private
  614. * @this Blockly.Block
  615. */
  616. updateType_: function(newOp) {
  617. this.outputConnection.setCheck(newOp == 'NUMBER' ? 'Number' : 'String');
  618. },
  619. /**
  620. * Create XML to represent the output type.
  621. * @return {!Element} XML storage element.
  622. * @this Blockly.Block
  623. */
  624. mutationToDom: function() {
  625. var container = document.createElement('mutation');
  626. container.setAttribute('type', this.getFieldValue('TYPE'));
  627. return container;
  628. },
  629. /**
  630. * Parse XML to restore the output type.
  631. * @param {!Element} xmlElement XML storage element.
  632. * @this Blockly.Block
  633. */
  634. domToMutation: function(xmlElement) {
  635. this.updateType_(xmlElement.getAttribute('type'));
  636. }
  637. };
  638. Blockly.Blocks['text_prompt'] = {
  639. /**
  640. * Block for prompt function (internal message).
  641. * The 'text_prompt_ext' block is preferred as it is more flexible.
  642. * @this Blockly.Block
  643. */
  644. init: function() {
  645. var TYPES =
  646. [[Blockly.Msg.TEXT_PROMPT_TYPE_TEXT, 'TEXT'],
  647. [Blockly.Msg.TEXT_PROMPT_TYPE_NUMBER, 'NUMBER']];
  648. // Assign 'this' to a variable for use in the closures below.
  649. var thisBlock = this;
  650. this.setHelpUrl(Blockly.Msg.TEXT_PROMPT_HELPURL);
  651. this.setColour(Blockly.Blocks.texts.HUE);
  652. var dropdown = new Blockly.FieldDropdown(TYPES, function(newOp) {
  653. thisBlock.updateType_(newOp);
  654. });
  655. this.appendDummyInput()
  656. .appendField(dropdown, 'TYPE')
  657. .appendField(this.newQuote_(true))
  658. .appendField(new Blockly.FieldTextInput(''), 'TEXT')
  659. .appendField(this.newQuote_(false));
  660. this.setOutput(true, 'String');
  661. this.setTooltip(function() {
  662. return (thisBlock.getFieldValue('TYPE') == 'TEXT') ?
  663. Blockly.Msg.TEXT_PROMPT_TOOLTIP_TEXT :
  664. Blockly.Msg.TEXT_PROMPT_TOOLTIP_NUMBER;
  665. });
  666. },
  667. newQuote_: Blockly.Blocks['text'].newQuote_,
  668. updateType_: Blockly.Blocks['text_prompt_ext'].updateType_,
  669. mutationToDom: Blockly.Blocks['text_prompt_ext'].mutationToDom,
  670. domToMutation: Blockly.Blocks['text_prompt_ext'].domToMutation
  671. };
  672. Blockly.Blocks['text_lower'] = {
  673. init: function() {
  674. this.appendValueInput("TEXT_INPUT")
  675. .appendField(Blockly.Msg.TEXT_LOWER)
  676. this.setOutput(true, 'String');
  677. this.setColour(Blockly.Blocks.texts.HUE);
  678. this.setInputsInline(true);
  679. }
  680. }