Adding automatically-generated variable lists to the accessible toolbox. (#1149)

Fixing a bug with the core-only uncompressed file not finding its own directory.
This commit is contained in:
CoryDCode
2017-06-05 13:42:51 -07:00
committed by GitHub
parent 7336d03538
commit f48a68a9ef
3 changed files with 70 additions and 42 deletions

View File

@@ -61,44 +61,57 @@ blocklyApp.ToolboxModalService = ng.core.Class({
'can be shown.');
};
// Populate the toolbox categories.
this.allToolboxCategories = [];
var toolboxXmlElt = document.getElementById('blockly-toolbox-xml');
var toolboxCategoryElts = toolboxXmlElt.getElementsByTagName('category');
if (toolboxCategoryElts.length) {
this.allToolboxCategories = Array.from(toolboxCategoryElts).map(
function(categoryElt) {
var tmpWorkspace = new Blockly.Workspace();
Blockly.Xml.domToWorkspace(categoryElt, tmpWorkspace);
return {
categoryName: categoryElt.attributes.name.value,
blocks: tmpWorkspace.topBlocks_
};
}
);
this.computeCategoriesForCreateNewGroupModal_();
} else {
// A timeout seems to be needed in order for the .children accessor to
// work correctly.
var that = this;
setTimeout(function() {
// If there are no top-level categories, we create a single category
// containing all the top-level blocks.
var tmpWorkspace = new Blockly.Workspace();
Array.from(toolboxXmlElt.children).forEach(function(topLevelNode) {
Blockly.Xml.domToBlock(tmpWorkspace, topLevelNode);
});
that.allToolboxCategories = [{
categoryName: '',
blocks: tmpWorkspace.topBlocks_
}];
that.computeCategoriesForCreateNewGroupModal_();
});
}
this.populateToolbox_();
}
],
populateToolbox_: function() {
// Populate the toolbox categories.
this.allToolboxCategories = [];
var toolboxXmlElt = document.getElementById('blockly-toolbox-xml');
var toolboxCategoryElts = toolboxXmlElt.getElementsByTagName('category');
if (toolboxCategoryElts.length) {
this.allToolboxCategories = Array.from(toolboxCategoryElts).map(
function(categoryElt) {
var tmpWorkspace = new Blockly.Workspace();
var custom = categoryElt.attributes.custom
// TODO (corydiers): Implement custom flyouts once #1153 is solved.
if (custom && custom.value == Blockly.VARIABLE_CATEGORY_NAME) {
var varBlocks =
Blockly.Variables.flyoutCategoryBlocks(blocklyApp.workspace);
varBlocks.forEach(function(block) {
Blockly.Xml.domToBlock(block, tmpWorkspace);
});
} else {
Blockly.Xml.domToWorkspace(categoryElt, tmpWorkspace);
}
return {
categoryName: categoryElt.attributes.name.value,
blocks: tmpWorkspace.topBlocks_
};
}
);
this.computeCategoriesForCreateNewGroupModal_();
} else {
// A timeout seems to be needed in order for the .children accessor to
// work correctly.
var that = this;
setTimeout(function() {
// If there are no top-level categories, we create a single category
// containing all the top-level blocks.
var tmpWorkspace = new Blockly.Workspace();
Array.from(toolboxXmlElt.children).forEach(function(topLevelNode) {
Blockly.Xml.domToBlock(tmpWorkspace, topLevelNode);
});
that.allToolboxCategories = [{
categoryName: '',
blocks: tmpWorkspace.topBlocks_
}];
that.computeCategoriesForCreateNewGroupModal_();
});
}
},
computeCategoriesForCreateNewGroupModal_: function() {
// Precompute toolbox categories for blocks that have no output
// connection (and that can therefore be used as the base block of a
@@ -165,6 +178,7 @@ blocklyApp.ToolboxModalService = ng.core.Class({
var that = this;
var selectedToolboxCategories = [];
this.populateToolbox_();
this.allToolboxCategories.forEach(function(toolboxCategory) {
var selectedBlocks = toolboxCategory.blocks.filter(function(block) {
return that.blockConnectionService.canBeAttachedToMarkedConnection(
@@ -202,6 +216,7 @@ blocklyApp.ToolboxModalService = ng.core.Class({
},
showToolboxModalForCreateNewGroup: function(sourceButtonId) {
var that = this;
this.populateToolbox_();
this.showModal_(this.toolboxCategoriesForNewGroup, function(block) {
var blockDescription = that.utilsService.getBlockDescription(block);
var xml = Blockly.Xml.blockToDom(block);

View File

@@ -115,7 +115,7 @@ window.BLOCKLY_DIR = (function() {
if (!isNodeJS) {
// Find name of current directory.
var scripts = document.getElementsByTagName('script');
var re = new RegExp('(.+)[\/]blockly_(.+)uncompressed\.js$');
var re = new RegExp('(.+)[\/]blockly_(.*)uncompressed\.js$');
for (var i = 0, script; script = scripts[i]; i++) {
var match = re.exec(script.src);
if (match) {

View File

@@ -101,14 +101,12 @@ Blockly.Variables.allVariables = function(root) {
};
/**
* Construct the blocks required by the flyout for the variable category.
* Construct the elements (blocks and button) required by the flyout for the
* variable category.
* @param {!Blockly.Workspace} workspace The workspace contianing variables.
* @return {!Array.<!Element>} Array of XML block elements.
* @return {!Array.<!Element>} Array of XML elements.
*/
Blockly.Variables.flyoutCategory = function(workspace) {
var variableModelList = workspace.getVariablesOfType('');
variableModelList.sort(Blockly.VariableModel.compareByName);
var xmlList = [];
var button = goog.dom.createDom('button');
button.setAttribute('text', Blockly.Msg.NEW_VARIABLE);
@@ -120,6 +118,21 @@ Blockly.Variables.flyoutCategory = function(workspace) {
xmlList.push(button);
var blockList = Blockly.Variables.flyoutCategoryBlocks(workspace);
xmlList = xmlList.concat(blockList);
return xmlList;
};
/**
* Construct the blocks required by the flyout for the variable category.
* @param {!Blockly.Workspace} workspace The workspace contianing variables.
* @return {!Array.<!Element>} Array of XML block elements.
*/
Blockly.Variables.flyoutCategoryBlocks = function(workspace) {
var variableModelList = workspace.getVariablesOfType('');
variableModelList.sort(Blockly.VariableModel.compareByName);
var xmlList = [];
if (variableModelList.length > 0) {
var firstVariable = variableModelList[0];
if (Blockly.Blocks['variables_set']) {