webpage.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. 'use strict';
  2. goog.provide('Blockly.Blocks.webpage');
  3. goog.require('Blockly.Blocks');
  4. goog.require('Blockly.Types');
  5. Blockly.Blocks.webpage.HUE = "#f28821";
  6. /**webpage Dashboard
  7. *label for data visualization
  8. *@param {String} data: to display in dashboard
  9. */
  10. Blockly.Blocks['webpage_label'] = {
  11. init: function() {
  12. this.appendDummyInput()
  13. .appendField(new Blockly.FieldImage("./../blockly/media/label_header-image.png", 50, 50, "*"));
  14. this.appendDummyInput()
  15. .appendField(Blockly.Msg.WEBPAGE_LABLE_TITLE)
  16. // this.appendDummyInput()
  17. // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/blockly/media/cocomod_blockly_wifi.png", 140, 40, "0"));
  18. // .appendField(new Blockly.FieldImage("./../blockly/media/transfer-2.png", 50, 40, "0"));
  19. this.appendValueInput("ADD0")
  20. .appendField(Blockly.Msg.WEBPAGE_LABLE)
  21. .appendField(new Blockly.FieldTextInput("label0"), "LABLE0")
  22. this.setPreviousStatement(true, null);
  23. this.setNextStatement(true, null);
  24. this.setColour(Blockly.Blocks.webpage.HUE);
  25. this.setTooltip("");
  26. this.setHelpUrl("");
  27. this.itemCount_ = 1;
  28. this.setMutator(new Blockly.Mutator(['webpage_create_with_item']));
  29. },
  30. mutationToDom: function() {
  31. var container = document.createElement('mutation');
  32. container.setAttribute('items', this.itemCount_);
  33. return container;
  34. },
  35. /**
  36. * Parse XML to restore the inputs.
  37. * @param {!Element} xmlElement XML storage element.
  38. * @this Blockly.Block
  39. */
  40. domToMutation: function(xmlElement) {
  41. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  42. this.updateShape_();
  43. },
  44. /**
  45. * Populate the mutator's dialog with this block's components.
  46. * @param {!Blockly.Workspace} workspace Mutator's workspace.
  47. * @return {!Blockly.Block} Root block in mutator.
  48. * @this Blockly.Block
  49. */
  50. decompose: function(workspace) {
  51. var containerBlock = workspace.newBlock('webpage_create_with_container');
  52. containerBlock.initSvg();
  53. var connection = containerBlock.getInput('STACK').connection;
  54. for (var i = 0; i < this.itemCount_; i++) {
  55. var itemBlock = workspace.newBlock('webpage_create_with_item');
  56. itemBlock.initSvg();
  57. connection.connect(itemBlock.previousConnection);
  58. connection = itemBlock.nextConnection;
  59. }
  60. return containerBlock;
  61. },
  62. /**
  63. * Reconfigure this block based on the mutator dialog's components.
  64. * @param {!Blockly.Block} containerBlock Root block in mutator.
  65. * @this Blockly.Block
  66. */
  67. compose: function(containerBlock) {
  68. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  69. // Count number of inputs.
  70. var connections = [];
  71. while (itemBlock) {
  72. connections.push(itemBlock.valueConnection_);
  73. itemBlock = itemBlock.nextConnection &&
  74. itemBlock.nextConnection.targetBlock();
  75. }
  76. // Disconnect any children that don't belong.
  77. for (var i = 0; i < this.itemCount_; i++) {
  78. var connection = this.getInput('ADD' + i).connection.targetConnection;
  79. if (connection && connections.indexOf(connection) == -1) {
  80. connection.disconnect();
  81. }
  82. }
  83. this.itemCount_ = connections.length;
  84. this.updateShape_();
  85. // Reconnect any child blocks.
  86. for (var i = 0; i < this.itemCount_; i++) {
  87. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  88. }
  89. },
  90. /**
  91. * Store pointers to any connected child blocks.
  92. * @param {!Blockly.Block} containerBlock Root block in mutator.
  93. * @this Blockly.Block
  94. */
  95. saveConnections: function(containerBlock) {
  96. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  97. var i = 0;
  98. while (itemBlock) {
  99. var input = this.getInput('ADD' + i);
  100. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  101. i++;
  102. itemBlock = itemBlock.nextConnection &&
  103. itemBlock.nextConnection.targetBlock();
  104. }
  105. },
  106. /**
  107. * Modify this block to have the correct number of inputs.
  108. * @private
  109. * @this Blockly.Block
  110. */
  111. updateShape_: function() {
  112. // if (this.itemCount_ && this.getInput('EMPTY')) {
  113. // this.removeInput('EMPTY');
  114. // } else if (!this.itemCount_ && !this.getInput('EMPTY')) {
  115. // this.appendDummyInput('EMPTY')
  116. // .appendField(this.newQuote_(true))
  117. // .appendField(this.newQuote_(false));
  118. // }
  119. // Add new inputs.
  120. for (var i = 0; i < this.itemCount_; i++) {
  121. if (!this.getInput('ADD' + i)) {
  122. var input = this.appendValueInput('ADD' + i)
  123. .appendField(Blockly.Msg.WEBPAGE_LABLE)
  124. .appendField(new Blockly.FieldTextInput("label" + i), "LABLE" + i);
  125. }
  126. }
  127. // Remove deleted inputs.
  128. while (this.getInput('ADD' + i)) {
  129. this.removeInput('ADD' + i);
  130. i++;
  131. }
  132. },
  133. };
  134. Blockly.Blocks['webpage_create_with_container'] = {
  135. /**
  136. * Mutator block for list container.
  137. * @this Blockly.Block
  138. */
  139. init: function() {
  140. this.setColour(Blockly.Blocks.webpage.HUE);
  141. this.appendDummyInput()
  142. .appendField(Blockly.Msg.THINGSPEAK_CREATE_WITH_CONTAINER_TITLE_ADD);
  143. this.appendStatementInput('STACK');
  144. this.setTooltip('');
  145. this.contextMenu = false;
  146. }
  147. };
  148. Blockly.Blocks['webpage_create_with_item'] = {
  149. /**
  150. * Mutator bolck for adding items.
  151. * @this Blockly.Block
  152. */
  153. init: function() {
  154. this.setColour(Blockly.Blocks.webpage.HUE);
  155. this.appendDummyInput()
  156. .appendField(Blockly.Msg.THINGSPEAK_CREATE_WITH_ITEM_TITLE);
  157. this.setPreviousStatement(true);
  158. this.setNextStatement(true);
  159. this.setTooltip('');
  160. this.contextMenu = false;
  161. }
  162. };
  163. /**webpage Dashboard
  164. *button for action trigger
  165. *@param {String} router: set path for button action to get/post
  166. *@param {String} data: data to send when button pressed
  167. */
  168. Blockly.Blocks['webpage_button'] = {
  169. init: function() {
  170. this.appendDummyInput()
  171. .appendField(new Blockly.FieldImage("./../blockly/media/button_header-image.png", 50, 50, "*"));
  172. this.appendDummyInput()
  173. .appendField(Blockly.Msg.WEBPAGE_BUTTON_TITLE)
  174. this.appendDummyInput("ADD0")
  175. .appendField(Blockly.Msg.WEBPAGE_BUTTON_NAME)
  176. .appendField(new Blockly.FieldTextInput("Button0"), "BUTTON0")
  177. .appendField(Blockly.Msg.WEBPAGE_BUTTON_ROUTER)
  178. .appendField(new Blockly.FieldTextInput("/url0"), "URL0")
  179. this.setColour(Blockly.Blocks.webpage.HUE);
  180. this.itemCount_ = 1;
  181. this.setTooltip("");
  182. this.setHelpUrl("");
  183. this.setPreviousStatement(true, null);
  184. this.setNextStatement(true, null);
  185. this.setMutator(new Blockly.Mutator(['webpage_create_with_item']));
  186. },
  187. mutationToDom: function() {
  188. var container = document.createElement('mutation');
  189. container.setAttribute('items', this.itemCount_);
  190. return container;
  191. },
  192. /**
  193. * Parse XML to restore the inputs.
  194. * @param {!Element} xmlElement XML storage element.
  195. * @this Blockly.Block
  196. */
  197. domToMutation: function(xmlElement) {
  198. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  199. this.updateShape_();
  200. },
  201. /**
  202. * Populate the mutator's dialog with this block's components.
  203. * @param {!Blockly.Workspace} workspace Mutator's workspace.
  204. * @return {!Blockly.Block} Root block in mutator.
  205. * @this Blockly.Block
  206. */
  207. decompose: function(workspace) {
  208. var containerBlock = workspace.newBlock('webpage_create_with_container');
  209. containerBlock.initSvg();
  210. var connection = containerBlock.getInput('STACK').connection;
  211. for (var i = 0; i < this.itemCount_; i++) {
  212. var itemBlock = workspace.newBlock('webpage_create_with_item');
  213. itemBlock.initSvg();
  214. connection.connect(itemBlock.previousConnection);
  215. connection = itemBlock.nextConnection;
  216. }
  217. return containerBlock;
  218. },
  219. /**
  220. * Reconfigure this block based on the mutator dialog's components.
  221. * @param {!Blockly.Block} containerBlock Root block in mutator.
  222. * @this Blockly.Block
  223. */
  224. compose: function(containerBlock) {
  225. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  226. // Count number of inputs.
  227. var connections = [];
  228. while (itemBlock) {
  229. connections.push(itemBlock.valueConnection_);
  230. itemBlock = itemBlock.nextConnection &&
  231. itemBlock.nextConnection.targetBlock();
  232. }
  233. // // Disconnect any children that don't belong.
  234. // for (var i = 0; i < this.itemCount_; i++) {
  235. // var connection = this.getInput('ADD' + i).connection.targetConnection;
  236. // if (connection && connections.indexOf(connection) == -1) {
  237. // connection.disconnect();
  238. // }
  239. // }
  240. this.itemCount_ = connections.length;
  241. this.updateShape_();
  242. // Reconnect any child blocks.
  243. for (var i = 0; i < this.itemCount_; i++) {
  244. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  245. }
  246. },
  247. /**
  248. * Store pointers to any connected child blocks.
  249. * @param {!Blockly.Block} containerBlock Root block in mutator.
  250. * @this Blockly.Block
  251. */
  252. saveConnections: function(containerBlock) {
  253. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  254. var i = 0;
  255. while (itemBlock) {
  256. var input = this.getInput('ADD' + i);
  257. // itemBlock.valueConnection_ = input && input.connection.targetConnection;
  258. i++;
  259. itemBlock = itemBlock.nextConnection &&
  260. itemBlock.nextConnection.targetBlock();
  261. }
  262. },
  263. /**
  264. * Modify this block to have the correct number of inputs.
  265. * @private
  266. * @this Blockly.Block
  267. */
  268. updateShape_: function() {
  269. // if (this.itemCount_ && this.getInput('EMPTY')) {
  270. // this.removeInput('EMPTY');
  271. // } else if (!this.itemCount_ && !this.getInput('EMPTY')) {
  272. // this.appendDummyInput('EMPTY')
  273. // .appendField(this.newQuote_(true))
  274. // .appendField(this.newQuote_(false));
  275. // }
  276. // Add new inputs.
  277. for (var i = 0; i < this.itemCount_; i++) {
  278. if (!this.getInput('ADD' + i)) {
  279. var input = this.appendDummyInput("ADD" + i)
  280. .appendField(Blockly.Msg.WEBPAGE_BUTTON_NAME)
  281. .appendField(new Blockly.FieldTextInput("Button" + i), "BUTTON" + i)
  282. .appendField(Blockly.Msg.WEBPAGE_BUTTON_ROUTER)
  283. .appendField(new Blockly.FieldTextInput("url" + i), "URL" + i)
  284. }
  285. }
  286. // Remove deleted inputs.
  287. while (this.getInput('ADD' + i)) {
  288. this.removeInput('ADD' + i);
  289. i++;
  290. }
  291. },
  292. };
  293. Blockly.Blocks['webpage_colorpicker'] = {
  294. init: function() {
  295. this.appendDummyInput()
  296. .appendField(new Blockly.FieldImage("./../blockly/media/colorpicker_header-image.png", 50, 50, "*"));
  297. this.appendDummyInput()
  298. .appendField(Blockly.Msg.WEBPAGE_COLORPICKER_TITLE);
  299. this.appendDummyInput()
  300. .appendField(Blockly.Msg.WEBPAGE_COLORPICKER_PATH)
  301. .appendField(new Blockly.FieldTextInput("/color"), "webpage_colorpicker_path");
  302. this.appendDummyInput()
  303. .appendField(Blockly.Msg.WEBPAGE_COLORPICKER_SUBTITLE);
  304. this.setPreviousStatement(true, null);
  305. this.setNextStatement(true, null);
  306. this.setColour(Blockly.Blocks.webpage.HUE);
  307. this.setTooltip("");
  308. this.setHelpUrl("");
  309. }
  310. };
  311. Blockly.Blocks['webpage_buttoncontroller'] = {
  312. init: function() {
  313. this.appendDummyInput()
  314. .appendField(new Blockly.FieldImage("./../blockly/media/buttoncontroller_header-image.png", 50, 50, "*"));
  315. this.appendDummyInput()
  316. .appendField(Blockly.Msg.WEBPAGE_BUTTONCTRL_TITLE);
  317. this.appendDummyInput()
  318. .appendField(Blockly.Msg.WEBPAGE_BUTTONCTRL_TEXT_TOP)
  319. .appendField(new Blockly.FieldTextInput("up"), "webpage_buttoncontroller_top")
  320. .appendField(Blockly.Msg.WEBPAGE_BUTTONCTRL_SETPATH)
  321. .appendField(new Blockly.FieldTextInput("/controller"), "webpage_buttoncontroller_top_path");
  322. this.appendDummyInput()
  323. .appendField(Blockly.Msg.WEBPAGE_BUTTONCTRL_TEXT_LEFT)
  324. .appendField(new Blockly.FieldTextInput("left"), "webpage_buttoncontroller_left")
  325. .appendField(Blockly.Msg.WEBPAGE_BUTTONCTRL_SETPATH)
  326. .appendField(new Blockly.FieldTextInput("/controller"), "webpage_buttoncontroller_left_path");
  327. this.appendDummyInput()
  328. .appendField(Blockly.Msg.WEBPAGE_BUTTONCTRL_TEXT_CENTER)
  329. .appendField(new Blockly.FieldTextInput("center"), "webpage_buttoncontroller_center")
  330. .appendField(Blockly.Msg.WEBPAGE_BUTTONCTRL_SETPATH)
  331. .appendField(new Blockly.FieldTextInput("/controller"), "webpage_buttoncontroller_center_path");
  332. this.appendDummyInput()
  333. .appendField(Blockly.Msg.WEBPAGE_BUTTONCTRL_TEXT_RIGHT)
  334. .appendField(new Blockly.FieldTextInput("righ"), "webpage_buttoncontroller_right")
  335. .appendField(Blockly.Msg.WEBPAGE_BUTTONCTRL_SETPATH)
  336. .appendField(new Blockly.FieldTextInput("/controller"), "webpage_buttoncontroller_right_path");
  337. this.appendDummyInput()
  338. .appendField(Blockly.Msg.WEBPAGE_BUTTONCTRL_TEXT_BOTTOM)
  339. .appendField(new Blockly.FieldTextInput("bottom"), "webpage_buttoncontroller_bottom")
  340. .appendField(Blockly.Msg.WEBPAGE_BUTTONCTRL_SETPATH)
  341. .appendField(new Blockly.FieldTextInput("/controller"), "webpage_buttoncontroller_bottom_path");
  342. this.setPreviousStatement(true, null);
  343. this.setNextStatement(true, null);
  344. this.setColour(Blockly.Blocks.webpage.HUE);
  345. this.setTooltip("");
  346. this.setHelpUrl("");
  347. }
  348. };