diff --git a/core/field_image.js b/core/field_image.js index 4b4859da7..3960169a4 100644 --- a/core/field_image.js +++ b/core/field_image.js @@ -38,7 +38,8 @@ goog.require('goog.userAgent'); * @param {number} width Width of the image. * @param {number} height Height of the image. * @param {string=} opt_alt Optional alt text for when block is collapsed. - * @param {Function=} opt_onClick Optional function to be called when image is clicked + * @param {Function=} opt_onClick Optional function to be called when the image + * is clicked. If opt_onClick is defined, opt_alt must also be defined. * @extends {Blockly.Field} * @constructor */ @@ -92,6 +93,8 @@ Blockly.FieldImage.prototype.init = function() { // Configure the field to be transparent with respect to tooltips. this.setTooltip(this.sourceBlock_); Blockly.Tooltip.bindMouseEvents(this.imageElement_); + + this.maybeAddClickHandler_(); }; /** @@ -103,6 +106,19 @@ Blockly.FieldImage.prototype.dispose = function() { this.imageElement_ = null; }; +/** + * Bind events for a mouse down on the image, but only if a click handler has + * been defined. + * @private + */ +Blockly.FieldImage.prototype.maybeAddClickHandler_ = function() { + if (this.clickHandler_) { + this.mouseDownWrapper_ = + Blockly.bindEventWithChecks_(this.fieldGroup_, 'mousedown', this, + this.onMouseDown_); + } +}; + /** * Change the tooltip text for this field. * @param {string|!Element} newTip Text for tooltip or a parent element to @@ -171,8 +187,8 @@ Blockly.FieldImage.prototype.updateWidth = function() { * If field click is called, and click handler defined, * call the handler. */ - Blockly.FieldImage.prototype.showEditor = function() { - if (this.clickHandler_){ - this.clickHandler_(this); - } - }; +Blockly.FieldImage.prototype.showEditor_ = function() { + if (this.clickHandler_){ + this.clickHandler_(this); + } +};