Wrote getAllUsedBlockTypes in model, small change to generator (squashed commit) (#554)

This commit is contained in:
Emma Dauterman
2016-08-16 16:00:27 -07:00
committed by picklesrus
parent 8ec5745611
commit 33f0f51968
2 changed files with 52 additions and 9 deletions

View File

@@ -66,7 +66,8 @@ FactoryGenerator.prototype.generateToolboxXml = function() {
});
if (!this.model.hasElements()) {
// Toolbox has no categories. Use XML directly from workspace.
this.loadToHiddenWorkspaceAndSave_(this.model.getSelectedXml(), xmlDom);
this.loadToHiddenWorkspace_(this.model.getSelectedXml());
this.appendHiddenWorkspaceToDom_(xmlDom);
} else {
// Toolbox has categories.
// Assert that selected != null
@@ -100,7 +101,8 @@ FactoryGenerator.prototype.generateToolboxXml = function() {
}
// Load that category to hidden workspace, setting user-generated shadow
// blocks as real shadow blocks.
this.loadToHiddenWorkspaceAndSave_(element.xml, nextElement);
this.loadToHiddenWorkspace_(element.xml);
this.appendHiddenWorkspaceToDom_(nextElement);
}
xmlDom.appendChild(nextElement);
}
@@ -127,22 +129,19 @@ FactoryGenerator.prototype.generateWorkspaceXml = function() {
generatedXml.setAttribute('id', 'preload_blocks');
generatedXml.setAttribute('style', 'display:none');
return generatedXml;
}
};
/**
* Load the given XML to the hidden workspace, set any user-generated shadow
* blocks to be actual shadow blocks, then append the XML from the workspace
* to the DOM element passed in.
* Loads the given XML to the hidden workspace and sets any user-generated
* shadow blocks to be actual shadow blocks.
* @private
*
* @param {!Element} xml The XML to be loaded to the hidden workspace.
* @param {!Element} dom The DOM element to append the generated XML to.
*/
FactoryGenerator.prototype.loadToHiddenWorkspaceAndSave_ = function(xml, dom) {
FactoryGenerator.prototype.loadToHiddenWorkspace_ = function(xml) {
this.hiddenWorkspace.clear();
Blockly.Xml.domToWorkspace(xml, this.hiddenWorkspace);
this.setShadowBlocksInHiddenWorkspace_();
this.appendHiddenWorkspaceToDom_(dom);
}
/**

View File

@@ -423,6 +423,50 @@ FactoryModel.prototype.setOptionsAttribute = function(name, value) {
this.options[name] = value;
};
/*
* Returns an array of all the block types currently being used in the toolbox
* and the pre-loaded blocks. No duplicates.
* TODO(evd2014): Move pushBlockTypesToList to FactoryUtils.
*
* @return {!Array<!string>} Array of block types currently being used.
*/
FactoryModel.prototype.getAllUsedBlockTypes = function() {
var blockTypeList = [];
// Given XML for the workspace, adds all block types included in the XML
// to the list, not including duplicates.
var pushBlockTypesToList = function (xml, list) {
// Get all block XML nodes.
var blocks = xml.getElementsByTagName('block');
// Add block types if not already in list.
for (var i = 0; i < blocks.length; i++) {
var type = blocks[i].getAttribute('type');
if (list.indexOf(type) == -1) {
list.push(type);
}
}
};
if (this.flyout) {
// If has a single flyout, add block types for the single flyout.
this.pushBlockTypesToList(this.getSelectedXml(), blockTypeList);
} else {
// If has categories, add block types for each category.
for (var i = 0, category; category = this.toolboxList[i]; i++) {
if (category.type == ListElement.TYPE_CATEGORY) {
this.pushBlockTypesToList(category.xml, blockTypeList);
}
}
}
// Add the block types from any pre-loaded blocks.
this.pushBlockTypesToList(this.getPreloadXml(), blockTypeList);
return blockTypeList;
}
/**
* Class for a ListElement.
* @constructor