mirror of
https://github.com/google/blockly.git
synced 2026-01-28 03:00:09 +01:00
Refactor validators to survive aggressive Closure compression.
This commit is contained in:
@@ -554,8 +554,7 @@ Blockly.Events.Change.prototype.run = function(forward) {
|
||||
if (field) {
|
||||
// Run the validator for any side-effects it may have.
|
||||
// The validator's opinion on validity is ignored.
|
||||
var validator = field.getValidator();
|
||||
validator && validator.call(field, value);
|
||||
field.callValidator(value);
|
||||
field.setValue(value);
|
||||
} else {
|
||||
console.warn("Can't set non-existant field: " + this.name);
|
||||
|
||||
@@ -236,6 +236,15 @@ Blockly.Field.prototype.getValidator = function() {
|
||||
return this.validator_;
|
||||
};
|
||||
|
||||
/**
|
||||
* 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;
|
||||
};
|
||||
|
||||
/**
|
||||
* Calls the validation function for this field, as well as all the validation
|
||||
* function for the field's class and its parents.
|
||||
@@ -243,25 +252,21 @@ Blockly.Field.prototype.getValidator = function() {
|
||||
* @return {?string} Revised text, or null if invalid.
|
||||
*/
|
||||
Blockly.Field.prototype.callValidator = function(text) {
|
||||
// Collect a list of validators, from Field, through to the subclass, ending
|
||||
// with the user's validator.
|
||||
var validators = [this.getValidator()];
|
||||
var fieldClass = this.constructor;
|
||||
while (fieldClass) {
|
||||
validators.unshift(fieldClass.classValidator);
|
||||
fieldClass = fieldClass.superClass_;
|
||||
var classResult = this.classValidator(text);
|
||||
if (classResult === null) {
|
||||
// Class validator rejects value. Game over.
|
||||
return null;
|
||||
} else if (classResult !== undefined) {
|
||||
text = classResult;
|
||||
}
|
||||
// Call each validator in turn, allowing each to rewrite or reject.
|
||||
for (var i = 0; i < validators.length; i++) {
|
||||
var validator = validators[i];
|
||||
if (validator) {
|
||||
var result = validator.call(this, text);
|
||||
if (result === null) {
|
||||
// Validator rejects value. Game over.
|
||||
return null;
|
||||
} else if (result !== undefined) {
|
||||
text = result;
|
||||
}
|
||||
var userValidator = this.getValidator();
|
||||
if (userValidator) {
|
||||
var userResult = userValidator(text);
|
||||
if (userResult === null) {
|
||||
// User validator rejects value. Game over.
|
||||
return null;
|
||||
} else if (userResult !== undefined) {
|
||||
text = userResult;
|
||||
}
|
||||
}
|
||||
return text;
|
||||
|
||||
@@ -205,7 +205,7 @@ Blockly.FieldAngle.prototype.onMouseMove = function(e) {
|
||||
angle = Math.round(angle / Blockly.FieldAngle.ROUND) *
|
||||
Blockly.FieldAngle.ROUND;
|
||||
}
|
||||
angle = Blockly.FieldAngle.classValidator(angle);
|
||||
angle = this.callValidator(angle);
|
||||
Blockly.FieldTextInput.htmlInput_.value = angle;
|
||||
this.setValue(angle);
|
||||
this.validate_();
|
||||
@@ -274,9 +274,8 @@ Blockly.FieldAngle.prototype.updateGraph_ = function() {
|
||||
* Ensure that only an angle may be entered.
|
||||
* @param {string} text The user's text.
|
||||
* @return {?string} A string representing a valid angle, or null if invalid.
|
||||
* @this {!Blockly.FieldAngle}
|
||||
*/
|
||||
Blockly.FieldAngle.classValidator = function(text) {
|
||||
Blockly.FieldAngle.prototype.classValidator = function(text) {
|
||||
if (text === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -75,9 +75,8 @@ Blockly.FieldNumber.prototype.setConstraints = function(min, max, precision) {
|
||||
* Ensure that only a number in the correct range may be entered.
|
||||
* @param {string} text The user's text.
|
||||
* @return {?string} A string representing a valid number, or null if invalid.
|
||||
* @this {!Blockly.FieldNumber}
|
||||
*/
|
||||
Blockly.FieldNumber.classValidator = function(text) {
|
||||
Blockly.FieldNumber.prototype.classValidator = function(text) {
|
||||
if (text === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -127,9 +127,8 @@ Blockly.FieldVariable.dropdownCreate = function() {
|
||||
* @return {null|undefined|string} An acceptable new variable name, or null if
|
||||
* change is to be either aborted (cancel button) or has been already
|
||||
* handled (rename), or undefined if an existing variable was chosen.
|
||||
* @this {!Blockly.FieldVariable}
|
||||
*/
|
||||
Blockly.FieldVariable.classValidator = function(text) {
|
||||
Blockly.FieldVariable.prototype.classValidator = function(text) {
|
||||
function promptName(promptText, defaultText) {
|
||||
Blockly.hideChaff();
|
||||
var newVar = window.prompt(promptText, defaultText);
|
||||
|
||||
Reference in New Issue
Block a user