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 Text input field.
|
|
|
|
* @author fraser@google.com (Neil Fraser)
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
goog.provide('Blockly.FieldTextInput');
|
|
|
|
|
2016-02-24 10:19:59 -08:00
|
|
|
goog.require('Blockly.BlockSvg.render');
|
2016-04-15 16:44:30 -04:00
|
|
|
goog.require('Blockly.Colours');
|
2013-10-30 14:46:03 -07:00
|
|
|
goog.require('Blockly.Field');
|
|
|
|
goog.require('Blockly.Msg');
|
2016-04-15 16:44:30 -04:00
|
|
|
goog.require('Blockly.utils');
|
2013-10-30 14:46:03 -07:00
|
|
|
goog.require('goog.asserts');
|
2015-02-06 15:27:25 -08:00
|
|
|
goog.require('goog.dom');
|
2013-10-30 14:46:03 -07:00
|
|
|
goog.require('goog.userAgent');
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class for an editable text field.
|
|
|
|
* @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
|
2013-10-30 14:46:03 -07:00
|
|
|
* to validate any constraints on what the user entered. Takes the new
|
2013-12-14 03:00:02 -08:00
|
|
|
* 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
|
|
|
* @extends {Blockly.Field}
|
|
|
|
* @constructor
|
|
|
|
*/
|
2016-02-10 14:33:13 -08:00
|
|
|
Blockly.FieldTextInput = function(text, opt_validator) {
|
|
|
|
Blockly.FieldTextInput.superClass_.constructor.call(this, text,
|
|
|
|
opt_validator);
|
2013-10-30 14:46:03 -07:00
|
|
|
};
|
|
|
|
goog.inherits(Blockly.FieldTextInput, Blockly.Field);
|
|
|
|
|
2016-04-15 16:44:30 -04:00
|
|
|
/**
|
|
|
|
* Length of animations in seconds.
|
|
|
|
*/
|
|
|
|
Blockly.FieldTextInput.ANIMATION_TIME = 0.25;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Padding to use for text measurement for the field during editing, in px.
|
|
|
|
*/
|
2016-04-26 21:32:29 -04:00
|
|
|
Blockly.FieldTextInput.TEXT_MEASURE_PADDING_MAGIC = 45;
|
|
|
|
|
2013-10-30 14:46:03 -07:00
|
|
|
/**
|
|
|
|
* Mouse cursor style when over the hotspot that initiates the editor.
|
|
|
|
*/
|
|
|
|
Blockly.FieldTextInput.prototype.CURSOR = 'text';
|
|
|
|
|
2015-02-25 13:35:37 -08:00
|
|
|
/**
|
|
|
|
* Allow browser to spellcheck this field.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Blockly.FieldTextInput.prototype.spellcheck_ = true;
|
|
|
|
|
2013-10-30 14:46:03 -07:00
|
|
|
/**
|
2014-09-08 14:26:52 -07:00
|
|
|
* Close the input widget if this input is being deleted.
|
2013-10-30 14:46:03 -07:00
|
|
|
*/
|
|
|
|
Blockly.FieldTextInput.prototype.dispose = function() {
|
2014-09-08 14:26:52 -07:00
|
|
|
Blockly.WidgetDiv.hideIfOwner(this);
|
2013-10-30 14:46:03 -07:00
|
|
|
Blockly.FieldTextInput.superClass_.dispose.call(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the text in this field.
|
|
|
|
* @param {?string} text New text.
|
|
|
|
* @override
|
|
|
|
*/
|
2016-01-20 19:11:03 -08:00
|
|
|
Blockly.FieldTextInput.prototype.setValue = function(text) {
|
2013-10-30 14:46:03 -07:00
|
|
|
if (text === null) {
|
2016-01-15 15:36:06 -08:00
|
|
|
return; // No change if null.
|
2013-10-30 14:46:03 -07:00
|
|
|
}
|
2016-02-10 14:33:13 -08:00
|
|
|
if (this.sourceBlock_ && this.validator_) {
|
|
|
|
var validated = this.validator_(text);
|
2013-10-30 14:46:03 -07:00
|
|
|
// If the new text is invalid, validation returns null.
|
|
|
|
// In this case we still want to display the illegal result.
|
|
|
|
if (validated !== null && validated !== undefined) {
|
|
|
|
text = validated;
|
|
|
|
}
|
|
|
|
}
|
2016-01-20 19:11:03 -08:00
|
|
|
Blockly.Field.prototype.setValue.call(this, text);
|
2013-10-30 14:46:03 -07:00
|
|
|
};
|
|
|
|
|
2015-02-25 13:35:37 -08:00
|
|
|
/**
|
|
|
|
* Set whether this field is spellchecked by the browser.
|
|
|
|
* @param {boolean} check True if checked.
|
|
|
|
*/
|
|
|
|
Blockly.FieldTextInput.prototype.setSpellcheck = function(check) {
|
|
|
|
this.spellcheck_ = check;
|
|
|
|
};
|
|
|
|
|
2013-10-30 14:46:03 -07:00
|
|
|
/**
|
|
|
|
* Show the inline free-text editor on top of the text.
|
2014-09-08 14:26:52 -07:00
|
|
|
* @param {boolean=} opt_quietInput True if editor should be created without
|
|
|
|
* focus. Defaults to false.
|
2016-04-26 21:32:29 -04:00
|
|
|
* @param {boolean=} opt_readOnly True if editor should be created with HTML
|
|
|
|
* input set to read-only, to prevent virtual keyboards.
|
2013-10-30 14:46:03 -07:00
|
|
|
* @private
|
|
|
|
*/
|
2016-04-26 21:32:29 -04:00
|
|
|
Blockly.FieldTextInput.prototype.showEditor_ = function(opt_quietInput, opt_readOnly) {
|
2016-03-29 21:41:20 -07:00
|
|
|
this.workspace_ = this.sourceBlock_.workspace;
|
2014-09-08 14:26:52 -07:00
|
|
|
var quietInput = opt_quietInput || false;
|
2016-04-26 21:32:29 -04:00
|
|
|
var readOnly = opt_readOnly || false;
|
2016-04-15 16:44:30 -04:00
|
|
|
Blockly.WidgetDiv.show(this, this.sourceBlock_.RTL,
|
|
|
|
this.widgetDispose_(), this.widgetDisposeAnimationFinished_(),
|
|
|
|
Blockly.FieldTextInput.ANIMATION_TIME);
|
2013-10-30 14:46:03 -07:00
|
|
|
var div = Blockly.WidgetDiv.DIV;
|
2016-04-06 21:01:55 -04:00
|
|
|
// Apply text-input-specific fixed CSS
|
|
|
|
div.className += ' fieldTextInput';
|
2013-10-30 14:46:03 -07:00
|
|
|
// Create the input.
|
|
|
|
var htmlInput = goog.dom.createDom('input', 'blocklyHtmlInput');
|
2015-02-25 13:35:37 -08:00
|
|
|
htmlInput.setAttribute('spellcheck', this.spellcheck_);
|
2016-04-26 21:32:29 -04:00
|
|
|
if (readOnly) {
|
|
|
|
htmlInput.setAttribute('readonly', 'true');
|
|
|
|
}
|
2015-08-10 13:46:16 -05:00
|
|
|
/** @type {!HTMLInputElement} */
|
2013-10-30 14:46:03 -07:00
|
|
|
Blockly.FieldTextInput.htmlInput_ = htmlInput;
|
|
|
|
div.appendChild(htmlInput);
|
|
|
|
|
|
|
|
htmlInput.value = htmlInput.defaultValue = this.text_;
|
|
|
|
htmlInput.oldValue_ = null;
|
|
|
|
this.validate_();
|
|
|
|
this.resizeEditor_();
|
2014-09-08 14:26:52 -07:00
|
|
|
if (!quietInput) {
|
|
|
|
htmlInput.focus();
|
|
|
|
htmlInput.select();
|
2016-04-26 21:32:29 -04:00
|
|
|
// For iOS only
|
|
|
|
htmlInput.setSelectionRange(0, 99999);
|
2014-09-08 14:26:52 -07:00
|
|
|
}
|
2013-10-30 14:46:03 -07:00
|
|
|
|
2015-02-17 23:20:36 +09:00
|
|
|
// Bind to keydown -- trap Enter without IME and Esc to hide.
|
|
|
|
htmlInput.onKeyDownWrapper_ =
|
|
|
|
Blockly.bindEvent_(htmlInput, 'keydown', this, this.onHtmlInputKeyDown_);
|
|
|
|
// Bind to keyup -- trap Enter; resize after every keystroke.
|
2013-10-30 14:46:03 -07:00
|
|
|
htmlInput.onKeyUpWrapper_ =
|
|
|
|
Blockly.bindEvent_(htmlInput, 'keyup', this, this.onHtmlInputChange_);
|
|
|
|
// Bind to keyPress -- repeatedly resize when holding down a key.
|
|
|
|
htmlInput.onKeyPressWrapper_ =
|
|
|
|
Blockly.bindEvent_(htmlInput, 'keypress', this, this.onHtmlInputChange_);
|
2016-06-16 14:32:39 -04:00
|
|
|
// For modern browsers (IE 9+, Chrome, Firefox, etc.) that support the
|
|
|
|
// DOM input event, also trigger onHtmlInputChange_ then. The input event
|
|
|
|
// is triggered on keypress but after the value of the text input
|
|
|
|
// has updated, allowing us to resize the block at that time.
|
|
|
|
htmlInput.onInputWrapper_ =
|
|
|
|
Blockly.bindEvent_(htmlInput, 'input', this, this.onHtmlInputChange_);
|
|
|
|
|
2016-02-11 21:40:33 -08:00
|
|
|
htmlInput.onWorkspaceChangeWrapper_ = this.resizeEditor_.bind(this);
|
2016-03-29 21:41:20 -07:00
|
|
|
this.workspace_.addChangeListener(htmlInput.onWorkspaceChangeWrapper_);
|
2016-04-15 16:44:30 -04:00
|
|
|
|
|
|
|
// Add animation transition properties
|
2016-06-15 18:08:32 -04:00
|
|
|
var transitionProperties = 'box-shadow ' + Blockly.FieldTextInput.ANIMATION_TIME + 's';
|
|
|
|
if (Blockly.BlockSvg.FIELD_TEXTINPUT_ANIMATE_POSITIONING) {
|
|
|
|
div.style.transition += ',padding ' + Blockly.FieldTextInput.ANIMATION_TIME + 's,' +
|
|
|
|
'width ' + Blockly.FieldTextInput.ANIMATION_TIME + 's,' +
|
|
|
|
'height ' + Blockly.FieldTextInput.ANIMATION_TIME + 's,' +
|
|
|
|
'margin-left ' + Blockly.FieldTextInput.ANIMATION_TIME + 's';
|
|
|
|
}
|
|
|
|
div.style.transition = transitionProperties;
|
2016-04-15 16:44:30 -04:00
|
|
|
htmlInput.style.transition = 'font-size ' + Blockly.FieldTextInput.ANIMATION_TIME + 's';
|
|
|
|
// The animated properties themselves
|
2016-06-15 18:08:32 -04:00
|
|
|
htmlInput.style.fontSize = Blockly.BlockSvg.FIELD_TEXTINPUT_FONTSIZE_FINAL + 'pt';
|
2016-04-15 16:44:30 -04:00
|
|
|
div.style.boxShadow = '0px 0px 0px 4px ' + Blockly.Colours.fieldShadow;
|
2013-10-30 14:46:03 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-02-17 23:20:36 +09:00
|
|
|
* Handle key down to the editor.
|
2013-10-30 14:46:03 -07:00
|
|
|
* @param {!Event} e Keyboard event.
|
|
|
|
* @private
|
|
|
|
*/
|
2015-02-17 23:20:36 +09:00
|
|
|
Blockly.FieldTextInput.prototype.onHtmlInputKeyDown_ = function(e) {
|
2013-10-30 14:46:03 -07:00
|
|
|
var htmlInput = Blockly.FieldTextInput.htmlInput_;
|
2015-10-14 16:23:23 -07:00
|
|
|
var tabKey = 9, enterKey = 13, escKey = 27;
|
2015-02-17 23:20:36 +09:00
|
|
|
if (e.keyCode == enterKey) {
|
2013-10-30 14:46:03 -07:00
|
|
|
Blockly.WidgetDiv.hide();
|
2015-02-17 23:20:36 +09:00
|
|
|
} else if (e.keyCode == escKey) {
|
2016-01-20 19:11:03 -08:00
|
|
|
htmlInput.value = htmlInput.defaultValue;
|
2013-10-30 14:46:03 -07:00
|
|
|
Blockly.WidgetDiv.hide();
|
2015-10-14 16:23:23 -07:00
|
|
|
} else if (e.keyCode == tabKey) {
|
|
|
|
Blockly.WidgetDiv.hide();
|
|
|
|
this.sourceBlock_.tab(this, !e.shiftKey);
|
|
|
|
e.preventDefault();
|
2015-02-17 23:20:36 +09:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a change to the editor.
|
|
|
|
* @param {!Event} e Keyboard event.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Blockly.FieldTextInput.prototype.onHtmlInputChange_ = function(e) {
|
|
|
|
var htmlInput = Blockly.FieldTextInput.htmlInput_;
|
2016-01-15 15:36:06 -08:00
|
|
|
// Update source block.
|
|
|
|
var text = htmlInput.value;
|
|
|
|
if (text !== htmlInput.oldValue_) {
|
|
|
|
htmlInput.oldValue_ = text;
|
|
|
|
this.setValue(text);
|
|
|
|
this.validate_();
|
2016-06-16 14:32:39 -04:00
|
|
|
this.resizeEditor_();
|
2016-01-15 15:36:06 -08:00
|
|
|
} else if (goog.userAgent.WEBKIT) {
|
|
|
|
// Cursor key. Render the source block to show the caret moving.
|
|
|
|
// Chrome only (version 26, OS X).
|
|
|
|
this.sourceBlock_.render();
|
2016-06-16 14:32:39 -04:00
|
|
|
this.resizeEditor_();
|
2013-10-30 14:46:03 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check to see if the contents of the editor validates.
|
|
|
|
* Style the editor accordingly.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Blockly.FieldTextInput.prototype.validate_ = function() {
|
|
|
|
var valid = true;
|
|
|
|
goog.asserts.assertObject(Blockly.FieldTextInput.htmlInput_);
|
2015-08-10 13:46:16 -05:00
|
|
|
var htmlInput = Blockly.FieldTextInput.htmlInput_;
|
2016-02-10 14:33:13 -08:00
|
|
|
if (this.sourceBlock_ && this.validator_) {
|
|
|
|
valid = this.validator_(htmlInput.value);
|
2013-10-30 14:46:03 -07:00
|
|
|
}
|
|
|
|
if (valid === null) {
|
|
|
|
Blockly.addClass_(htmlInput, 'blocklyInvalidInput');
|
|
|
|
} else {
|
|
|
|
Blockly.removeClass_(htmlInput, 'blocklyInvalidInput');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resize the editor and the underlying block to fit the text.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Blockly.FieldTextInput.prototype.resizeEditor_ = function() {
|
2016-04-05 18:36:02 -04:00
|
|
|
var scale = this.sourceBlock_.workspace.scale;
|
2013-10-30 14:46:03 -07:00
|
|
|
var div = Blockly.WidgetDiv.DIV;
|
2016-06-15 18:08:32 -04:00
|
|
|
|
|
|
|
var width;
|
|
|
|
if (Blockly.BlockSvg.FIELD_TEXTINPUT_EXPAND_PAST_TRUNCATION) {
|
|
|
|
// Resize the box based on the measured width of the text, pre-truncation
|
|
|
|
var textWidth = Blockly.measureText(
|
|
|
|
Blockly.FieldTextInput.htmlInput_.style.fontSize,
|
|
|
|
Blockly.FieldTextInput.htmlInput_.style.fontFamily,
|
|
|
|
Blockly.FieldTextInput.htmlInput_.style.fontWeight,
|
|
|
|
Blockly.FieldTextInput.htmlInput_.value
|
|
|
|
);
|
|
|
|
// Size drawn in the canvas needs padding and scaling
|
|
|
|
textWidth += Blockly.FieldTextInput.TEXT_MEASURE_PADDING_MAGIC;
|
|
|
|
textWidth *= scale;
|
|
|
|
width = textWidth;
|
|
|
|
} else {
|
|
|
|
// Set width to (truncated) block size.
|
|
|
|
width = this.sourceBlock_.getHeightWidth().width * scale;
|
|
|
|
}
|
2016-04-15 16:44:30 -04:00
|
|
|
// The width must be at least FIELD_WIDTH and at most FIELD_WIDTH_MAX_EDIT
|
2016-06-15 18:08:32 -04:00
|
|
|
width = Math.max(width, Blockly.BlockSvg.FIELD_WIDTH_MIN_EDIT * scale);
|
2016-04-15 16:44:30 -04:00
|
|
|
width = Math.min(width, Blockly.BlockSvg.FIELD_WIDTH_MAX_EDIT * scale);
|
2016-04-05 19:37:32 -04:00
|
|
|
// Add 1px to width and height to account for border (pre-scale)
|
2016-04-05 18:36:02 -04:00
|
|
|
div.style.width = (width / scale + 1) + 'px';
|
2016-04-15 16:44:30 -04:00
|
|
|
div.style.height = (Blockly.BlockSvg.FIELD_HEIGHT_MAX_EDIT + 1) + 'px';
|
2016-04-05 18:36:02 -04:00
|
|
|
div.style.transform = 'scale(' + scale + ')';
|
|
|
|
|
2016-04-15 16:44:30 -04:00
|
|
|
// Use margin-left to animate repositioning of the box (value is unscaled).
|
|
|
|
// This is the difference between the default position and the positioning
|
|
|
|
// after growing the box.
|
2016-06-15 18:08:32 -04:00
|
|
|
var fieldWidth = this.sourceBlock_.getHeightWidth().width;
|
|
|
|
var initialWidth = fieldWidth * scale;
|
2016-04-15 16:44:30 -04:00
|
|
|
var finalWidth = width;
|
|
|
|
div.style.marginLeft = -0.5 * (finalWidth - initialWidth) + 'px';
|
|
|
|
|
2016-04-05 19:37:32 -04:00
|
|
|
// Add 0.5px to account for slight difference between SVG and CSS border
|
|
|
|
var borderRadius = this.getBorderRadius() + 0.5;
|
2016-04-05 18:36:02 -04:00
|
|
|
div.style.borderRadius = borderRadius + 'px';
|
2016-04-05 19:37:32 -04:00
|
|
|
// Pull stroke colour from the existing shadow block
|
|
|
|
var strokeColour = this.sourceBlock_.getColourTertiary();
|
|
|
|
div.style.borderColor = strokeColour;
|
2016-04-05 18:36:02 -04:00
|
|
|
|
2015-04-28 13:51:25 -07:00
|
|
|
var xy = this.getAbsoluteXY_();
|
2016-04-05 19:37:32 -04:00
|
|
|
// Account for border width, post-scale
|
2016-04-05 18:36:02 -04:00
|
|
|
xy.x -= scale / 2;
|
|
|
|
xy.y -= scale / 2;
|
2013-12-20 16:25:26 -08:00
|
|
|
// In RTL mode block fields and LTR input fields the left edge moves,
|
2013-10-30 14:46:03 -07:00
|
|
|
// whereas the right edge is fixed. Reposition the editor.
|
2015-04-28 13:51:25 -07:00
|
|
|
if (this.sourceBlock_.RTL) {
|
2016-04-05 19:41:04 -04:00
|
|
|
xy.x += width;
|
2016-06-15 18:08:32 -04:00
|
|
|
xy.x -= div.offsetWidth * scale;
|
2013-10-30 14:46:03 -07:00
|
|
|
}
|
|
|
|
// Shift by a few pixels to line up exactly.
|
2016-04-05 18:36:02 -04:00
|
|
|
xy.y += 1 * scale;
|
2015-08-21 14:13:07 -07:00
|
|
|
if (goog.userAgent.GECKO && Blockly.WidgetDiv.DIV.style.top) {
|
|
|
|
// Firefox mis-reports the location of the border by a pixel
|
|
|
|
// once the WidgetDiv is moved into position.
|
2016-04-05 18:36:02 -04:00
|
|
|
xy.x += 2 * scale;
|
|
|
|
xy.y += 1 * scale;
|
2015-08-21 14:13:07 -07:00
|
|
|
}
|
2013-10-30 14:46:03 -07:00
|
|
|
if (goog.userAgent.WEBKIT) {
|
2016-04-05 18:36:02 -04:00
|
|
|
xy.y -= 1 * scale;
|
2013-10-30 14:46:03 -07:00
|
|
|
}
|
2016-04-05 19:43:41 -04:00
|
|
|
// Finally, set the actual style
|
2013-10-30 14:46:03 -07:00
|
|
|
div.style.left = xy.x + 'px';
|
|
|
|
div.style.top = xy.y + 'px';
|
|
|
|
};
|
|
|
|
|
2016-04-05 19:37:32 -04:00
|
|
|
/**
|
2016-05-23 16:21:31 -04:00
|
|
|
* Border radius for drawing this field, called when rendering the owning shadow block.
|
2016-04-05 19:37:32 -04:00
|
|
|
* @return {Number} Border radius in px.
|
|
|
|
*/
|
2016-05-23 16:21:31 -04:00
|
|
|
Blockly.FieldTextInput.prototype.getBorderRadius = function() {
|
|
|
|
return Blockly.BlockSvg.TEXT_FIELD_CORNER_RADIUS;
|
2016-04-05 19:37:32 -04:00
|
|
|
};
|
|
|
|
|
2013-10-30 14:46:03 -07:00
|
|
|
/**
|
2016-04-15 16:44:30 -04:00
|
|
|
* Close the editor, save the results, and start animating the disposal of elements.
|
2013-10-30 14:46:03 -07:00
|
|
|
* @return {!Function} Closure to call on destruction of the WidgetDiv.
|
|
|
|
* @private
|
|
|
|
*/
|
2014-09-08 14:26:52 -07:00
|
|
|
Blockly.FieldTextInput.prototype.widgetDispose_ = function() {
|
2013-10-30 14:46:03 -07:00
|
|
|
var thisField = this;
|
|
|
|
return function() {
|
2016-04-15 16:44:30 -04:00
|
|
|
var div = Blockly.WidgetDiv.DIV;
|
2013-10-30 14:46:03 -07:00
|
|
|
var htmlInput = Blockly.FieldTextInput.htmlInput_;
|
|
|
|
// Save the edit (if it validates).
|
2014-09-08 14:26:52 -07:00
|
|
|
var text = htmlInput.value;
|
2016-02-10 14:33:13 -08:00
|
|
|
if (thisField.sourceBlock_ && thisField.validator_) {
|
|
|
|
var text1 = thisField.validator_(text);
|
2015-06-11 18:06:44 -07:00
|
|
|
if (text1 === null) {
|
2013-10-30 14:46:03 -07:00
|
|
|
// Invalid edit.
|
|
|
|
text = htmlInput.defaultValue;
|
2015-06-11 18:06:44 -07:00
|
|
|
} else if (text1 !== undefined) {
|
2016-02-10 14:33:13 -08:00
|
|
|
// Validation function has changed the text.
|
2015-06-11 18:06:44 -07:00
|
|
|
text = text1;
|
2013-10-30 14:46:03 -07:00
|
|
|
}
|
|
|
|
}
|
2016-01-20 19:11:03 -08:00
|
|
|
thisField.setValue(text);
|
2014-09-08 14:26:52 -07:00
|
|
|
thisField.sourceBlock_.rendered && thisField.sourceBlock_.render();
|
2015-02-17 23:20:36 +09:00
|
|
|
Blockly.unbindEvent_(htmlInput.onKeyDownWrapper_);
|
2013-10-30 14:46:03 -07:00
|
|
|
Blockly.unbindEvent_(htmlInput.onKeyUpWrapper_);
|
|
|
|
Blockly.unbindEvent_(htmlInput.onKeyPressWrapper_);
|
2016-06-16 14:32:39 -04:00
|
|
|
Blockly.unbindEvent_(htmlInput.onInputWrapper_);
|
2016-03-29 21:41:20 -07:00
|
|
|
thisField.workspace_.removeChangeListener(
|
2016-02-11 21:40:33 -08:00
|
|
|
htmlInput.onWorkspaceChangeWrapper_);
|
2016-04-15 16:44:30 -04:00
|
|
|
|
|
|
|
// Animation of disposal
|
2016-06-15 18:08:32 -04:00
|
|
|
htmlInput.style.fontSize = Blockly.BlockSvg.FIELD_TEXTINPUT_FONTSIZE_INITIAL + 'pt';
|
2016-04-15 16:44:30 -04:00
|
|
|
div.style.boxShadow = '';
|
2016-06-15 18:08:32 -04:00
|
|
|
// Resize to actual size of final source block.
|
|
|
|
if (thisField.sourceBlock_) {
|
|
|
|
var size = thisField.sourceBlock_.getHeightWidth();
|
|
|
|
div.style.width = (size.width + 1) + 'px';
|
|
|
|
div.style.height = (size.height + 1) + 'px';
|
|
|
|
}
|
2016-04-15 16:44:30 -04:00
|
|
|
div.style.marginLeft = 0;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Final disposal of the text field's elements and properties.
|
|
|
|
* @return {!Function} Closure to call on finish animation of the WidgetDiv.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Blockly.FieldTextInput.prototype.widgetDisposeAnimationFinished_ = function() {
|
|
|
|
return function() {
|
2015-08-19 17:21:05 -07:00
|
|
|
// Delete style properties.
|
|
|
|
var style = Blockly.WidgetDiv.DIV.style;
|
|
|
|
style.width = 'auto';
|
|
|
|
style.height = 'auto';
|
|
|
|
style.fontSize = '';
|
2016-04-06 21:01:55 -04:00
|
|
|
// Reset class
|
|
|
|
Blockly.WidgetDiv.DIV.className = 'blocklyWidgetDiv';
|
2016-06-14 16:34:24 -04:00
|
|
|
// Remove all styles
|
|
|
|
Blockly.WidgetDiv.DIV.removeAttribute('style');
|
2016-04-15 16:44:30 -04:00
|
|
|
Blockly.FieldTextInput.htmlInput_.style.transition = '';
|
|
|
|
Blockly.FieldTextInput.htmlInput_ = null;
|
2013-10-30 14:46:03 -07:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure that only a number may be entered.
|
|
|
|
* @param {string} text The user's text.
|
|
|
|
* @return {?string} A string representing a valid number, or null if invalid.
|
|
|
|
*/
|
|
|
|
Blockly.FieldTextInput.numberValidator = function(text) {
|
2015-01-22 15:58:10 -08:00
|
|
|
if (text === null) {
|
2014-12-07 19:19:35 -06:00
|
|
|
return null;
|
|
|
|
}
|
2015-06-08 15:54:18 -07:00
|
|
|
text = String(text);
|
2013-10-30 14:46:03 -07:00
|
|
|
// TODO: Handle cases like 'ten', '1.203,14', etc.
|
|
|
|
// 'O' is sometimes mistaken for '0' by inexperienced users.
|
|
|
|
text = text.replace(/O/ig, '0');
|
|
|
|
// Strip out thousands separators.
|
|
|
|
text = text.replace(/,/g, '');
|
|
|
|
var n = parseFloat(text || 0);
|
|
|
|
return isNaN(n) ? null : String(n);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure that only a nonnegative integer may be entered.
|
|
|
|
* @param {string} text The user's text.
|
|
|
|
* @return {?string} A string representing a valid int, or null if invalid.
|
|
|
|
*/
|
|
|
|
Blockly.FieldTextInput.nonnegativeIntegerValidator = function(text) {
|
|
|
|
var n = Blockly.FieldTextInput.numberValidator(text);
|
|
|
|
if (n) {
|
|
|
|
n = String(Math.max(0, Math.floor(n)));
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
};
|