scratch-blocks/core/field_textinput.js

304 lines
9.4 KiB
JavaScript
Raw Normal View History

/**
* @license
* Visual Blocks Editor
*
* Copyright 2012 Google Inc.
2014-10-07 13:09:55 -07:00
* https://developers.google.com/blockly/
*
* 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');
goog.require('Blockly.Field');
goog.require('Blockly.Msg');
goog.require('goog.asserts');
goog.require('goog.dom');
goog.require('goog.userAgent');
/**
* Class for an editable text field.
* @param {string} text The initial content of the field.
2015-07-13 15:03:22 -07:00
* @param {Function=} opt_changeHandler 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.
* @extends {Blockly.Field}
* @constructor
*/
Blockly.FieldTextInput = function(text, opt_changeHandler) {
Blockly.FieldTextInput.superClass_.constructor.call(this, text);
this.setChangeHandler(opt_changeHandler);
};
goog.inherits(Blockly.FieldTextInput, Blockly.Field);
/**
* Mouse cursor style when over the hotspot that initiates the editor.
*/
Blockly.FieldTextInput.prototype.CURSOR = 'text';
/**
* Allow browser to spellcheck this field.
* @private
*/
Blockly.FieldTextInput.prototype.spellcheck_ = true;
/**
2014-09-08 14:26:52 -07:00
* Close the input widget if this input is being deleted.
*/
Blockly.FieldTextInput.prototype.dispose = function() {
2014-09-08 14:26:52 -07:00
Blockly.WidgetDiv.hideIfOwner(this);
Blockly.FieldTextInput.superClass_.dispose.call(this);
};
/**
* Set the text in this field.
* @param {?string} text New text.
* @override
*/
Blockly.FieldTextInput.prototype.setText = function(text) {
if (text === null) {
// No change if null.
return;
}
2014-12-23 11:22:02 -08:00
if (this.sourceBlock_ && this.changeHandler_) {
var validated = this.changeHandler_(text);
// 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;
}
}
Blockly.Field.prototype.setText.call(this, text);
};
/**
* Set whether this field is spellchecked by the browser.
* @param {boolean} check True if checked.
*/
Blockly.FieldTextInput.prototype.setSpellcheck = function(check) {
this.spellcheck_ = check;
};
/**
* 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.
* @private
*/
2014-09-08 14:26:52 -07:00
Blockly.FieldTextInput.prototype.showEditor_ = function(opt_quietInput) {
var quietInput = opt_quietInput || false;
if (!quietInput && (goog.userAgent.MOBILE || goog.userAgent.ANDROID ||
goog.userAgent.IPAD)) {
// Mobile browsers have issues with in-line textareas (focus & keyboards).
var newValue = window.prompt(Blockly.Msg.CHANGE_VALUE_TITLE, this.text_);
2014-12-23 11:22:02 -08:00
if (this.sourceBlock_ && this.changeHandler_) {
var override = this.changeHandler_(newValue);
if (override !== undefined) {
newValue = override;
}
}
if (newValue !== null) {
this.setText(newValue);
}
return;
}
2015-04-28 13:51:25 -07:00
Blockly.WidgetDiv.show(this, this.sourceBlock_.RTL, this.widgetDispose_());
var div = Blockly.WidgetDiv.DIV;
// Create the input.
var htmlInput = goog.dom.createDom('input', 'blocklyHtmlInput');
htmlInput.setAttribute('spellcheck', this.spellcheck_);
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();
}
// 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.
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_);
var workspaceSvg = this.sourceBlock_.workspace.getCanvas();
htmlInput.onWorkspaceChangeWrapper_ =
Blockly.bindEvent_(workspaceSvg, 'blocklyWorkspaceChange', this,
this.resizeEditor_);
};
/**
* Handle key down to the editor.
* @param {!Event} e Keyboard event.
* @private
*/
Blockly.FieldTextInput.prototype.onHtmlInputKeyDown_ = function(e) {
var htmlInput = Blockly.FieldTextInput.htmlInput_;
var enterKey = 13, escKey = 27;
if (e.keyCode == enterKey) {
Blockly.WidgetDiv.hide();
} else if (e.keyCode == escKey) {
this.setText(htmlInput.defaultValue);
Blockly.WidgetDiv.hide();
}
};
/**
* Handle a change to the editor.
* @param {!Event} e Keyboard event.
* @private
*/
Blockly.FieldTextInput.prototype.onHtmlInputChange_ = function(e) {
var htmlInput = Blockly.FieldTextInput.htmlInput_;
var escKey = 27;
if (e.keyCode != escKey) {
// Update source block.
var text = htmlInput.value;
if (text !== htmlInput.oldValue_) {
htmlInput.oldValue_ = text;
this.setText(text);
this.validate_();
} 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();
}
}
};
/**
* 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_);
var htmlInput = /** @type {!Element} */ (Blockly.FieldTextInput.htmlInput_);
2014-12-23 11:22:02 -08:00
if (this.sourceBlock_ && this.changeHandler_) {
valid = this.changeHandler_(htmlInput.value);
}
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() {
var div = Blockly.WidgetDiv.DIV;
var bBox = this.fieldGroup_.getBBox();
div.style.width = bBox.width + 'px';
2015-04-28 13:51:25 -07:00
var xy = this.getAbsoluteXY_();
2013-12-20 16:25:26 -08:00
// In RTL mode block fields and LTR input fields the left edge moves,
// whereas the right edge is fixed. Reposition the editor.
2015-04-28 13:51:25 -07:00
if (this.sourceBlock_.RTL) {
var borderBBox = this.borderRect_.getBBox();
xy.x += borderBBox.width;
xy.x -= div.offsetWidth;
}
// Shift by a few pixels to line up exactly.
xy.y += 1;
if (goog.userAgent.WEBKIT) {
xy.y -= 3;
}
div.style.left = xy.x + 'px';
div.style.top = xy.y + 'px';
};
/**
* Close the editor, save the results, and dispose of the editable
* text field's elements.
* @return {!Function} Closure to call on destruction of the WidgetDiv.
* @private
*/
2014-09-08 14:26:52 -07:00
Blockly.FieldTextInput.prototype.widgetDispose_ = function() {
var thisField = this;
return function() {
var htmlInput = Blockly.FieldTextInput.htmlInput_;
// Save the edit (if it validates).
2014-09-08 14:26:52 -07:00
var text = htmlInput.value;
2014-12-23 11:22:02 -08:00
if (thisField.sourceBlock_ && thisField.changeHandler_) {
2015-06-11 18:06:44 -07:00
var text1 = thisField.changeHandler_(text);
if (text1 === null) {
// Invalid edit.
text = htmlInput.defaultValue;
2015-06-11 18:06:44 -07:00
} else if (text1 !== undefined) {
// Change handler has changed the text.
text = text1;
}
}
thisField.setText(text);
2014-09-08 14:26:52 -07:00
thisField.sourceBlock_.rendered && thisField.sourceBlock_.render();
Blockly.unbindEvent_(htmlInput.onKeyDownWrapper_);
Blockly.unbindEvent_(htmlInput.onKeyUpWrapper_);
Blockly.unbindEvent_(htmlInput.onKeyPressWrapper_);
Blockly.unbindEvent_(htmlInput.onWorkspaceChangeWrapper_);
Blockly.FieldTextInput.htmlInput_ = null;
2014-09-08 14:26:52 -07:00
// Delete the width property.
Blockly.WidgetDiv.DIV.style.width = 'auto';
};
};
/**
* 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;
}
text = String(text);
// 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;
};