Skip to content

Commit

Permalink
Add functionality to ComponentManager
Browse files Browse the repository at this point in the history
  • Loading branch information
kozbial committed Jun 7, 2021
1 parent ab5616c commit 3c6a2e3
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions core/component_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,62 @@ Blockly.ComponentManager.prototype.addComponent = function(componentInfo) {
}
};

/**
* Adds capability to existing registered component.
* @param {string} id The ID of the component to add capability to.
* @param {string|!Blockly.ComponentManager.Capability<Blockly.IComponent>
* } capability The capability to add.
*/
Blockly.ComponentManager.prototype.addCapability = function(id, capability) {
capability = String(capability).toLowerCase();
if (!this.getComponent(id)) {
throw Error('Cannot add capability, "' + capability + '". Plugin "' +
id + '" has not been added to the ComponentManager');
}
if (this.hasCapability(id, capability)) {
console.warn('Plugin "' + id + 'already has capability "' +
capability + '"');
return;
}
this.componentData_[id].capabilities.push(capability);
this.capabilityToComponentIds_[capability].push(id);
};

/**
* Adds capability to existing registered component.
* @param {string} id The ID of the component to add capability to.
* @param {string|!Blockly.ComponentManager.Capability<Blockly.IComponent>
* } capability The capability to remove.
*/
Blockly.ComponentManager.prototype.removeCapability = function(id, capability) {
capability = String(capability).toLowerCase();
if (!this.getComponent(id)) {
throw Error('Cannot remove capability, "' + capability + '". Plugin "' +
id + '" has not been added to the ComponentManager');
}
if (!this.hasCapability(id, capability)) {
console.warn('Plugin "' + id + 'doesn\'t have capability "' +
capability + '" to remove');
return;
}
this.componentData_[id].capabilities.splice(
this.componentData_[id].capabilities.indexOf(capability), 1);
this.capabilityToComponentIds_[capability].splice(
this.capabilityToComponentIds_[capability].indexOf(id), 1);
};

/**
* Returns whether the component with this id has the specified capability.
* @param {string} id The ID of the component to check.
* @param {string|!Blockly.ComponentManager.Capability<Blockly.IComponent>
* } capability The capability to check.
* @return {boolean} Whether the component has the capability
*/
Blockly.ComponentManager.prototype.hasCapability = function(id, capability) {
capability = String(capability).toLowerCase();
return this.componentData_[id].capabilities.indexOf(capability) !== -1;
};

/**
* Gets the component with the given ID and the given type.
* @param {string} id The ID of the component to get.
Expand Down

0 comments on commit 3c6a2e3

Please sign in to comment.