block_library_controller.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /**
  2. * @license
  3. * Blockly Demos: Block Factory
  4. *
  5. * Copyright 2016 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 Contains the code for Block Library Controller, which
  22. * depends on Block Library Storage and Block Library UI. Provides the
  23. * interfaces for the user to
  24. * - save their blocks to the browser
  25. * - re-open and edit saved blocks
  26. * - delete blocks
  27. * - clear their block library
  28. * Depends on BlockFactory functions defined in factory.js.
  29. *
  30. * @author quachtina96 (Tina Quach)
  31. */
  32. 'use strict';
  33. goog.provide('BlockLibraryController');
  34. goog.require('BlockLibraryStorage');
  35. goog.require('BlockLibraryView');
  36. goog.require('BlockFactory');
  37. /**
  38. * Block Library Controller Class
  39. * @param {string} blockLibraryName Desired name of Block Library, also used
  40. * to create the key for where it's stored in local storage.
  41. * @param {!BlockLibraryStorage} opt_blockLibraryStorage Optional storage
  42. * object that allows user to import a block library.
  43. * @constructor
  44. */
  45. BlockLibraryController = function(blockLibraryName, opt_blockLibraryStorage) {
  46. this.name = blockLibraryName;
  47. // Create a new, empty Block Library Storage object, or load existing one.
  48. this.storage = opt_blockLibraryStorage || new BlockLibraryStorage(this.name);
  49. // The BlockLibraryView object handles the proper updating and formatting of
  50. // the block library dropdown.
  51. this.view = new BlockLibraryView();
  52. };
  53. /**
  54. * Returns the block type of the block the user is building.
  55. * @return {string} The current block's type.
  56. * @private
  57. */
  58. BlockLibraryController.prototype.getCurrentBlockType = function() {
  59. var rootBlock = FactoryUtils.getRootBlock(BlockFactory.mainWorkspace);
  60. var blockType = rootBlock.getFieldValue('NAME').trim().toLowerCase();
  61. // Replace white space with underscores
  62. return blockType.replace(/\W/g, '_').replace(/^(\d)/, '_\\1');
  63. };
  64. /**
  65. * Removes current block from Block Library and updates the save and delete
  66. * buttons so that user may save block to library and but not delete.
  67. * @param {string} blockType Type of block.
  68. */
  69. BlockLibraryController.prototype.removeFromBlockLibrary = function() {
  70. var blockType = this.getCurrentBlockType();
  71. this.storage.removeBlock(blockType);
  72. this.storage.saveToLocalStorage();
  73. this.populateBlockLibrary();
  74. this.view.updateButtons(blockType, false, false);
  75. };
  76. /**
  77. * Updates the workspace to show the block user selected from library
  78. * @param {string} blockType Block to edit on block factory.
  79. */
  80. BlockLibraryController.prototype.openBlock = function(blockType) {
  81. if (blockType) {
  82. var xml = this.storage.getBlockXml(blockType);
  83. BlockFactory.mainWorkspace.clear();
  84. Blockly.Xml.domToWorkspace(xml, BlockFactory.mainWorkspace);
  85. BlockFactory.mainWorkspace.clearUndo();
  86. } else {
  87. BlockFactory.showStarterBlock();
  88. this.view.setSelectedBlockType(null);
  89. }
  90. };
  91. /**
  92. * Returns type of block selected from library.
  93. * @return {string} Type of block selected.
  94. */
  95. BlockLibraryController.prototype.getSelectedBlockType = function() {
  96. return this.view.getSelectedBlockType();
  97. };
  98. /**
  99. * Confirms with user before clearing the block library in local storage and
  100. * updating the dropdown and displaying the starter block (factory_base).
  101. */
  102. BlockLibraryController.prototype.clearBlockLibrary = function() {
  103. var check = confirm('Delete all blocks from library?');
  104. if (check) {
  105. // Clear Block Library Storage.
  106. this.storage.clear();
  107. this.storage.saveToLocalStorage();
  108. // Update dropdown.
  109. this.view.clearOptions();
  110. // Show default block.
  111. BlockFactory.showStarterBlock();
  112. // User may not save the starter block, but will get explicit instructions
  113. // upon clicking the red save button.
  114. this.view.updateButtons(null);
  115. }
  116. };
  117. /**
  118. * Saves current block to local storage and updates dropdown.
  119. */
  120. BlockLibraryController.prototype.saveToBlockLibrary = function() {
  121. var blockType = this.getCurrentBlockType();
  122. // If user has not changed the name of the starter block.
  123. if (blockType == 'block_type') {
  124. // Do not save block if it has the default type, 'block_type'.
  125. alert('You cannot save a block under the name "block_type". Try changing ' +
  126. 'the name before saving. Then, click on the "Block Library" button ' +
  127. 'to view your saved blocks.');
  128. return;
  129. }
  130. // Create block XML.
  131. var xmlElement = goog.dom.createDom('xml');
  132. var block = FactoryUtils.getRootBlock(BlockFactory.mainWorkspace);
  133. xmlElement.appendChild(Blockly.Xml.blockToDomWithXY(block));
  134. // Do not add option again if block type is already in library.
  135. if (!this.has(blockType)) {
  136. this.view.addOption(blockType, true, true);
  137. }
  138. // Save block.
  139. this.storage.addBlock(blockType, xmlElement);
  140. this.storage.saveToLocalStorage();
  141. // Show saved block without other stray blocks sitting in Block Factory's
  142. // main workspace.
  143. this.openBlock(blockType);
  144. // Add select handler to the new option.
  145. this.addOptionSelectHandler(blockType);
  146. };
  147. /**
  148. * Checks to see if the given blockType is already in Block Library
  149. * @param {string} blockType Type of block.
  150. * @return {boolean} Boolean indicating whether or not block is in the library.
  151. */
  152. BlockLibraryController.prototype.has = function(blockType) {
  153. var blockLibrary = this.storage.blocks;
  154. return (blockType in blockLibrary && blockLibrary[blockType] != null);
  155. };
  156. /**
  157. * Populates the dropdown menu.
  158. */
  159. BlockLibraryController.prototype.populateBlockLibrary = function() {
  160. this.view.clearOptions();
  161. // Add an unselected option for each saved block.
  162. var blockLibrary = this.storage.blocks;
  163. for (var blockType in blockLibrary) {
  164. this.view.addOption(blockType, false);
  165. }
  166. this.addOptionSelectHandlers();
  167. };
  168. /**
  169. * Return block library mapping block type to XML.
  170. * @return {Object} Object mapping block type to XML text.
  171. */
  172. BlockLibraryController.prototype.getBlockLibrary = function() {
  173. return this.storage.getBlockXmlTextMap();
  174. };
  175. /**
  176. * Return stored XML of a given block type.
  177. * @param {string} blockType The type of block.
  178. * @return {!Element} XML element of a given block type or null.
  179. */
  180. BlockLibraryController.prototype.getBlockXml = function(blockType) {
  181. return this.storage.getBlockXml(blockType);
  182. };
  183. /**
  184. * Set the block library storage object from which exporter exports.
  185. * @param {!BlockLibraryStorage} blockLibStorage Block Library Storage object.
  186. */
  187. BlockLibraryController.prototype.setBlockLibraryStorage
  188. = function(blockLibStorage) {
  189. this.storage = blockLibStorage;
  190. };
  191. /**
  192. * Get the block library storage object from which exporter exports.
  193. * @return {!BlockLibraryStorage} blockLibStorage Block Library Storage object
  194. * that stores the blocks.
  195. */
  196. BlockLibraryController.prototype.getBlockLibraryStorage = function() {
  197. return this.blockLibStorage;
  198. };
  199. /**
  200. * Get the block library storage object from which exporter exports.
  201. * @return {boolean} True if the Block Library is empty, false otherwise.
  202. */
  203. BlockLibraryController.prototype.hasEmptyBlockLibrary = function() {
  204. return this.storage.isEmpty();
  205. };
  206. /**
  207. * Get all block types stored in block library.
  208. * @return {!Array.<string>} Array of block types.
  209. */
  210. BlockLibraryController.prototype.getStoredBlockTypes = function() {
  211. return this.storage.getBlockTypes();
  212. };
  213. /**
  214. * Sets the currently selected block option to none.
  215. */
  216. BlockLibraryController.prototype.setNoneSelected = function() {
  217. this.view.setSelectedBlockType(null);
  218. };
  219. /**
  220. * If there are unsaved changes to the block in open in Block Factory
  221. * and the block is not the starter block, check if user wants to proceed,
  222. * knowing that it will cause them to lose their changes.
  223. * @return {boolean} Whether or not to proceed.
  224. */
  225. BlockLibraryController.prototype.warnIfUnsavedChanges = function() {
  226. if (!FactoryUtils.savedBlockChanges(this)) {
  227. return confirm('You have unsaved changes. By proceeding without saving ' +
  228. ' your block first, you will lose these changes.');
  229. }
  230. return true;
  231. };
  232. /**
  233. * Add select handler for an option of a given block type. The handler will to
  234. * update the view and the selected block accordingly.
  235. * @param {string} blockType The type of block represented by the option is for.
  236. */
  237. BlockLibraryController.prototype.addOptionSelectHandler = function(blockType) {
  238. var self = this;
  239. // Click handler for a block option. Sets the block option as the selected
  240. // option and opens the block for edit in Block Factory.
  241. var setSelectedAndOpen_ = function(blockOption) {
  242. var blockType = blockOption.textContent;
  243. self.view.setSelectedBlockType(blockType);
  244. self.openBlock(blockType);
  245. // The block is saved in the block library and all changes have been saved
  246. // when the user opens a block from the block library dropdown.
  247. // Thus, the buttons show up as a disabled update button and an enabled
  248. // delete.
  249. self.view.updateButtons(blockType, true, true);
  250. blocklyFactory.closeModal();
  251. };
  252. // Returns a block option select handler.
  253. var makeOptionSelectHandler_ = function(blockOption) {
  254. return function() {
  255. // If there are unsaved changes warn user, check if they'd like to
  256. // proceed with unsaved changes, and act accordingly.
  257. var proceedWithUnsavedChanges = self.warnIfUnsavedChanges();
  258. if (!proceedWithUnsavedChanges) {
  259. return;
  260. }
  261. setSelectedAndOpen_(blockOption);
  262. };
  263. };
  264. // Assign a click handler to the block option.
  265. var blockOption = this.view.optionMap[blockType];
  266. // Use an additional closure to correctly assign the tab callback.
  267. blockOption.addEventListener(
  268. 'click', makeOptionSelectHandler_(blockOption));
  269. };
  270. /**
  271. * Add select handlers to each option to update the view and the selected
  272. * blocks accordingly.
  273. */
  274. BlockLibraryController.prototype.addOptionSelectHandlers = function() {
  275. // Assign a click handler to each block option.
  276. for (var blockType in this.view.optionMap) {
  277. this.addOptionSelectHandler(blockType);
  278. }
  279. };
  280. /**
  281. * Update the save and delete buttons based on the current block type of the
  282. * block the user is currently editing.
  283. * @param {boolean} Whether changes to the block have been saved.
  284. */
  285. BlockLibraryController.prototype.updateButtons = function(savedChanges) {
  286. var blockType = this.getCurrentBlockType();
  287. var isInLibrary = this.has(blockType);
  288. this.view.updateButtons(blockType, isInLibrary, savedChanges);
  289. };