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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2016-01-15 15:36:06 -08:00
|
|
|
* @fileoverview Field. Used for editable titles, variables, etc.
|
2013-10-30 14:46:03 -07:00
|
|
|
* This is an abstract class that defines the UI on the block. Actual
|
|
|
|
* instances would be Blockly.FieldTextInput, Blockly.FieldDropdown, etc.
|
|
|
|
* @author fraser@google.com (Neil Fraser)
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
goog.provide('Blockly.Field');
|
|
|
|
|
2013-12-14 03:00:02 -08:00
|
|
|
goog.require('goog.asserts');
|
2015-02-06 15:27:25 -08:00
|
|
|
goog.require('goog.dom');
|
2014-12-24 00:22:01 -08:00
|
|
|
goog.require('goog.math.Size');
|
2015-04-28 13:51:25 -07:00
|
|
|
goog.require('goog.style');
|
2014-09-08 14:26:52 -07:00
|
|
|
goog.require('goog.userAgent');
|
2013-10-30 14:46:03 -07:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2016-01-15 15:36:06 -08:00
|
|
|
* Abstract class for an editable field.
|
2013-10-30 14:46:03 -07:00
|
|
|
* @param {string} text The initial content of the field.
|
2016-02-10 14:33:13 -08:00
|
|
|
* @param {Function=} opt_validator An optional function that is called
|
|
|
|
* to validate any constraints on what the user entered. Takes the new
|
|
|
|
* text as an argument and returns either the accepted text, a replacement
|
|
|
|
* text, or null to abort the change.
|
2013-10-30 14:46:03 -07:00
|
|
|
* @constructor
|
|
|
|
*/
|
2016-02-10 14:33:13 -08:00
|
|
|
Blockly.Field = function(text, opt_validator) {
|
2014-12-24 00:22:01 -08:00
|
|
|
this.size_ = new goog.math.Size(0, 25);
|
2016-01-20 19:11:03 -08:00
|
|
|
this.setValue(text);
|
2016-02-10 14:33:13 -08:00
|
|
|
this.setValidator(opt_validator);
|
2013-10-30 14:46:03 -07:00
|
|
|
};
|
|
|
|
|
2015-09-15 16:20:13 -07:00
|
|
|
/**
|
|
|
|
* Temporary cache of text widths.
|
|
|
|
* @type {Object}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Blockly.Field.cacheWidths_ = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of current references to cache.
|
|
|
|
* @type {number}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Blockly.Field.cacheReference_ = 0;
|
|
|
|
|
2016-01-15 15:36:06 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of field. Unique within each block.
|
|
|
|
* Static labels are usually unnamed.
|
|
|
|
* @type {string=}
|
|
|
|
*/
|
|
|
|
Blockly.Field.prototype.name = undefined;
|
|
|
|
|
2015-07-21 18:24:07 -07:00
|
|
|
/**
|
2015-09-07 18:52:07 -07:00
|
|
|
* Maximum characters of text to display before adding an ellipsis.
|
2016-02-10 14:33:13 -08:00
|
|
|
* @type {number}
|
2015-07-21 18:24:07 -07:00
|
|
|
*/
|
|
|
|
Blockly.Field.prototype.maxDisplayLength = 50;
|
|
|
|
|
2016-01-15 15:36:06 -08:00
|
|
|
/**
|
|
|
|
* Visible text to display.
|
2016-02-10 14:33:13 -08:00
|
|
|
* @type {string}
|
2016-01-15 15:36:06 -08:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Blockly.Field.prototype.text_ = '';
|
|
|
|
|
2014-12-24 00:22:01 -08:00
|
|
|
/**
|
|
|
|
* Block this field is attached to. Starts as null, then in set in init.
|
2016-02-10 14:33:13 -08:00
|
|
|
* @type {Blockly.Block}
|
2014-12-24 00:22:01 -08:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Blockly.Field.prototype.sourceBlock_ = null;
|
|
|
|
|
2014-12-23 11:22:02 -08:00
|
|
|
/**
|
|
|
|
* Is the field visible, or hidden due to the block being collapsed?
|
2016-02-10 14:33:13 -08:00
|
|
|
* @type {boolean}
|
2014-12-23 11:22:02 -08:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Blockly.Field.prototype.visible_ = true;
|
|
|
|
|
2015-06-08 15:54:18 -07:00
|
|
|
/**
|
2016-02-10 14:33:13 -08:00
|
|
|
* Validation function called when user edits an editable field.
|
|
|
|
* @type {Function}
|
2015-06-08 15:54:18 -07:00
|
|
|
* @private
|
|
|
|
*/
|
2016-02-10 14:33:13 -08:00
|
|
|
Blockly.Field.prototype.validator_ = null;
|
2015-06-08 15:54:18 -07:00
|
|
|
|
2013-10-30 14:46:03 -07:00
|
|
|
/**
|
|
|
|
* Non-breaking space.
|
2016-01-15 15:36:06 -08:00
|
|
|
* @const
|
2013-10-30 14:46:03 -07:00
|
|
|
*/
|
|
|
|
Blockly.Field.NBSP = '\u00A0';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Editable fields are saved by the XML renderer, non-editable fields are not.
|
|
|
|
*/
|
|
|
|
Blockly.Field.prototype.EDITABLE = true;
|
|
|
|
|
|
|
|
/**
|
2016-04-04 17:47:15 -07:00
|
|
|
* Attach this field to a block.
|
2013-10-30 14:46:03 -07:00
|
|
|
* @param {!Blockly.Block} block The block containing this field.
|
|
|
|
*/
|
2016-04-04 17:47:15 -07:00
|
|
|
Blockly.Field.prototype.setSourceBlock = function(block) {
|
|
|
|
goog.asserts.assert(!this.sourceBlock_, 'Field already bound to a block.');
|
|
|
|
this.sourceBlock_ = block;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Install this field on a block.
|
|
|
|
*/
|
|
|
|
Blockly.Field.prototype.init = function() {
|
|
|
|
if (this.fieldGroup_) {
|
2014-12-23 11:22:02 -08:00
|
|
|
// Field has already been initialized once.
|
|
|
|
return;
|
2013-10-30 14:46:03 -07:00
|
|
|
}
|
2014-12-24 00:22:01 -08:00
|
|
|
// Build the DOM.
|
|
|
|
this.fieldGroup_ = Blockly.createSvgElement('g', {}, null);
|
2015-01-05 12:04:01 -08:00
|
|
|
if (!this.visible_) {
|
|
|
|
this.fieldGroup_.style.display = 'none';
|
|
|
|
}
|
2014-12-24 00:22:01 -08:00
|
|
|
this.borderRect_ = Blockly.createSvgElement('rect',
|
|
|
|
{'rx': 4,
|
|
|
|
'ry': 4,
|
|
|
|
'x': -Blockly.BlockSvg.SEP_SPACE_X / 2,
|
2015-09-07 18:52:07 -07:00
|
|
|
'y': 0,
|
2015-08-19 17:21:05 -07:00
|
|
|
'height': 16}, this.fieldGroup_, this.sourceBlock_.workspace);
|
2015-09-22 17:28:51 -07:00
|
|
|
/** @type {!Element} */
|
2014-12-24 00:22:01 -08:00
|
|
|
this.textElement_ = Blockly.createSvgElement('text',
|
2015-09-22 17:28:51 -07:00
|
|
|
{'class': 'blocklyText', 'y': this.size_.height - 12.5},
|
|
|
|
this.fieldGroup_);
|
2014-12-24 00:22:01 -08:00
|
|
|
|
2013-10-30 14:46:03 -07:00
|
|
|
this.updateEditable();
|
2016-04-04 17:47:15 -07:00
|
|
|
this.sourceBlock_.getSvgRoot().appendChild(this.fieldGroup_);
|
2013-10-30 14:46:03 -07:00
|
|
|
this.mouseUpWrapper_ =
|
|
|
|
Blockly.bindEvent_(this.fieldGroup_, 'mouseup', this, this.onMouseUp_);
|
2014-12-24 00:22:01 -08:00
|
|
|
// Force a render.
|
|
|
|
this.updateTextNode_();
|
2013-10-30 14:46:03 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispose of all DOM objects belonging to this editable field.
|
|
|
|
*/
|
|
|
|
Blockly.Field.prototype.dispose = function() {
|
|
|
|
if (this.mouseUpWrapper_) {
|
|
|
|
Blockly.unbindEvent_(this.mouseUpWrapper_);
|
|
|
|
this.mouseUpWrapper_ = null;
|
|
|
|
}
|
|
|
|
this.sourceBlock_ = null;
|
|
|
|
goog.dom.removeNode(this.fieldGroup_);
|
|
|
|
this.fieldGroup_ = null;
|
|
|
|
this.textElement_ = null;
|
|
|
|
this.borderRect_ = null;
|
2016-02-10 14:33:13 -08:00
|
|
|
this.validator_ = null;
|
2013-10-30 14:46:03 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add or remove the UI indicating if this field is editable or not.
|
|
|
|
*/
|
|
|
|
Blockly.Field.prototype.updateEditable = function() {
|
2016-07-26 17:23:57 -07:00
|
|
|
var group = this.fieldGroup_;
|
|
|
|
if (!this.EDITABLE || !group) {
|
2013-10-30 14:46:03 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.sourceBlock_.isEditable()) {
|
2016-07-26 17:23:57 -07:00
|
|
|
Blockly.addClass_(group, 'blocklyEditableText');
|
|
|
|
Blockly.removeClass_(group, 'blocklyNonEditableText');
|
2013-10-30 14:46:03 -07:00
|
|
|
this.fieldGroup_.style.cursor = this.CURSOR;
|
|
|
|
} else {
|
2016-07-26 17:23:57 -07:00
|
|
|
Blockly.addClass_(group, 'blocklyNonEditableText');
|
|
|
|
Blockly.removeClass_(group, 'blocklyEditableText');
|
2013-10-30 14:46:03 -07:00
|
|
|
this.fieldGroup_.style.cursor = '';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets whether this editable field is visible or not.
|
|
|
|
* @return {boolean} True if visible.
|
|
|
|
*/
|
|
|
|
Blockly.Field.prototype.isVisible = function() {
|
|
|
|
return this.visible_;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets whether this editable field is visible or not.
|
|
|
|
* @param {boolean} visible True if visible.
|
|
|
|
*/
|
|
|
|
Blockly.Field.prototype.setVisible = function(visible) {
|
2014-12-23 11:22:02 -08:00
|
|
|
if (this.visible_ == visible) {
|
|
|
|
return;
|
|
|
|
}
|
2013-10-30 14:46:03 -07:00
|
|
|
this.visible_ = visible;
|
2014-12-24 00:22:01 -08:00
|
|
|
var root = this.getSvgRoot();
|
|
|
|
if (root) {
|
|
|
|
root.style.display = visible ? 'block' : 'none';
|
|
|
|
this.render_();
|
|
|
|
}
|
2013-10-30 14:46:03 -07:00
|
|
|
};
|
|
|
|
|
2015-06-08 15:54:18 -07:00
|
|
|
/**
|
2016-02-10 14:33:13 -08:00
|
|
|
* Sets a new validation function for editable fields.
|
|
|
|
* @param {Function} handler New validation function, or null.
|
2015-06-08 15:54:18 -07:00
|
|
|
*/
|
2016-02-10 14:33:13 -08:00
|
|
|
Blockly.Field.prototype.setValidator = function(handler) {
|
|
|
|
this.validator_ = handler;
|
2015-06-08 15:54:18 -07:00
|
|
|
};
|
|
|
|
|
2016-06-14 18:42:49 -07:00
|
|
|
/**
|
|
|
|
* Gets the validation function for editable fields.
|
|
|
|
* @return {Function} Validation function, or null.
|
|
|
|
*/
|
|
|
|
Blockly.Field.prototype.getValidator = function() {
|
|
|
|
return this.validator_;
|
|
|
|
};
|
|
|
|
|
2016-07-15 20:59:20 -07:00
|
|
|
/**
|
|
|
|
* Validates a change. Does nothing. Subclasses may override this.
|
|
|
|
* @param {string} text The user's text.
|
|
|
|
* @return {string} No change needed.
|
|
|
|
*/
|
|
|
|
Blockly.Field.prototype.classValidator = function(text) {
|
|
|
|
return text;
|
|
|
|
};
|
|
|
|
|
2016-06-29 17:44:12 -07:00
|
|
|
/**
|
|
|
|
* Calls the validation function for this field, as well as all the validation
|
|
|
|
* function for the field's class and its parents.
|
|
|
|
* @param {string} text Proposed text.
|
|
|
|
* @return {?string} Revised text, or null if invalid.
|
|
|
|
*/
|
|
|
|
Blockly.Field.prototype.callValidator = function(text) {
|
2016-07-15 20:59:20 -07:00
|
|
|
var classResult = this.classValidator(text);
|
|
|
|
if (classResult === null) {
|
|
|
|
// Class validator rejects value. Game over.
|
|
|
|
return null;
|
|
|
|
} else if (classResult !== undefined) {
|
|
|
|
text = classResult;
|
2016-06-29 17:44:12 -07:00
|
|
|
}
|
2016-07-15 20:59:20 -07:00
|
|
|
var userValidator = this.getValidator();
|
|
|
|
if (userValidator) {
|
2016-07-18 00:54:34 -07:00
|
|
|
var userResult = userValidator.call(this, text);
|
2016-07-15 20:59:20 -07:00
|
|
|
if (userResult === null) {
|
|
|
|
// User validator rejects value. Game over.
|
|
|
|
return null;
|
|
|
|
} else if (userResult !== undefined) {
|
|
|
|
text = userResult;
|
2016-06-29 17:44:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
};
|
|
|
|
|
2013-10-30 14:46:03 -07:00
|
|
|
/**
|
|
|
|
* Gets the group element for this editable field.
|
|
|
|
* Used for measuring the size and for positioning.
|
|
|
|
* @return {!Element} The group element.
|
|
|
|
*/
|
2014-12-23 11:22:02 -08:00
|
|
|
Blockly.Field.prototype.getSvgRoot = function() {
|
2013-10-30 14:46:03 -07:00
|
|
|
return /** @type {!Element} */ (this.fieldGroup_);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draws the border with the correct width.
|
|
|
|
* Saves the computed width in a property.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Blockly.Field.prototype.render_ = function() {
|
2015-01-01 14:30:37 -08:00
|
|
|
if (this.visible_ && this.textElement_) {
|
2015-09-15 16:20:13 -07:00
|
|
|
var key = this.textElement_.textContent + '\n' +
|
|
|
|
this.textElement_.className.baseVal;
|
|
|
|
if (Blockly.Field.cacheWidths_ && Blockly.Field.cacheWidths_[key]) {
|
|
|
|
var width = Blockly.Field.cacheWidths_[key];
|
|
|
|
} else {
|
|
|
|
try {
|
2016-05-10 14:36:51 -07:00
|
|
|
var width = this.textElement_.getComputedTextLength();
|
2015-09-15 16:20:13 -07:00
|
|
|
} catch (e) {
|
|
|
|
// MSIE 11 is known to throw "Unexpected call to method or property
|
|
|
|
// access." if Blockly is hidden.
|
2016-05-10 14:36:51 -07:00
|
|
|
var width = this.textElement_.textContent.length * 8;
|
2015-09-15 16:20:13 -07:00
|
|
|
}
|
|
|
|
if (Blockly.Field.cacheWidths_) {
|
|
|
|
Blockly.Field.cacheWidths_[key] = width;
|
|
|
|
}
|
2014-12-23 11:22:02 -08:00
|
|
|
}
|
|
|
|
if (this.borderRect_) {
|
|
|
|
this.borderRect_.setAttribute('width',
|
|
|
|
width + Blockly.BlockSvg.SEP_SPACE_X);
|
|
|
|
}
|
|
|
|
} else {
|
2016-05-10 14:36:51 -07:00
|
|
|
var width = 0;
|
2013-10-30 14:46:03 -07:00
|
|
|
}
|
|
|
|
this.size_.width = width;
|
|
|
|
};
|
|
|
|
|
2015-09-15 16:20:13 -07:00
|
|
|
/**
|
|
|
|
* Start caching field widths. Every call to this function MUST also call
|
|
|
|
* stopCache. Caches must not survive between execution threads.
|
|
|
|
*/
|
|
|
|
Blockly.Field.startCache = function() {
|
|
|
|
Blockly.Field.cacheReference_++;
|
|
|
|
if (!Blockly.Field.cacheWidths_) {
|
|
|
|
Blockly.Field.cacheWidths_ = {};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop caching field widths. Unless caching was already on when the
|
|
|
|
* corresponding call to startCache was made.
|
|
|
|
*/
|
|
|
|
Blockly.Field.stopCache = function() {
|
|
|
|
Blockly.Field.cacheReference_--;
|
|
|
|
if (!Blockly.Field.cacheReference_) {
|
|
|
|
Blockly.Field.cacheWidths_ = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-30 14:46:03 -07:00
|
|
|
/**
|
2013-12-20 16:25:26 -08:00
|
|
|
* Returns the height and width of the field.
|
2014-12-24 00:22:01 -08:00
|
|
|
* @return {!goog.math.Size} Height and width.
|
2013-10-30 14:46:03 -07:00
|
|
|
*/
|
|
|
|
Blockly.Field.prototype.getSize = function() {
|
|
|
|
if (!this.size_.width) {
|
|
|
|
this.render_();
|
|
|
|
}
|
|
|
|
return this.size_;
|
|
|
|
};
|
|
|
|
|
2015-08-19 17:21:05 -07:00
|
|
|
/**
|
|
|
|
* Returns the height and width of the field,
|
|
|
|
* accounting for the workspace scaling.
|
2015-08-21 14:13:07 -07:00
|
|
|
* @return {!goog.math.Size} Height and width.
|
2016-03-18 15:19:26 -07:00
|
|
|
* @private
|
2015-08-19 17:21:05 -07:00
|
|
|
*/
|
|
|
|
Blockly.Field.prototype.getScaledBBox_ = function() {
|
|
|
|
var bBox = this.borderRect_.getBBox();
|
2015-08-21 14:13:07 -07:00
|
|
|
// Create new object, as getBBox can return an uneditable SVGRect in IE.
|
|
|
|
return new goog.math.Size(bBox.width * this.sourceBlock_.workspace.scale,
|
|
|
|
bBox.height * this.sourceBlock_.workspace.scale);
|
2015-08-19 17:21:05 -07:00
|
|
|
};
|
|
|
|
|
2013-10-30 14:46:03 -07:00
|
|
|
/**
|
|
|
|
* Get the text from this field.
|
|
|
|
* @return {string} Current text.
|
|
|
|
*/
|
|
|
|
Blockly.Field.prototype.getText = function() {
|
|
|
|
return this.text_;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the text in this field. Trigger a rerender of the source block.
|
2015-06-08 15:54:18 -07:00
|
|
|
* @param {*} text New text.
|
2013-10-30 14:46:03 -07:00
|
|
|
*/
|
|
|
|
Blockly.Field.prototype.setText = function(text) {
|
2015-06-08 15:54:18 -07:00
|
|
|
if (text === null) {
|
2013-10-30 14:46:03 -07:00
|
|
|
// No change if null.
|
|
|
|
return;
|
|
|
|
}
|
2015-06-08 15:54:18 -07:00
|
|
|
text = String(text);
|
|
|
|
if (text === this.text_) {
|
|
|
|
// No change.
|
|
|
|
return;
|
|
|
|
}
|
2013-10-30 14:46:03 -07:00
|
|
|
this.text_ = text;
|
2014-09-08 14:26:52 -07:00
|
|
|
this.updateTextNode_();
|
|
|
|
|
|
|
|
if (this.sourceBlock_ && this.sourceBlock_.rendered) {
|
|
|
|
this.sourceBlock_.render();
|
|
|
|
this.sourceBlock_.bumpNeighbours_();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the text node of this field to display the current text.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Blockly.Field.prototype.updateTextNode_ = function() {
|
2014-12-24 00:22:01 -08:00
|
|
|
if (!this.textElement_) {
|
|
|
|
// Not rendered yet.
|
|
|
|
return;
|
|
|
|
}
|
2014-09-08 14:26:52 -07:00
|
|
|
var text = this.text_;
|
2015-07-21 18:24:07 -07:00
|
|
|
if (text.length > this.maxDisplayLength) {
|
|
|
|
// Truncate displayed string and add an ellipsis ('...').
|
|
|
|
text = text.substring(0, this.maxDisplayLength - 2) + '\u2026';
|
|
|
|
}
|
2013-10-30 14:46:03 -07:00
|
|
|
// Empty the text element.
|
|
|
|
goog.dom.removeChildren(/** @type {!Element} */ (this.textElement_));
|
|
|
|
// Replace whitespace with non-breaking spaces so the text doesn't collapse.
|
|
|
|
text = text.replace(/\s/g, Blockly.Field.NBSP);
|
2015-04-28 13:51:25 -07:00
|
|
|
if (this.sourceBlock_.RTL && text) {
|
2014-09-08 14:26:52 -07:00
|
|
|
// The SVG is LTR, force text to be RTL.
|
|
|
|
text += '\u200F';
|
|
|
|
}
|
2013-10-30 14:46:03 -07:00
|
|
|
if (!text) {
|
|
|
|
// Prevent the field from disappearing if empty.
|
|
|
|
text = Blockly.Field.NBSP;
|
|
|
|
}
|
|
|
|
var textNode = document.createTextNode(text);
|
|
|
|
this.textElement_.appendChild(textNode);
|
|
|
|
|
|
|
|
// Cached width is obsolete. Clear it.
|
|
|
|
this.size_.width = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* By default there is no difference between the human-readable text and
|
|
|
|
* the language-neutral values. Subclasses (such as dropdown) may define this.
|
|
|
|
* @return {string} Current text.
|
|
|
|
*/
|
|
|
|
Blockly.Field.prototype.getValue = function() {
|
|
|
|
return this.getText();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* By default there is no difference between the human-readable text and
|
|
|
|
* the language-neutral values. Subclasses (such as dropdown) may define this.
|
2016-01-15 15:36:06 -08:00
|
|
|
* @param {string} newText New text.
|
2013-10-30 14:46:03 -07:00
|
|
|
*/
|
2016-01-15 15:36:06 -08:00
|
|
|
Blockly.Field.prototype.setValue = function(newText) {
|
|
|
|
if (newText === null) {
|
|
|
|
// No change if null.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var oldText = this.getValue();
|
|
|
|
if (oldText == newText) {
|
|
|
|
return;
|
|
|
|
}
|
2016-01-20 19:11:03 -08:00
|
|
|
if (this.sourceBlock_ && Blockly.Events.isEnabled()) {
|
|
|
|
Blockly.Events.fire(new Blockly.Events.Change(
|
|
|
|
this.sourceBlock_, 'field', this.name, oldText, newText));
|
|
|
|
}
|
2016-01-15 15:36:06 -08:00
|
|
|
this.setText(newText);
|
2013-10-30 14:46:03 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a mouse up event on an editable field.
|
|
|
|
* @param {!Event} e Mouse up event.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Blockly.Field.prototype.onMouseUp_ = function(e) {
|
2016-07-20 17:03:06 -07:00
|
|
|
Blockly.touchIdentifier_ = null;
|
2014-09-08 14:26:52 -07:00
|
|
|
if ((goog.userAgent.IPHONE || goog.userAgent.IPAD) &&
|
2015-01-15 14:29:42 -08:00
|
|
|
!goog.userAgent.isVersionOrHigher('537.51.2') &&
|
|
|
|
e.layerX !== 0 && e.layerY !== 0) {
|
2014-12-07 11:35:41 -06:00
|
|
|
// Old iOS spawns a bogus event on the next touch after a 'prompt()' edit.
|
2014-09-08 14:26:52 -07:00
|
|
|
// Unlike the real events, these have a layerX and layerY set.
|
|
|
|
return;
|
|
|
|
} else if (Blockly.isRightButton(e)) {
|
2013-10-30 14:46:03 -07:00
|
|
|
// Right-click.
|
|
|
|
return;
|
2016-06-29 03:11:48 +02:00
|
|
|
} else if (this.sourceBlock_.workspace.isDragging()) {
|
2013-10-30 14:46:03 -07:00
|
|
|
// Drag operation is concluding. Don't open the editor.
|
|
|
|
return;
|
|
|
|
} else if (this.sourceBlock_.isEditable()) {
|
|
|
|
// Non-abstract sub-classes must define a showEditor_ method.
|
|
|
|
this.showEditor_();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.Field.prototype.setTooltip = function(newTip) {
|
|
|
|
// Non-abstract sub-classes may wish to implement this. See FieldLabel.
|
|
|
|
};
|
2015-04-28 13:51:25 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the absolute coordinates of the top-left corner of this field.
|
|
|
|
* The origin (0,0) is the top-left corner of the page body.
|
2016-05-04 15:05:45 -07:00
|
|
|
* @return {!goog.math.Coordinate} Object with .x and .y properties.
|
2015-04-28 13:51:25 -07:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Blockly.Field.prototype.getAbsoluteXY_ = function() {
|
|
|
|
return goog.style.getPageOffset(this.borderRect_);
|
|
|
|
};
|