From e829f30f1ab2b2aa5ca1405e816e68c94003c7eb Mon Sep 17 00:00:00 2001 From: carlosperate Date: Tue, 14 Jul 2015 22:22:13 +0100 Subject: [PATCH] setWarningText can now take optional ID to maintain multiple warnings. --- core/block_svg.js | 14 +++++++++---- core/warning.js | 53 ++++++++++++++++++++++++++++++++++++----------- 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/core/block_svg.js b/core/block_svg.js index 38adfd47f..210b40fd3 100644 --- a/core/block_svg.js +++ b/core/block_svg.js @@ -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) { diff --git a/core/warning.js b/core/warning.js index 79f176cdb..ccbc51952 100644 --- a/core/warning.js +++ b/core/warning.js @@ -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. */