block_exporter_view.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 the Block Exporter View class. Reads from and
  22. * manages a block selector through which users select blocks to export.
  23. *
  24. * @author quachtina96 (Tina Quach)
  25. */
  26. 'use strict';
  27. goog.provide('BlockExporterView');
  28. goog.require('BlockExporterTools');
  29. goog.require('BlockOption');
  30. goog.require('goog.dom');
  31. /**
  32. * BlockExporter View Class
  33. * @param {!Object} blockOptions Map of block types to BlockOption objects.
  34. * @constructor
  35. */
  36. BlockExporterView = function(blockOptions) {
  37. // Map of block types to BlockOption objects to select from.
  38. this.blockOptions = blockOptions;
  39. };
  40. /**
  41. * Set the block options in the selector of this instance of
  42. * BlockExporterView.
  43. * @param {!Object} blockOptions Map of block types to BlockOption objects.
  44. */
  45. BlockExporterView.prototype.setBlockOptions = function(blockOptions) {
  46. this.blockOptions = blockOptions;
  47. };
  48. /**
  49. * Updates the helper text to show list of currently selected blocks.
  50. */
  51. BlockExporterView.prototype.listSelectedBlocks = function() {
  52. var selectedBlocksText = this.getSelectedBlockTypes().join(",\n ");
  53. document.getElementById('selectedBlocksText').textContent = selectedBlocksText;
  54. };
  55. /**
  56. * Selects a given block type in the selector.
  57. * @param {string} blockType Type of block to selector.
  58. */
  59. BlockExporterView.prototype.select = function(blockType) {
  60. this.blockOptions[blockType].setSelected(true);
  61. };
  62. /**
  63. * Deselects a block in the selector.
  64. * @param {!Blockly.Block} block Type of block to add to selector workspce.
  65. */
  66. BlockExporterView.prototype.deselect = function(blockType) {
  67. this.blockOptions[blockType].setSelected(false);
  68. };
  69. /**
  70. * Deselects all blocks.
  71. */
  72. BlockExporterView.prototype.deselectAllBlocks = function() {
  73. for (var blockType in this.blockOptions) {
  74. this.deselect(blockType);
  75. }
  76. };
  77. /**
  78. * Given an array of selected blocks, selects these blocks in the view, marking
  79. * the checkboxes accordingly.
  80. * @param {Array.<Blockly.Block>} blockTypes Array of block types to select.
  81. */
  82. BlockExporterView.prototype.setSelectedBlockTypes = function(blockTypes) {
  83. for (var i = 0, blockType; blockType = blockTypes[i]; i++) {
  84. this.select(blockType);
  85. }
  86. };
  87. /**
  88. * Returns array of selected blocks.
  89. * @return {!Array.<string>} Array of all selected block types.
  90. */
  91. BlockExporterView.prototype.getSelectedBlockTypes = function() {
  92. var selectedTypes = [];
  93. for (var blockType in this.blockOptions) {
  94. var blockOption = this.blockOptions[blockType];
  95. if (blockOption.isSelected()) {
  96. selectedTypes.push(blockType);
  97. }
  98. }
  99. return selectedTypes;
  100. };
  101. /**
  102. * Centers the preview block of each block option in the exporter selector.
  103. */
  104. BlockExporterView.prototype.centerPreviewBlocks = function() {
  105. for (var blockType in this.blockOptions) {
  106. this.blockOptions[blockType].centerBlock();
  107. }
  108. };