Skip to content

Commit ee35430

Browse files
committed
Improvements: less code needed, better documentation
1 parent e0867b4 commit ee35430

3 files changed

Lines changed: 22 additions & 27 deletions

File tree

ardublockly/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<script src="../blockly/arduino_compressed.js"></script>
1919
<!-- To use the uncompressed version comment out the above and comment in the ones below -->
2020
<!--script src="../blockly/blockly_uncompressed.js"></script>
21+
<script src="../blockly/blocks/component_variable.js"></script>
2122
<script src="../blockly/blocks/logic.js"></script>
2223
<script src="../blockly/blocks/loops.js"></script>
2324
<script src="../blockly/blocks/math.js"></script>

blockly/blocks/arduino/stepper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ Blockly.Blocks['stepper_step'] = {
168168
if (!this.workspace) { return; } // Block has been deleted.
169169

170170
var currentDropdown = this.getFieldValue('STEPPER_NAME');
171-
if (Blockly.Blocks.ComponentFieldVariable.CheckSetupPresent(currentDropdown, 'Stepper')) {
171+
if (Blockly.Blocks.ComponentFieldVariable.CheckSetupPresent(this.workspace, currentDropdown, 'Stepper')) {
172172
this.setWarningText(null);
173173
} else {
174174
// Set a warning to select a valid stepper config

blockly/blocks/component_variable.js

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,40 +27,27 @@
2727
goog.provide('Blockly.Blocks.ComponentFieldVariable');
2828

2929
goog.require('Blockly.Blocks');
30-
//goog.require('Blockly.FieldVariable');
3130

3231

3332
/**
3433
* Class for a variable's dropdown field.
3534
* @param {?string} varname The default name for the variable. If null,
3635
* a unique variable name will be generated.
36+
* @param {?string} component_type The type of component to show in the dropdown list, eg 'Stepper'
3737
* @param {Function=} opt_validator A function that is executed when a new
3838
* option is selected. Its sole argument is the new option value.
3939
* @extends {Blockly.FieldVariable}
4040
* @constructor
4141
*/
4242
Blockly.Blocks.ComponentFieldVariable = function(varname, component_type, opt_validator) {
43-
this.component_type = component_type
44-
//override the dropdownCreate to use for this field
45-
this.dropdownCreate = Blockly.Blocks.ComponentFieldVariable.dropdownCreateComponents;
43+
/** @type {string} */
44+
this.component_type = component_type;
4645
Blockly.Blocks.ComponentFieldVariable.superClass_.constructor.call(this,
4746
varname, opt_validator);
4847
this.setValue(varname || '');
4948
};
5049
goog.inherits(Blockly.Blocks.ComponentFieldVariable, Blockly.FieldVariable);
5150

52-
/**
53-
* Install this dropdown on a block.
54-
* @param {!Blockly.Block} block The block containing this text.
55-
*/
56-
Blockly.Blocks.ComponentFieldVariable.prototype.init = function(block) {
57-
if (this.sourceBlock_) {
58-
// Dropdown has already been initialized once.
59-
return;
60-
}
61-
Blockly.Blocks.ComponentFieldVariable.superClass_.init.call(this, block);
62-
};
63-
6451
Blockly.Blocks.ComponentFieldVariable.ComponentVariables = function(root, component_type) {
6552
var blocks;
6653
if (root.getDescendants) {
@@ -100,9 +87,9 @@ Blockly.Blocks.ComponentFieldVariable.ComponentVariables = function(root, compon
10087
* Return a sorted list of variable names for variable dropdown menus.
10188
* Include a special option at the end for creating a new variable name.
10289
* @return {!Array.<string>} Array of variable names.
103-
* @this {!Blockly.FieldVariable}
90+
* @this {!ComponentFieldVariable}
10491
*/
105-
Blockly.Blocks.ComponentFieldVariable.dropdownCreateComponents = function() {
92+
Blockly.Blocks.ComponentFieldVariable.prototype.dropdownCreate = function() {
10693
if (this.sourceBlock_ && this.sourceBlock_.workspace) {
10794
var variableList =
10895
Blockly.Blocks.ComponentFieldVariable.ComponentVariables(this.sourceBlock_.workspace,
@@ -130,9 +117,11 @@ Blockly.Blocks.ComponentFieldVariable.dropdownCreateComponents = function() {
130117

131118
/**
132119
* Finds all user-created instances of the ComponentFieldVariable block config.
120+
* @param {!Blockly.Workspace} workspace The block's workspace.
121+
* @param {?string} component_type The type of component setup block to obtain instances from
133122
* @return {!Array.<string>} Array of instance names.
134123
*/
135-
Blockly.Blocks.ComponentFieldVariable.Instances = function(component_type) {
124+
Blockly.Blocks.ComponentFieldVariable.Instances = function(workspace, component_type) {
136125
var instList = [];
137126
var blocks = Blockly.mainWorkspace.getAllBlocks();
138127
for (var x = 0; x < blocks.length; x++) {
@@ -149,8 +138,17 @@ Blockly.Blocks.ComponentFieldVariable.Instances = function(component_type) {
149138
return instList;
150139
};
151140

152-
Blockly.Blocks.ComponentFieldVariable.CheckSetupPresent = function(currentDropdown, component_type) {
153-
var instances = Blockly.Blocks.ComponentFieldVariable.Instances(component_type);
141+
/**
142+
* Check to know of a setup block (which sets pin number) is present for a used
143+
* ComponentFieldVariable.
144+
* @param {!Blockly.Workspace} workspace The block's workspace.
145+
* @param {?string} currentDropdown The selected name in the dropdown for which to check
146+
* if setup block present
147+
* @param {?string} component_type The type of component setup block must be checked
148+
* @return {<boolean>} Wether setup block is present or not
149+
*/
150+
Blockly.Blocks.ComponentFieldVariable.CheckSetupPresent = function(workspace, currentDropdown, component_type) {
151+
var instances = Blockly.Blocks.ComponentFieldVariable.Instances(workspace, component_type);
154152

155153
// Check for configuration block presence
156154
if (! instances) {
@@ -163,10 +161,6 @@ Blockly.Blocks.ComponentFieldVariable.CheckSetupPresent = function(currentDropdo
163161
existingConfigSelected = true;
164162
}
165163
}
166-
if (existingConfigSelected) {
167-
return true;
168-
} else {
169-
return false;
170-
}
164+
return existingConfigSelected;
171165
}
172166
}

0 commit comments

Comments
 (0)