Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions core/bubble_dragger.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,9 @@ Blockly.BubbleDragger.prototype.shouldDelete_ = function(dragTarget) {
var couldDeleteBubble = this.draggingBubble_.isDeletable();

if (couldDeleteBubble && dragTarget) {
// TODO(#4881) use hasCapability instead of getComponents
var deleteAreas = this.workspace_.getComponentManager().getComponents(
Blockly.ComponentManager.Capability.DELETE_AREA, false);
var isDeleteArea = deleteAreas.some(function(deleteArea) {
return dragTarget === deleteArea;
});
var componentManager = this.workspace_.getComponentManager();
var isDeleteArea = componentManager.hasCapability(dragTarget.id,
Blockly.ComponentManager.Capability.DELETE_AREA);
if (isDeleteArea) {
return (/** @type {!Blockly.IDeleteArea} */ (dragTarget))
.wouldDeleteBubble(this.draggingBubble_);
Expand Down
41 changes: 23 additions & 18 deletions core/component_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ Blockly.ComponentManager = function() {
/**
* An object storing component information.
* @typedef {{
* id: string,
* component: !Blockly.IComponent,
* capabilities: (
* !Array<string|!Blockly.ComponentManager.Capability<!Blockly.IComponent>>),
* !Array<string|!Blockly.ComponentManager.Capability<!Blockly.IComponent>>
* ),
* weight: number
* }}
*/
Expand All @@ -58,26 +58,28 @@ Blockly.ComponentManager.ComponentDatum;
* the component to register.
* @param {boolean=} opt_allowOverrides True to prevent an error when overriding
* an already registered item.
* @template T
*/
Blockly.ComponentManager.prototype.addComponent = function(
componentInfo, opt_allowOverrides) {
// Don't throw an error if opt_allowOverrides is true.
if (!opt_allowOverrides && this.componentData_[componentInfo.id]) {
var id = componentInfo.component.id;
if (!opt_allowOverrides && this.componentData_[id]) {
throw Error(
'Plugin "' + componentInfo.id + '" with capabilities "' +
this.componentData_[componentInfo.id].capabilities +
'" already added.');
'Plugin "' + id + '" with capabilities "' +
this.componentData_[id].capabilities + '" already added.');
}
this.componentData_[componentInfo.id] = componentInfo;
this.componentData_[id] = componentInfo;
var stringCapabilities = [];
for (var i = 0; i < componentInfo.capabilities.length; i++) {
var capability = String(componentInfo.capabilities[i]).toLowerCase();
stringCapabilities.push(capability);
if (this.capabilityToComponentIds_[capability] === undefined) {
this.capabilityToComponentIds_[capability] = [componentInfo.id];
this.capabilityToComponentIds_[capability] = [id];
} else {
this.capabilityToComponentIds_[capability].push(componentInfo.id);
this.capabilityToComponentIds_[capability].push(id);
}
}
this.componentData_[id].capabilities = stringCapabilities;
};

/**
Expand All @@ -100,11 +102,11 @@ Blockly.ComponentManager.prototype.removeComponent = function(id) {
/**
* Adds a capability to a existing registered component.
* @param {string} id The ID of the component to add the capability to.
* @param {string|!Blockly.ComponentManager.Capability<!Blockly.IComponent>
* } capability The capability to add.
* @param {string|!Blockly.ComponentManager.Capability<T>} capability The
* capability to add.
* @template T
*/
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');
Expand All @@ -114,18 +116,19 @@ Blockly.ComponentManager.prototype.addCapability = function(id, capability) {
capability + '"');
return;
}
capability = String(capability).toLowerCase();
this.componentData_[id].capabilities.push(capability);
this.capabilityToComponentIds_[capability].push(id);
};

/**
* Removes a capability from an existing registered component.
* @param {string} id The ID of the component to remove the capability from.
* @param {string|!Blockly.ComponentManager.Capability<!Blockly.IComponent>
* } capability The capability to remove.
* @param {string|!Blockly.ComponentManager.Capability<T>} capability The
* capability to remove.
* @template T
*/
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');
Expand All @@ -135,6 +138,7 @@ Blockly.ComponentManager.prototype.removeCapability = function(id, capability) {
capability + '" to remove');
return;
}
capability = String(capability).toLowerCase();
this.componentData_[id].capabilities.splice(
this.componentData_[id].capabilities.indexOf(capability), 1);
this.capabilityToComponentIds_[capability].splice(
Expand All @@ -144,9 +148,10 @@ Blockly.ComponentManager.prototype.removeCapability = function(id, capability) {
/**
* 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 for.
* @param {string|!Blockly.ComponentManager.Capability<T>} capability The
* capability to check for.
* @return {boolean} Whether the component has the capability.
* @template T
*/
Blockly.ComponentManager.prototype.hasCapability = function(id, capability) {
capability = String(capability).toLowerCase();
Expand Down
3 changes: 1 addition & 2 deletions core/flyout_base.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Blockly.Flyout = function(workspaceOptions) {
* The unique id for this component.
* @type {string}
*/
this.id = 'flyout' + this.workspace_.id;
this.id = Blockly.utils.genUid();

/**
* Is RTL vs LTR.
Expand Down Expand Up @@ -311,7 +311,6 @@ Blockly.Flyout.prototype.init = function(targetWorkspace) {
this.workspace_.createPotentialVariableMap();

targetWorkspace.getComponentManager().addComponent({
id: this.id,
component: this,
weight: 1,
capabilities: [
Expand Down
9 changes: 3 additions & 6 deletions core/insertion_marker_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,12 +460,9 @@ Blockly.InsertionMarkerManager.prototype.shouldDelete_ = function(
!this.topBlock_.getParent() && this.topBlock_.isDeletable();

if (couldDeleteBlock && dragTarget) {
// TODO(#4881) use hasCapability instead of getComponents
var deleteAreas = this.workspace_.getComponentManager().getComponents(
Blockly.ComponentManager.Capability.DELETE_AREA, false);
var isDeleteArea = deleteAreas.some(function(deleteArea) {
return dragTarget === deleteArea;
});
var componentManager = this.workspace_.getComponentManager();
var isDeleteArea = componentManager.hasCapability(dragTarget.id,
Blockly.ComponentManager.Capability.DELETE_AREA);
if (isDeleteArea) {
return (
/** @type {!Blockly.IDeleteArea} */ (dragTarget))
Expand Down
5 changes: 5 additions & 0 deletions core/interfaces/i_component.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ goog.provide('Blockly.IComponent');
*/
Blockly.IComponent = function() {};

/**
* The unique id for this component.
* @type {string}
*/
Blockly.IComponent.id;
7 changes: 6 additions & 1 deletion core/toolbox/toolbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ Blockly.Toolbox = function(workspace) {
*/
this.workspace_ = workspace;

/**
* The unique id for this component.
* @type {string}
*/
this.id = Blockly.utils.genUid();

/**
* The JSON describing the contents of this toolbox.
* @type {!Blockly.utils.toolbox.ToolboxInfo}
Expand Down Expand Up @@ -193,7 +199,6 @@ Blockly.Toolbox.prototype.init = function() {
'background-color');
themeManager.subscribe(this.HtmlDiv, 'toolboxForegroundColour', 'color');
this.workspace_.getComponentManager().addComponent({
id: 'toolbox',
component: this,
weight: 1,
capabilities: [
Expand Down
8 changes: 7 additions & 1 deletion core/trashcan.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ goog.require('Blockly.IPositionable');
goog.require('Blockly.Options');
goog.require('Blockly.registry');
goog.require('Blockly.uiPosition');
goog.require('Blockly.utils');
goog.require('Blockly.utils.dom');
goog.require('Blockly.utils.Rect');
goog.require('Blockly.utils.Svg');
Expand Down Expand Up @@ -53,6 +54,12 @@ Blockly.Trashcan = function(workspace) {
*/
this.workspace_ = workspace;

/**
* The unique id for this component.
* @type {string}
*/
this.id = Blockly.utils.genUid();

/**
* A list of XML (stored as strings) representing blocks in the trashcan.
* @type {!Array<string>}
Expand Down Expand Up @@ -363,7 +370,6 @@ Blockly.Trashcan.prototype.init = function() {
this.flyout.init(this.workspace_);
}
this.workspace_.getComponentManager().addComponent({
id: 'trashcan',
component: this,
weight: 1,
capabilities: [
Expand Down
8 changes: 7 additions & 1 deletion core/zoom_controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ goog.require('Blockly.Events.Click');
goog.require('Blockly.IPositionable');
goog.require('Blockly.Touch');
goog.require('Blockly.uiPosition');
goog.require('Blockly.utils');
goog.require('Blockly.utils.dom');
goog.require('Blockly.utils.Rect');
goog.require('Blockly.utils.Svg');
Expand All @@ -43,6 +44,12 @@ Blockly.ZoomControls = function(workspace) {
*/
this.workspace_ = workspace;

/**
* The unique id for this component.
* @type {string}
*/
this.id = Blockly.utils.genUid();

/**
* A handle to use to unbind the mouse down event handler for zoom reset
* button. Opaque data returned from Blockly.bindEventWithChecks_.
Expand Down Expand Up @@ -191,7 +198,6 @@ Blockly.ZoomControls.prototype.createDom = function() {
*/
Blockly.ZoomControls.prototype.init = function() {
this.workspace_.getComponentManager().addComponent({
id: 'zoomControls',
component: this,
weight: 2,
capabilities: [Blockly.ComponentManager.Capability.POSITIONABLE]
Expand Down