block_library_view.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 Javascript for BlockLibraryView class. It manages the display
  22. * of the Block Library dropdown, save, and delete buttons.
  23. *
  24. * @author quachtina96 (Tina Quach)
  25. */
  26. 'use strict';
  27. goog.provide('BlockLibraryView');
  28. goog.require('goog.dom');
  29. goog.require('goog.dom.classlist');
  30. /**
  31. * BlockLibraryView Class
  32. * @constructor
  33. */
  34. var BlockLibraryView = function() {
  35. // Div element to contain the block types to choose from.
  36. this.dropdown = document.getElementById('dropdownDiv_blockLib');
  37. // Map of block type to corresponding 'a' element that is the option in the
  38. // dropdown. Used to quickly and easily get a specific option.
  39. this.optionMap = Object.create(null);
  40. // Save and delete buttons.
  41. this.saveButton = document.getElementById('saveToBlockLibraryButton');
  42. this.deleteButton = document.getElementById('removeBlockFromLibraryButton');
  43. // Initially, user should not be able to delete a block. They must save a
  44. // block or select a stored block first.
  45. this.deleteButton.disabled = true;
  46. };
  47. /**
  48. * Creates a node of a given element type and appends to the node with given ID.
  49. * @param {string} blockType Type of block.
  50. * @param {boolean} selected Whether or not the option should be selected on
  51. * the dropdown.
  52. */
  53. BlockLibraryView.prototype.addOption = function(blockType, selected) {
  54. // Create option.
  55. var option = goog.dom.createDom('a', {
  56. 'id': 'dropdown_' + blockType,
  57. 'class': 'blockLibOpt'
  58. }, blockType);
  59. // Add option to dropdown.
  60. this.dropdown.appendChild(option);
  61. this.optionMap[blockType] = option;
  62. // Select the block.
  63. if (selected) {
  64. this.setSelectedBlockType(blockType);
  65. }
  66. };
  67. /**
  68. * Sets a given block type to selected and all other blocks to deselected.
  69. * If null, deselects all blocks.
  70. * @param {string} blockTypeToSelect Type of block to select or null.
  71. */
  72. BlockLibraryView.prototype.setSelectedBlockType = function(blockTypeToSelect) {
  73. // Select given block type and deselect all others. Will deselect all blocks
  74. // if null or invalid block type selected.
  75. for (var blockType in this.optionMap) {
  76. var option = this.optionMap[blockType];
  77. if (blockType == blockTypeToSelect) {
  78. this.selectOption_(option);
  79. } else {
  80. this.deselectOption_(option);
  81. }
  82. }
  83. };
  84. /**
  85. * Selects a given option.
  86. * @param {!Element} option HTML 'a' element in the dropdown that represents
  87. * a particular block type.
  88. * @private
  89. */
  90. BlockLibraryView.prototype.selectOption_ = function(option) {
  91. goog.dom.classlist.add(option, 'dropdown-content-selected');
  92. };
  93. /**
  94. * Deselects a given option.
  95. * @param {!Element} option HTML 'a' element in the dropdown that represents
  96. * a particular block type.
  97. * @private
  98. */
  99. BlockLibraryView.prototype.deselectOption_ = function(option) {
  100. goog.dom.classlist.remove(option, 'dropdown-content-selected');
  101. };
  102. /**
  103. * Updates the save and delete buttons to represent how the current block will
  104. * be saved by including the block type in the button text as well as indicating
  105. * whether the block is being saved or updated.
  106. * @param {string} blockType The type of block being edited.
  107. * @param {boolean} isInLibrary Whether the block type is in the library.
  108. * @param {boolean} savedChanges Whether changes to block have been saved.
  109. */
  110. BlockLibraryView.prototype.updateButtons =
  111. function(blockType, isInLibrary, savedChanges) {
  112. if (blockType) {
  113. // User is editing a block.
  114. if (!isInLibrary) {
  115. // Block type has not been saved to library yet. Disable the delete button
  116. // and allow user to save.
  117. this.saveButton.textContent = 'Save "' + blockType + '"';
  118. this.saveButton.disabled = false;
  119. this.deleteButton.disabled = true;
  120. } else {
  121. // Block type has already been saved. Disable the save button unless the
  122. // there are unsaved changes (checked below).
  123. this.saveButton.textContent = 'Update "' + blockType + '"';
  124. this.saveButton.disabled = true;
  125. this.deleteButton.disabled = false;
  126. }
  127. this.deleteButton.textContent = 'Delete "' + blockType + '"';
  128. // If changes to block have been made and are not saved, make button
  129. // green to encourage user to save the block.
  130. if (!savedChanges) {
  131. var buttonFormatClass = 'button_warn';
  132. // If block type is the default, 'block_type', make button red to alert
  133. // user.
  134. if (blockType == 'block_type') {
  135. buttonFormatClass = 'button_alert';
  136. }
  137. goog.dom.classlist.add(this.saveButton, buttonFormatClass);
  138. this.saveButton.disabled = false;
  139. } else {
  140. // No changes to save.
  141. var classesToRemove = ['button_alert', 'button_warn'];
  142. goog.dom.classlist.removeAll(this.saveButton, classesToRemove);
  143. this.saveButton.disabled = true;
  144. }
  145. }
  146. };
  147. /**
  148. * Removes option currently selected in dropdown from dropdown menu.
  149. */
  150. BlockLibraryView.prototype.removeSelectedOption = function() {
  151. var selectedOption = this.getSelectedOption();
  152. this.dropdown.removeNode(selectedOption);
  153. };
  154. /**
  155. * Returns block type of selected block.
  156. * @return {string} Type of block selected.
  157. */
  158. BlockLibraryView.prototype.getSelectedBlockType = function() {
  159. var selectedOption = this.getSelectedOption();
  160. var blockType = selectedOption.textContent;
  161. return blockType;
  162. };
  163. /**
  164. * Returns selected option.
  165. * @return {!Element} HTML 'a' element that is the option for a block type.
  166. */
  167. BlockLibraryView.prototype.getSelectedOption = function() {
  168. return this.dropdown.getElementsByClassName('dropdown-content-selected')[0];
  169. };
  170. /**
  171. * Removes all options from dropdown.
  172. */
  173. BlockLibraryView.prototype.clearOptions = function() {
  174. var blockOpts = this.dropdown.getElementsByClassName('blockLibOpt');
  175. var option;
  176. while ((option = blockOpts[0])) {
  177. option.parentNode.removeChild(option);
  178. }
  179. };