Change checkboxes_ from array to object map

This commit is contained in:
adroitwhiz 2020-05-22 22:58:37 -04:00
parent a355ca0b63
commit 8fec74c1a1

View file

@ -59,13 +59,13 @@ Blockly.VerticalFlyout = function(workspaceOptions) {
this.horizontalLayout_ = false;
/**
* List of checkboxes next to variable blocks.
* Map of checkboxes that correspond to monitored blocks.
* Each element is an object containing the SVG for the checkbox, a boolean
* for its checked state, and the block the checkbox is associated with.
* @type {!Array.<!Object>}
* @type {!Object.<string, !Object>}
* @private
*/
this.checkboxes_ = [];
this.checkboxes_ = {};
};
goog.inherits(Blockly.VerticalFlyout, Blockly.Flyout);
@ -458,11 +458,15 @@ Blockly.VerticalFlyout.prototype.clearOldBlocks_ = function() {
Blockly.VerticalFlyout.superClass_.clearOldBlocks_.call(this);
// Do the same for checkboxes.
for (var i = 0, elem; elem = this.checkboxes_[i]; i++) {
elem.block.flyoutCheckbox = null;
goog.dom.removeNode(elem.svgRoot);
for (var checkboxId in this.checkboxes_) {
if (!Object.prototype.hasOwnProperty.call(this.checkboxes_, checkboxId)) {
continue;
}
var checkbox = this.checkboxes_[checkboxId];
checkbox.block.flyoutCheckbox = null;
goog.dom.removeNode(checkbox.svgRoot);
}
this.checkboxes_ = [];
this.checkboxes_ = {};
};
/**
@ -638,7 +642,7 @@ Blockly.VerticalFlyout.prototype.createCheckbox_ = function(block, cursorX,
block.flyoutCheckbox = checkboxObj;
this.workspace_.getCanvas().insertBefore(checkboxGroup, svgRoot);
this.checkboxes_.push(checkboxObj);
this.checkboxes_[block.id] = checkboxObj;
};
/**
@ -665,26 +669,22 @@ Blockly.VerticalFlyout.prototype.checkboxClicked_ = function(checkboxObj) {
* @public
*/
Blockly.VerticalFlyout.prototype.setCheckboxState = function(blockId, value) {
for (var i = 0; i < this.checkboxes_.length; i++) {
var checkboxObj = this.checkboxes_[i];
if (checkboxObj.block.id === blockId) {
if (checkboxObj.clicked === value) return;
var oldValue = checkboxObj.clicked;
checkboxObj.clicked = value;
if (checkboxObj.clicked) {
Blockly.utils.addClass((checkboxObj.svgRoot), 'checked');
} else {
Blockly.utils.removeClass((checkboxObj.svgRoot), 'checked');
}
Blockly.Events.fire(new Blockly.Events.Change(
checkboxObj.block, 'checkbox', null, oldValue, value));
return;
}
var checkboxObj = this.checkboxes_[blockId];
if (!checkboxObj || checkboxObj.clicked === value) {
return;
}
var oldValue = checkboxObj.clicked;
checkboxObj.clicked = value;
if (checkboxObj.clicked) {
Blockly.utils.addClass(checkboxObj.svgRoot, 'checked');
} else {
Blockly.utils.removeClass(checkboxObj.svgRoot, 'checked');
}
Blockly.Events.fire(new Blockly.Events.Change(
checkboxObj.block, 'checkbox', null, oldValue, value));
};
/**