Ask user for confirmation when deleting multiple uses of a variable.

This commit is contained in:
Rachel Fenichel 2016-08-08 16:51:54 -07:00
parent 9adb9c0cdb
commit bf6220cb42
2 changed files with 71 additions and 65 deletions

View file

@ -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<b.length;c++){var d=b[c].getVars();if(d)for(var e=0;e<d.length;e++){var f=d[e];f&&(a[f.toLowerCase()]=f)}}b=[];for(var g in a)b.push(a[g]);return b};
Blockly.Variables.allVariables=function(a){a instanceof Blockly.Block&&console.warn("Deprecated call to Blockly.Variables.allVariables with a block instead of a workspace. You may want Blockly.Variables.allUsedVariables");return a.variableList};Blockly.Variables.renameVariable=function(a,b,c){Blockly.Events.setGroup(!0);for(var d=c.getAllBlocks(),e=0;e<d.length;e++)d[e].renameVar(a,b);Blockly.Events.setGroup(!1);c.renameVariable(a,b)};
Blockly.Variables.flyoutCategory=function(a){a=a.variableList;a.sort(goog.string.caseInsensitiveCompare);goog.array.remove(a,Blockly.Msg.VARIABLES_DEFAULT_NAME);a.unshift(Blockly.Msg.VARIABLES_DEFAULT_NAME);var b=[],c=goog.dom.createDom("button");c.setAttribute("text","Create variable");b.push(c);if(Blockly.Blocks.variables_set){c=goog.dom.createDom("block");c.setAttribute("type","variables_set");Blockly.Blocks.math_change?c.setAttribute("gap",8):c.setAttribute("gap",24);var d=goog.dom.createDom("field",
null,a[0]);d.setAttribute("name","VAR");c.appendChild(d);b.push(c)}if(Blockly.Blocks.math_change){c=goog.dom.createDom("block");c.setAttribute("type","math_change");Blockly.Blocks.variables_get&&c.setAttribute("gap",20);d=goog.dom.createDom("value");d.setAttribute("name","DELTA");c.appendChild(d);var e=goog.dom.createDom("shadow");e.setAttribute("type","math_number");d.appendChild(e);d=goog.dom.createDom("field",null,"1");d.setAttribute("name","NUM");e.appendChild(d);b.push(c)}for(e=0;e<a.length;e++)Blockly.Blocks.variables_get&&
(c=goog.dom.createDom("block"),c.setAttribute("type","variables_get"),Blockly.Blocks.variables_set&&c.setAttribute("gap",8),d=goog.dom.createDom("field",null,a[e]),d.setAttribute("name","VAR"),c.appendChild(d),b.push(c));return b};
Blockly.Variables.flyoutCategory=function(a){a=a.variableList;a.sort(goog.string.caseInsensitiveCompare);var b=[],c=goog.dom.createDom("button");c.setAttribute("text","Create variable");b.push(c);if(0<a.length){if(Blockly.Blocks.variables_set){c=goog.dom.createDom("block");c.setAttribute("type","variables_set");Blockly.Blocks.math_change?c.setAttribute("gap",8):c.setAttribute("gap",24);var d=goog.dom.createDom("field",null,a[0]);d.setAttribute("name","VAR");c.appendChild(d);b.push(c)}if(Blockly.Blocks.math_change){c=
goog.dom.createDom("block");c.setAttribute("type","math_change");Blockly.Blocks.variables_get&&c.setAttribute("gap",20);var e=goog.dom.createDom("value");e.setAttribute("name","DELTA");c.appendChild(e);d=goog.dom.createDom("field",null,a[0]);d.setAttribute("name","VAR");c.appendChild(d);d=goog.dom.createDom("shadow");d.setAttribute("type","math_number");e.appendChild(d);e=goog.dom.createDom("field",null,"1");e.setAttribute("name","NUM");d.appendChild(e);b.push(c)}for(e=0;e<a.length;e++)Blockly.Blocks.variables_get&&
(c=goog.dom.createDom("block"),c.setAttribute("type","variables_get"),Blockly.Blocks.variables_set&&c.setAttribute("gap",8),d=goog.dom.createDom("field",null,a[e]),d.setAttribute("name","VAR"),c.appendChild(d),b.push(c))}return b};
Blockly.Variables.generateUniqueName=function(a){a=a.variableList;var b="";if(a.length)for(var c=1,d=0,e="ijkmnopqrstuvwxyzabcdefgh".charAt(d);!b;){for(var f=!1,g=0;g<a.length;g++)if(a[g].toLowerCase()==e){f=!0;break}f?(d++,25==d&&(d=0,c++),e="ijkmnopqrstuvwxyzabcdefgh".charAt(d),1<c&&(e+=c)):b=e}else b="i";return b};
Blockly.Variables.getUses=function(a,b){for(var c=[],d=b.getAllBlocks(),e=0;e<d.length;e++){var f=d[e].getVars();if(f)for(var g=0;g<f.length;g++){var h=f[g];h&&Blockly.Names.equals(h,a)&&c.push(d[e])}}return c};Blockly.Variables.disposeUses=function(a,b){var c=Blockly.Variables.getUses(a,b);Blockly.Events.setGroup(!0);for(var d=0;d<c.length;d++)c[d].dispose(!0,!1);Blockly.Events.setGroup(!1)};
Blockly.Variables["delete"]=function(a,b){var c=b.variableList.indexOf(a);-1!=c&&b.variableList.splice(c,1);Blockly.Variables.disposeUses(a,b)};Blockly.Variables.createVariable=function(a){var b=Blockly.Variables.promptName(Blockly.Msg.NEW_VARIABLE_TITLE,"");return b?(a.createVariable(b),b):null};Blockly.Variables.promptName=function(a,b){var c=window.prompt(a,b);c&&(c=c.replace(/[\s\xa0]+/g," ").replace(/^ | $/g,""),c==Blockly.Msg.RENAME_VARIABLE||c==Blockly.Msg.NEW_VARIABLE)&&(c=null);return c};Blockly.FieldVariable=function(a,b){Blockly.FieldVariable.superClass_.constructor.call(this,Blockly.FieldVariable.dropdownCreate,b);this.setValue(a||"")};goog.inherits(Blockly.FieldVariable,Blockly.FieldDropdown);Blockly.FieldVariable.prototype.init=function(){this.fieldGroup_||(Blockly.FieldVariable.superClass_.init.call(this),this.getValue()||this.setValue(Blockly.Variables.generateUniqueName(this.sourceBlock_.isInFlyout?this.sourceBlock_.workspace.targetWorkspace:this.sourceBlock_.workspace)))};
Blockly.Variables.getUses=function(a,b){for(var c=[],d=b.getAllBlocks(),e=0;e<d.length;e++){var f=d[e].getVars();if(f)for(var g=0;g<f.length;g++){var h=f[g];h&&Blockly.Names.equals(h,a)&&c.push(d[e])}}return c};Blockly.Variables.disposeUses=function(a){Blockly.Events.setGroup(!0);for(var b=0;b<a.length;b++)a[b].dispose(!0,!1);Blockly.Events.setGroup(!1)};
Blockly.Variables["delete"]=function(a,b){var c=b.variableList.indexOf(a);if(-1!=c){var d=Blockly.Variables.getUses(a,b);1<d.length&&window.confirm(Blockly.Msg.DELETE_VARIABLE_CONFIRMATION.replace("%1",d.length).replace("%2",a));Blockly.Variables.disposeUses(d,b);b.variableList.splice(c,1)}};Blockly.Variables.createVariable=function(a){var b=Blockly.Variables.promptName(Blockly.Msg.NEW_VARIABLE_TITLE,"");return b?(a.createVariable(b),b):null};
Blockly.Variables.promptName=function(a,b){var c=window.prompt(a,b);c&&(c=c.replace(/[\s\xa0]+/g," ").replace(/^ | $/g,""),c==Blockly.Msg.RENAME_VARIABLE||c==Blockly.Msg.NEW_VARIABLE)&&(c=null);return c};Blockly.FieldVariable=function(a,b){Blockly.FieldVariable.superClass_.constructor.call(this,Blockly.FieldVariable.dropdownCreate,b);this.setValue(a||"")};goog.inherits(Blockly.FieldVariable,Blockly.FieldDropdown);Blockly.FieldVariable.prototype.init=function(){this.fieldGroup_||(Blockly.FieldVariable.superClass_.init.call(this),this.getValue()||this.setValue(Blockly.Variables.generateUniqueName(this.sourceBlock_.isInFlyout?this.sourceBlock_.workspace.targetWorkspace:this.sourceBlock_.workspace)))};
Blockly.FieldVariable.prototype.getValue=function(){return this.getText()};Blockly.FieldVariable.prototype.setValue=function(a){this.sourceBlock_&&Blockly.Events.isEnabled()&&Blockly.Events.fire(new Blockly.Events.Change(this.sourceBlock_,"field",this.name,this.value_,a));this.value_=a;this.setText(a)};
Blockly.FieldVariable.dropdownCreate=function(){var a=this.sourceBlock_&&this.sourceBlock_.workspace?this.sourceBlock_.workspace.variableList.slice(0):[],b=this.getText();b&&-1==a.indexOf(b)&&a.push(b);a.sort(goog.string.caseInsensitiveCompare);a.push(Blockly.Msg.RENAME_VARIABLE);a.push(Blockly.Msg.DELETE_VARIABLE.replace("%1",b));for(var b=[],c=0;c<a.length;c++)b[c]=[a[c],a[c]];return b};
Blockly.FieldVariable.prototype.classValidator=function(a){var b=this.sourceBlock_.workspace;if(a==Blockly.Msg.RENAME_VARIABLE){var c=this.getText();Blockly.hideChaff();(a=Blockly.Variables.promptName(Blockly.Msg.RENAME_VARIABLE_TITLE.replace("%1",c),c))&&Blockly.Variables.renameVariable(c,a,b);return null}if(a==Blockly.Msg.DELETE_VARIABLE.replace("%1",this.getText()))return Blockly.Variables["delete"](this.getText(),this.sourceBlock_.workspace),null};Blockly.Generator=function(a){this.name_=a;this.FUNCTION_NAME_PLACEHOLDER_REGEXP_=new RegExp(this.FUNCTION_NAME_PLACEHOLDER_,"g")};Blockly.Generator.NAME_TYPE="generated_function";Blockly.Generator.prototype.INFINITE_LOOP_TRAP=null;Blockly.Generator.prototype.STATEMENT_PREFIX=null;Blockly.Generator.prototype.INDENT=" ";Blockly.Generator.prototype.COMMENT_WRAP=60;Blockly.Generator.prototype.ORDER_OVERRIDES=[];

