From bf6220cb42bddedc2ffff4e69f9dbba07233f808 Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Mon, 8 Aug 2016 16:51:54 -0700 Subject: [PATCH] Ask user for confirmation when deleting multiple uses of a variable. --- blockly_compressed.js | 11 ++-- core/variables.js | 125 ++++++++++++++++++++++-------------------- 2 files changed, 71 insertions(+), 65 deletions(-) diff --git a/blockly_compressed.js b/blockly_compressed.js index 48cd34200..4fb027b86 100644 --- a/blockly_compressed.js +++ b/blockly_compressed.js @@ -1280,12 +1280,13 @@ var a=this.rectElement_||this.imageElement_;a.tooltip=this.sourceBlock_;Blockly. Blockly.FieldImage.prototype.setValue=function(a){null!==a&&(this.src_=a,this.imageElement_&&this.imageElement_.setAttributeNS("http://www.w3.org/1999/xlink","xlink:href",goog.isString(a)?a:""))};Blockly.FieldImage.prototype.setText=function(a){null!==a&&(this.text_=a)};Blockly.FieldImage.prototype.render_=function(){};Blockly.FieldNumber=function(a,b,c,d,e){a=String(a);Blockly.FieldNumber.superClass_.constructor.call(this,a,e);this.setConstraints(b,c,d)};goog.inherits(Blockly.FieldNumber,Blockly.FieldTextInput);Blockly.FieldNumber.prototype.setConstraints=function(a,b,c){c=parseFloat(c);this.precision_=isNaN(c)?0:c;a=parseFloat(a);this.min_=isNaN(a)?-Infinity:a;b=parseFloat(b);this.max_=isNaN(b)?Infinity:b;this.setValue(this.callValidator(this.getValue()))}; Blockly.FieldNumber.prototype.classValidator=function(a){if(null===a)return null;a=String(a);a=a.replace(/O/ig,"0");a=a.replace(/,/g,"");a=parseFloat(a||0);if(isNaN(a))return null;this.precision_&&Number.isFinite(a)&&(a=Math.round(a/this.precision_)*this.precision_);a=goog.math.clamp(a,this.min_,this.max_);return String(a)};Blockly.Variables={};Blockly.Variables.NAME_TYPE="VARIABLE";Blockly.Variables.allUsedVariables=function(a){var b;if(a instanceof Blockly.Block)b=a.getDescendants();else if(a.getAllBlocks)b=a.getAllBlocks();else throw"Not Block or Workspace: "+a;a=Object.create(null);for(var c=0;c - // item - // - var block = goog.dom.createDom('block'); - block.setAttribute('type', 'variables_set'); - if (Blockly.Blocks['math_change']) { - block.setAttribute('gap', 8); - } else { - block.setAttribute('gap', 24); - } - var field = goog.dom.createDom('field', null, variableList[0]); - field.setAttribute('name', 'VAR'); - block.appendChild(field); - xmlList.push(block); - } - if (Blockly.Blocks['math_change']) { - // - // - // - // 1 - // - // - // - var block = goog.dom.createDom('block'); - block.setAttribute('type', 'math_change'); - if (Blockly.Blocks['variables_get']) { - block.setAttribute('gap', 20); - } - var value = goog.dom.createDom('value'); - value.setAttribute('name', 'DELTA'); - block.appendChild(value); - - var shadowBlock = goog.dom.createDom('shadow'); - shadowBlock.setAttribute('type', 'math_number'); - value.appendChild(shadowBlock); - - var field = goog.dom.createDom('field', null, '1'); - field.setAttribute('name', 'NUM'); - shadowBlock.appendChild(field); - - xmlList.push(block); - } - - for (var i = 0; i < variableList.length; i++) { - if (Blockly.Blocks['variables_get']) { - // + if (variableList.length > 0) { + if (Blockly.Blocks['variables_set']) { + // // item // var block = goog.dom.createDom('block'); - block.setAttribute('type', 'variables_get'); - if (Blockly.Blocks['variables_set']) { + block.setAttribute('type', 'variables_set'); + if (Blockly.Blocks['math_change']) { block.setAttribute('gap', 8); + } else { + block.setAttribute('gap', 24); } - var field = goog.dom.createDom('field', null, variableList[i]); + var field = goog.dom.createDom('field', null, variableList[0]); field.setAttribute('name', 'VAR'); block.appendChild(field); xmlList.push(block); } + if (Blockly.Blocks['math_change']) { + // + // + // + // 1 + // + // + // + var block = goog.dom.createDom('block'); + block.setAttribute('type', 'math_change'); + if (Blockly.Blocks['variables_get']) { + block.setAttribute('gap', 20); + } + var value = goog.dom.createDom('value'); + value.setAttribute('name', 'DELTA'); + block.appendChild(value); + + var field = goog.dom.createDom('field', null, variableList[0]); + field.setAttribute('name', 'VAR'); + block.appendChild(field); + + var shadowBlock = goog.dom.createDom('shadow'); + shadowBlock.setAttribute('type', 'math_number'); + value.appendChild(shadowBlock); + + var numberField = goog.dom.createDom('field', null, '1'); + numberField.setAttribute('name', 'NUM'); + shadowBlock.appendChild(numberField); + + xmlList.push(block); + } + + for (var i = 0; i < variableList.length; i++) { + if (Blockly.Blocks['variables_get']) { + // + // item + // + var block = goog.dom.createDom('block'); + block.setAttribute('type', 'variables_get'); + if (Blockly.Blocks['variables_set']) { + block.setAttribute('gap', 8); + } + var field = goog.dom.createDom('field', null, variableList[i]); + field.setAttribute('name', 'VAR'); + block.appendChild(field); + xmlList.push(block); + } + } } return xmlList; }; @@ -267,11 +268,9 @@ Blockly.Variables.getUses = function(name, workspace) { /** * When a variable is deleted, find and dispose of all uses of it. - * @param {string} name Name of deleted variable. - * @param {!Blockly.Workspace} workspace The workspace to delete uses from. + * @param {!Array.} uses An array of blocks using the variable. */ -Blockly.Variables.disposeUses = function(name, workspace) { - var uses = Blockly.Variables.getUses(name, workspace); +Blockly.Variables.disposeUses = function(uses) { Blockly.Events.setGroup(true); for (var i = 0; i < uses.length; i++) { uses[i].dispose(true, false); @@ -287,10 +286,16 @@ Blockly.Variables.disposeUses = function(name, workspace) { Blockly.Variables.delete = function(name, workspace) { var variableIndex = workspace.variableList.indexOf(name); if (variableIndex != -1) { + var uses = Blockly.Variables.getUses(name, workspace); + if (uses.length > 1) { + window.confirm( + Blockly.Msg.DELETE_VARIABLE_CONFIRMATION.replace('%1', uses.length). + replace('%2', name)); + } + Blockly.Variables.disposeUses(uses); workspace.variableList.splice(variableIndex, 1); } - Blockly.Variables.disposeUses(name, workspace); }; /**