imported.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. Blockly.Blocks['classics_get_all'] = {
  3. init: function() {
  4. this.setHelpUrl('http://www.example.com/');
  5. this.setColour(WEATHER_HUE);
  6. this.appendDummyInput()
  7. .appendField("classics.get all books");
  8. this.setInputsInline(false);
  9. this.setOutput(true, "Number");
  10. this.setTooltip('Returns all the books');
  11. }
  12. };
  13. Blockly.Python['classics_get_all'] = function(block) {
  14. Blockly.Python.definitions_['import_classics'] = 'import classics';
  15. var code = 'classics.get_all()';
  16. return [code, Blockly.Python.ORDER_ATOMIC];
  17. };
  18. PythonToBlocks.KNOWN_MODULES['classics'] = {
  19. "get_all": ["classics_get_all"]
  20. };
  21. */
  22. function newBlock(name) {
  23. var block = blockpy.components.editor.blockly.newBlock(name);
  24. block.initSvg();
  25. block.render();
  26. }
  27. /**
  28. * Decode an XML DOM and create blocks on the workspace, clearing out old blocks.
  29. * @param {!Element} xml XML DOM.
  30. * @param {!Blockly.Workspace} workspace The workspace.
  31. */
  32. Blockly.Xml.domToWorkspaceDestructive = function(xml, workspace, errorXml) {
  33. if (xml instanceof Blockly.Workspace) {
  34. var swap = xml;
  35. xml = workspace;
  36. workspace = swap;
  37. console.warn('Deprecated call to Blockly.Xml.domToWorkspace, ' +
  38. 'swap the arguments.');
  39. }
  40. var width; // Not used in LTR.
  41. if (workspace.RTL) {
  42. width = workspace.getWidth();
  43. }
  44. Blockly.Field.startCache();
  45. // Safari 7.1.3 is known to provide node lists with extra references to
  46. // children beyond the lists' length. Trust the length, do not use the
  47. // looping pattern of checking the index for an object.
  48. var childCount = xml.childNodes.length;
  49. var existingGroup = Blockly.Events.getGroup();
  50. if (!existingGroup) {
  51. Blockly.Events.setGroup(true);
  52. }
  53. Blockly.Events.disable();
  54. while (workspace.topBlocks_.length) {
  55. workspace.topBlocks_[0].dispose();
  56. }
  57. //workspace.variableList.length = 0;
  58. Blockly.Events.enable();
  59. // Disable workspace resizes as an optimization.
  60. if (workspace.setResizesEnabled) {
  61. workspace.setResizesEnabled(false);
  62. }
  63. for (var i = 0; i < childCount; i++) {
  64. var xmlChild = xml.childNodes[i];
  65. var name = xmlChild.nodeName.toLowerCase();
  66. if (name == 'block' ||
  67. (name == 'shadow' && !Blockly.Events.recordUndo)) {
  68. // Allow top-level shadow blocks if recordUndo is disabled since
  69. // that means an undo is in progress. Such a block is expected
  70. // to be moved to a nested destination in the next operation.
  71. var block = Blockly.Xml.domToBlock(xmlChild, workspace);
  72. var blockX = parseInt(xmlChild.getAttribute('x'), 10);
  73. var blockY = parseInt(xmlChild.getAttribute('y'), 10);
  74. if (!isNaN(blockX) && !isNaN(blockY)) {
  75. block.moveBy(workspace.RTL ? width - blockX : blockX, blockY);
  76. }
  77. } else if (name == 'shadow') {
  78. goog.asserts.fail('Shadow block cannot be a top-level block.');
  79. }
  80. }
  81. if (!existingGroup) {
  82. Blockly.Events.setGroup(false);
  83. }
  84. Blockly.Field.stopCache();
  85. //workspace.updateVariableList(false);
  86. // Re-enable workspace resizing.
  87. if (workspace.setResizesEnabled) {
  88. workspace.setResizesEnabled(true);
  89. }
  90. }
  91. function PLUS_MINUS_updateShape(listItemName, startMessage) {
  92. return function() {
  93. var that = this;
  94. function addField(field, block, e) {
  95. var rect = field.fieldGroup_.getBoundingClientRect();
  96. var yPosition = e.clientY;
  97. if (yPosition < rect.top+rect.height/2) {
  98. var input = that.appendValueInput(listItemName + that.itemCount_);
  99. that.itemCount_ += 1;
  100. } else {
  101. if (that.itemCount_ > 0) {
  102. that.itemCount_ -= 1;
  103. that.removeInput(listItemName + that.itemCount_)
  104. }
  105. }
  106. }
  107. if (!this.getInput('START')) {
  108. var clickablePlusMinus = new Blockly.FieldClickImage("images/plus-minus-button.svg", 12, 24, '+', addField, '-2px');
  109. //clickablePlusMinus.imageElement_.style.y = '-2px';
  110. this.appendDummyInput('START')
  111. .appendField(startMessage)
  112. .appendField(clickablePlusMinus);
  113. }
  114. // Add new inputs.
  115. for (var i = 0; i < this.itemCount_; i++) {
  116. if (!this.getInput(listItemName + i)) {
  117. var input = this.appendValueInput(listItemName + i);
  118. }
  119. }
  120. // Remove deleted inputs.
  121. while (this.getInput(listItemName + i)) {
  122. this.removeInput(listItemName + i);
  123. i++;
  124. }
  125. }
  126. }