From 63fe91180b700a6904f3b487fae949e266b71a0e Mon Sep 17 00:00:00 2001 From: Derek Brown Date: Fri, 19 May 2017 19:18:54 -0400 Subject: [PATCH] Add image_onclick option (#1080) --- core/field_image.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/core/field_image.js b/core/field_image.js index 09ee479ac..e5177e63e 100644 --- a/core/field_image.js +++ b/core/field_image.js @@ -38,10 +38,11 @@ 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 * @extends {Blockly.Field} * @constructor */ -Blockly.FieldImage = function(src, width, height, opt_alt) { +Blockly.FieldImage = function(src, width, height, opt_alt, opt_onclick) { this.sourceBlock_ = null; // Ensure height and width are numbers. Strings are bad at math. @@ -51,6 +52,11 @@ Blockly.FieldImage = function(src, width, height, opt_alt) { this.height_ + 2 * Blockly.BlockSvg.INLINE_PADDING_Y); this.text_ = opt_alt || ''; this.setValue(src); + + if(typeof opt_onclick === "function"){ + this.clickHandler_ = opt_onclick; + } + }; goog.inherits(Blockly.FieldImage, Blockly.Field); @@ -153,6 +159,7 @@ Blockly.FieldImage.prototype.setText = function(alt) { Blockly.FieldImage.prototype.render_ = function() { // NOP }; + /** * Images are fixed width, no need to update. * @private @@ -160,3 +167,14 @@ Blockly.FieldImage.prototype.render_ = function() { Blockly.FieldImage.prototype.updateWidth = function() { // NOP }; + +/** + * If field click is called, and click handler defined, + * call the handler. + * @private + */ + Blockly.FieldImage.prototype.showEditor = function(){ + if(typeof this.clickHandler_ === "function"){ + this.clickHandler_(this); + } + }