2013-10-30 14:46:03 -07:00
|
|
|
/**
|
2014-01-28 03:00:09 -08:00
|
|
|
* @license
|
2013-10-30 14:46:03 -07:00
|
|
|
* Visual Blocks Editor
|
|
|
|
*
|
|
|
|
* Copyright 2012 Google Inc.
|
2014-10-07 13:09:55 -07:00
|
|
|
* https://developers.google.com/blockly/
|
2013-10-30 14:46:03 -07:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @fileoverview Image field. Used for titles, labels, etc.
|
|
|
|
* @author fraser@google.com (Neil Fraser)
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
goog.provide('Blockly.FieldImage');
|
|
|
|
|
|
|
|
goog.require('Blockly.Field');
|
2015-02-06 15:27:25 -08:00
|
|
|
goog.require('goog.dom');
|
2015-08-19 17:21:05 -07:00
|
|
|
goog.require('goog.math.Size');
|
2013-10-30 14:46:03 -07:00
|
|
|
goog.require('goog.userAgent');
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class for an image.
|
|
|
|
* @param {string} src The URL of the image.
|
|
|
|
* @param {number} width Width of the image.
|
|
|
|
* @param {number} height Height of the image.
|
2015-07-13 15:03:22 -07:00
|
|
|
* @param {string=} opt_alt Optional alt text for when block is collapsed.
|
2016-03-03 17:32:22 -05:00
|
|
|
* @param {boolean} flip_rtl Whether to flip the icon in RTL
|
2013-10-30 14:46:03 -07:00
|
|
|
* @extends {Blockly.Field}
|
|
|
|
* @constructor
|
|
|
|
*/
|
2016-03-03 17:32:22 -05:00
|
|
|
Blockly.FieldImage = function(src, width, height, opt_alt, flip_rtl) {
|
2013-10-30 14:46:03 -07:00
|
|
|
this.sourceBlock_ = null;
|
|
|
|
// Ensure height and width are numbers. Strings are bad at math.
|
2013-12-14 03:00:02 -08:00
|
|
|
this.height_ = Number(height);
|
|
|
|
this.width_ = Number(width);
|
2016-01-28 16:58:42 -05:00
|
|
|
this.size_ = new goog.math.Size(this.width_, this.height_);
|
2014-01-13 03:00:02 -08:00
|
|
|
this.text_ = opt_alt || '';
|
2016-03-03 17:32:22 -05:00
|
|
|
this.flipRTL_ = flip_rtl;
|
2014-01-13 03:00:02 -08:00
|
|
|
this.setValue(src);
|
2013-10-30 14:46:03 -07:00
|
|
|
};
|
|
|
|
goog.inherits(Blockly.FieldImage, Blockly.Field);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rectangular mask used by Firefox.
|
|
|
|
* @type {Element}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Blockly.FieldImage.prototype.rectElement_ = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Editable fields are saved by the XML renderer, non-editable fields are not.
|
|
|
|
*/
|
|
|
|
Blockly.FieldImage.prototype.EDITABLE = false;
|
|
|
|
|
|
|
|
/**
|
2015-01-06 15:23:45 -08:00
|
|
|
* Install this image on a block.
|
2013-10-30 14:46:03 -07:00
|
|
|
*/
|
2016-04-04 17:47:15 -07:00
|
|
|
Blockly.FieldImage.prototype.init = function() {
|
|
|
|
if (this.fieldGroup_) {
|
2014-12-23 11:22:02 -08:00
|
|
|
// Image has already been initialized once.
|
|
|
|
return;
|
2013-10-30 14:46:03 -07:00
|
|
|
}
|
2015-01-06 15:23:45 -08:00
|
|
|
// Build the DOM.
|
2015-12-01 14:04:54 -06:00
|
|
|
/** @type {SVGElement} */
|
2015-01-06 15:23:45 -08:00
|
|
|
this.fieldGroup_ = Blockly.createSvgElement('g', {}, null);
|
2015-10-09 15:32:21 -07:00
|
|
|
if (!this.visible_) {
|
|
|
|
this.fieldGroup_.style.display = 'none';
|
|
|
|
}
|
2015-12-01 14:04:54 -06:00
|
|
|
/** @type {SVGElement} */
|
2015-01-06 15:23:45 -08:00
|
|
|
this.imageElement_ = Blockly.createSvgElement('image',
|
|
|
|
{'height': this.height_ + 'px',
|
2015-09-07 18:52:07 -07:00
|
|
|
'width': this.width_ + 'px'}, this.fieldGroup_);
|
2015-01-06 15:23:45 -08:00
|
|
|
this.setValue(this.src_);
|
|
|
|
if (goog.userAgent.GECKO) {
|
2015-12-01 14:04:54 -06:00
|
|
|
/**
|
|
|
|
* Due to a Firefox bug which eats mouse events on image elements,
|
|
|
|
* a transparent rectangle needs to be placed on top of the image.
|
|
|
|
* @type {SVGElement}
|
|
|
|
*/
|
2015-01-06 15:23:45 -08:00
|
|
|
this.rectElement_ = Blockly.createSvgElement('rect',
|
|
|
|
{'height': this.height_ + 'px',
|
|
|
|
'width': this.width_ + 'px',
|
|
|
|
'fill-opacity': 0}, this.fieldGroup_);
|
|
|
|
}
|
2016-04-04 17:47:15 -07:00
|
|
|
this.sourceBlock_.getSvgRoot().appendChild(this.fieldGroup_);
|
2013-10-30 14:46:03 -07:00
|
|
|
|
|
|
|
// Configure the field to be transparent with respect to tooltips.
|
|
|
|
var topElement = this.rectElement_ || this.imageElement_;
|
|
|
|
topElement.tooltip = this.sourceBlock_;
|
2014-09-08 14:26:52 -07:00
|
|
|
Blockly.Tooltip.bindMouseEvents(topElement);
|
2013-10-30 14:46:03 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispose of all DOM objects belonging to this text.
|
|
|
|
*/
|
|
|
|
Blockly.FieldImage.prototype.dispose = function() {
|
|
|
|
goog.dom.removeNode(this.fieldGroup_);
|
|
|
|
this.fieldGroup_ = null;
|
|
|
|
this.imageElement_ = null;
|
|
|
|
this.rectElement_ = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change the tooltip text for this field.
|
|
|
|
* @param {string|!Element} newTip Text for tooltip or a parent element to
|
|
|
|
* link to for its tooltip.
|
|
|
|
*/
|
|
|
|
Blockly.FieldImage.prototype.setTooltip = function(newTip) {
|
|
|
|
var topElement = this.rectElement_ || this.imageElement_;
|
|
|
|
topElement.tooltip = newTip;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the source URL of this image.
|
|
|
|
* @return {string} Current text.
|
|
|
|
* @override
|
|
|
|
*/
|
2014-01-13 03:00:02 -08:00
|
|
|
Blockly.FieldImage.prototype.getValue = function() {
|
2013-10-30 14:46:03 -07:00
|
|
|
return this.src_;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the source URL of this image.
|
|
|
|
* @param {?string} src New source.
|
|
|
|
* @override
|
|
|
|
*/
|
2014-01-13 03:00:02 -08:00
|
|
|
Blockly.FieldImage.prototype.setValue = function(src) {
|
2013-10-30 14:46:03 -07:00
|
|
|
if (src === null) {
|
|
|
|
// No change if null.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.src_ = src;
|
2015-01-06 15:23:45 -08:00
|
|
|
if (this.imageElement_) {
|
|
|
|
this.imageElement_.setAttributeNS('http://www.w3.org/1999/xlink',
|
|
|
|
'xlink:href', goog.isString(src) ? src : '');
|
|
|
|
}
|
2013-10-30 14:46:03 -07:00
|
|
|
};
|
2014-01-13 03:00:02 -08:00
|
|
|
|
2016-03-03 17:32:22 -05:00
|
|
|
/**
|
|
|
|
* Get whether to flip this image in RTL
|
|
|
|
* @return {boolean} True if we should flip in RTL.
|
|
|
|
*/
|
|
|
|
Blockly.FieldImage.prototype.getFlipRTL = function() {
|
|
|
|
return this.flipRTL_;
|
|
|
|
};
|
|
|
|
|
2014-01-13 03:00:02 -08:00
|
|
|
/**
|
|
|
|
* Set the alt text of this image.
|
|
|
|
* @param {?string} alt New alt text.
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
Blockly.FieldImage.prototype.setText = function(alt) {
|
|
|
|
if (alt === null) {
|
|
|
|
// No change if null.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.text_ = alt;
|
|
|
|
};
|
2014-12-23 11:22:02 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Images are fixed width, no need to render.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Blockly.FieldImage.prototype.render_ = function() {
|
|
|
|
// NOP
|
|
|
|
};
|