blockly > Variables

Variables namespace


Function Description
allDeveloperVariables(workspace) 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.
allUsedVarModels(ws) 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.
createVariableButtonHandler(workspace, opt_callback, opt_type)

Handles "Create Variable" button in the default variables toolbox category. It will prompt the user for a variable 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.

flyoutCategory(workspace) Construct the elements (blocks and button) required by the flyout for the variable category.
flyoutCategoryBlocks(workspace) Construct the blocks required by the flyout for the variable category.
generateUniqueName(workspace) 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'.
generateUniqueNameFromOptions(startChar, usedNames) Returns a unique name that is not present in the usedNames array. This will try to generate single letter names in the range a - z (skip l). It will start with the character passed to startChar.
generateVariableFieldDom(variableModel) Generate DOM objects representing a variable field.
getOrCreateVariablePackage(workspace, id, opt_name, opt_type) Helper function to look up or create a variable on the given workspace. If no variable exists, creates and returns it.
getVariable(workspace, id, opt_name, opt_type) 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.
nameUsedWithAnyType(name, workspace) Check whether there exists a variable with the given name of any type.
promptName(promptText, defaultText, callback) Prompt the user for a new variable name.
renameVariable(workspace, variable, opt_callback) Opens a prompt that allows the user to enter a new name for a variable. Triggers a rename if the new name is valid. Or re-prompts if there is a collision.


Variable Description
CATEGORY_NAME String for use in the "custom" attribute of a category in toolbox XML. This string indicates that the category should be dynamically populated with variable blocks. See also Blockly.Procedures.CATEGORY_NAME and Blockly.VariablesDynamic.CATEGORY_NAME.