View file

@ -117,76 +117,77 @@ Blockly.Variables.renameVariable = function(oldName, newName, workspace) {
Blockly.Variables.flyoutCategory = function(workspace) {
var variableList = workspace.variableList;
variableList.sort(goog.string.caseInsensitiveCompare);
// In addition to the user's variables, we also want to display the default
// variable name at the top. We also don't want this duplicated if the
// user has created a variable of the same name.
goog.array.remove(variableList, Blockly.Msg.VARIABLES_DEFAULT_NAME);
variableList.unshift(Blockly.Msg.VARIABLES_DEFAULT_NAME);
var xmlList = [];
var button = goog.dom.createDom('button');
button.setAttribute('text', 'Create variable');
xmlList.push(button);
if (Blockly.Blocks['variables_set']) {
// <block type="variables_set" gap="20">
// <field name="VAR">item</field>
// </block>
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']) {
// <block type="math_change">
// <value name="DELTA">
// <shadow type="math_number">
// <field name="NUM">1</field>
// </shadow>
// </value>
// </block>
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']) {
// <block type="variables_get" gap="8">
if (variableList.length > 0) {
if (Blockly.Blocks['variables_set']) {
// <block type="variables_set" gap="20">
// <field name="VAR">item</field>
// </block>
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']) {
// <block type="math_change">
// <value name="DELTA">
// <shadow type="math_number">
// <field name="NUM">1</field>
// </shadow>
// </value>
// </block>
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']) {
// <block type="variables_get" gap="8">
// <field name="VAR">item</field>
// </block>
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.<!Blockly.Block>} 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);
};
/**