2013-10-30 14:46:03 -07:00
|
|
|
/**
|
2014-01-28 03:00:09 -08:00
|
|
|
* @license
|
2013-10-30 14:46:03 -07:00
|
|
|
* Visual Blocks Editor
|
|
|
|
*
|
|
|
|
* Copyright 2012 Google Inc.
|
2014-10-07 13:09:55 -07:00
|
|
|
* https://developers.google.com/blockly/
|
2013-10-30 14:46:03 -07:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @fileoverview Utility functions for handling procedures.
|
|
|
|
* @author fraser@google.com (Neil Fraser)
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
goog.provide('Blockly.Procedures');
|
|
|
|
|
|
|
|
// TODO(scr): Fix circular dependencies
|
|
|
|
// goog.require('Blockly.Block');
|
2014-12-18 17:26:08 -08:00
|
|
|
goog.require('Blockly.Field');
|
2013-10-30 14:46:03 -07:00
|
|
|
goog.require('Blockly.Names');
|
|
|
|
goog.require('Blockly.Workspace');
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Category to separate procedure names from variables and generated functions.
|
|
|
|
*/
|
|
|
|
Blockly.Procedures.NAME_TYPE = 'PROCEDURE';
|
|
|
|
|
|
|
|
/**
|
2015-01-12 14:57:00 -08:00
|
|
|
* Find all user-created procedure definitions in a workspace.
|
|
|
|
* @param {!Blockly.Workspace} root Root workspace.
|
2013-10-30 14:46:03 -07:00
|
|
|
* @return {!Array.<!Array.<!Array>>} Pair of arrays, the
|
|
|
|
* first contains procedures without return variables, the second with.
|
|
|
|
* Each procedure is defined by a three-element list of name, parameter
|
|
|
|
* list, and return value boolean.
|
|
|
|
*/
|
2015-01-12 14:57:00 -08:00
|
|
|
Blockly.Procedures.allProcedures = function(root) {
|
|
|
|
var blocks = root.getAllBlocks();
|
2013-10-30 14:46:03 -07:00
|
|
|
var proceduresReturn = [];
|
|
|
|
var proceduresNoReturn = [];
|
2015-08-25 15:09:55 +01:00
|
|
|
for (var i = 0; i < blocks.length; i++) {
|
|
|
|
if (blocks[i].getProcedureDef) {
|
|
|
|
var tuple = blocks[i].getProcedureDef();
|
2013-10-30 14:46:03 -07:00
|
|
|
if (tuple) {
|
|
|
|
if (tuple[2]) {
|
|
|
|
proceduresReturn.push(tuple);
|
|
|
|
} else {
|
|
|
|
proceduresNoReturn.push(tuple);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
proceduresNoReturn.sort(Blockly.Procedures.procTupleComparator_);
|
|
|
|
proceduresReturn.sort(Blockly.Procedures.procTupleComparator_);
|
|
|
|
return [proceduresNoReturn, proceduresReturn];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Comparison function for case-insensitive sorting of the first element of
|
|
|
|
* a tuple.
|
|
|
|
* @param {!Array} ta First tuple.
|
|
|
|
* @param {!Array} tb Second tuple.
|
|
|
|
* @return {number} -1, 0, or 1 to signify greater than, equality, or less than.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Blockly.Procedures.procTupleComparator_ = function(ta, tb) {
|
2015-08-25 15:09:55 +01:00
|
|
|
return ta[0].toLowerCase().localeCompare(tb[0].toLowerCase());
|
2013-10-30 14:46:03 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure two identically-named procedures don't exist.
|
|
|
|
* @param {string} name Proposed procedure name.
|
|
|
|
* @param {!Blockly.Block} block Block to disambiguate.
|
|
|
|
* @return {string} Non-colliding name.
|
|
|
|
*/
|
|
|
|
Blockly.Procedures.findLegalName = function(name, block) {
|
|
|
|
if (block.isInFlyout) {
|
2015-08-25 14:11:07 +01:00
|
|
|
// Flyouts can have multiple procedures called 'do something'.
|
2013-10-30 14:46:03 -07:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
while (!Blockly.Procedures.isLegalName(name, block.workspace, block)) {
|
|
|
|
// Collision with another procedure.
|
|
|
|
var r = name.match(/^(.*?)(\d+)$/);
|
|
|
|
if (!r) {
|
|
|
|
name += '2';
|
|
|
|
} else {
|
|
|
|
name = r[1] + (parseInt(r[2], 10) + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does this procedure have a legal name? Illegal names include names of
|
|
|
|
* procedures already defined.
|
|
|
|
* @param {string} name The questionable name.
|
|
|
|
* @param {!Blockly.Workspace} workspace The workspace to scan for collisions.
|
2015-07-13 15:03:22 -07:00
|
|
|
* @param {Blockly.Block=} opt_exclude Optional block to exclude from
|
2013-10-30 14:46:03 -07:00
|
|
|
* comparisons (one doesn't want to collide with oneself).
|
|
|
|
* @return {boolean} True if the name is legal.
|
|
|
|
*/
|
|
|
|
Blockly.Procedures.isLegalName = function(name, workspace, opt_exclude) {
|
|
|
|
var blocks = workspace.getAllBlocks();
|
|
|
|
// Iterate through every block and check the name.
|
2015-08-25 15:09:55 +01:00
|
|
|
for (var i = 0; i < blocks.length; i++) {
|
|
|
|
if (blocks[i] == opt_exclude) {
|
2013-10-30 14:46:03 -07:00
|
|
|
continue;
|
|
|
|
}
|
2015-08-25 15:09:55 +01:00
|
|
|
if (blocks[i].getProcedureDef) {
|
|
|
|
var procName = blocks[i].getProcedureDef();
|
2013-10-30 14:46:03 -07:00
|
|
|
if (Blockly.Names.equals(procName[0], name)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rename a procedure. Called by the editable field.
|
|
|
|
* @param {string} text The proposed new name.
|
|
|
|
* @return {string} The accepted name.
|
2014-12-18 17:26:08 -08:00
|
|
|
* @this {!Blockly.Field}
|
2013-10-30 14:46:03 -07:00
|
|
|
*/
|
|
|
|
Blockly.Procedures.rename = function(text) {
|
|
|
|
// Strip leading and trailing whitespace. Beyond this, all names are legal.
|
|
|
|
text = text.replace(/^[\s\xa0]+|[\s\xa0]+$/g, '');
|
|
|
|
|
|
|
|
// Ensure two identically-named procedures don't exist.
|
|
|
|
text = Blockly.Procedures.findLegalName(text, this.sourceBlock_);
|
|
|
|
// Rename any callers.
|
|
|
|
var blocks = this.sourceBlock_.workspace.getAllBlocks();
|
2015-08-25 15:09:55 +01:00
|
|
|
for (var i = 0; i < blocks.length; i++) {
|
|
|
|
if (blocks[i].renameProcedure) {
|
|
|
|
blocks[i].renameProcedure(this.text_, text);
|
2013-10-30 14:46:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct the blocks required by the flyout for the procedure category.
|
2015-10-25 22:20:08 -04:00
|
|
|
* @param {!Blockly.Workspace} workspace The workspace contianing procedures.
|
|
|
|
* @return {!Array.<!Element>} Array of XML block elements.
|
2013-10-30 14:46:03 -07:00
|
|
|
*/
|
2015-10-25 22:20:08 -04:00
|
|
|
Blockly.Procedures.flyoutCategory = function(workspace) {
|
|
|
|
var xmlList = [];
|
2013-10-30 14:46:03 -07:00
|
|
|
if (Blockly.Blocks['procedures_defnoreturn']) {
|
2015-10-25 22:20:08 -04:00
|
|
|
// <block type="procedures_defnoreturn" gap="16"></block>
|
|
|
|
var block = goog.dom.createDom('block');
|
|
|
|
block.setAttribute('type', 'procedures_defnoreturn');
|
|
|
|
block.setAttribute('gap', 16);
|
|
|
|
xmlList.push(block);
|
2013-10-30 14:46:03 -07:00
|
|
|
}
|
|
|
|
if (Blockly.Blocks['procedures_defreturn']) {
|
2015-10-25 22:20:08 -04:00
|
|
|
// <block type="procedures_defreturn" gap="16"></block>
|
|
|
|
var block = goog.dom.createDom('block');
|
|
|
|
block.setAttribute('type', 'procedures_defreturn');
|
|
|
|
block.setAttribute('gap', 16);
|
|
|
|
xmlList.push(block);
|
2013-10-30 14:46:03 -07:00
|
|
|
}
|
|
|
|
if (Blockly.Blocks['procedures_ifreturn']) {
|
2015-10-25 22:20:08 -04:00
|
|
|
// <block type="procedures_ifreturn" gap="16"></block>
|
|
|
|
var block = goog.dom.createDom('block');
|
|
|
|
block.setAttribute('type', 'procedures_ifreturn');
|
|
|
|
block.setAttribute('gap', 16);
|
|
|
|
xmlList.push(block);
|
2013-10-30 14:46:03 -07:00
|
|
|
}
|
2015-10-25 22:20:08 -04:00
|
|
|
if (xmlList.length) {
|
2013-10-30 14:46:03 -07:00
|
|
|
// Add slightly larger gap between system blocks and user calls.
|
2015-10-25 22:20:08 -04:00
|
|
|
xmlList[xmlList.length - 1].setAttribute('gap', 24);
|
2013-10-30 14:46:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function populateProcedures(procedureList, templateName) {
|
2015-10-25 22:20:08 -04:00
|
|
|
for (var i = 0; i < procedureList.length; i++) {
|
|
|
|
var name = procedureList[i][0];
|
|
|
|
var args = procedureList[i][1];
|
|
|
|
// <block type="procedures_callnoreturn" gap="16">
|
|
|
|
// <mutation name="do something">
|
|
|
|
// <arg name="x"></arg>
|
|
|
|
// </mutation>
|
|
|
|
// </block>
|
|
|
|
var block = goog.dom.createDom('block');
|
|
|
|
block.setAttribute('type', templateName);
|
|
|
|
block.setAttribute('gap', 16);
|
|
|
|
var mutation = goog.dom.createDom('mutation');
|
|
|
|
mutation.setAttribute('name', name);
|
|
|
|
block.appendChild(mutation);
|
|
|
|
for (var t = 0; t < args.length; t++) {
|
|
|
|
var arg = goog.dom.createDom('arg');
|
|
|
|
arg.setAttribute('name', args[t]);
|
|
|
|
mutation.appendChild(arg);
|
2013-10-30 14:46:03 -07:00
|
|
|
}
|
2015-10-25 22:20:08 -04:00
|
|
|
xmlList.push(block);
|
2013-10-30 14:46:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-25 22:20:08 -04:00
|
|
|
var tuple = Blockly.Procedures.allProcedures(workspace);
|
2013-10-30 14:46:03 -07:00
|
|
|
populateProcedures(tuple[0], 'procedures_callnoreturn');
|
|
|
|
populateProcedures(tuple[1], 'procedures_callreturn');
|
2015-10-25 22:20:08 -04:00
|
|
|
return xmlList;
|
2013-10-30 14:46:03 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find all the callers of a named procedure.
|
|
|
|
* @param {string} name Name of procedure.
|
|
|
|
* @param {!Blockly.Workspace} workspace The workspace to find callers in.
|
|
|
|
* @return {!Array.<!Blockly.Block>} Array of caller blocks.
|
|
|
|
*/
|
|
|
|
Blockly.Procedures.getCallers = function(name, workspace) {
|
|
|
|
var callers = [];
|
|
|
|
var blocks = workspace.getAllBlocks();
|
|
|
|
// Iterate through every block and check the name.
|
2015-08-25 15:09:55 +01:00
|
|
|
for (var i = 0; i < blocks.length; i++) {
|
|
|
|
if (blocks[i].getProcedureCall) {
|
|
|
|
var procName = blocks[i].getProcedureCall();
|
2013-10-30 14:46:03 -07:00
|
|
|
// Procedure name may be null if the block is only half-built.
|
|
|
|
if (procName && Blockly.Names.equals(procName, name)) {
|
2015-08-25 15:09:55 +01:00
|
|
|
callers.push(blocks[i]);
|
2013-10-30 14:46:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return callers;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When a procedure definition is disposed of, find and dispose of all its
|
|
|
|
* callers.
|
|
|
|
* @param {string} name Name of deleted procedure definition.
|
|
|
|
* @param {!Blockly.Workspace} workspace The workspace to delete callers from.
|
|
|
|
*/
|
|
|
|
Blockly.Procedures.disposeCallers = function(name, workspace) {
|
|
|
|
var callers = Blockly.Procedures.getCallers(name, workspace);
|
2015-08-25 15:09:55 +01:00
|
|
|
for (var i = 0; i < callers.length; i++) {
|
|
|
|
callers[i].dispose(true, false);
|
2013-10-30 14:46:03 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When a procedure definition changes its parameters, find and edit all its
|
|
|
|
* callers.
|
2016-03-22 10:48:13 -07:00
|
|
|
* @param {!Blockly.Block} defBlock Procedure definition block.
|
2013-10-30 14:46:03 -07:00
|
|
|
*/
|
2016-03-22 10:48:13 -07:00
|
|
|
Blockly.Procedures.mutateCallers = function(defBlock) {
|
|
|
|
var oldRecordUndo = Blockly.Events.recordUndo;
|
|
|
|
var name = defBlock.getProcedureDef()[0];
|
|
|
|
var xmlElement = defBlock.mutationToDom(true);
|
|
|
|
var callers = Blockly.Procedures.getCallers(name, defBlock.workspace);
|
|
|
|
for (var i = 0, caller; caller = callers[i]; i++) {
|
|
|
|
var oldMutationDom = caller.mutationToDom();
|
|
|
|
var oldMutation = oldMutationDom && Blockly.Xml.domToText(oldMutationDom);
|
|
|
|
caller.domToMutation(xmlElement);
|
|
|
|
var newMutationDom = caller.mutationToDom();
|
|
|
|
var newMutation = newMutationDom && Blockly.Xml.domToText(newMutationDom);
|
|
|
|
if (oldMutation != newMutation) {
|
|
|
|
// Fire a mutation on every caller block. But don't record this as an
|
|
|
|
// undo action since it is deterministically tied to the procedure's
|
|
|
|
// definition mutation.
|
|
|
|
Blockly.Events.recordUndo = false;
|
|
|
|
Blockly.Events.fire(new Blockly.Events.Change(
|
|
|
|
caller, 'mutation', null, oldMutation, newMutation));
|
|
|
|
Blockly.Events.recordUndo = oldRecordUndo;
|
|
|
|
}
|
2013-10-30 14:46:03 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the definition block for the named procedure.
|
|
|
|
* @param {string} name Name of procedure.
|
|
|
|
* @param {!Blockly.Workspace} workspace The workspace to search.
|
|
|
|
* @return {Blockly.Block} The procedure definition block, or null not found.
|
|
|
|
*/
|
|
|
|
Blockly.Procedures.getDefinition = function(name, workspace) {
|
|
|
|
var blocks = workspace.getAllBlocks();
|
2015-08-25 15:09:55 +01:00
|
|
|
for (var i = 0; i < blocks.length; i++) {
|
|
|
|
if (blocks[i].getProcedureDef) {
|
|
|
|
var tuple = blocks[i].getProcedureDef();
|
2013-10-30 14:46:03 -07:00
|
|
|
if (tuple && Blockly.Names.equals(tuple[0], name)) {
|
2015-08-25 15:09:55 +01:00
|
|
|
return blocks[i];
|
2013-10-30 14:46:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|