-
Notifications
You must be signed in to change notification settings - Fork 284
Stepper rework as variable #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
e586dc7
bb59cba
fc4cfee
e258e1e
8160857
64ff54a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| /** | ||
| * @license | ||
| * Visual Blocks Editor | ||
| * | ||
| * Copyright 2012 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 Variable input field of a specific component type. | ||
| * @author fraser@google.com (Neil Fraser) | ||
| */ | ||
| 'use strict'; | ||
|
|
||
| goog.provide('Blockly.Blocks.ComponentFieldVariable'); | ||
|
|
||
| goog.require('Blockly.Blocks'); | ||
| //goog.require('Blockly.FieldVariable'); | ||
|
|
||
|
|
||
| /** | ||
| * Class for a variable's dropdown field. | ||
| * @param {?string} varname The default name for the variable. If null, | ||
| * a unique variable name will be generated. | ||
| * @param {Function=} opt_validator A function that is executed when a new | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. component_type is missing from the doc. |
||
| * option is selected. Its sole argument is the new option value. | ||
| * @extends {Blockly.FieldVariable} | ||
| * @constructor | ||
| */ | ||
| Blockly.Blocks.ComponentFieldVariable = function(varname, component_type, opt_validator) { | ||
| this.component_type = component_type | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally we should add javadocs for member variable declarations, like in this example. Also missing semicolon. |
||
| //override the dropdownCreate to use for this field | ||
| this.dropdownCreate = Blockly.Blocks.ComponentFieldVariable.dropdownCreateComponents; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Haven't tested this yet, but if this is overwritten from the parent class and the super constructor is called after this assignment, wouldn't it get overwritten back to whatever the parent sets it to?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Best solution would be some more rework in the parent class, but this was the way that needs least changes in parent class of core blockly. It works due to the changes in the parent class |
||
| Blockly.Blocks.ComponentFieldVariable.superClass_.constructor.call(this, | ||
| varname, opt_validator); | ||
| this.setValue(varname || ''); | ||
| }; | ||
| goog.inherits(Blockly.Blocks.ComponentFieldVariable, Blockly.FieldVariable); | ||
|
|
||
| /** | ||
| * Install this dropdown on a block. | ||
| * @param {!Blockly.Block} block The block containing this text. | ||
| */ | ||
| Blockly.Blocks.ComponentFieldVariable.prototype.init = function(block) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we are not adding any additional functionality here, wouldn't this be automatically inherited from the superclass from |
||
| if (this.sourceBlock_) { | ||
| // Dropdown has already been initialized once. | ||
| return; | ||
| } | ||
| Blockly.Blocks.ComponentFieldVariable.superClass_.init.call(this, block); | ||
| }; | ||
|
|
||
| Blockly.Blocks.ComponentFieldVariable.ComponentVariables = function(root, component_type) { | ||
| var blocks; | ||
| if (root.getDescendants) { | ||
| // Root is Block. | ||
| blocks = root.getDescendants(); | ||
| } else if (root.getAllBlocks) { | ||
| // Root is Workspace. | ||
| blocks = root.getAllBlocks(); | ||
| } else { | ||
| throw 'Not Block or Workspace: ' + root; | ||
| } | ||
| var variableHash = Object.create(null); | ||
| // Iterate through every block and add each variable to the hash. | ||
| for (var x = 0; x < blocks.length; x++) { | ||
| if (blocks[x].getComponentName && blocks[x].getVars && | ||
| (blocks[x].getComponentName() == component_type)) { | ||
| var blockVariables = blocks[x].getVars(); | ||
| for (var y = 0; y < blockVariables.length; y++) { | ||
| var varName = blockVariables[y]; | ||
| // Variable name may be null if the block is only half-built. | ||
| if (varName) { | ||
| variableHash[varName.toLowerCase()] = varName; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| // Flatten the hash into a list. | ||
| var variableList = []; | ||
| for (var name in variableHash) { | ||
| variableList.push(variableHash[name]); | ||
| } | ||
| return variableList; | ||
| }; | ||
|
|
||
|
|
||
| /** | ||
| * Return a sorted list of variable names for variable dropdown menus. | ||
| * Include a special option at the end for creating a new variable name. | ||
| * @return {!Array.<string>} Array of variable names. | ||
| * @this {!Blockly.FieldVariable} | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| */ | ||
| Blockly.Blocks.ComponentFieldVariable.dropdownCreateComponents = function() { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we just overwrite the superclass method
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would need to test. I arrived at current method because override was not working, but this was due to dropdownCreate not being part of the prototype, so perhaps you are right, and it could be like you say |
||
| if (this.sourceBlock_ && this.sourceBlock_.workspace) { | ||
| var variableList = | ||
| Blockly.Blocks.ComponentFieldVariable.ComponentVariables(this.sourceBlock_.workspace, | ||
| this.component_type); | ||
| } else { | ||
| var variableList = []; | ||
| } | ||
| // Ensure that the currently selected variable is an option. | ||
| var name = this.getText(); | ||
| if (name && variableList.indexOf(name) == -1) { | ||
| variableList.push(name); | ||
| } | ||
| variableList.sort(goog.string.caseInsensitiveCompare); | ||
| variableList.push(Blockly.Msg.RENAME_VARIABLE); | ||
| variableList.push(Blockly.Msg.NEW_VARIABLE); | ||
| // Variables are not language-specific, use the name as both the user-facing | ||
| // text and the internal representation. | ||
| var options = []; | ||
| for (var x = 0; x < variableList.length; x++) { | ||
| options[x] = [variableList[x], variableList[x]]; | ||
| } | ||
| return options; | ||
| }; | ||
|
|
||
|
|
||
| /** | ||
| * Finds all user-created instances of the ComponentFieldVariable block config. | ||
| * @return {!Array.<string>} Array of instance names. | ||
| */ | ||
| Blockly.Blocks.ComponentFieldVariable.Instances = function(component_type) { | ||
| var instList = []; | ||
| var blocks = Blockly.mainWorkspace.getAllBlocks(); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| for (var x = 0; x < blocks.length; x++) { | ||
| var getSetupInstance = blocks[x].getComponentName; | ||
| if (getSetupInstance) { | ||
| var Instances = getSetupInstance.call(); | ||
| if (Instances) { | ||
| if (Instances == component_type) { | ||
| instList.push(blocks[x].getVars()[0]); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return instList; | ||
| }; | ||
|
|
||
| Blockly.Blocks.ComponentFieldVariable.CheckSetupPresent = function(currentDropdown, component_type) { | ||
| var instances = Blockly.Blocks.ComponentFieldVariable.Instances(component_type); | ||
|
|
||
| // Check for configuration block presence | ||
| if (! instances) { | ||
| return false; | ||
| } else { | ||
| // Configuration blocks present, check if any selected in this block | ||
| var existingConfigSelected = false; | ||
| for (var x = 0; x < instances.length; x++) { | ||
| if (instances[x] === currentDropdown) { | ||
| existingConfigSelected = true; | ||
| } | ||
| } | ||
| if (existingConfigSelected) { | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a suggestion, I quite like the syntax |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this might be required, even if it compiles due to the closure parsing ordering.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I saw other block files which depend on not mentioned requires which are present due to closure parsing, so commented it out. Originally I tried to have this present in blocks/arduino/ which is not possible due to dependencies, then this require is needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you can see with the new FieldNumber, it's good practice to explicitly indicate the dependency, if it works here I would leave it in.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It gives error in build version:
For some reason the compiler gives wrong js file where the error happens ...