diff --git a/blockly_compressed_horizontal.js b/blockly_compressed_horizontal.js index f7b83b9778..ea00db37c5 100644 --- a/blockly_compressed_horizontal.js +++ b/blockly_compressed_horizontal.js @@ -613,6 +613,7 @@ Blockly.Variables.noVariableText=function(){return"No variable selected"};Blockl Blockly.Variables.realizePotentialVar=function(a,b,c,d){var e=c.getPotentialVariableMap();c=c.targetWorkspace;if(e){var f;d&&(f=Blockly.Variables.getVariable(c,null,a,b));d=e.getVariable(a,b);if(!d)return null;var g=d.getId();e.deleteVariable(d);return f?f:c.createVariable(a,b,g)}console.warn("Called Blockly.Variables.realizePotentialVar with incorrect workspace. The provided workspace does not have a potential variable map.")}; Blockly.Variables.createVariable=function(a,b,c){if(c==Blockly.BROADCAST_MESSAGE_VARIABLE_TYPE){var d=Blockly.Msg.NEW_BROADCAST_MESSAGE_TITLE;var e=Blockly.Msg.BROADCAST_MODAL_TITLE}else c==Blockly.LIST_VARIABLE_TYPE?(d=Blockly.Msg.NEW_LIST_TITLE,e=Blockly.Msg.LIST_MODAL_TITLE):(c=c?c:"",d=Blockly.Msg.NEW_VARIABLE_TITLE,e=Blockly.Msg.VARIABLE_MODAL_TITLE);var f=Blockly.Variables.nameValidator_.bind(null,c);Blockly.prompt(d,"",function(d,e,k){var g="local"===k.scope||!1;k=k.isCloud||!1;e=e||[];if(d= f(d,a,g?[]:e,k,b)){var h;a.getPotentialVariableMap()&&c&&(h=Blockly.Variables.realizePotentialVar(d,c,a,!1));h||(h=a.createVariable(d,c,null,g,k));g=a.isFlyout?a:a.getFlyout();h=h.getId();g.setCheckboxState&&g.setCheckboxState(h,!0);b&&b(h)}else b&&b(null)},e,c)}; +Blockly.Variables.createCloudVarialbe=function(a,b,c,d){var e="local"===c.scope||!1;c=c.isCloud||!1;b=b||[];b=e?[]:b;if(a=Blockly.Variables.nameValidator_.bind(null,"")(a,d,b,c,null)){var f;d.getPotentialVariableMap()&&(f=Blockly.Variables.realizePotentialVar(a,"",d,!1));f||(f=d.createVariable(a,"",null,e,c));d=d.isFlyout?d:d.getFlyout();f=f.getId();d.setCheckboxState&&d.setCheckboxState(f,!0)}}; Blockly.Variables.nameValidator_=function(a,b,c,d,e,f){return a==Blockly.BROADCAST_MESSAGE_VARIABLE_TYPE?Blockly.Variables.validateBroadcastMessageName_(b,c,f):a==Blockly.LIST_VARIABLE_TYPE?Blockly.Variables.validateScalarVarOrListName_(b,c,d,!1,a,Blockly.Msg.LIST_ALREADY_EXISTS):Blockly.Variables.validateScalarVarOrListName_(b,c,d,e,a,Blockly.Msg.VARIABLE_ALREADY_EXISTS)}; Blockly.Variables.validateBroadcastMessageName_=function(a,b,c){return a?(b=b.getVariable(a,Blockly.BROADCAST_MESSAGE_VARIABLE_TYPE))?(c&&c(b.getId()),null):a:null};Blockly.Variables.validateScalarVarOrListName_=function(a,b,c,d,e,f){a=Blockly.Variables.trimName_(a);if(!a)return null;d&&(a=Blockly.Variables.CLOUD_PREFIX+a);return b.getVariable(a,e)||0<=c.indexOf(a)?(Blockly.alert(f.replace("%1",a)),null):a}; Blockly.Variables.renameVariable=function(a,b,c){var d=b.type;if(d==Blockly.BROADCAST_MESSAGE_VARIABLE_TYPE)console.warn("Unexpected attempt to rename a broadcast message with id: "+b.getId()+" and name: "+b.name);else{if(d==Blockly.LIST_VARIABLE_TYPE){var e=Blockly.Msg.RENAME_LIST_TITLE;var f=Blockly.Msg.RENAME_LIST_MODAL_TITLE}else e=Blockly.Msg.RENAME_VARIABLE_TITLE,f=Blockly.Msg.RENAME_VARIABLE_MODAL_TITLE;var g=Blockly.Variables.nameValidator_.bind(null,d);e=e.replace("%1",b.name);Blockly.prompt(e, diff --git a/blockly_compressed_vertical.js b/blockly_compressed_vertical.js index d5d98449f3..04888a6be4 100644 --- a/blockly_compressed_vertical.js +++ b/blockly_compressed_vertical.js @@ -613,6 +613,7 @@ Blockly.Variables.noVariableText=function(){return"No variable selected"};Blockl Blockly.Variables.realizePotentialVar=function(a,b,c,d){var e=c.getPotentialVariableMap();c=c.targetWorkspace;if(e){var f;d&&(f=Blockly.Variables.getVariable(c,null,a,b));d=e.getVariable(a,b);if(!d)return null;var g=d.getId();e.deleteVariable(d);return f?f:c.createVariable(a,b,g)}console.warn("Called Blockly.Variables.realizePotentialVar with incorrect workspace. The provided workspace does not have a potential variable map.")}; Blockly.Variables.createVariable=function(a,b,c){if(c==Blockly.BROADCAST_MESSAGE_VARIABLE_TYPE){var d=Blockly.Msg.NEW_BROADCAST_MESSAGE_TITLE;var e=Blockly.Msg.BROADCAST_MODAL_TITLE}else c==Blockly.LIST_VARIABLE_TYPE?(d=Blockly.Msg.NEW_LIST_TITLE,e=Blockly.Msg.LIST_MODAL_TITLE):(c=c?c:"",d=Blockly.Msg.NEW_VARIABLE_TITLE,e=Blockly.Msg.VARIABLE_MODAL_TITLE);var f=Blockly.Variables.nameValidator_.bind(null,c);Blockly.prompt(d,"",function(d,e,k){var g="local"===k.scope||!1;k=k.isCloud||!1;e=e||[];if(d= f(d,a,g?[]:e,k,b)){var h;a.getPotentialVariableMap()&&c&&(h=Blockly.Variables.realizePotentialVar(d,c,a,!1));h||(h=a.createVariable(d,c,null,g,k));g=a.isFlyout?a:a.getFlyout();h=h.getId();g.setCheckboxState&&g.setCheckboxState(h,!0);b&&b(h)}else b&&b(null)},e,c)}; +Blockly.Variables.createCloudVarialbe=function(a,b,c,d){var e="local"===c.scope||!1;c=c.isCloud||!1;b=b||[];b=e?[]:b;if(a=Blockly.Variables.nameValidator_.bind(null,"")(a,d,b,c,null)){var f;d.getPotentialVariableMap()&&(f=Blockly.Variables.realizePotentialVar(a,"",d,!1));f||(f=d.createVariable(a,"",null,e,c));d=d.isFlyout?d:d.getFlyout();f=f.getId();d.setCheckboxState&&d.setCheckboxState(f,!0)}}; Blockly.Variables.nameValidator_=function(a,b,c,d,e,f){return a==Blockly.BROADCAST_MESSAGE_VARIABLE_TYPE?Blockly.Variables.validateBroadcastMessageName_(b,c,f):a==Blockly.LIST_VARIABLE_TYPE?Blockly.Variables.validateScalarVarOrListName_(b,c,d,!1,a,Blockly.Msg.LIST_ALREADY_EXISTS):Blockly.Variables.validateScalarVarOrListName_(b,c,d,e,a,Blockly.Msg.VARIABLE_ALREADY_EXISTS)}; Blockly.Variables.validateBroadcastMessageName_=function(a,b,c){return a?(b=b.getVariable(a,Blockly.BROADCAST_MESSAGE_VARIABLE_TYPE))?(c&&c(b.getId()),null):a:null};Blockly.Variables.validateScalarVarOrListName_=function(a,b,c,d,e,f){a=Blockly.Variables.trimName_(a);if(!a)return null;d&&(a=Blockly.Variables.CLOUD_PREFIX+a);return b.getVariable(a,e)||0<=c.indexOf(a)?(Blockly.alert(f.replace("%1",a)),null):a}; Blockly.Variables.renameVariable=function(a,b,c){var d=b.type;if(d==Blockly.BROADCAST_MESSAGE_VARIABLE_TYPE)console.warn("Unexpected attempt to rename a broadcast message with id: "+b.getId()+" and name: "+b.name);else{if(d==Blockly.LIST_VARIABLE_TYPE){var e=Blockly.Msg.RENAME_LIST_TITLE;var f=Blockly.Msg.RENAME_LIST_MODAL_TITLE}else e=Blockly.Msg.RENAME_VARIABLE_TITLE,f=Blockly.Msg.RENAME_VARIABLE_MODAL_TITLE;var g=Blockly.Variables.nameValidator_.bind(null,d);e=e.replace("%1",b.name);Blockly.prompt(e, diff --git a/core/blockly.js b/core/blockly.js index fa0b484896..bd0aa43e1d 100644 --- a/core/blockly.js +++ b/core/blockly.js @@ -399,6 +399,7 @@ Blockly.prompt = function(message, defaultValue, callback, _opt_title, _opt_varType) { // opt_title and opt_varType are unused because we only need them to pass // information to the scratch-gui, which overwrites this function + callback(window.prompt(message, defaultValue)); }; diff --git a/core/data_category.js b/core/data_category.js index 5488a579ea..6559eb5a1f 100644 --- a/core/data_category.js +++ b/core/data_category.js @@ -387,7 +387,8 @@ Blockly.DataCategory.addCreateButton = function(xmlList, workspace, type) { var msg = Blockly.Msg.NEW_VARIABLE; var callbackKey = 'CREATE_VARIABLE'; var callback = function(button) { - Blockly.Variables.createVariable(button.getTargetWorkspace(), null, '');}; + Blockly.Variables.createVariable(button.getTargetWorkspace(), null, ''); + }; if (type === 'LIST') { msg = Blockly.Msg.NEW_LIST; diff --git a/core/variables.js b/core/variables.js index 9728ed14d8..eb2aabbca3 100644 --- a/core/variables.js +++ b/core/variables.js @@ -330,7 +330,6 @@ Blockly.Variables.createVariable = function(workspace, opt_callback, opt_type) { if (flyout.setCheckboxState) { flyout.setCheckboxState(variableBlockId, true); } - if (opt_callback) { opt_callback(variableBlockId); } @@ -367,6 +366,52 @@ Blockly.Variables.createVariable = function(workspace, opt_callback, opt_type) { * proceed with creating or renaming the variable. * @private */ +/* +* xigua +*/ +Blockly.Variables.createCloudVarialbe = function(text, additionalVars, variableOptions, workspace) { + var scope = variableOptions.scope; + var isLocal = (scope === 'local') || false; + var isCloud = variableOptions.isCloud || false; + // Default to [] if additionalVars is not provided + additionalVars = additionalVars || []; + // Only use additionalVars for global variable creation. + var additionalVarNames = isLocal ? [] : additionalVars; + var validate = Blockly.Variables.nameValidator_.bind(null, ''); + var validatedText = validate(text, workspace, additionalVarNames, isCloud, null); + if (validatedText) { + // The name is valid according to the type, create the variable + var potentialVarMap = workspace.getPotentialVariableMap(); + var variable; + // This check ensures that if a new variable is being created from a + // workspace that already has a variable of the same name and type as + // a potential variable, that potential variable gets turned into a + // real variable and thus there aren't duplicate options in the field_variable + // dropdown. + if (potentialVarMap) { + variable = Blockly.Variables.realizePotentialVar(validatedText, + '', workspace, false); + } + if (!variable) { + variable = workspace.createVariable(validatedText, '', null, isLocal, isCloud); + } + + var flyout = workspace.isFlyout ? workspace : workspace.getFlyout(); + var variableBlockId = variable.getId(); + if (flyout.setCheckboxState) { + flyout.setCheckboxState(variableBlockId, true); + } + // if (opt_callback) { + // opt_callback(variableBlockId); + // } + } else { + // User canceled prompt without a value. + // if (opt_callback) { + // opt_callback(null); + // } + } +} + Blockly.Variables.nameValidator_ = function(type, text, workspace, additionalVars, isCloud, opt_callback) { // The validators for the different variable types require slightly different arguments.