Merge pull request #128 from carlosperate/multiple_warnings

setWarningText can now take optional ID to maintain multiple warnings.
This commit is contained in:
Neil Fraser
2015-07-16 10:21:30 -07:00
2 changed files with 51 additions and 16 deletions

View File

@@ -1209,8 +1209,10 @@ Blockly.BlockSvg.prototype.setCommentText = function(text) {
/**
* Set this block's warning text.
* @param {?string} text The text, or null to delete.
* @param {string=} opt_id An optional ID for the warning text to be able to
* maintain multiple warnings.
*/
Blockly.BlockSvg.prototype.setWarningText = function(text) {
Blockly.BlockSvg.prototype.setWarningText = function(text, opt_id) {
if (this.setWarningText.pid_) {
// Only queue up the latest change. Kill any earlier pending process.
clearTimeout(this.setWarningText.pid_);
@@ -1222,7 +1224,7 @@ Blockly.BlockSvg.prototype.setWarningText = function(text) {
var thisBlock = this;
this.setWarningText.pid_ = setTimeout(function() {
thisBlock.setWarningText.pid_ = 0;
thisBlock.setWarningText(text);
thisBlock.setWarningText(text, opt_id);
}, 100);
return;
}
@@ -1235,11 +1237,15 @@ Blockly.BlockSvg.prototype.setWarningText = function(text) {
this.warning = new Blockly.Warning(this);
changedState = true;
}
this.warning.setText(/** @type {string} */ (text));
this.warning.setText(/** @type {string} */ (text), opt_id);
} else {
if (this.warning) {
// Dispose all warnings if no id is given
if (this.warning && opt_id === undefined) {
this.warning.dispose();
changedState = true;
} else if (this.warning) {
this.warning.removeText(opt_id);
changedState = true;
}
}
if (changedState && this.rendered) {

View File

@@ -39,6 +39,8 @@ goog.require('Blockly.Icon');
Blockly.Warning = function(block) {
Blockly.Warning.superClass_.constructor.call(this, block);
this.createIcon();
// The text_ object can contain multiple warnings
this.text_ = { default_: '' };
};
goog.inherits(Blockly.Warning, Blockly.Icon);
@@ -70,12 +72,6 @@ Blockly.Warning.textToDom_ = function(text) {
return paragraph;
};
/**
* Warning text (if bubble is not visible).
* @private
*/
Blockly.Warning.prototype.text_ = '';
/**
* Show or hide the warning bubble.
* @param {boolean} visible True if the bubble should be visible.
@@ -86,8 +82,12 @@ Blockly.Warning.prototype.setVisible = function(visible) {
return;
}
if (visible) {
// Create the bubble.
var paragraph = Blockly.Warning.textToDom_(this.text_);
// Create the bubble to display all warnings.
var allWarnings = [];
for (var id_ in this.text_) {
allWarnings.push(this.text_[id_]);
}
var paragraph = Blockly.Warning.textToDom_(allWarnings.join('\n'));
this.bubble_ = new Blockly.Bubble(
/** @type {!Blockly.Workspace} */ (this.block_.workspace),
paragraph, this.block_.svgPath_,
@@ -125,18 +125,47 @@ Blockly.Warning.prototype.bodyFocus_ = function(e) {
/**
* Set this warning's text.
* @param {string} text Warning text.
* @param {string=} opt_id An optional ID for this text entry to be able to
* maintain multiple warnings.
*/
Blockly.Warning.prototype.setText = function(text) {
if (this.text_ == text) {
return;
Blockly.Warning.prototype.setText = function(text, opt_id) {
if (opt_id !== undefined) {
if (this.text_[opt_id] == text) {
return;
}
this.text_[opt_id] = text;
} else {
if (this.text_.default_ == text) {
return;
}
this.text_.default_ = text;
}
this.text_ = text;
if (this.isVisible()) {
this.setVisible(false);
this.setVisible(true);
}
};
/**
* Removes the specified warning text.
* @param {string} textId ID of the warning to be removed.
*/
Blockly.Warning.prototype.removeText = function(textId) {
if (this.text_[textId] === undefined) {
return; // ID not found, no change.
}
delete this.text_[textId];
if (Object.keys(this.text_).length === 0 ||
(Object.keys(this.text_).length === 1 && !this.text_.default_)) {
this.dispose();
} else {
if (this.isVisible()) {
this.setVisible(false);
this.setVisible(true);
}
}
};
/**
* Dispose of this warning.
*/