From 93eb06e947f5cd5fd5d7dc516b7824e55794147e Mon Sep 17 00:00:00 2001 From: dube1503 Date: Sat, 7 Oct 2017 13:28:32 -0500 Subject: [PATCH 1/5] Refactors individual checks to use indexOf in graphs.js for plot block menu. --- blocks/graphs.js | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/blocks/graphs.js b/blocks/graphs.js index 69ea4efd..a46789bc 100644 --- a/blocks/graphs.js +++ b/blocks/graphs.js @@ -56,14 +56,9 @@ Blockly.Blocks['plot'] = { for (var curr in allVariables) { // allVariables is not a hash list and contains variable object functions // Filter out the names of object funtions that get unwantingly returned - if (!(curr === "append" || - curr === "copy" || - curr === "extend" || - curr === "index" || - curr === "insert" || - curr === "remove" || - curr === "+")) { - + var objFuncList = ["append","copy","extend","index","insert","remove","+"]; + + if (objFuncList.indexOf(curr) < 0) { var variableUses = thisBlock.workspace.getVariableUses(allVariables[curr]) if (variableUses.length > 1) { var topBlock = {index: null, height: null}; From fb66a815c3a85c96ff07a04a34f7f529bd85790b Mon Sep 17 00:00:00 2001 From: dube1503 Date: Fri, 13 Oct 2017 14:28:27 -0500 Subject: [PATCH 2/5] Sets all variables with default isIterator to false and gives for_loop onchange function to change relevant variables to iterators. --- blocks/loops.js | 14 ++++++++++++++ blocks/variables.js | 4 +++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/blocks/loops.js b/blocks/loops.js index 1add4329..511a8b64 100644 --- a/blocks/loops.js +++ b/blocks/loops.js @@ -195,6 +195,20 @@ Blockly.Blocks['controls_for'] = { option.callback = Blockly.ContextMenu.callbackFactory(this, xmlBlock); options.push(option); } + }, + /** + * Add onchange function to find all "iterator" variables created + * by this for_loop block and assign isIterator to True. + * @this Blockly.BlockSvg + */ + onchange: function(){ + var name = this.getFieldValue('VAR'); + var allIterators = this.workspace.getVariableUses(name); + for (var block in allIterators) { + if (allIterators[block].type === "variables_get") { + allIterators[block].isIterator = true; + } + } } }; diff --git a/blocks/variables.js b/blocks/variables.js index 13a68d2a..a76f3dec 100644 --- a/blocks/variables.js +++ b/blocks/variables.js @@ -223,7 +223,8 @@ Blockly.Blocks['variables_get'] = { .appendField(new Blockly.FieldVariable( Blockly.Msg.VARIABLES_DEFAULT_NAME , function(selection){ thisBlock.component = 'none'; - thisBlock.setNewType(selection); + thisBlock.setNewType(selection) + thisBlock.isIterator = false; }) , 'VAR'); @@ -233,6 +234,7 @@ Blockly.Blocks['variables_get'] = { this.selectedType = null; this.attribute = 'none'; this.component = 'none'; + this.isIterator = false; }, From 277f5abaf57a937f4865372073c260d51c93971b Mon Sep 17 00:00:00 2001 From: dube1503 Date: Fri, 20 Oct 2017 12:03:30 -0500 Subject: [PATCH 3/5] Adds iterator check to plot block dynamicOptions and refactors said code. --- blocks/graphs.js | 65 ++++++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 27 deletions(-) diff --git a/blocks/graphs.js b/blocks/graphs.js index 69ea4efd..7eb832ab 100644 --- a/blocks/graphs.js +++ b/blocks/graphs.js @@ -45,48 +45,59 @@ Blockly.Blocks['plot'] = { }, dynamicOptions: function(thisBlock) { + // Options list to fill and push to dropdown var options = [] - // Variable for storing a list of all variable blocks in workspace - var allVariables = Blockly.Variables.allVariables(thisBlock.workspace); // Variable for menu options if no Series type variables are found in workspace - var empty = ["none","none"]; + var empty = ["none","none"]; + // Don't push variable names if in flyout menu if (this.isInFlyout) options.push(empty); + // Variable for storing a list of all variable block NAMES in workspace + var allVariables = Blockly.Variables.allVariables(thisBlock.workspace); + if (!(allVariables.length==0)) { - for (var curr in allVariables) { - // allVariables is not a hash list and contains variable object functions - // Filter out the names of object funtions that get unwantingly returned - if (!(curr === "append" || - curr === "copy" || - curr === "extend" || - curr === "index" || - curr === "insert" || - curr === "remove" || - curr === "+")) { - - var variableUses = thisBlock.workspace.getVariableUses(allVariables[curr]) - if (variableUses.length > 1) { + + for (var i = 0; i < allVariables.length; i++) { + // List of REFERENCES to actual blocks with given name + // Used to loop through + var variableUses = thisBlock.workspace.getVariableUses(allVariables[i]); + // List of variableUses to store only variables that are not iterators + var currentUses = thisBlock.workspace.getVariableUses(allVariables[i]); + // Check if current variable name is an iterator (or a variable_set feild on loop block) + // If current variable is iterator, remove from currentUses + for (var j = 0; j < variableUses.length; j++) { + if(variableUses[j].isIterator || variableUses[j].type === "controls_for") { + currentUses = []; + break; + } + } + // If carruentUses is now empty, then the current variable name is an iterator + // Move to the next variable name and check + // If currentUses contains more than one block reference, find the topBlock reference + // If it contains only one reference, that is topBlock decleration block + if (!(currentUses.length == 0)) { + if (currentUses.length > 1) { var topBlock = {index: null, height: null}; - for(var i = 0; i < variableUses.length; i++){ + for(var k = 0; k < currentUses.length; k++){ // add the first value, then find the highest but only if it is not itself - if((topBlock.index == null || topBlock.height > variableUses[i].getRelativeToSurfaceXY().y) - && variableUses[i].type == "variables_set" ){ - topBlock["index"] = i; - topBlock["height"] = variableUses[i].getRelativeToSurfaceXY().y; + if((topBlock.index == null || topBlock.height > currentUses[k].getRelativeToSurfaceXY().y) + && currentUses[k].type == "variables_set" ){ + topBlock["index"] = k; + topBlock["height"] = currentUses[k].getRelativeToSurfaceXY().y; } } - var varBlock = variableUses[topBlock.index]; + var varBlock = currentUses[topBlock.index]; } else { - var varBlock = variableUses[0]; + var varBlock = currentUses[0]; } varBlock = varBlock.inputList[0].connection; // Only push variable block to menu if it has a Series type connected to it if (!(varBlock===null)) { - if (!(varBlock.targetConnection==null) && varBlock.targetConnection.check_[0]==="Series") { - options.push([allVariables[curr],allVariables[curr].toUpperCase()]); - } + if (!(varBlock.targetConnection==null) && varBlock.targetConnection.check_[0]==="Series") { + options.push([allVariables[i],allVariables[i].toUpperCase()]); + } } - } + } } if (options.length==0) { options.push(empty); From 32abbcefc78afb26fed6a1bddf94c9d558ad9542 Mon Sep 17 00:00:00 2001 From: cody Date: Fri, 27 Oct 2017 16:29:04 -0500 Subject: [PATCH 4/5] adds conditional statement to not call 'set new type' on loop blocks --- core/field_variable.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/field_variable.js b/core/field_variable.js index 5b1e6a36..d0306268 100644 --- a/core/field_variable.js +++ b/core/field_variable.js @@ -86,7 +86,10 @@ Blockly.FieldVariable.prototype.setValue = function(newValue) { if (this.sourceBlock_ && Blockly.Events.isEnabled()) { Blockly.Events.fire(new Blockly.Events.Change( this.sourceBlock_, 'field', this.name, this.value_, newValue)); - this.sourceBlock_.setNewType(newValue); + if(this.sourceBlock_.type!=='controls_for'){ + this.sourceBlock_.setNewType(newValue); + } + } this.value_ = newValue; this.setText(newValue); From a2ebe5189762e169b1d9405e559c2e6d3e8d8ef8 Mon Sep 17 00:00:00 2001 From: dube1503 Date: Sun, 29 Oct 2017 16:40:31 -0500 Subject: [PATCH 5/5] Update bower.json --- bower.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bower.json b/bower.json index eb927b05..1eeb8304 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "blocks-vpython", - "version": "0.1.4", + "version": "0.1.5", "description": "Blockly + VPython Glowscript", "main": [ "blockly_compressed.js",