Add image_onclick option (#1080)

This commit is contained in:
Derek Brown
2017-05-19 19:18:54 -04:00
committed by Rachel Fenichel
parent d4694b49d3
commit 63fe91180b

View File

@@ -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);
}
}