variables.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 Variable blocks for Blockly.
  22. * @author fraser@google.com (Neil Fraser)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.Blocks.variables');
  26. goog.require('Blockly.Blocks');
  27. /**
  28. * Common HSV hue for all blocks in this category.
  29. */
  30. Blockly.Blocks.variables.HUE = 30;
  31. Blockly.Blocks['variables_get'] = {
  32. /**
  33. * Block for variable getter.
  34. * @this Blockly.Block
  35. */
  36. init: function() {
  37. this.setHelpUrl(Blockly.Msg.VARIABLES_GET_HELPURL);
  38. this.setColour(Blockly.Blocks.variables.HUE);
  39. this.appendDummyInput()
  40. .appendField(new Blockly.FieldVariable(
  41. Blockly.Msg.VARIABLES_DEFAULT_NAME), 'VAR');
  42. this.setOutput(true);
  43. this.setTooltip(Blockly.Msg.VARIABLES_GET_TOOLTIP);
  44. this.contextMenuMsg_ = Blockly.Msg.VARIABLES_GET_CREATE_SET;
  45. },
  46. contextMenuType_: 'variables_set',
  47. /**
  48. * Add menu option to create getter/setter block for this setter/getter.
  49. * @param {!Array} options List of menu options to add to.
  50. * @this Blockly.Block
  51. */
  52. customContextMenu: function(options) {
  53. var option = {enabled: true};
  54. var name = this.getFieldValue('VAR');
  55. option.text = this.contextMenuMsg_.replace('%1', name);
  56. var xmlField = goog.dom.createDom('field', null, name);
  57. xmlField.setAttribute('name', 'VAR');
  58. var xmlBlock = goog.dom.createDom('block', null, xmlField);
  59. xmlBlock.setAttribute('type', this.contextMenuType_);
  60. option.callback = Blockly.ContextMenu.callbackFactory(this, xmlBlock);
  61. options.push(option);
  62. }
  63. };
  64. Blockly.Blocks['variables_set'] = {
  65. /**
  66. * Block for variable setter.
  67. * @this Blockly.Block
  68. */
  69. init: function() {
  70. this.jsonInit({
  71. "message0": Blockly.Msg.VARIABLES_SET,
  72. "args0": [
  73. {
  74. "type": "field_variable",
  75. "name": "VAR",
  76. "variable": Blockly.Msg.VARIABLES_DEFAULT_NAME
  77. },
  78. {
  79. "type": "input_value",
  80. "name": "VALUE"
  81. }
  82. ],
  83. "previousStatement": null,
  84. "nextStatement": null,
  85. "colour": Blockly.Blocks.variables.HUE,
  86. "tooltip": Blockly.Msg.VARIABLES_SET_TOOLTIP,
  87. "helpUrl": Blockly.Msg.VARIABLES_SET_HELPURL
  88. });
  89. this.contextMenuMsg_ = Blockly.Msg.VARIABLES_SET_CREATE_GET;
  90. },
  91. contextMenuType_: 'variables_get',
  92. customContextMenu: Blockly.Blocks['variables_get'].customContextMenu
  93. };
  94. Blockly.Blocks['simple_variables_set'] = {
  95. /**
  96. * Block for variable setter.
  97. * @this Blockly.Block
  98. */
  99. init: function() {
  100. this.jsonInit({
  101. "message0": ' ',
  102. "args0": [
  103. {
  104. "type": "field_variable",
  105. "name": "VAR",
  106. "variable": Blockly.Msg.VARIABLES_DEFAULT_NAME
  107. },
  108. {
  109. "type": "input_value",
  110. "name": "VALUE"
  111. }
  112. ],
  113. "previousStatement": null,
  114. "nextStatement": null,
  115. "colour": Blockly.Blocks.variables.HUE,
  116. "tooltip": Blockly.Msg.VARIABLES_SET_TOOLTIP,
  117. "helpUrl": Blockly.Msg.VARIABLES_SET_HELPURL
  118. });
  119. this.contextMenuMsg_ = Blockly.Msg.VARIABLES_SET_CREATE_GET;
  120. },
  121. /**
  122. * Return all variables referenced by this block.
  123. * @return {!Array.<string>} List of variable names.
  124. * @this Blockly.Block
  125. */
  126. getVars: function() {
  127. return [this.getFieldValue('VAR')];
  128. },
  129. /**
  130. * Notification that a variable is renaming.
  131. * If the name matches one of this block's variables, rename it.
  132. * @param {string} oldName Previous name of variable.
  133. * @param {string} newName Renamed variable.
  134. * @this Blockly.Block
  135. */
  136. renameVar: function(oldName, newName) {
  137. if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) {
  138. this.setFieldValue(newName, 'VAR');
  139. }
  140. },
  141. contextMenuType_: 'variables_get',
  142. customContextMenu: Blockly.Blocks['variables_get'].customContextMenu
  143. };
  144. Blockly.Blocks['variables_getself'] = {
  145. init: function() {
  146. this.appendValueInput("VAR")
  147. .appendField(Blockly.Msg.VARIABLES_GET_SELF)
  148. this.setOutput(true);
  149. this.setColour(Blockly.Blocks.variables.HUE);
  150. this.setInputsInline(true);
  151. }
  152. }
  153. Blockly.Blocks['variables_setself'] = {
  154. init: function() {
  155. this.appendValueInput("VAR")
  156. .appendField(Blockly.Msg.VARIABLES_SET_SELF)
  157. this.appendValueInput("TO")
  158. .appendField(Blockly.Msg.VARIABLES_SET_SELF_TO)
  159. this.setPreviousStatement(true);
  160. this.setNextStatement(true);
  161. this.setColour(Blockly.Blocks.variables.HUE);
  162. this.setInputsInline(true);
  163. }
  164. }