lists.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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 List blocks for Blockly.
  22. * @author fraser@google.com (Neil Fraser)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.Blocks.lists');
  26. goog.require('Blockly.Blocks');
  27. /**
  28. * Common HSV hue for all blocks in this category.
  29. */
  30. Blockly.Blocks.lists.HUE = 195;
  31. Blockly.Blocks['lists_create_empty'] = {
  32. /**
  33. * Block for creating an empty list.
  34. * The 'list_create_with' block is preferred as it is more flexible.
  35. * <block type="lists_create_with">
  36. * <mutation items="0"></mutation>
  37. * </block>
  38. * @this Blockly.Block
  39. */
  40. init: function() {
  41. this.jsonInit({
  42. "message0": Blockly.Msg.LISTS_CREATE_EMPTY_TITLE,
  43. "output": "Array",
  44. "colour": Blockly.Blocks.lists.HUE,
  45. "tooltip": Blockly.Msg.LISTS_CREATE_EMPTY_TOOLTIP,
  46. "helpUrl": Blockly.Msg.LISTS_CREATE_EMPTY_HELPURL
  47. });
  48. }
  49. };
  50. Blockly.Blocks['lists_create_with'] = {
  51. /**
  52. * Block for creating a list with any number of elements of any type.
  53. * @this Blockly.Block
  54. */
  55. init: function() {
  56. this.setHelpUrl(Blockly.Msg.LISTS_CREATE_WITH_HELPURL);
  57. this.setColour(Blockly.Blocks.lists.HUE);
  58. this.itemCount_ = 3;
  59. this.updateShape_();
  60. this.setOutput(true, 'Array');
  61. this.setMutator(new Blockly.Mutator(['lists_create_with_item']));
  62. this.setInputsInline(true);
  63. this.setTooltip(Blockly.Msg.LISTS_CREATE_WITH_TOOLTIP);
  64. },
  65. /**
  66. * Create XML to represent list inputs.
  67. * @return {!Element} XML storage element.
  68. * @this Blockly.Block
  69. */
  70. mutationToDom: function() {
  71. var container = document.createElement('mutation');
  72. container.setAttribute('items', this.itemCount_);
  73. /*if (this.itemCount_ > 3) {
  74. container.setAttribute("inline", "false");
  75. } */
  76. return container;
  77. },
  78. /**
  79. * Parse XML to restore the list inputs.
  80. * @param {!Element} xmlElement XML storage element.
  81. * @this Blockly.Block
  82. */
  83. domToMutation: function(xmlElement) {
  84. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  85. this.updateShape_();
  86. },
  87. /**
  88. * Populate the mutator's dialog with this block's components.
  89. * @param {!Blockly.Workspace} workspace Mutator's workspace.
  90. * @return {!Blockly.Block} Root block in mutator.
  91. * @this Blockly.Block
  92. */
  93. decompose: function(workspace) {
  94. var containerBlock = workspace.newBlock('lists_create_with_container');
  95. containerBlock.initSvg();
  96. var connection = containerBlock.getInput('STACK').connection;
  97. for (var i = 0; i < this.itemCount_; i++) {
  98. var itemBlock = workspace.newBlock('lists_create_with_item');
  99. itemBlock.initSvg();
  100. connection.connect(itemBlock.previousConnection);
  101. connection = itemBlock.nextConnection;
  102. }
  103. return containerBlock;
  104. },
  105. /**
  106. * Reconfigure this block based on the mutator dialog's components.
  107. * @param {!Blockly.Block} containerBlock Root block in mutator.
  108. * @this Blockly.Block
  109. */
  110. compose: function(containerBlock) {
  111. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  112. // Count number of inputs.
  113. var connections = [];
  114. while (itemBlock) {
  115. connections.push(itemBlock.valueConnection_);
  116. itemBlock = itemBlock.nextConnection &&
  117. itemBlock.nextConnection.targetBlock();
  118. }
  119. // Disconnect any children that don't belong.
  120. for (var i = 0; i < this.itemCount_; i++) {
  121. var connection = this.getInput('ADD' + i).connection.targetConnection;
  122. if (connection && connections.indexOf(connection) == -1) {
  123. connection.disconnect();
  124. }
  125. }
  126. this.itemCount_ = connections.length;
  127. this.updateShape_();
  128. // Reconnect any child blocks.
  129. for (var i = 0; i < this.itemCount_; i++) {
  130. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  131. }
  132. },
  133. /**
  134. * Store pointers to any connected child blocks.
  135. * @param {!Blockly.Block} containerBlock Root block in mutator.
  136. * @this Blockly.Block
  137. */
  138. saveConnections: function(containerBlock) {
  139. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  140. var i = 0;
  141. while (itemBlock) {
  142. var input = this.getInput('ADD' + i);
  143. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  144. i++;
  145. itemBlock = itemBlock.nextConnection &&
  146. itemBlock.nextConnection.targetBlock();
  147. }
  148. },
  149. /**
  150. * Modify this block to have the correct number of inputs.
  151. * @private
  152. * @this Blockly.Block
  153. */
  154. updateShape_: function() {
  155. if (this.itemCount_ && this.getInput('EMPTY')) {
  156. this.removeInput('EMPTY');
  157. } else if (!this.itemCount_ && !this.getInput('EMPTY')) {
  158. this.appendDummyInput('EMPTY')
  159. .appendField(Blockly.Msg.LISTS_CREATE_EMPTY_TITLE);
  160. }
  161. // Add new inputs.
  162. for (var i = 0; i < this.itemCount_; i++) {
  163. if (!this.getInput('ADD' + i)) {
  164. var input = this.appendValueInput('ADD' + i);
  165. if (i == 0) {
  166. input.appendField(Blockly.Msg.LISTS_CREATE_WITH_INPUT_WITH);
  167. }
  168. }
  169. }
  170. if (this.itemCount_ > 3) {
  171. this.setInputsInline(false);
  172. } else {
  173. this.setInputsInline(true);
  174. }
  175. // Remove deleted inputs.
  176. while (this.getInput('ADD' + i)) {
  177. this.removeInput('ADD' + i);
  178. i++;
  179. }
  180. }
  181. };
  182. Blockly.Blocks['lists_create_with_container'] = {
  183. /**
  184. * Mutator block for list container.
  185. * @this Blockly.Block
  186. */
  187. init: function() {
  188. this.setColour(Blockly.Blocks.lists.HUE);
  189. this.appendDummyInput()
  190. .appendField(Blockly.Msg.LISTS_CREATE_WITH_CONTAINER_TITLE_ADD);
  191. this.appendStatementInput('STACK');
  192. this.setTooltip(Blockly.Msg.LISTS_CREATE_WITH_CONTAINER_TOOLTIP);
  193. this.contextMenu = false;
  194. }
  195. };
  196. Blockly.Blocks['lists_create_with_item'] = {
  197. /**
  198. * Mutator bolck for adding items.
  199. * @this Blockly.Block
  200. */
  201. init: function() {
  202. this.setColour(Blockly.Blocks.lists.HUE);
  203. this.appendDummyInput()
  204. .appendField(Blockly.Msg.LISTS_CREATE_WITH_ITEM_TITLE);
  205. this.setPreviousStatement(true);
  206. this.setNextStatement(true);
  207. this.setTooltip(Blockly.Msg.LISTS_CREATE_WITH_ITEM_TOOLTIP);
  208. this.contextMenu = false;
  209. }
  210. };
  211. Blockly.Blocks['lists_repeat'] = {
  212. /**
  213. * Block for creating a list with one element repeated.
  214. * @this Blockly.Block
  215. */
  216. init: function() {
  217. this.jsonInit({
  218. "message0": Blockly.Msg.LISTS_REPEAT_TITLE,
  219. "args0": [
  220. {
  221. "type": "input_value",
  222. "name": "ITEM"
  223. },
  224. {
  225. "type": "input_value",
  226. "name": "NUM",
  227. "check": "Number"
  228. }
  229. ],
  230. "output": "Array",
  231. "colour": Blockly.Blocks.lists.HUE,
  232. "tooltip": Blockly.Msg.LISTS_REPEAT_TOOLTIP,
  233. "helpUrl": Blockly.Msg.LISTS_REPEAT_HELPURL
  234. });
  235. }
  236. };
  237. Blockly.Blocks['lists_length'] = {
  238. /**
  239. * Block for list length.
  240. * @this Blockly.Block
  241. */
  242. init: function() {
  243. this.jsonInit({
  244. "message0": Blockly.Msg.LISTS_LENGTH_TITLE,
  245. "args0": [
  246. {
  247. "type": "input_value",
  248. "name": "VALUE",
  249. "check": ['String', 'Array']
  250. }
  251. ],
  252. "output": 'Number',
  253. "colour": Blockly.Blocks.lists.HUE,
  254. "tooltip": Blockly.Msg.LISTS_LENGTH_TOOLTIP,
  255. "helpUrl": Blockly.Msg.LISTS_LENGTH_HELPURL
  256. });
  257. }
  258. };
  259. Blockly.Blocks['lists_isEmpty'] = {
  260. /**
  261. * Block for is the list empty?
  262. * @this Blockly.Block
  263. */
  264. init: function() {
  265. this.jsonInit({
  266. "message0": Blockly.Msg.LISTS_ISEMPTY_TITLE,
  267. "args0": [
  268. {
  269. "type": "input_value",
  270. "name": "VALUE",
  271. "check": ['String', 'Array']
  272. }
  273. ],
  274. "output": 'Boolean',
  275. "colour": Blockly.Blocks.lists.HUE,
  276. "tooltip": Blockly.Msg.LISTS_ISEMPTY_TOOLTIP,
  277. "helpUrl": Blockly.Msg.LISTS_ISEMPTY_HELPURL
  278. });
  279. }
  280. };
  281. Blockly.Blocks['lists_indexOf'] = {
  282. /**
  283. * Block for finding an item in the list.
  284. * @this Blockly.Block
  285. */
  286. init: function() {
  287. var OPERATORS =
  288. [[Blockly.Msg.LISTS_INDEX_OF_FIRST, 'FIRST'],
  289. [Blockly.Msg.LISTS_INDEX_OF_LAST, 'LAST']];
  290. this.setHelpUrl(Blockly.Msg.LISTS_INDEX_OF_HELPURL);
  291. this.setColour(Blockly.Blocks.lists.HUE);
  292. this.setOutput(true, 'Number');
  293. this.appendValueInput('VALUE')
  294. .setCheck(['Array', 'String'])
  295. .appendField(Blockly.Msg.LISTS_INDEX_OF_INPUT_IN_LIST);
  296. this.appendValueInput('FIND')
  297. .appendField(new Blockly.FieldDropdown(OPERATORS), 'END');
  298. this.setInputsInline(true);
  299. // Assign 'this' to a variable for use in the tooltip closure below.
  300. var thisBlock = this;
  301. this.setTooltip(function() {
  302. return Blockly.Msg.LISTS_INDEX_OF_TOOLTIP.replace('%1',
  303. this.workspace.options.oneBasedIndex ? '0' : '-1');
  304. });
  305. }
  306. };
  307. Blockly.Blocks['lists_index'] = {
  308. /**
  309. * Block for getting element at index.
  310. * @this Blockly.Block
  311. */
  312. init: function() {
  313. this.setHelpUrl(Blockly.Msg.LISTS_GET_INDEX_HELPURL);
  314. this.setColour(Blockly.Blocks.lists.HUE);
  315. this.appendValueInput('ITEM')
  316. .setCheck('Number')
  317. .appendField("get");
  318. this.appendValueInput('LIST')
  319. .setCheck(['Array', 'String'])
  320. .appendField("th item of");
  321. this.setInputsInline(true);
  322. this.setOutput(true);
  323. // Assign 'this' to a variable for use in the tooltip closure below.
  324. var thisBlock = this;
  325. this.setTooltip("Get's the ith item from the list");
  326. }
  327. }
  328. Blockly.Blocks['lists_getIndex'] = {
  329. /**
  330. * Block for getting element at index.
  331. * @this Blockly.Block
  332. */
  333. init: function() {
  334. var MODE =
  335. [[Blockly.Msg.LISTS_GET_INDEX_GET, 'GET'],
  336. [Blockly.Msg.LISTS_GET_INDEX_GET_REMOVE, 'GET_REMOVE'],
  337. [Blockly.Msg.LISTS_GET_INDEX_REMOVE, 'REMOVE']];
  338. this.WHERE_OPTIONS =
  339. [[Blockly.Msg.LISTS_GET_INDEX_FROM_START, 'FROM_START'],
  340. [Blockly.Msg.LISTS_GET_INDEX_FROM_END, 'FROM_END'],
  341. [Blockly.Msg.LISTS_GET_INDEX_FIRST, 'FIRST'],
  342. [Blockly.Msg.LISTS_GET_INDEX_LAST, 'LAST'],
  343. [Blockly.Msg.LISTS_GET_INDEX_RANDOM, 'RANDOM']
  344. ];
  345. this.setHelpUrl(Blockly.Msg.LISTS_GET_INDEX_HELPURL);
  346. this.setColour(Blockly.Blocks.lists.HUE);
  347. var modeMenu = new Blockly.FieldDropdown(MODE, function(value) {
  348. var isStatement = (value == 'REMOVE');
  349. this.sourceBlock_.updateStatement_(isStatement);
  350. });
  351. this.appendValueInput('VALUE')
  352. .setCheck('Array')
  353. .appendField(Blockly.Msg.LISTS_GET_INDEX_INPUT_IN_LIST);
  354. this.appendDummyInput()
  355. .appendField(modeMenu, 'MODE')
  356. .appendField('', 'SPACE');
  357. this.appendDummyInput('AT');
  358. if (Blockly.Msg.LISTS_GET_INDEX_TAIL) {
  359. this.appendDummyInput('TAIL')
  360. .appendField(Blockly.Msg.LISTS_GET_INDEX_TAIL);
  361. }
  362. this.setInputsInline(true);
  363. this.setOutput(true);
  364. this.updateAt_(true);
  365. // Assign 'this' to a variable for use in the tooltip closure below.
  366. var thisBlock = this;
  367. this.setTooltip(function() {
  368. var mode = thisBlock.getFieldValue('MODE');
  369. var where = thisBlock.getFieldValue('WHERE');
  370. var tooltip = '';
  371. switch (mode + ' ' + where) {
  372. case 'GET FROM_START':
  373. case 'GET FROM_END':
  374. tooltip = Blockly.Msg.LISTS_GET_INDEX_TOOLTIP_GET_FROM;
  375. break;
  376. case 'GET FIRST':
  377. tooltip = Blockly.Msg.LISTS_GET_INDEX_TOOLTIP_GET_FIRST;
  378. break;
  379. case 'GET LAST':
  380. tooltip = Blockly.Msg.LISTS_GET_INDEX_TOOLTIP_GET_LAST;
  381. break;
  382. case 'GET RANDOM':
  383. tooltip = Blockly.Msg.LISTS_GET_INDEX_TOOLTIP_GET_RANDOM;
  384. break;
  385. case 'GET_REMOVE FROM_START':
  386. case 'GET_REMOVE FROM_END':
  387. tooltip = Blockly.Msg.LISTS_GET_INDEX_TOOLTIP_GET_REMOVE_FROM;
  388. break;
  389. case 'GET_REMOVE FIRST':
  390. tooltip = Blockly.Msg.LISTS_GET_INDEX_TOOLTIP_GET_REMOVE_FIRST;
  391. break;
  392. case 'GET_REMOVE LAST':
  393. tooltip = Blockly.Msg.LISTS_GET_INDEX_TOOLTIP_GET_REMOVE_LAST;
  394. break;
  395. case 'GET_REMOVE RANDOM':
  396. tooltip = Blockly.Msg.LISTS_GET_INDEX_TOOLTIP_GET_REMOVE_RANDOM;
  397. break;
  398. case 'REMOVE FROM_START':
  399. case 'REMOVE FROM_END':
  400. tooltip = Blockly.Msg.LISTS_GET_INDEX_TOOLTIP_REMOVE_FROM;
  401. break;
  402. case 'REMOVE FIRST':
  403. tooltip = Blockly.Msg.LISTS_GET_INDEX_TOOLTIP_REMOVE_FIRST;
  404. break;
  405. case 'REMOVE LAST':
  406. tooltip = Blockly.Msg.LISTS_GET_INDEX_TOOLTIP_REMOVE_LAST;
  407. break;
  408. case 'REMOVE RANDOM':
  409. tooltip = Blockly.Msg.LISTS_GET_INDEX_TOOLTIP_REMOVE_RANDOM;
  410. break;
  411. }
  412. if (where == 'FROM_START' || where == 'FROM_END') {
  413. var msg = (where == 'FROM_START') ?
  414. Blockly.Msg.LISTS_INDEX_FROM_START_TOOLTIP :
  415. Blockly.Msg.LISTS_INDEX_FROM_END_TOOLTIP;
  416. tooltip += ' ' + msg.replace('%1',
  417. thisBlock.workspace.options.oneBasedIndex ? '#1' : '#0');
  418. }
  419. return tooltip;
  420. });
  421. },
  422. /**
  423. * Create XML to represent whether the block is a statement or a value.
  424. * Also represent whether there is an 'AT' input.
  425. * @return {Element} XML storage element.
  426. * @this Blockly.Block
  427. */
  428. mutationToDom: function() {
  429. var container = document.createElement('mutation');
  430. var isStatement = !this.outputConnection;
  431. container.setAttribute('statement', isStatement);
  432. var isAt = this.getInput('AT').type == Blockly.INPUT_VALUE;
  433. container.setAttribute('at', isAt);
  434. return container;
  435. },
  436. /**
  437. * Parse XML to restore the 'AT' input.
  438. * @param {!Element} xmlElement XML storage element.
  439. * @this Blockly.Block
  440. */
  441. domToMutation: function(xmlElement) {
  442. // Note: Until January 2013 this block did not have mutations,
  443. // so 'statement' defaults to false and 'at' defaults to true.
  444. var isStatement = (xmlElement.getAttribute('statement') == 'true');
  445. this.updateStatement_(isStatement);
  446. var isAt = (xmlElement.getAttribute('at') != 'false');
  447. this.updateAt_(isAt);
  448. },
  449. /**
  450. * Switch between a value block and a statement block.
  451. * @param {boolean} newStatement True if the block should be a statement.
  452. * False if the block should be a value.
  453. * @private
  454. * @this Blockly.Block
  455. */
  456. updateStatement_: function(newStatement) {
  457. var oldStatement = !this.outputConnection;
  458. if (newStatement != oldStatement) {
  459. this.unplug(true, true);
  460. if (newStatement) {
  461. this.setOutput(false);
  462. this.setPreviousStatement(true);
  463. this.setNextStatement(true);
  464. } else {
  465. this.setPreviousStatement(false);
  466. this.setNextStatement(false);
  467. this.setOutput(true);
  468. }
  469. }
  470. },
  471. /**
  472. * Create or delete an input for the numeric index.
  473. * @param {boolean} isAt True if the input should exist.
  474. * @private
  475. * @this Blockly.Block
  476. */
  477. updateAt_: function(isAt) {
  478. // Destroy old 'AT' and 'ORDINAL' inputs.
  479. this.removeInput('AT');
  480. this.removeInput('ORDINAL', true);
  481. // Create either a value 'AT' input or a dummy input.
  482. if (isAt) {
  483. this.appendValueInput('AT').setCheck('Number');
  484. if (Blockly.Msg.ORDINAL_NUMBER_SUFFIX) {
  485. this.appendDummyInput('ORDINAL')
  486. .appendField(Blockly.Msg.ORDINAL_NUMBER_SUFFIX);
  487. }
  488. } else {
  489. this.appendDummyInput('AT');
  490. }
  491. var menu = new Blockly.FieldDropdown(this.WHERE_OPTIONS, function(value) {
  492. var newAt = (value == 'FROM_START') || (value == 'FROM_END');
  493. // The 'isAt' variable is available due to this function being a closure.
  494. if (newAt != isAt) {
  495. var block = this.sourceBlock_;
  496. block.updateAt_(newAt);
  497. // This menu has been destroyed and replaced. Update the replacement.
  498. block.setFieldValue(value, 'WHERE');
  499. return null;
  500. }
  501. return undefined;
  502. });
  503. this.getInput('AT').appendField(menu, 'WHERE');
  504. if (Blockly.Msg.LISTS_GET_INDEX_TAIL) {
  505. this.moveInputBefore('TAIL', null);
  506. }
  507. }
  508. };
  509. Blockly.Blocks['lists_setIndex'] = {
  510. /**
  511. * Block for setting the element at index.
  512. * @this Blockly.Block
  513. */
  514. init: function() {
  515. var MODE =
  516. [[Blockly.Msg.LISTS_SET_INDEX_SET, 'SET'],
  517. [Blockly.Msg.LISTS_SET_INDEX_INSERT, 'INSERT']];
  518. this.WHERE_OPTIONS =
  519. [[Blockly.Msg.LISTS_GET_INDEX_FROM_START, 'FROM_START'],
  520. [Blockly.Msg.LISTS_GET_INDEX_FROM_END, 'FROM_END'],
  521. [Blockly.Msg.LISTS_GET_INDEX_FIRST, 'FIRST'],
  522. [Blockly.Msg.LISTS_GET_INDEX_LAST, 'LAST'],
  523. [Blockly.Msg.LISTS_GET_INDEX_RANDOM, 'RANDOM']];
  524. this.setHelpUrl(Blockly.Msg.LISTS_SET_INDEX_HELPURL);
  525. this.setColour(Blockly.Blocks.lists.HUE);
  526. this.appendValueInput('LIST')
  527. .setCheck('Array')
  528. .appendField(Blockly.Msg.LISTS_SET_INDEX_INPUT_IN_LIST);
  529. this.appendDummyInput()
  530. .appendField(new Blockly.FieldDropdown(MODE), 'MODE')
  531. .appendField('', 'SPACE');
  532. this.appendDummyInput('AT');
  533. this.appendValueInput('TO')
  534. .appendField(Blockly.Msg.LISTS_SET_INDEX_INPUT_TO);
  535. this.setInputsInline(true);
  536. this.setPreviousStatement(true);
  537. this.setNextStatement(true);
  538. this.setTooltip(Blockly.Msg.LISTS_SET_INDEX_TOOLTIP);
  539. this.updateAt_(true);
  540. // Assign 'this' to a variable for use in the tooltip closure below.
  541. var thisBlock = this;
  542. this.setTooltip(function() {
  543. var mode = thisBlock.getFieldValue('MODE');
  544. var where = thisBlock.getFieldValue('WHERE');
  545. var tooltip = '';
  546. switch (mode + ' ' + where) {
  547. case 'SET FROM_START':
  548. case 'SET FROM_END':
  549. tooltip = Blockly.Msg.LISTS_SET_INDEX_TOOLTIP_SET_FROM;
  550. break;
  551. case 'SET FIRST':
  552. tooltip = Blockly.Msg.LISTS_SET_INDEX_TOOLTIP_SET_FIRST;
  553. break;
  554. case 'SET LAST':
  555. tooltip = Blockly.Msg.LISTS_SET_INDEX_TOOLTIP_SET_LAST;
  556. break;
  557. case 'SET RANDOM':
  558. tooltip = Blockly.Msg.LISTS_SET_INDEX_TOOLTIP_SET_RANDOM;
  559. break;
  560. case 'INSERT FROM_START':
  561. case 'INSERT FROM_END':
  562. tooltip = Blockly.Msg.LISTS_SET_INDEX_TOOLTIP_INSERT_FROM;
  563. break;
  564. case 'INSERT FIRST':
  565. tooltip = Blockly.Msg.LISTS_SET_INDEX_TOOLTIP_INSERT_FIRST;
  566. break;
  567. case 'INSERT LAST':
  568. tooltip = Blockly.Msg.LISTS_SET_INDEX_TOOLTIP_INSERT_LAST;
  569. break;
  570. case 'INSERT RANDOM':
  571. tooltip = Blockly.Msg.LISTS_SET_INDEX_TOOLTIP_INSERT_RANDOM;
  572. break;
  573. }
  574. if (where == 'FROM_START' || where == 'FROM_END') {
  575. tooltip += ' ' + Blockly.Msg.LISTS_INDEX_FROM_START_TOOLTIP
  576. .replace('%1',
  577. thisBlock.workspace.options.oneBasedIndex ? '#1' : '#0');
  578. }
  579. return tooltip;
  580. });
  581. },
  582. /**
  583. * Create XML to represent whether there is an 'AT' input.
  584. * @return {Element} XML storage element.
  585. * @this Blockly.Block
  586. */
  587. mutationToDom: function() {
  588. var container = document.createElement('mutation');
  589. var isAt = this.getInput('AT').type == Blockly.INPUT_VALUE;
  590. container.setAttribute('at', isAt);
  591. return container;
  592. },
  593. /**
  594. * Parse XML to restore the 'AT' input.
  595. * @param {!Element} xmlElement XML storage element.
  596. * @this Blockly.Block
  597. */
  598. domToMutation: function(xmlElement) {
  599. // Note: Until January 2013 this block did not have mutations,
  600. // so 'at' defaults to true.
  601. var isAt = (xmlElement.getAttribute('at') != 'false');
  602. this.updateAt_(isAt);
  603. },
  604. /**
  605. * Create or delete an input for the numeric index.
  606. * @param {boolean} isAt True if the input should exist.
  607. * @private
  608. * @this Blockly.Block
  609. */
  610. updateAt_: function(isAt) {
  611. // Destroy old 'AT' and 'ORDINAL' input.
  612. this.removeInput('AT');
  613. this.removeInput('ORDINAL', true);
  614. // Create either a value 'AT' input or a dummy input.
  615. if (isAt) {
  616. this.appendValueInput('AT').setCheck('Number');
  617. if (Blockly.Msg.ORDINAL_NUMBER_SUFFIX) {
  618. this.appendDummyInput('ORDINAL')
  619. .appendField(Blockly.Msg.ORDINAL_NUMBER_SUFFIX);
  620. }
  621. } else {
  622. this.appendDummyInput('AT');
  623. }
  624. var menu = new Blockly.FieldDropdown(this.WHERE_OPTIONS, function(value) {
  625. var newAt = (value == 'FROM_START') || (value == 'FROM_END');
  626. // The 'isAt' variable is available due to this function being a closure.
  627. if (newAt != isAt) {
  628. var block = this.sourceBlock_;
  629. block.updateAt_(newAt);
  630. // This menu has been destroyed and replaced. Update the replacement.
  631. block.setFieldValue(value, 'WHERE');
  632. return null;
  633. }
  634. return undefined;
  635. });
  636. this.moveInputBefore('AT', 'TO');
  637. if (this.getInput('ORDINAL')) {
  638. this.moveInputBefore('ORDINAL', 'TO');
  639. }
  640. this.getInput('AT').appendField(menu, 'WHERE');
  641. }
  642. };
  643. Blockly.Blocks['lists_getSublist'] = {
  644. /**
  645. * Block for getting sublist.
  646. * @this Blockly.Block
  647. */
  648. init: function() {
  649. this['WHERE_OPTIONS_1'] =
  650. [[Blockly.Msg.LISTS_GET_SUBLIST_START_FROM_START, 'FROM_START'],
  651. [Blockly.Msg.LISTS_GET_SUBLIST_START_FROM_END, 'FROM_END'],
  652. [Blockly.Msg.LISTS_GET_SUBLIST_START_FIRST, 'FIRST']];
  653. this['WHERE_OPTIONS_2'] =
  654. [[Blockly.Msg.LISTS_GET_SUBLIST_END_FROM_START, 'FROM_START'],
  655. [Blockly.Msg.LISTS_GET_SUBLIST_END_FROM_END, 'FROM_END'],
  656. [Blockly.Msg.LISTS_GET_SUBLIST_END_LAST, 'LAST']];
  657. this.setHelpUrl(Blockly.Msg.LISTS_GET_SUBLIST_HELPURL);
  658. this.setColour(Blockly.Blocks.lists.HUE);
  659. this.appendValueInput('LIST')
  660. .setCheck(['Array', "String"])
  661. .appendField(Blockly.Msg.LISTS_GET_SUBLIST_INPUT_IN_LIST);
  662. this.appendDummyInput('AT1');
  663. this.appendDummyInput('AT2');
  664. if (Blockly.Msg.LISTS_GET_SUBLIST_TAIL) {
  665. this.appendDummyInput('TAIL')
  666. .appendField(Blockly.Msg.LISTS_GET_SUBLIST_TAIL);
  667. }
  668. this.setInputsInline(true);
  669. this.setOutput(true, 'Array');
  670. this.updateAt_(1, true);
  671. this.updateAt_(2, true);
  672. this.setTooltip(Blockly.Msg.LISTS_GET_SUBLIST_TOOLTIP);
  673. },
  674. /**
  675. * Create XML to represent whether there are 'AT' inputs.
  676. * @return {Element} XML storage element.
  677. * @this Blockly.Block
  678. */
  679. mutationToDom: function() {
  680. var container = document.createElement('mutation');
  681. var isAt1 = this.getInput('AT1').type == Blockly.INPUT_VALUE;
  682. container.setAttribute('at1', isAt1);
  683. var isAt2 = this.getInput('AT2').type == Blockly.INPUT_VALUE;
  684. container.setAttribute('at2', isAt2);
  685. return container;
  686. },
  687. /**
  688. * Parse XML to restore the 'AT' inputs.
  689. * @param {!Element} xmlElement XML storage element.
  690. * @this Blockly.Block
  691. */
  692. domToMutation: function(xmlElement) {
  693. var isAt1 = (xmlElement.getAttribute('at1') == 'true');
  694. var isAt2 = (xmlElement.getAttribute('at2') == 'true');
  695. this.updateAt_(1, isAt1);
  696. this.updateAt_(2, isAt2);
  697. },
  698. /**
  699. * Create or delete an input for a numeric index.
  700. * This block has two such inputs, independant of each other.
  701. * @param {number} n Specify first or second input (1 or 2).
  702. * @param {boolean} isAt True if the input should exist.
  703. * @private
  704. * @this Blockly.Block
  705. */
  706. updateAt_: function(n, isAt) {
  707. // Create or delete an input for the numeric index.
  708. // Destroy old 'AT' and 'ORDINAL' inputs.
  709. this.removeInput('AT' + n);
  710. this.removeInput('ORDINAL' + n, true);
  711. // Create either a value 'AT' input or a dummy input.
  712. if (isAt) {
  713. this.appendValueInput('AT' + n).setCheck('Number');
  714. if (Blockly.Msg.ORDINAL_NUMBER_SUFFIX) {
  715. this.appendDummyInput('ORDINAL' + n)
  716. .appendField(Blockly.Msg.ORDINAL_NUMBER_SUFFIX);
  717. }
  718. } else {
  719. this.appendDummyInput('AT' + n);
  720. }
  721. var menu = new Blockly.FieldDropdown(this['WHERE_OPTIONS_' + n],
  722. function(value) {
  723. var newAt = (value == 'FROM_START') || (value == 'FROM_END');
  724. // The 'isAt' variable is available due to this function being a
  725. // closure.
  726. if (newAt != isAt) {
  727. var block = this.sourceBlock_;
  728. block.updateAt_(n, newAt);
  729. // This menu has been destroyed and replaced.
  730. // Update the replacement.
  731. block.setFieldValue(value, 'WHERE' + n);
  732. return null;
  733. }
  734. return undefined;
  735. });
  736. this.getInput('AT' + n)
  737. .appendField(menu, 'WHERE' + n);
  738. if (n == 1) {
  739. this.moveInputBefore('AT1', 'AT2');
  740. if (this.getInput('ORDINAL1')) {
  741. this.moveInputBefore('ORDINAL1', 'AT2');
  742. }
  743. }
  744. if (Blockly.Msg.LISTS_GET_SUBLIST_TAIL) {
  745. this.moveInputBefore('TAIL', null);
  746. }
  747. }
  748. };
  749. Blockly.Blocks['lists_sort'] = {
  750. /**
  751. * Block for sorting a list.
  752. * @this Blockly.Block
  753. */
  754. init: function() {
  755. this.jsonInit({
  756. "message0": Blockly.Msg.LISTS_SORT_TITLE,
  757. "args0": [
  758. {
  759. "type": "field_dropdown",
  760. "name": "TYPE",
  761. "options": [
  762. [Blockly.Msg.LISTS_SORT_TYPE_NUMERIC, "NUMERIC"],
  763. [Blockly.Msg.LISTS_SORT_TYPE_TEXT, "TEXT"],
  764. [Blockly.Msg.LISTS_SORT_TYPE_IGNORECASE, "IGNORE_CASE"]
  765. ]
  766. },
  767. {
  768. "type": "field_dropdown",
  769. "name": "DIRECTION",
  770. "options": [
  771. [Blockly.Msg.LISTS_SORT_ORDER_ASCENDING, "1"],
  772. [Blockly.Msg.LISTS_SORT_ORDER_DESCENDING, "-1"]
  773. ]
  774. },
  775. {
  776. "type": "input_value",
  777. "name": "LIST",
  778. "check": "Array"
  779. }
  780. ],
  781. "output": "Array",
  782. "colour": Blockly.Blocks.lists.HUE,
  783. "tooltip": Blockly.Msg.LISTS_SORT_TOOLTIP,
  784. "helpUrl": Blockly.Msg.LISTS_SORT_HELPURL
  785. });
  786. }
  787. };
  788. Blockly.Blocks['lists_split'] = {
  789. /**
  790. * Block for splitting text into a list, or joining a list into text.
  791. * @this Blockly.Block
  792. */
  793. init: function() {
  794. // Assign 'this' to a variable for use in the closures below.
  795. var thisBlock = this;
  796. var dropdown = new Blockly.FieldDropdown(
  797. [[Blockly.Msg.LISTS_SPLIT_LIST_FROM_TEXT, 'SPLIT'],
  798. [Blockly.Msg.LISTS_SPLIT_TEXT_FROM_LIST, 'JOIN']],
  799. function(newMode) {
  800. thisBlock.updateType_(newMode);
  801. });
  802. this.setHelpUrl(Blockly.Msg.LISTS_SPLIT_HELPURL);
  803. this.setColour(Blockly.Blocks.lists.HUE);
  804. this.appendValueInput('INPUT')
  805. .setCheck('String')
  806. .appendField(dropdown, 'MODE');
  807. this.appendValueInput('DELIM')
  808. .setCheck('String')
  809. .appendField(Blockly.Msg.LISTS_SPLIT_WITH_DELIMITER);
  810. this.setInputsInline(true);
  811. this.setOutput(true, 'Array');
  812. this.setTooltip(function() {
  813. var mode = thisBlock.getFieldValue('MODE');
  814. if (mode == 'SPLIT') {
  815. return Blockly.Msg.LISTS_SPLIT_TOOLTIP_SPLIT;
  816. } else if (mode == 'JOIN') {
  817. return Blockly.Msg.LISTS_SPLIT_TOOLTIP_JOIN;
  818. }
  819. throw 'Unknown mode: ' + mode;
  820. });
  821. },
  822. /**
  823. * Modify this block to have the correct input and output types.
  824. * @param {string} newMode Either 'SPLIT' or 'JOIN'.
  825. * @private
  826. * @this Blockly.Block
  827. */
  828. updateType_: function(newMode) {
  829. if (newMode == 'SPLIT') {
  830. this.outputConnection.setCheck('Array');
  831. this.getInput('INPUT').setCheck('String');
  832. } else {
  833. this.outputConnection.setCheck('String');
  834. this.getInput('INPUT').setCheck('Array');
  835. }
  836. },
  837. /**
  838. * Create XML to represent the input and output types.
  839. * @return {!Element} XML storage element.
  840. * @this Blockly.Block
  841. */
  842. mutationToDom: function() {
  843. var container = document.createElement('mutation');
  844. container.setAttribute('mode', this.getFieldValue('MODE'));
  845. return container;
  846. },
  847. /**
  848. * Parse XML to restore the input and output types.
  849. * @param {!Element} xmlElement XML storage element.
  850. * @this Blockly.Block
  851. */
  852. domToMutation: function(xmlElement) {
  853. this.updateType_(xmlElement.getAttribute('mode'));
  854. }
  855. };
  856. Blockly.Blocks['lists_append'] = {
  857. // Set element at index.
  858. init: function() {
  859. this.setHelpUrl(Blockly.Msg.LISTS_APPEND_HELPURL);
  860. this.setColour(Blockly.Blocks.lists.HUE);
  861. this.appendValueInput('LIST')
  862. .setCheck('Array')
  863. .appendField(Blockly.Msg.LISTS_APPEND_TO);
  864. this.appendValueInput('ITEM')
  865. .appendField(Blockly.Msg.LISTS_APPEND);
  866. this.setInputsInline(true);
  867. this.setPreviousStatement(true);
  868. this.setNextStatement(true);
  869. //this.setTooltip(Blockly.Msg.LISTS_APPEND_TOOLTIP);
  870. }
  871. };