Simplify Blockly.utils.IdGenerator

Also drop unused menu highlight hook.
This commit is contained in:
Neil Fraser
2019-09-19 18:33:06 -07:00
committed by Neil Fraser
parent be24605b27
commit 28953d8ea0
4 changed files with 6 additions and 68 deletions

View File

@@ -96,13 +96,6 @@ Blockly.Component = function() {
};
/**
* Generator for unique IDs.
* @type {!Blockly.utils.IdGenerator}
* @private
*/
Blockly.Component.prototype.idGenerator_ = Blockly.utils.IdGenerator.getInstance();
/**
* The default right to left value.
* @type {?boolean}
@@ -171,27 +164,7 @@ Blockly.Component.setDefaultRightToLeft = function(rightToLeft) {
* @package
*/
Blockly.Component.prototype.getId = function() {
return this.id_ || (this.id_ = this.idGenerator_.getNextUniqueId());
};
/**
* Assigns an ID to this component instance. It is the caller's responsibility
* to guarantee that the ID is unique. If the component is a child of a parent
* component, then the parent component's child index is updated to reflect the
* new ID; this may throw an error if the parent already has a child with an ID
* that conflicts with the new ID.
* @param {string} id Unique component ID.
* @protected
*/
Blockly.Component.prototype.setId = function(id) {
if (this.parent_ && this.parent_.childIndex_) {
// Update the parent's child index.
delete this.parent_.childIndex_[this.id_];
this.parent_.childIndex_[id] = this;
}
// Update the component ID.
this.id_ = id;
return this.id_ || (this.id_ = Blockly.utils.IdGenerator.getNextUniqueId());
};
/**

View File

@@ -267,14 +267,8 @@ Blockly.Menu.prototype.setHighlightedIndex = function(index) {
if (child) {
child.setHighlighted(true);
this.highlightedIndex_ = index;
if (this.highlightHandler_) {
this.highlightHandler_(child);
}
} else if (this.highlightedIndex_ > -1) {
this.getHighlighted().setHighlighted(false);
if (this.highlightHandler_) {
this.highlightHandler_();
}
this.highlightedIndex_ = -1;
}
@@ -359,15 +353,6 @@ Blockly.Menu.prototype.canHighlightItem = function(item) {
return item.isEnabled();
};
/**
* Set the handler that's triggered when a menuitem is highlighted.
* @param {function(?Blockly.MenuItem)} fn The handler.
* @package
*/
Blockly.Menu.prototype.onHighlighted = function(fn) {
this.highlightHandler_ = fn;
};
// Mouse events.
/**

View File

@@ -534,7 +534,6 @@ Blockly.FieldColour.prototype.dropdownCreate_ = function() {
Math.floor(colours.length / columns));
Blockly.utils.aria.setState(table, 'colcount', columns);
var row;
var idGenerator = Blockly.utils.IdGenerator.getInstance();
for (var i = 0; i < colours.length; i++) {
if (i % columns == 0) {
row = document.createElement('tr');
@@ -545,7 +544,7 @@ Blockly.FieldColour.prototype.dropdownCreate_ = function() {
row.appendChild(cell);
cell.label = colours[i]; // This becomes the value, if clicked.
cell.title = titles[i] || colours[i];
cell.id = idGenerator.getNextUniqueId();
cell.id = Blockly.utils.IdGenerator.getNextUniqueId();
cell.setAttribute('data-index', i);
Blockly.utils.aria.setRole(cell, Blockly.utils.aria.Role.GRIDCELL);
Blockly.utils.aria.setState(cell,

View File

@@ -29,38 +29,19 @@
goog.provide('Blockly.utils.IdGenerator');
/**
* Creates a new ID generator.
* @constructor
* @final
*/
Blockly.utils.IdGenerator = function() {};
/**
* Get the singleton instance of Blockly.utils.IdGenerator.
* @return {Blockly.utils.IdGenerator} singleton instance
*/
Blockly.utils.IdGenerator.getInstance = function() {
if (!Blockly.utils.IdGenerator.instance_) {
Blockly.utils.IdGenerator.instance_ = new Blockly.utils.IdGenerator();
}
return Blockly.utils.IdGenerator.instance_;
};
/**
* Next unique ID to use.
* @type {number}
* @private
*/
Blockly.utils.IdGenerator.prototype.nextId_ = 0;
Blockly.utils.IdGenerator.nextId_ = 0;
/**
* Gets the next unique ID.
* The difference between this and genUid is that getNextUniqueId generates
* IDs compatible with the HTML4 id attribute restrictions:
* IDs are compatible with the HTML4 id attribute restrictions:
* Use only ASCII letters, digits, '_', '-' and '.'
* @return {string} The next unique identifier.
*/
Blockly.utils.IdGenerator.prototype.getNextUniqueId = function() {
return 'blockly:' + (this.nextId_++).toString(36);
Blockly.utils.IdGenerator.getNextUniqueId = function() {
return 'blockly:' + (Blockly.utils.IdGenerator.nextId_++).toString(36);
};