summaryrefslogtreecommitdiff
path: root/blockly/demos/blocklyfactory/block_exporter_view.js
blob: 5b703f388555e34202779c33b835cc7ac8e85835 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
 * @license
 * Visual Blocks Editor
 *
 * Copyright 2016 Google Inc.
 * https://developers.google.com/blockly/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @fileoverview Javascript for the Block Exporter View class. Reads from and
 * manages a block selector through which users select blocks to export.
 *
 * @author quachtina96 (Tina Quach)
 */

'use strict';

goog.provide('BlockExporterView');

goog.require('BlockExporterTools');
goog.require('BlockOption');
goog.require('goog.dom');

/**
 * BlockExporter View Class
 * @constructor
 *
 * @param {!Object} blockOptions - Map of block types to BlockOption objects.
 */
BlockExporterView = function(blockOptions) {
  //  Map of block types to BlockOption objects to select from.
  this.blockOptions = blockOptions;
};

/**
 * Set the block options in the selector of this instance of
 * BlockExporterView.
 *
 * @param {!Object} blockOptions - Map of block types to BlockOption objects.
 */
BlockExporterView.prototype.setBlockOptions = function(blockOptions) {
  this.blockOptions = blockOptions;
};

/**
 * Updates the helper text.
 *
 * @param {string} newText - New helper text.
 * @param {boolean} opt_append - True if appending to helper Text, false if
 *    replacing.
 */
BlockExporterView.prototype.updateHelperText = function(newText, opt_append) {
  if (opt_append) {
    goog.dom.getElement('helperText').textContent =
        goog.dom.getElement('helperText').textContent + newText;
  } else {
    goog.dom.getElement('helperText').textContent = newText;
  }
};

/**
 * Updates the helper text to show list of currently selected blocks.
 */
BlockExporterView.prototype.listSelectedBlocks = function() {

  var selectedBlocksText = this.getSelectedBlockTypes().join(",\n ");
  goog.dom.getElement('selectedBlocksText').textContent = selectedBlocksText;
};

/**
 * Selects a given block type in the selector.
 *
 * @param {string} blockType - Type of block to selector.
 */
BlockExporterView.prototype.select = function(blockType) {
  this.blockOptions[blockType].setSelected(true);
};

/**
 * Deselects a block in the selector.
 *
 * @param {!Blockly.Block} block - Type of block to add to selector workspce.
 */
BlockExporterView.prototype.deselect = function(blockType) {
  this.blockOptions[blockType].setSelected(false);
};


/**
 * Deselects all blocks.
 */
BlockExporterView.prototype.deselectAllBlocks = function() {
  for (var blockType in this.blockOptions) {
    this.deselect(blockType);
  }
};

/**
 * Given an array of selected blocks, selects these blocks in the view, marking
 * the checkboxes accordingly.
 *
 * @param {Array.<Blockly.Block>} blockTypes - Array of block types to select.
 */
BlockExporterView.prototype.setSelectedBlockTypes = function(blockTypes) {
  for (var i = 0, blockType; blockType = blockTypes[i]; i++) {
    this.select(blockType);
  }
};

/**
 * Returns array of selected blocks.
 *
 * @return {!Array.<!string>} Array of all selected block types.
 */
BlockExporterView.prototype.getSelectedBlockTypes = function() {
  var selectedTypes = [];
  for (var blockType in this.blockOptions) {
    var blockOption = this.blockOptions[blockType];
    if (blockOption.isSelected()) {
      selectedTypes.push(blockType);
    }
  }
  return selectedTypes;
};

/**
 * Centers the preview block of each block option in the exporter selector.
 */
BlockExporterView.prototype.centerPreviewBlocks = function() {
  for (var blockType in this.blockOptions) {
    this.blockOptions[blockType].centerBlock();
  }
};