Decompose the showEditor_ function in FieldTextInput (#1285)

* Explicit annotations

* Decompose the showEditor_ function in FieldTextInput

* Remove extra newline
This commit is contained in:
Rachel Fenichel
2017-08-18 15:19:05 -07:00
committed by GitHub
parent 2d5f1156d7
commit 00b2b85e8c

View File

@@ -142,18 +142,35 @@ 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 fieldText = this;
Blockly.prompt(Blockly.Msg.CHANGE_VALUE_TITLE, this.text_,
function(newValue) {
if (fieldText.sourceBlock_) {
newValue = fieldText.callValidator(newValue);
}
fieldText.setValue(newValue);
});
return;
this.showPromptEditor_();
} else {
this.showInlineEditor_(quietInput);
}
};
/**
* Create and show a text input editor that is a prompt (usually a popup).
* Mobile browsers have issues with in-line textareas (focus and keyboards).
* @private
*/
Blockly.FieldTextInput.showPromptEditor_ = function() {
var fieldText = this;
Blockly.prompt(Blockly.Msg.CHANGE_VALUE_TITLE, this.text_,
function(newValue) {
if (fieldText.sourceBlock_) {
newValue = fieldText.callValidator(newValue);
}
fieldText.setValue(newValue);
});
};
/**
* Create and show a text input editor that sits directly over the text input.
* @param {boolean} quietInput True if editor should be created without
* focus.
* @private
*/
Blockly.FieldTextInput.prototype.showInlineEditor_ = function(quietInput) {
Blockly.WidgetDiv.show(this, this.sourceBlock_.RTL, this.widgetDispose_());
var div = Blockly.WidgetDiv.DIV;
// Create the input.
@@ -320,22 +337,7 @@ Blockly.FieldTextInput.prototype.widgetDispose_ = function() {
return function() {
var htmlInput = Blockly.FieldTextInput.htmlInput_;
// Save the edit (if it validates).
var text = htmlInput.value;
if (thisField.sourceBlock_) {
var text1 = thisField.callValidator(text);
if (text1 === null) {
// Invalid edit.
text = htmlInput.defaultValue;
} else {
// Validation function has changed the text.
text = text1;
if (thisField.onFinishEditing_) {
thisField.onFinishEditing_(text);
}
}
}
thisField.setText(text);
thisField.sourceBlock_.rendered && thisField.sourceBlock_.render();
thisField.maybeSaveEdit_();
thisField.unbindEvents_(htmlInput);
Blockly.FieldTextInput.htmlInput_ = null;
@@ -349,6 +351,27 @@ Blockly.FieldTextInput.prototype.widgetDispose_ = function() {
};
};
Blockly.FieldTextInput.prototype.maybeSaveEdit_ = function() {
var htmlInput = Blockly.FieldTextInput.htmlInput_;
// Save the edit (if it validates).
var text = htmlInput.value;
if (this.sourceBlock_) {
var text1 = this.callValidator(text);
if (text1 === null) {
// Invalid edit.
text = htmlInput.defaultValue;
} else {
// Validation function has changed the text.
text = text1;
if (this.onFinishEditing_) {
this.onFinishEditing_(text);
}
}
}
this.setText(text);
this.sourceBlock_.rendered && this.sourceBlock_.render();
};
/**
* Ensure that only a number may be entered.
* @param {string} text The user's text.