Properties

createVariable

static

Original name of Blockly.Variables.createVariableButtonHandler(..).

Parameter

workspace

Blockly.Workspace

The workspace on which to create the variable.

Value must not be null.

opt_callback

Optional

function(optional nullable string)

A callback. It will be passed an acceptable new variable name, or null if change is to be aborted (cancel button), or undefined if an existing variable was chosen.

opt_type

Optional

string

The type of the variable like 'int', 'string', or ''. This will default to '', which is a specific type.

Deprecated
Use Blockly.Variables.createVariableButtonHandler(..).

NAME_TYPE

static

Constant to separate variable names from procedures and generated functions when running generators.

Deprecated
Use Blockly.VARIABLE_CATEGORY_NAME

Methods

allDeveloperVariables

static

allDeveloperVariables(workspace) returns Array of string

Find all developer variables used by blocks in the workspace. Developer variables are never shown to the user, but are declared as global variables in the generated code. To declare developer variables, define the getDeveloperVariables function on your block and return a list of variable names. For use by generators.

Parameter

workspace

Blockly.Workspace

The workspace to search.

Value must not be null.

Returns

non-null Array of string A list of non-duplicated variable names.

allUsedVariables

static

allUsedVariables()

Find all user-created variables that are in use in the workspace and return only their names. For use by generators. To get a list of all variables on a workspace, including unused variables, call Workspace.getAllVariables.

Deprecated
January 2018

allUsedVarModels

static

allUsedVarModels(ws) returns Array of non-null Blockly.VariableModel

Find all user-created variables that are in use in the workspace. For use by generators. To get a list of all variables on a workspace, including unused variables, call Workspace.getAllVariables.

Parameter

ws

Blockly.Workspace

The workspace to search for variables.

Value must not be null.

Returns

non-null Array of non-null Blockly.VariableModel Array of variable models.

createVariableButtonHandler

static

createVariableButtonHandler(workspace, opt_callback, opt_type)

Handles "Create Variable" button in the default variables toolbox category. It will prompt the user for a varibale name, including re-prompts if a name is already in use among the workspace's variables.

Custom button handlers can delegate to this function, allowing variables types and after-creation processing. More complex customization (e.g., prompting for variable type) is beyond the scope of this function.

Parameter

workspace

Blockly.Workspace

The workspace on which to create the variable.

Value must not be null.

opt_callback

Optional

function(optional nullable string)

A callback. It will be passed an acceptable new variable name, or null if change is to be aborted (cancel button), or undefined if an existing variable was chosen.

opt_type

Optional

string

The type of the variable like 'int', 'string', or ''. This will default to '', which is a specific type.

flyoutCategory

static

flyoutCategory(workspace) returns Array of non-null Element

Construct the elements (blocks and button) required by the flyout for the variable category.

Parameter

workspace

Blockly.Workspace

The workspace containing variables.

Value must not be null.

Returns

non-null Array of non-null Element Array of XML elements.

flyoutCategoryBlocks

static

flyoutCategoryBlocks(workspace) returns Array of non-null Element

Construct the blocks required by the flyout for the variable category.

Parameter

workspace

Blockly.Workspace

The workspace containing variables.

Value must not be null.

Returns

non-null Array of non-null Element Array of XML block elements.

generateUniqueName

static

generateUniqueName(workspace) returns string

Return a new variable name that is not yet being used. This will try to generate single letter variable names in the range 'i' to 'z' to start with. If no unique name is located it will try 'i' to 'z', 'a' to 'h', then 'i2' to 'z2' etc. Skip 'l'.

Parameter

workspace

Blockly.Workspace

The workspace to be unique in.

Value must not be null.

Returns

string New variable name.

generateVariableFieldDom

public   static

generateVariableFieldDom(variableModel) returns Element

Generate DOM objects representing a variable field.

Parameter

variableModel

Blockly.VariableModel

The variable model to represent.

Value must not be null.

Returns

Element The generated DOM.

generateVariableFieldXmlString

static

generateVariableFieldXmlString(variableModel) returns string

Generate XML string for variable field.

Parameter

variableModel

Blockly.VariableModel

The variable model to generate an XML string from.

Value must not be null.

Returns

string The generated XML.

getAddedVariables

static

getAddedVariables(workspace, originalVariables) returns Array of non-null Blockly.VariableModel

Helper function to get the list of variables that have been added to the workspace after adding a new block, using the given list of variables that were in the workspace before the new block was added.

Parameter

workspace

Blockly.Workspace

The workspace to inspect.

Value must not be null.

originalVariables

Array of non-null Blockly.VariableModel

The array of variables that existed in the workspace before adding the new block.

Value must not be null.

Returns

non-null Array of non-null Blockly.VariableModel The new array of variables that were freshly added to the workspace after creating the new block, or [] if no new variables were added to the workspace.

getOrCreateVariablePackage

static

getOrCreateVariablePackage(workspace, id, opt_name, opt_type) returns Blockly.VariableModel

Helper function to look up or create a variable on the given workspace. If no variable exists, creates and returns it.

Parameter

workspace

Blockly.Workspace

The workspace to search for the variable. It may be a flyout workspace or main workspace.

Value must not be null.

id

string

The ID to use to look up or create the variable, or null.

opt_name

Optional

string

The string to use to look up or create the variable.

opt_type

Optional

string

The type to use to look up or create the variable.

Returns

non-null Blockly.VariableModel The variable corresponding to the given ID or name + type combination.

getVariable

static

getVariable(workspace, id, opt_name, opt_type) returns Blockly.VariableModel

Look up a variable on the given workspace. Always looks in the main workspace before looking in the flyout workspace. Always prefers lookup by ID to lookup by name + type.

Parameter

workspace

Blockly.Workspace

The workspace to search for the variable. It may be a flyout workspace or main workspace.

Value must not be null.

id

string

The ID to use to look up the variable, or null.

opt_name

Optional

string

The string to use to look up the variable. Only used if lookup by ID fails.

opt_type

Optional

string

The type to use to look up the variable. Only used if lookup by ID fails.

Returns

nullable Blockly.VariableModel The variable corresponding to the given ID or name + type combination, or null if not found.

promptName

static

promptName(promptText, defaultText, callback)

Prompt the user for a new variable name.

Parameter

promptText

string

The string of the prompt.

defaultText

string

The default value to show in the prompt's field.

callback

function(nullable string)

A callback. It will return the new variable name, or null if the user picked something illegal.

renameVariable

static

renameVariable(workspace, variable, opt_callback)

Rename a variable with the given workspace, variableType, and oldName.

Parameter

workspace

Blockly.Workspace

The workspace on which to rename the variable.

Value must not be null.

variable

Blockly.VariableModel

Variable to rename.

opt_callback

Optional

function(optional nullable string)

A callback. It will be passed an acceptable new variable name, or null if change is to be aborted (cancel button), or undefined if an existing variable was chosen.