File size: 59,185 Bytes
fbe84a7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 |
/**
* @license
* Visual Blocks Editor
*
* Copyright 2011 Google Inc.
* https://developers.google.com/blockly/
*
* 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 The class representing one block.
* @author fraser@google.com (Neil Fraser)
*/
'use strict';
goog.provide('Blockly.Block');
goog.require('Blockly.Blocks');
goog.require('Blockly.Colours');
goog.require('Blockly.Comment');
goog.require('Blockly.ScratchBlockComment');
goog.require('Blockly.Connection');
goog.require('Blockly.Events.BlockChange');
goog.require('Blockly.Events.BlockCreate');
goog.require('Blockly.Events.BlockDelete');
goog.require('Blockly.Events.BlockMove');
goog.require('Blockly.Extensions');
goog.require('Blockly.FieldLabelSerializable');
goog.require('Blockly.FieldVariableGetter');
goog.require('Blockly.Input');
goog.require('Blockly.Mutator');
goog.require('Blockly.Warning');
goog.require('Blockly.Workspace');
goog.require('Blockly.Xml');
goog.require('goog.array');
goog.require('goog.asserts');
goog.require('goog.math.Coordinate');
goog.require('goog.string');
/**
* Class for one block.
* Not normally called directly, workspace.newBlock() is preferred.
* @param {!Blockly.Workspace} workspace The block's workspace.
* @param {?string} prototypeName Name of the language object containing
* type-specific functions for this block.
* @param {string=} opt_id Optional ID. Use this ID if provided, otherwise
* create a new ID. If the ID conflicts with an in-use ID, a new one will
* be generated.
* @constructor
*/
Blockly.Block = function(workspace, prototypeName, opt_id) {
var flyoutWorkspace = workspace && workspace.getFlyout && workspace.getFlyout() ?
workspace.getFlyout().getWorkspace() : null;
/** @type {string} */
this.id = (opt_id && !workspace.getBlockById(opt_id) &&
(!flyoutWorkspace || !flyoutWorkspace.getBlockById(opt_id))) ?
opt_id : Blockly.utils.genUid();
workspace.blockDB_[this.id] = this;
/** @type {Blockly.Connection} */
this.outputConnection = null;
/** @type {Blockly.Connection} */
this.nextConnection = null;
/** @type {Blockly.Connection} */
this.previousConnection = null;
/** @type {!Array.<!Blockly.Input>} */
this.inputList = [];
/** @type {boolean|undefined} */
this.inputsInline = true;
/** @type {boolean} */
this.disabled = false;
/** @type {string|!Function} */
this.tooltip = '';
/** @type {boolean} */
this.contextMenu = true;
/**
* @type {Blockly.Block}
* @protected
*/
this.parentBlock_ = null;
/**
* @type {!Array.<!Blockly.Block>}
* @protected
*/
this.childBlocks_ = [];
/**
* @type {boolean}
* @private
*/
this.deletable_ = true;
/**
* @type {boolean}
* @private
*/
this.movable_ = true;
/**
* @type {boolean}
* @private
*/
this.editable_ = true;
/**
* @type {boolean}
* @private
*/
this.isShadow_ = false;
/**
* @type {boolean}
* @protected
*/
this.collapsed_ = false;
/**
* @type {boolean}
* @private
*/
this.checkboxInFlyout_ = false;
/**
* @type {boolean} pm: Determines whether or not this block will duplicate on drag.
* @private
*/
this.canDragDuplicate_ = false;
/** @type {string|Blockly.Comment} */
this.comment = null;
/**
* @type {?number}
* @private
*/
this.outputShape_ = null;
/**
* @type {?string}
* @private
*/
this.category_ = null;
/**
* The block's position in workspace units. (0, 0) is at the workspace's
* origin; scale does not change this value.
* @type {!goog.math.Coordinate}
* @private
*/
this.xy_ = new goog.math.Coordinate(0, 0);
/** @type {!Blockly.Workspace} */
this.workspace = workspace;
/** @type {boolean} */
this.isInFlyout = workspace.isFlyout;
/** @type {boolean} */
this.isInMutator = workspace.isMutator;
/** @type {boolean} */
this.RTL = workspace.RTL;
/** @type {boolean} */
this.isInsertionMarker_ = false;
// Copy the type-specific functions and data from the prototype.
if (prototypeName) {
/** @type {string} */
this.type = prototypeName;
var prototype = Blockly.Blocks[prototypeName];
goog.asserts.assertObject(prototype,
'Error: Unknown block type "%s".', prototypeName);
goog.mixin(this, prototype);
}
workspace.addTopBlock(this);
// Call an initialization function, if it exists.
if (goog.isFunction(this.init)) {
this.init();
}
// Record initial inline state.
/** @type {boolean|undefined} */
this.inputsInlineDefault = this.inputsInline;
// Fire a create event.
if (Blockly.Events.isEnabled()) {
var existingGroup = Blockly.Events.getGroup();
if (!existingGroup) {
Blockly.Events.setGroup(true);
}
try {
Blockly.Events.fire(new Blockly.Events.BlockCreate(this));
} finally {
if (!existingGroup) {
Blockly.Events.setGroup(false);
}
}
}
// Bind an onchange function, if it exists.
if (goog.isFunction(this.onchange)) {
this.setOnChange(this.onchange);
}
};
/**
* Optional text data that round-trips beween blocks and XML.
* Has no effect. May be used by 3rd parties for meta information.
* @type {?string}
*/
Blockly.Block.prototype.data = null;
/**
* Colour of the block in '#RRGGBB' format.
* @type {string}
* @private
*/
Blockly.Block.prototype.colour_ = '#FF0000';
/**
* Secondary colour of the block in '#RRGGBB' format.
* @type {string}
* @private
*/
Blockly.Block.prototype.colourSecondary_ = '#FF0000';
/**
* Tertiary colour of the block in '#RRGGBB' format.
* @type {string}
* @private
*/
Blockly.Block.prototype.colourTertiary_ = '#FF0000';
/**
* Fill colour used to override default shadow colour behaviour.
* @type {string}
* @private
*/
Blockly.Block.prototype.shadowColour_ = null;
/**
* Dispose of this block.
* @param {boolean} healStack If true, then try to heal any gap by connecting
* the next statement with the previous statement. Otherwise, dispose of
* all children of this block.
*/
Blockly.Block.prototype.dispose = function(healStack) {
if (!this.workspace) {
// Already deleted.
return;
}
// Terminate onchange event calls.
if (this.onchangeWrapper_) {
this.workspace.removeChangeListener(this.onchangeWrapper_);
}
this.unplug(healStack);
if (Blockly.Events.isEnabled()) {
Blockly.Events.fire(new Blockly.Events.BlockDelete(this));
}
Blockly.Events.disable();
try {
// This block is now at the top of the workspace.
// Remove this block from the workspace's list of top-most blocks.
if (this.workspace) {
this.workspace.removeTopBlock(this);
// Remove from block database.
delete this.workspace.blockDB_[this.id];
this.workspace = null;
}
// Just deleting this block from the DOM would result in a memory leak as
// well as corruption of the connection database. Therefore we must
// methodically step through the blocks and carefully disassemble them.
if (Blockly.selected == this) {
Blockly.selected = null;
}
// First, dispose of all my children.
for (var i = this.childBlocks_.length - 1; i >= 0; i--) {
this.childBlocks_[i].dispose(false);
}
// Then dispose of myself.
// Dispose of all inputs and their fields.
for (var i = 0, input; input = this.inputList[i]; i++) {
input.dispose();
}
this.inputList.length = 0;
// Dispose of any remaining connections (next/previous/output).
var connections = this.getConnections_(true);
for (var i = 0; i < connections.length; i++) {
var connection = connections[i];
if (connection.isConnected()) {
connection.disconnect();
}
connections[i].dispose();
}
} finally {
Blockly.Events.enable();
}
};
/**
* Call initModel on all fields on the block.
* May be called more than once.
* Either initModel or initSvg must be called after creating a block and before
* the first interaction with it. Interactions include UI actions
* (e.g. clicking and dragging) and firing events (e.g. create, delete, and
* change).
* @public
*/
Blockly.Block.prototype.initModel = function() {
for (var i = 0, input; input = this.inputList[i]; i++) {
for (var j = 0, field; field = input.fieldRow[j]; j++) {
if (field.initModel) {
field.initModel();
}
}
}
};
/**
* Unplug this block from its superior block. If this block is a statement,
* optionally reconnect the block underneath with the block on top.
* @param {boolean=} opt_healStack Disconnect child statement and reconnect
* stack. Defaults to false.
*/
Blockly.Block.prototype.unplug = function(opt_healStack) {
if (this.outputConnection) {
if (this.outputConnection.isConnected()) {
// Disconnect from any superior block.
this.outputConnection.disconnect();
}
} else {
if (this.previousConnection) {
var previousTarget = null;
if (this.previousConnection.isConnected()) {
// Remember the connection that any next statements need to connect to.
previousTarget = this.previousConnection.targetConnection;
// Detach this block from the parent's tree.
this.previousConnection.disconnect();
}
}
var nextBlock = this.getNextBlock();
if (opt_healStack && nextBlock) {
// Disconnect the next statement.
var nextTarget = this.nextConnection.targetConnection;
nextTarget.disconnect();
if (previousTarget && previousTarget.checkType_(nextTarget)) {
// Attach the next statement to the previous statement.
previousTarget.connect(nextTarget);
}
}
}
};
/**
* Returns all connections originating from this block.
* @return {!Array.<!Blockly.Connection>} Array of connections.
* @private
*/
Blockly.Block.prototype.getConnections_ = function() {
var myConnections = [];
if (this.outputConnection) {
myConnections.push(this.outputConnection);
}
if (this.previousConnection) {
myConnections.push(this.previousConnection);
}
if (this.nextConnection) {
myConnections.push(this.nextConnection);
}
for (var i = 0, input; input = this.inputList[i]; i++) {
if (input.connection) {
myConnections.push(input.connection);
}
}
return myConnections;
};
/**
* Walks down a stack of blocks and finds the last next connection on the stack.
* @return {Blockly.Connection} The last next connection on the stack, or null.
* @package
*/
Blockly.Block.prototype.lastConnectionInStack = function() {
var nextConnection = this.nextConnection;
while (nextConnection) {
var nextBlock = nextConnection.targetBlock();
if (!nextBlock) {
// Found a next connection with nothing on the other side.
return nextConnection;
}
nextConnection = nextBlock.nextConnection;
}
// Ran out of next connections.
return null;
};
/**
* Bump unconnected blocks out of alignment. Two blocks which aren't actually
* connected should not coincidentally line up on screen.
* @protected
*/
Blockly.Block.prototype.bumpNeighbours_ = function() {
console.warn('Not expected to reach this bumpNeighbours_ function. The ' +
'BlockSvg function for bumpNeighbours_ was expected to be called instead.');
};
/**
* Return the parent block or null if this block is at the top level.
* @return {Blockly.Block} The block that holds the current block.
*/
Blockly.Block.prototype.getParent = function() {
// Look at the DOM to see if we are nested in another block.
return this.parentBlock_;
};
/**
* Return the input that connects to the specified block.
* @param {!Blockly.Block} block A block connected to an input on this block.
* @return {Blockly.Input} The input that connects to the specified block.
*/
Blockly.Block.prototype.getInputWithBlock = function(block) {
for (var i = 0, input; input = this.inputList[i]; i++) {
if (input.connection && input.connection.targetBlock() == block) {
return input;
}
}
return null;
};
/**
* Return the input that contains the specified connection
* @param {!Blockly.Connection} conn A connection on this block.
* @return {Blockly.Input} The input that contains the specified connection.
*/
Blockly.Block.prototype.getInputWithConnection = function(conn) {
for (var i = 0, input; input = this.inputList[i]; i++) {
if (input.connection == conn) {
return input;
}
}
return null;
};
/**
* Return the parent block that surrounds the current block, or null if this
* block has no surrounding block. A parent block might just be the previous
* statement, whereas the surrounding block is an if statement, while loop, etc.
* @return {Blockly.Block} The block that surrounds the current block.
*/
Blockly.Block.prototype.getSurroundParent = function() {
var block = this;
do {
var prevBlock = block;
block = block.getParent();
if (!block) {
// Ran off the top.
return null;
}
} while (block.getNextBlock() == prevBlock);
// This block is an enclosing parent, not just a statement in a stack.
return block;
};
/**
* Return the next statement block directly connected to this block.
* @return {Blockly.Block} The next statement block or null.
*/
Blockly.Block.prototype.getNextBlock = function() {
return this.nextConnection && this.nextConnection.targetBlock();
};
/**
* Return the previous statement block directly connected to this block.
* @return {Blockly.Block} The previous statement block or null.
*/
Blockly.Block.prototype.getPreviousBlock = function() {
return this.previousConnection && this.previousConnection.targetBlock();
};
/**
* Return the connection on the first statement input on this block, or null if
* there are none.
* @return {Blockly.Connection} The first statement connection or null.
*/
Blockly.Block.prototype.getFirstStatementConnection = function() {
for (var i = 0, input; input = this.inputList[i]; i++) {
if (input.connection && input.connection.type == Blockly.NEXT_STATEMENT) {
return input.connection;
}
}
return null;
};
/**
* Return the top-most block in this block's tree.
* This will return itself if this block is at the top level.
* @return {!Blockly.Block} The root block.
*/
Blockly.Block.prototype.getRootBlock = function() {
var rootBlock;
var block = this;
do {
rootBlock = block;
block = rootBlock.parentBlock_;
} while (block);
return rootBlock;
};
/**
* Find all the blocks that are directly nested inside this one.
* Includes value and statement inputs, as well as any following statement.
* Excludes any connection on an output tab or any preceding statement.
* Blocks are optionally sorted by position; top to bottom.
* @param {boolean} ordered Sort the list if true.
* @return {!Array.<!Blockly.Block>} Array of blocks.
*/
Blockly.Block.prototype.getChildren = function(ordered) {
if (!ordered) {
return this.childBlocks_;
}
var blocks = [];
for (var i = 0, input; input = this.inputList[i]; i++) {
if (input.connection) {
var child = input.connection.targetBlock();
if (child) {
blocks.push(child);
}
}
}
var next = this.getNextBlock();
if (next) {
blocks.push(next);
}
return blocks;
};
/**
* Set parent of this block to be a new block or null.
* @param {Blockly.Block} newParent New parent block.
*/
Blockly.Block.prototype.setParent = function(newParent) {
if (newParent == this.parentBlock_) {
return;
}
if (this.parentBlock_) {
// Remove this block from the old parent's child list.
goog.array.remove(this.parentBlock_.childBlocks_, this);
// Disconnect from superior blocks.
if (this.previousConnection && this.previousConnection.isConnected()) {
throw 'Still connected to previous block.';
}
if (this.outputConnection && this.outputConnection.isConnected()) {
throw 'Still connected to parent block.';
}
this.parentBlock_ = null;
// This block hasn't actually moved on-screen, so there's no need to update
// its connection locations.
} else {
// Remove this block from the workspace's list of top-most blocks.
this.workspace.removeTopBlock(this);
}
this.parentBlock_ = newParent;
if (newParent) {
// Add this block to the new parent's child list.
newParent.childBlocks_.push(this);
} else {
this.workspace.addTopBlock(this);
}
};
/**
* Find all the blocks that are directly or indirectly nested inside this one.
* Includes this block in the list.
* Includes value and statement inputs, as well as any following statements.
* Excludes any connection on an output tab or any preceding statements.
* Blocks are optionally sorted by position, top to bottom.
* @param {boolean} ordered Sort the list if true.
* @param {boolean=} opt_ignoreShadows If set, don't include shadow blocks.
* @return {!Array.<!Blockly.Block>} Flattened array of blocks.
*/
Blockly.Block.prototype.getDescendants = function(ordered, opt_ignoreShadows) {
var blocks = [this];
var childBlocks = this.getChildren(ordered);
for (var child, i = 0; child = childBlocks[i]; i++) {
if (!opt_ignoreShadows || !child.isShadow_) {
blocks.push.apply(
blocks, child.getDescendants(ordered, opt_ignoreShadows));
}
}
return blocks;
};
/**
* Get whether this block is deletable or not.
* @return {boolean} True if deletable.
*/
Blockly.Block.prototype.isDeletable = function() {
return this.deletable_ && !this.isShadow_ &&
!(this.workspace && this.workspace.options.readOnly);
};
/**
* Set whether this block is deletable or not.
* @param {boolean} deletable True if deletable.
*/
Blockly.Block.prototype.setDeletable = function(deletable) {
this.deletable_ = deletable;
};
/**
* Get whether this block is movable or not.
* @return {boolean} True if movable.
*/
Blockly.Block.prototype.isMovable = function() {
return this.movable_ && !this.isShadow_ &&
!(this.workspace && this.workspace.options.readOnly);
};
/**
* Set whether this block is movable or not.
* @param {boolean} movable True if movable.
*/
Blockly.Block.prototype.setMovable = function(movable) {
this.movable_ = movable;
};
/**
* Get whether this block is a shadow block or not.
* @return {boolean} True if a shadow.
*/
Blockly.Block.prototype.isShadow = function() {
return this.isShadow_;
};
/**
* Set whether this block is a shadow block or not.
* @param {boolean} shadow True if a shadow.
*/
Blockly.Block.prototype.setShadow = function(shadow) {
this.isShadow_ = shadow;
};
/**
* Get whether this block is an insertion marker block or not.
* @return {boolean} True if an insertion marker.
*/
Blockly.Block.prototype.isInsertionMarker = function() {
return this.isInsertionMarker_;
};
/**
* Set whether this block is an insertion marker block or not.
* @param {boolean} insertionMarker True if an insertion marker.
*/
Blockly.Block.prototype.setInsertionMarker = function(insertionMarker) {
if (this.isInsertionMarker_ == insertionMarker) {
return; // No change.
}
this.isInsertionMarker_ = insertionMarker;
// TODO: handle removing insertion marker status.
if (this.isInsertionMarker_) {
this.setColour(Blockly.Colours.insertionMarker);
this.setOpacity(Blockly.Colours.insertionMarkerOpacity);
Blockly.utils.addClass(/** @type {!Element} */ (this.svgGroup_),
'blocklyInsertionMarker');
}
};
/**
* Get whether this block is editable or not.
* @return {boolean} True if editable.
*/
Blockly.Block.prototype.isEditable = function() {
return this.editable_ && !(this.workspace && this.workspace.options.readOnly);
};
/**
* Set whether this block is editable or not.
* @param {boolean} editable True if editable.
*/
Blockly.Block.prototype.setEditable = function(editable) {
this.editable_ = editable;
for (var i = 0, input; input = this.inputList[i]; i++) {
for (var j = 0, field; field = input.fieldRow[j]; j++) {
field.updateEditable();
}
}
};
/**
* Set whether the connections are hidden (not tracked in a database) or not.
* Recursively walk down all child blocks (except collapsed blocks).
* @param {boolean} hidden True if connections are hidden.
*/
Blockly.Block.prototype.setConnectionsHidden = function(hidden) {
if (!hidden && this.isCollapsed()) {
if (this.outputConnection) {
this.outputConnection.setHidden(hidden);
}
if (this.previousConnection) {
this.previousConnection.setHidden(hidden);
}
if (this.nextConnection) {
this.nextConnection.setHidden(hidden);
var child = this.nextConnection.targetBlock();
if (child) {
child.setConnectionsHidden(hidden);
}
}
} else {
var myConnections = this.getConnections_(true);
for (var i = 0, connection; connection = myConnections[i]; i++) {
connection.setHidden(hidden);
if (connection.isSuperior()) {
var child = connection.targetBlock();
if (child) {
child.setConnectionsHidden(hidden);
}
}
}
}
};
/**
* Find the connection on this block that corresponds to the given connection
* on the other block.
* Used to match connections between a block and its insertion marker.
* @param {!Blockly.Block} otherBlock The other block to match against.
* @param {!Blockly.Connection} conn The other connection to match.
* @return {Blockly.Connection} the matching connection on this block, or null.
*/
Blockly.Block.prototype.getMatchingConnection = function(otherBlock, conn) {
var connections = this.getConnections_(true);
var otherConnections = otherBlock.getConnections_(true);
if (connections.length != otherConnections.length) {
throw "Connection lists did not match in length.";
}
for (var i = 0; i < otherConnections.length; i++) {
if (otherConnections[i] == conn) {
return connections[i];
}
}
return null;
};
/**
* Set the URL of this block's help page.
* @param {string|Function} url URL string for block help, or function that
* returns a URL. Null for no help.
*/
Blockly.Block.prototype.setHelpUrl = function(url) {
this.helpUrl = url;
};
/**
* Change the tooltip text for a block.
* @param {string|!Function} newTip Text for tooltip or a parent element to
* link to for its tooltip. May be a function that returns a string.
*/
Blockly.Block.prototype.setTooltip = function(newTip) {
this.tooltip = newTip;
};
/**
* Get the colour of a block.
* @return {string} #RRGGBB string.
*/
Blockly.Block.prototype.getColour = function() {
return this.colour_;
};
/**
* Get the secondary colour of a block.
* @return {string} #RRGGBB string.
*/
Blockly.Block.prototype.getColourSecondary = function() {
return this.colourSecondary_;
};
/**
* Get the tertiary colour of a block.
* @return {string} #RRGGBB string.
*/
Blockly.Block.prototype.getColourTertiary = function() {
return this.colourTertiary_;
};
/**
* Get the shadow colour of a block.
* @return {string} #RRGGBB string.
*/
Blockly.Block.prototype.getShadowColour = function() {
return this.shadowColour_;
};
/**
* Set the shadow colour of a block.
* @param {number|string} colour HSV hue value, or #RRGGBB string.
*/
Blockly.Block.prototype.setShadowColour = function(colour) {
this.shadowColour_ = this.makeColour_(colour);
if (this.rendered) {
this.updateColour();
}
};
/**
* Clear the shadow colour of a block.
*/
Blockly.Block.prototype.clearShadowColour = function() {
this.shadowColour_ = null;
if (this.rendered) {
this.updateColour();
}
};
/**
* Create an #RRGGBB string colour from a colour HSV hue value or #RRGGBB string.
* @param {number|string} colour HSV hue value, or #RRGGBB string.
* @return {string} #RRGGBB string.
* @private
*/
Blockly.Block.prototype.makeColour_ = function(colour) {
var hue = Number(colour);
if (!isNaN(hue)) {
return Blockly.hueToRgb(hue);
} else if (goog.isString(colour) && colour.match(/^#[0-9a-fA-F]{6,8}$/)) {
return colour;
} else {
throw 'Invalid colour: ' + colour;
}
};
/**
* Change the colour of a block, and optional secondary/teriarty colours.
* @param {number|string} colour HSV hue value, or #RRGGBB string.
* @param {number|string} colourSecondary HSV hue value, or #RRGGBB string.
* @param {number|string} colourTertiary HSV hue value, or #RRGGBB string.
*/
Blockly.Block.prototype.setColour = function(colour, colourSecondary, colourTertiary) {
this.colour_ = this.makeColour_(colour);
if (colourSecondary !== undefined) {
this.colourSecondary_ = this.makeColour_(colourSecondary);
} else {
this.colourSecondary_ = goog.color.rgbArrayToHex(
goog.color.darken(goog.color.hexToRgb(this.colour_), 0.1));
}
if (colourTertiary !== undefined) {
this.colourTertiary_ = this.makeColour_(colourTertiary);
} else {
this.colourTertiary_ = goog.color.rgbArrayToHex(
goog.color.darken(goog.color.hexToRgb(this.colour_), 0.2));
}
if (this.rendered) {
this.updateColour();
}
};
/**
* Sets a callback function to use whenever the block's parent workspace
* changes, replacing any prior onchange handler. This is usually only called
* from the constructor, the block type initializer function, or an extension
* initializer function.
* @param {function(Blockly.Events.Abstract)} onchangeFn The callback to call
* when the block's workspace changes.
* @throws {Error} if onchangeFn is not falsey or a function.
*/
Blockly.Block.prototype.setOnChange = function(onchangeFn) {
if (onchangeFn && !goog.isFunction(onchangeFn)) {
throw new Error("onchange must be a function.");
}
if (this.onchangeWrapper_) {
this.workspace.removeChangeListener(this.onchangeWrapper_);
}
this.onchange = onchangeFn;
if (this.onchange) {
this.onchangeWrapper_ = onchangeFn.bind(this);
this.workspace.addChangeListener(this.onchangeWrapper_);
}
};
/**
* Returns the named field from a block.
* @param {string} name The name of the field.
* @return {Blockly.Field} Named field, or null if field does not exist.
*/
Blockly.Block.prototype.getField = function(name) {
for (var i = 0, input; input = this.inputList[i]; i++) {
for (var j = 0, field; field = input.fieldRow[j]; j++) {
if (field.name === name) {
return field;
}
}
}
return null;
};
/**
* Return all variables referenced by this block.
* @return {!Array.<string>} List of variable names.
* @package
*/
Blockly.Block.prototype.getVars = function() {
var vars = [];
for (var i = 0, input; input = this.inputList[i]; i++) {
for (var j = 0, field; field = input.fieldRow[j]; j++) {
if (field.referencesVariables()) {
vars.push(field.getValue());
}
}
}
return vars;
};
/**
* Return all variables referenced by this block.
* @return {!Array.<!Blockly.VariableModel>} List of variable models.
* @package
*/
Blockly.Block.prototype.getVarModels = function() {
var vars = [];
for (var i = 0, input; input = this.inputList[i]; i++) {
for (var j = 0, field; field = input.fieldRow[j]; j++) {
if (field.referencesVariables()) {
var model = this.workspace.getVariableById(field.getValue());
// Check if the variable actually exists (and isn't just a potential
// variable).
if (model) {
vars.push(model);
}
}
}
}
return vars;
};
/**
* Notification that a variable is renaming but keeping the same ID. If the
* variable is in use on this block, rerender to show the new name.
* @param {!Blockly.VariableModel} variable The variable being renamed.
* @package
*/
Blockly.Block.prototype.updateVarName = function(variable) {
for (var i = 0, input; input = this.inputList[i]; i++) {
for (var j = 0, field; field = input.fieldRow[j]; j++) {
if (field.referencesVariables() &&
variable.getId() == field.getValue()) {
field.setText(variable.name);
}
}
}
};
/**
* Notification that a variable is renaming.
* If the ID matches one of this block's variables, rename it.
* @param {string} oldId ID of variable to rename.
* @param {string} newId ID of new variable. May be the same as oldId, but with
* an updated name.
*/
Blockly.Block.prototype.renameVarById = function(oldId, newId) {
for (var i = 0, input; input = this.inputList[i]; i++) {
for (var j = 0, field; field = input.fieldRow[j]; j++) {
if (field.referencesVariables() &&
oldId == field.getValue()) {
field.setValue(newId);
}
}
}
};
/**
* Returns the language-neutral value from the field of a block.
* @param {string} name The name of the field.
* @return {?string} Value from the field or null if field does not exist.
*/
Blockly.Block.prototype.getFieldValue = function(name) {
var field = this.getField(name);
if (field) {
return field.getValue();
}
return null;
};
/**
* Change the field value for a block (e.g. 'CHOOSE' or 'REMOVE').
* @param {string} newValue Value to be the new field.
* @param {string} name The name of the field.
*/
Blockly.Block.prototype.setFieldValue = function(newValue, name) {
var field = this.getField(name);
goog.asserts.assertObject(field, 'Field "%s" not found.', name);
field.setValue(newValue);
};
/**
* Set whether this block can chain onto the bottom of another block.
* @param {boolean} newBoolean True if there can be a previous statement.
* @param {(string|Array.<string>|null)=} opt_check Statement type or
* list of statement types. Null/undefined if any type could be connected.
*/
Blockly.Block.prototype.setPreviousStatement = function(newBoolean, opt_check) {
if (newBoolean) {
if (opt_check === undefined) {
opt_check = null;
}
if (!this.previousConnection) {
goog.asserts.assert(!this.outputConnection,
'Remove output connection prior to adding previous connection.');
this.previousConnection =
this.makeConnection_(Blockly.PREVIOUS_STATEMENT);
}
this.previousConnection.setCheck(opt_check);
} else {
if (this.previousConnection) {
goog.asserts.assert(!this.previousConnection.isConnected(),
'Must disconnect previous statement before removing connection.');
this.previousConnection.dispose();
this.previousConnection = null;
}
}
};
/**
* Set whether another block can chain onto the bottom of this block.
* @param {boolean} newBoolean True if there can be a next statement.
* @param {(string|Array.<string>|null)=} opt_check Statement type or
* list of statement types. Null/undefined if any type could be connected.
*/
Blockly.Block.prototype.setNextStatement = function(newBoolean, opt_check) {
if (newBoolean) {
if (opt_check === undefined) {
opt_check = null;
}
if (!this.nextConnection) {
this.nextConnection = this.makeConnection_(Blockly.NEXT_STATEMENT);
}
this.nextConnection.setCheck(opt_check);
} else {
if (this.nextConnection) {
goog.asserts.assert(!this.nextConnection.isConnected(),
'Must disconnect next statement before removing connection.');
this.nextConnection.dispose();
this.nextConnection = null;
}
}
};
/**
* Set whether this block returns a value.
* @param {boolean} newBoolean True if there is an output.
* @param {(string|Array.<string>|null)=} opt_check Returned type or list
* of returned types. Null or undefined if any type could be returned
* (e.g. variable get).
*/
Blockly.Block.prototype.setOutput = function(newBoolean, opt_check) {
if (newBoolean) {
if (opt_check === undefined) {
opt_check = null;
}
if (!this.outputConnection) {
goog.asserts.assert(!this.previousConnection,
'Remove previous connection prior to adding output connection.');
this.outputConnection = this.makeConnection_(Blockly.OUTPUT_VALUE);
}
this.outputConnection.setCheck(opt_check);
} else {
if (this.outputConnection) {
goog.asserts.assert(!this.outputConnection.isConnected(),
'Must disconnect output value before removing connection.');
this.outputConnection.dispose();
this.outputConnection = null;
}
}
};
/**
* Set whether value inputs are arranged horizontally or vertically.
* @param {boolean} newBoolean True if inputs are horizontal.
*/
Blockly.Block.prototype.setInputsInline = function(newBoolean) {
if (this.inputsInline != newBoolean) {
Blockly.Events.fire(new Blockly.Events.BlockChange(
this, 'inline', null, this.inputsInline, newBoolean));
this.inputsInline = newBoolean;
}
};
/**
* Get whether value inputs are arranged horizontally or vertically.
* @return {boolean} True if inputs are horizontal.
*/
Blockly.Block.prototype.getInputsInline = function() {
if (this.inputsInline != undefined) {
// Set explicitly.
return this.inputsInline;
}
// Not defined explicitly. Figure out what would look best.
for (var i = 1; i < this.inputList.length; i++) {
if (this.inputList[i - 1].type == Blockly.DUMMY_INPUT &&
this.inputList[i].type == Blockly.DUMMY_INPUT) {
// Two dummy inputs in a row. Don't inline them.
return false;
}
}
for (var i = 1; i < this.inputList.length; i++) {
if (this.inputList[i - 1].type == Blockly.INPUT_VALUE &&
this.inputList[i].type == Blockly.DUMMY_INPUT) {
// Dummy input after a value input. Inline them.
return true;
}
}
return false;
};
/**
* Set whether the block is disabled or not.
* @param {boolean} disabled True if disabled.
*/
Blockly.Block.prototype.setDisabled = function(disabled) {
if (this.disabled != disabled) {
Blockly.Events.fire(new Blockly.Events.BlockChange(
this, 'disabled', null, this.disabled, disabled));
this.disabled = disabled;
}
};
/**
* Get whether the block is disabled or not due to parents.
* The block's own disabled property is not considered.
* @return {boolean} True if disabled.
*/
Blockly.Block.prototype.getInheritedDisabled = function() {
var ancestor = this.getSurroundParent();
while (ancestor) {
if (ancestor.disabled) {
return true;
}
ancestor = ancestor.getSurroundParent();
}
// Ran off the top.
return false;
};
/**
* Get whether the block is collapsed or not.
* @return {boolean} True if collapsed.
*/
Blockly.Block.prototype.isCollapsed = function() {
return this.collapsed_;
};
/**
* Set whether the block is collapsed or not.
* @param {boolean} collapsed True if collapsed.
*/
Blockly.Block.prototype.setCollapsed = function(collapsed) {
if (this.collapsed_ != collapsed) {
Blockly.Events.fire(new Blockly.Events.BlockChange(
this, 'collapsed', null, this.collapsed_, collapsed));
this.collapsed_ = collapsed;
}
};
/**
* Create a human-readable text representation of this block and any children.
* @param {number=} opt_maxLength Truncate the string to this length.
* @param {string=} opt_emptyToken The placeholder string used to denote an
* empty field. If not specified, '?' is used.
* @return {string} Text of block.
*/
Blockly.Block.prototype.toString = function(opt_maxLength, opt_emptyToken) {
var text = [];
var emptyFieldPlaceholder = opt_emptyToken || '?';
if (this.collapsed_) {
text.push(this.getInput('_TEMP_COLLAPSED_INPUT').fieldRow[0].text_);
} else {
for (var i = 0, input; input = this.inputList[i]; i++) {
for (var j = 0, field; field = input.fieldRow[j]; j++) {
if (field instanceof Blockly.FieldDropdown && !field.getValue()) {
text.push(emptyFieldPlaceholder);
} else {
text.push(field.getText());
}
}
if (input.connection) {
var child = input.connection.targetBlock();
if (child) {
text.push(child.toString(undefined, opt_emptyToken));
} else {
text.push(emptyFieldPlaceholder);
}
}
}
}
text = goog.string.trim(text.join(' ')) || '???';
if (opt_maxLength) {
// TODO: Improve truncation so that text from this block is given priority.
// E.g. "1+2+3+4+5+6+7+8+9=0" should be "...6+7+8+9=0", not "1+2+3+4+5...".
// E.g. "1+2+3+4+5=6+7+8+9+0" should be "...4+5=6+7...".
text = goog.string.truncate(text, opt_maxLength);
}
return text;
};
/**
* Shortcut for appending a value input row.
* @param {string} name Language-neutral identifier which may used to find this
* input again. Should be unique to this block.
* @return {!Blockly.Input} The input object created.
*/
Blockly.Block.prototype.appendValueInput = function(name) {
return this.appendInput_(Blockly.INPUT_VALUE, name);
};
/**
* Shortcut for appending a statement input row.
* @param {string} name Language-neutral identifier which may used to find this
* input again. Should be unique to this block.
* @return {!Blockly.Input} The input object created.
*/
Blockly.Block.prototype.appendStatementInput = function(name) {
return this.appendInput_(Blockly.NEXT_STATEMENT, name);
};
/**
* Shortcut for appending a dummy input row.
* @param {string=} opt_name Language-neutral identifier which may used to find
* this input again. Should be unique to this block.
* @return {!Blockly.Input} The input object created.
*/
Blockly.Block.prototype.appendDummyInput = function(opt_name) {
return this.appendInput_(Blockly.DUMMY_INPUT, opt_name || '');
};
/**
* Initialize this block using a cross-platform, internationalization-friendly
* JSON description.
* @param {!Object} json Structured data describing the block.
*/
Blockly.Block.prototype.jsonInit = function(json) {
var warningPrefix = json['type'] ? 'Block "' + json['type'] + '": ' : '';
// Validate inputs.
goog.asserts.assert(
json['output'] == undefined || json['previousStatement'] == undefined,
warningPrefix + 'Must not have both an output and a previousStatement.');
// Set basic properties of block.
if (json['colour'] !== undefined) {
this.setColourFromJson_(json);
}
// Interpolate the message blocks.
var i = 0;
while (json['message' + i] !== undefined) {
this.interpolate_(json['message' + i], json['args' + i] || [],
json['lastDummyAlign' + i]);
i++;
}
if (json['inputsInline'] !== undefined) {
this.setInputsInline(json['inputsInline']);
}
// Set output and previous/next connections.
if (json['output'] !== undefined) {
this.setOutput(true, json['output']);
}
if (json['previousStatement'] !== undefined) {
this.setPreviousStatement(true, json['previousStatement']);
}
if (json['nextStatement'] !== undefined) {
this.setNextStatement(true, json['nextStatement']);
}
if (json['tooltip'] !== undefined) {
var rawValue = json['tooltip'];
var localizedText = Blockly.utils.replaceMessageReferences(rawValue);
this.setTooltip(localizedText);
}
if (json['enableContextMenu'] !== undefined) {
var rawValue = json['enableContextMenu'];
this.contextMenu = !!rawValue;
}
if (json['helpUrl'] !== undefined) {
var rawValue = json['helpUrl'];
var localizedValue = Blockly.utils.replaceMessageReferences(rawValue);
this.setHelpUrl(localizedValue);
}
if (goog.isString(json['extensions'])) {
console.warn('JSON attribute \'extensions\' should be an array of ' +
'strings. Found raw string in JSON for \'' + json['type'] + '\' block.');
json['extensions'] = [json['extensions']]; // Correct and continue.
}
// Add the mutator to the block
if (json['mutator'] !== undefined) {
Blockly.Extensions.apply(json['mutator'], this, true);
}
if (Array.isArray(json['extensions'])) {
var extensionNames = json['extensions'];
for (var i = 0; i < extensionNames.length; ++i) {
var extensionName = extensionNames[i];
Blockly.Extensions.apply(extensionName, this, false);
}
}
if (json['outputShape'] !== undefined) {
this.setOutputShape(json['outputShape']);
}
if (json['canDragDuplicate'] !== undefined) {
this.setDragDuplication(json['canDragDuplicate']);
}
if (json['checkboxInFlyout'] !== undefined) {
this.setCheckboxInFlyout(json['checkboxInFlyout']);
}
if (json['category'] !== undefined) {
this.setCategory(json['category']);
}
};
/**
* Add key/values from mixinObj to this block object. By default, this method
* will check that the keys in mixinObj will not overwrite existing values in
* the block, including prototype values. This provides some insurance against
* mixin / extension incompatibilities with future block features. This check
* can be disabled by passing true as the second argument.
* @param {!Object} mixinObj The key/values pairs to add to this block object.
* @param {boolean=} opt_disableCheck Option flag to disable overwrite checks.
*/
Blockly.Block.prototype.mixin = function(mixinObj, opt_disableCheck) {
if (goog.isDef(opt_disableCheck) && !goog.isBoolean(opt_disableCheck)) {
throw new Error("opt_disableCheck must be a boolean if provided");
}
if (!opt_disableCheck) {
var overwrites = [];
for (var key in mixinObj) {
if (this[key] !== undefined) {
overwrites.push(key);
}
}
if (overwrites.length) {
throw new Error('Mixin will overwrite block members: ' +
JSON.stringify(overwrites));
}
}
goog.mixin(this, mixinObj);
};
/**
* Set the colour of the block from strings or string table references.
* @param {string|?} primary Primary colour, which may be a string that contains
* string table references.
* @param {string|?} secondary Secondary colour, which may be a string that
* contains string table references.
* @param {string|?} tertiary Tertiary colour, which may be a string that
* contains string table references.
* @private
*/
Blockly.Block.prototype.setColourFromRawValues_ = function(primary, secondary,
tertiary) {
primary = goog.isString(primary) ?
Blockly.utils.replaceMessageReferences(primary) : primary;
secondary = goog.isString(secondary) ?
Blockly.utils.replaceMessageReferences(secondary) : secondary;
tertiary = goog.isString(tertiary) ?
Blockly.utils.replaceMessageReferences(tertiary) : tertiary;
this.setColour(primary, secondary, tertiary);
};
/**
* Set the colour of the block from JSON, replacing message references as
* needed.
* @param {!Object} json Structured data describing the block.
* @private
*/
Blockly.Block.prototype.setColourFromJson_ = function(json) {
this.setColourFromRawValues_(json['colour'], json['colourSecondary'],
json['colourTertiary']);
};
/**
* Interpolate a message description onto the block.
* @param {string} message Text contains interpolation tokens (%1, %2, ...)
* that match with fields or inputs defined in the args array.
* @param {!Array} args Array of arguments to be interpolated.
* @param {string=} lastDummyAlign If a dummy input is added at the end,
* how should it be aligned?
* @private
*/
Blockly.Block.prototype.interpolate_ = function(message, args, lastDummyAlign) {
var tokens = Blockly.utils.tokenizeInterpolation(message);
// Interpolate the arguments. Build a list of elements.
var indexDup = [];
var indexCount = 0;
var elements = [];
for (var i = 0; i < tokens.length; i++) {
var token = tokens[i];
if (typeof token == 'number') {
if (token <= 0 || token > args.length) {
throw new Error('Block "' + this.type + '": ' +
'Message index %' + token + ' out of range.');
}
if (indexDup[token]) {
throw new Error('Block "' + this.type + '": ' +
'Message index %' + token + ' duplicated.');
}
indexDup[token] = true;
indexCount++;
elements.push(args[token - 1]);
} else {
token = token.trim();
if (token) {
elements.push(token);
}
}
}
if (indexCount != args.length) {
throw new Error('Block "' + this.type + '": ' +
'Message does not reference all ' + args.length + ' arg(s).');
}
// Add last dummy input if needed.
if (elements.length && (typeof elements[elements.length - 1] == 'string' ||
goog.string.startsWith(
elements[elements.length - 1]['type'], 'field_'))) {
var dummyInput = {type: 'input_dummy'};
if (lastDummyAlign) {
dummyInput['align'] = lastDummyAlign;
}
elements.push(dummyInput);
}
// Lookup of alignment constants.
var alignmentLookup = {
'LEFT': Blockly.ALIGN_LEFT,
'RIGHT': Blockly.ALIGN_RIGHT,
'CENTRE': Blockly.ALIGN_CENTRE
};
// Populate block with inputs and fields.
var fieldStack = [];
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
if (typeof element == 'string') {
fieldStack.push([element, undefined]);
} else {
var field = null;
var input = null;
do {
var altRepeat = false;
if (typeof element == 'string') {
field = new Blockly.FieldLabel(element);
} else {
switch (element['type']) {
case 'input_value':
input = this.appendValueInput(element['name']);
break;
case 'input_statement':
input = this.appendStatementInput(element['name']);
break;
case 'input_dummy':
input = this.appendDummyInput(element['name']);
break;
default:
field = Blockly.Field.fromJson(element);
// Unknown field.
if (!field) {
if (element['alt']) {
element = element['alt'];
altRepeat = true;
} else {
console.warn('Blockly could not create a field of type ' +
element['type'] +
'. You may need to register your custom field. See ' +
'github.com/google/blockly/issues/1584');
}
}
}
}
} while (altRepeat);
if (field) {
fieldStack.push([field, element['name']]);
} else if (input) {
if (element['shape']) {
input.setShape(element['shape']);
}
if (element['check']) {
input.setCheck(element['check']);
}
if (element['align']) {
input.setAlign(alignmentLookup[element['align']]);
}
for (var j = 0; j < fieldStack.length; j++) {
input.appendField(fieldStack[j][0], fieldStack[j][1]);
}
fieldStack.length = 0;
}
}
}
};
/**
* Add a value input, statement input or local variable to this block.
* @param {number} type Either Blockly.INPUT_VALUE or Blockly.NEXT_STATEMENT or
* Blockly.DUMMY_INPUT.
* @param {string} name Language-neutral identifier which may used to find this
* input again. Should be unique to this block.
* @return {!Blockly.Input} The input object created.
* @protected
*/
Blockly.Block.prototype.appendInput_ = function(type, name) {
var connection = null;
if (type == Blockly.INPUT_VALUE || type == Blockly.NEXT_STATEMENT) {
connection = this.makeConnection_(type);
}
var input = new Blockly.Input(type, name, this, connection);
// Append input to list.
this.inputList.push(input);
return input;
};
/**
* Move a named input to a different location on this block.
* @param {string} name The name of the input to move.
* @param {?string} refName Name of input that should be after the moved input,
* or null to be the input at the end.
*/
Blockly.Block.prototype.moveInputBefore = function(name, refName) {
if (name == refName) {
return;
}
// Find both inputs.
var inputIndex = -1;
var refIndex = refName ? -1 : this.inputList.length;
for (var i = 0, input; input = this.inputList[i]; i++) {
if (input.name == name) {
inputIndex = i;
if (refIndex != -1) {
break;
}
} else if (refName && input.name == refName) {
refIndex = i;
if (inputIndex != -1) {
break;
}
}
}
goog.asserts.assert(inputIndex != -1, 'Named input "%s" not found.', name);
goog.asserts.assert(
refIndex != -1, 'Reference input "%s" not found.', refName);
this.moveNumberedInputBefore(inputIndex, refIndex);
};
/**
* Move a numbered input to a different location on this block.
* @param {number} inputIndex Index of the input to move.
* @param {number} refIndex Index of input that should be after the moved input.
*/
Blockly.Block.prototype.moveNumberedInputBefore = function(
inputIndex, refIndex) {
// Validate arguments.
goog.asserts.assert(inputIndex != refIndex, 'Can\'t move input to itself.');
goog.asserts.assert(inputIndex < this.inputList.length,
'Input index ' + inputIndex + ' out of bounds.');
goog.asserts.assert(refIndex <= this.inputList.length,
'Reference input ' + refIndex + ' out of bounds.');
// Remove input.
var input = this.inputList[inputIndex];
this.inputList.splice(inputIndex, 1);
if (inputIndex < refIndex) {
refIndex--;
}
// Reinsert input.
this.inputList.splice(refIndex, 0, input);
};
/**
* Remove an input from this block.
* @param {string} name The name of the input.
* @param {boolean=} opt_quiet True to prevent error if input is not present.
* @throws {goog.asserts.AssertionError} if the input is not present and
* opt_quiet is not true.
*/
Blockly.Block.prototype.removeInput = function(name, opt_quiet) {
for (var i = 0, input; input = this.inputList[i]; i++) {
if (input.name == name) {
if (input.connection && input.connection.isConnected()) {
input.connection.setShadowDom(null);
var block = input.connection.targetBlock();
if (block.isShadow()) {
// Destroy any attached shadow block.
block.dispose();
} else {
// Disconnect any attached normal block.
block.unplug();
}
}
input.dispose();
this.inputList.splice(i, 1);
return;
}
}
if (!opt_quiet) {
goog.asserts.fail('Input "%s" not found.', name);
}
};
/**
* Fetches the named input object.
* @param {string} name The name of the input.
* @return {Blockly.Input} The input object, or null if input does not exist.
*/
Blockly.Block.prototype.getInput = function(name) {
for (var i = 0, input; input = this.inputList[i]; i++) {
if (input.name == name) {
return input;
}
}
// This input does not exist.
return null;
};
/**
* Fetches the block attached to the named input.
* @param {string} name The name of the input.
* @return {Blockly.Block} The attached value block, or null if the input is
* either disconnected or if the input does not exist.
*/
Blockly.Block.prototype.getInputTargetBlock = function(name) {
var input = this.getInput(name);
return input && input.connection && input.connection.targetBlock();
};
/**
* Returns the comment on this block (or '' if none).
* @return {string} Block's comment.
*/
Blockly.Block.prototype.getCommentText = function() {
return this.comment || '';
};
/**
* Set this block's comment text.
* @param {?string} text The text, or null to delete.
*/
Blockly.Block.prototype.setCommentText = function(text) {
if (this.comment != text) {
Blockly.Events.fire(new Blockly.Events.BlockChange(
this, 'comment', null, this.comment, text || ''));
this.comment = text;
}
};
/**
* Set this block's output shape.
* e.g., null, OUTPUT_SHAPE_HEXAGONAL, OUTPUT_SHAPE_ROUND, OUTPUT_SHAPE_SQUARE, etc.
* @param {?number} outputShape Value representing output shape
* (see constants.js).
*/
Blockly.Block.prototype.setOutputShape = function(outputShape) {
this.outputShape_ = outputShape;
};
/**
* Get this block's output shape.
* @return {?number} Value representing output shape (see constants.js).
*/
Blockly.Block.prototype.getOutputShape = function() {
return this.outputShape_;
};
/**
* Set this block's category (for styling purposes)
* @param {?string} category The block's category (see constants.js).
*/
Blockly.Block.prototype.setCategory = function(category) {
this.category_ = category;
};
/**
* Get this block's category (for styling purposes)
* @return {?string} category The block's category (see constants.js).
*/
Blockly.Block.prototype.getCategory = function() {
return this.category_;
};
/**
* Set whether this block has a checkbox next to it in the flyout.
* @param {boolean} hasCheckbox True if this block should have a checkbox.
*/
Blockly.Block.prototype.setCheckboxInFlyout = function(hasCheckbox) {
this.checkboxInFlyout_ = hasCheckbox;
};
/**
* Get whether this block has a checkbox next to it in the flyout.
* @return {boolean} True if this block should have a checkbox.
*/
Blockly.Block.prototype.hasCheckboxInFlyout = function() {
return this.checkboxInFlyout_;
};
/**
* pm: Set whether this block can duplicate on drag.
* Note that a block must be a shadow block to duplicate on drag.
* @param {boolean} canDragDuplicate True if this block should duplicate on drag.
*/
Blockly.Block.prototype.setDragDuplication = function(canDragDuplicate) {
this.canDragDuplicate_ = canDragDuplicate;
};
/**
* pm: Get whether this block can duplicate on drag.
* This will only return true if the block is also a shadow block.
* @return {boolean} True if this block can duplicate on drag.
*/
Blockly.Block.prototype.canDragDuplicate = function() {
return this.canDragDuplicate_ && this.isShadow();
};
/**
* Set this block's warning text.
* @param {?string} text The text, or null to delete.
* @abstract
*/
Blockly.Block.prototype.setWarningText = function(/* text */) {
// NOP.
};
/**
* Give this block a mutator dialog.
* @param {Blockly.Mutator} mutator A mutator dialog instance or null to remove.
* @abstract
*/
Blockly.Block.prototype.setMutator = function(/* mutator */) {
// NOP.
};
/**
* Return the coordinates of the top-left corner of this block relative to the
* drawing surface's origin (0,0), in workspace units.
* @return {!goog.math.Coordinate} Object with .x and .y properties.
*/
Blockly.Block.prototype.getRelativeToSurfaceXY = function() {
return this.xy_;
};
/**
* Move a block by a relative offset.
* @param {number} dx Horizontal offset, in workspace units.
* @param {number} dy Vertical offset, in workspace units.
*/
Blockly.Block.prototype.moveBy = function(dx, dy) {
goog.asserts.assert(!this.parentBlock_, 'Block has parent.');
var event = new Blockly.Events.BlockMove(this);
this.xy_.translate(dx, dy);
event.recordNew();
Blockly.Events.fire(event);
};
/**
* Create a connection of the specified type.
* @param {number} type The type of the connection to create.
* @return {!Blockly.Connection} A new connection of the specified type.
* @private
*/
Blockly.Block.prototype.makeConnection_ = function(type) {
return new Blockly.Connection(this, type);
};
/**
* Recursively checks whether all statement and value inputs are filled with
* blocks. Also checks all following statement blocks in this stack.
* @param {boolean=} opt_shadowBlocksAreFilled An optional argument controlling
* whether shadow blocks are counted as filled. Defaults to true.
* @return {boolean} True if all inputs are filled, false otherwise.
*/
Blockly.Block.prototype.allInputsFilled = function(opt_shadowBlocksAreFilled) {
// Account for the shadow block filledness toggle.
if (opt_shadowBlocksAreFilled === undefined) {
opt_shadowBlocksAreFilled = true;
}
if (!opt_shadowBlocksAreFilled && this.isShadow()) {
return false;
}
// Recursively check each input block of the current block.
for (var i = 0, input; input = this.inputList[i]; i++) {
if (!input.connection) {
continue;
}
var target = input.connection.targetBlock();
if (!target || !target.allInputsFilled(opt_shadowBlocksAreFilled)) {
return false;
}
}
// Recursively check the next block after the current block.
var next = this.getNextBlock();
if (next) {
return next.allInputsFilled(opt_shadowBlocksAreFilled);
}
return true;
};
/**
* This method returns a string describing this Block in developer terms (type
* name and ID; English only).
*
* Intended to on be used in console logs and errors. If you need a string that
* uses the user's native language (including block text, field values, and
* child blocks), use [toString()]{@link Blockly.Block#toString}.
* @return {string} The description.
*/
Blockly.Block.prototype.toDevString = function() {
var msg = this.type ? '"' + this.type + '" block' : 'Block';
if (this.id) {
msg += ' (id="' + this.id + '")';
}
return msg;
};
|