From 63c112932b58616e20cacc2735e7ca9f6de5f46c Mon Sep 17 00:00:00 2001 From: Neil Fraser Date: Wed, 10 Feb 2016 14:33:13 -0800 Subject: [PATCH] Rename Field.setChangeHandler to Field.setValidator --- blocks/loops.js | 2 +- core/block_svg.js | 1 - core/field.js | 26 ++++++++++++++++++-------- core/field_angle.js | 21 ++++++++++----------- core/field_checkbox.js | 14 ++++++-------- core/field_colour.js | 13 ++++++------- core/field_date.js | 17 ++++++++--------- core/field_dropdown.js | 14 +++++++------- core/field_textinput.js | 26 +++++++++++++------------- core/field_variable.js | 11 +++++------ 10 files changed, 74 insertions(+), 71 deletions(-) diff --git a/blocks/loops.js b/blocks/loops.js index 347d60ea9..adf03a1bd 100644 --- a/blocks/loops.js +++ b/blocks/loops.js @@ -84,7 +84,7 @@ Blockly.Blocks['controls_repeat'] = { }); this.appendStatementInput('DO') .appendField(Blockly.Msg.CONTROLS_REPEAT_INPUT_DO); - this.getField('TIMES').setChangeHandler( + this.getField('TIMES').setValidator( Blockly.FieldTextInput.nonnegativeIntegerValidator); } }; diff --git a/core/block_svg.js b/core/block_svg.js index 66fb6a640..16e82c4b5 100644 --- a/core/block_svg.js +++ b/core/block_svg.js @@ -1123,7 +1123,6 @@ Blockly.BlockSvg.prototype.dispose = function(healStack, animate) { } Blockly.Events.enable(); - goog.dom.removeNode(this.svgGroup_); // Sever JavaScript to DOM connections. this.svgGroup_ = null; diff --git a/core/field.js b/core/field.js index 84621fee4..a7f093d09 100644 --- a/core/field.js +++ b/core/field.js @@ -38,11 +38,16 @@ goog.require('goog.userAgent'); /** * Abstract class for an editable field. * @param {string} text The initial content of the field. + * @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. * @constructor */ -Blockly.Field = function(text) { +Blockly.Field = function(text, opt_validator) { this.size_ = new goog.math.Size(0, 25); this.setValue(text); + this.setValidator(opt_validator); }; /** @@ -69,32 +74,37 @@ Blockly.Field.prototype.name = undefined; /** * Maximum characters of text to display before adding an ellipsis. + * @type {number} */ Blockly.Field.prototype.maxDisplayLength = 50; /** * Visible text to display. + * @type {string} * @private */ Blockly.Field.prototype.text_ = ''; /** * Block this field is attached to. Starts as null, then in set in init. + * @type {Blockly.Block} * @private */ Blockly.Field.prototype.sourceBlock_ = null; /** * Is the field visible, or hidden due to the block being collapsed? + * @type {boolean} * @private */ Blockly.Field.prototype.visible_ = true; /** - * Change handler called when user edits an editable field. + * Validation function called when user edits an editable field. + * @type {Function} * @private */ -Blockly.Field.prototype.changeHandler_ = null; +Blockly.Field.prototype.validator_ = null; /** * Non-breaking space. @@ -158,7 +168,7 @@ Blockly.Field.prototype.dispose = function() { this.fieldGroup_ = null; this.textElement_ = null; this.borderRect_ = null; - this.changeHandler_ = null; + this.validator_ = null; }; /** @@ -208,11 +218,11 @@ Blockly.Field.prototype.setVisible = function(visible) { }; /** - * Sets a new change handler for editable fields. - * @param {Function} handler New change handler, or null. + * Sets a new validation function for editable fields. + * @param {Function} handler New validation function, or null. */ -Blockly.Field.prototype.setChangeHandler = function(handler) { - this.changeHandler_ = handler; +Blockly.Field.prototype.setValidator = function(handler) { + this.validator_ = handler; }; /** diff --git a/core/field_angle.js b/core/field_angle.js index fa6f16475..f39991a37 100644 --- a/core/field_angle.js +++ b/core/field_angle.js @@ -34,20 +34,19 @@ goog.require('goog.userAgent'); /** * Class for an editable angle field. * @param {string} text The initial content of the field. - * @param {Function=} opt_changeHandler An optional function that is called + * @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 the accepted text or null to abort * the change. * @extends {Blockly.FieldTextInput} * @constructor */ -Blockly.FieldAngle = function(text, opt_changeHandler) { +Blockly.FieldAngle = function(text, opt_validator) { // Add degree symbol: "360°" (LTR) or "°360" (RTL) this.symbol_ = Blockly.createSvgElement('tspan', {}, null); this.symbol_.appendChild(document.createTextNode('\u00B0')); - Blockly.FieldAngle.superClass_.constructor.call(this, text, null); - this.setChangeHandler(opt_changeHandler); + Blockly.FieldAngle.superClass_.constructor.call(this, text, opt_validator); }; goog.inherits(Blockly.FieldAngle, Blockly.FieldTextInput); @@ -55,7 +54,7 @@ goog.inherits(Blockly.FieldAngle, Blockly.FieldTextInput); * Sets a new change handler for angle field. * @param {Function} handler New change handler, or null. */ -Blockly.FieldAngle.prototype.setChangeHandler = function(handler) { +Blockly.FieldAngle.prototype.setValidator = function(handler) { var wrappedHandler; if (handler) { // Wrap the user's change handler together with the angle validator. @@ -77,7 +76,7 @@ Blockly.FieldAngle.prototype.setChangeHandler = function(handler) { } else { wrappedHandler = Blockly.FieldAngle.angleValidator; } - Blockly.FieldAngle.superClass_.setChangeHandler.call(this, wrappedHandler); + Blockly.FieldAngle.superClass_.setValidator.call(this, wrappedHandler); }; /** @@ -276,16 +275,16 @@ Blockly.FieldAngle.prototype.updateGraph_ = function() { var x2 = Blockly.FieldAngle.HALF; var y2 = Blockly.FieldAngle.HALF; if (!isNaN(angleRadians)) { - var angle0 = goog.math.toRadians(Blockly.FieldAngle.OFFSET); - var x1 = Math.cos(angle0) * Blockly.FieldAngle.RADIUS; - var y1 = Math.sin(angle0) * -Blockly.FieldAngle.RADIUS; + var angle1 = goog.math.toRadians(Blockly.FieldAngle.OFFSET); + var x1 = Math.cos(angle1) * Blockly.FieldAngle.RADIUS; + var y1 = Math.sin(angle1) * -Blockly.FieldAngle.RADIUS; if (Blockly.FieldAngle.CLOCKWISE) { - angleRadians = 2 * angle0 - angleRadians; + angleRadians = 2 * angle1 - angleRadians; } x2 += Math.cos(angleRadians) * Blockly.FieldAngle.RADIUS; y2 -= Math.sin(angleRadians) * Blockly.FieldAngle.RADIUS; // Don't ask how the flag calculations work. They just do. - var largeFlag = Math.abs(Math.floor((angleRadians - angle0) / Math.PI) % 2); + var largeFlag = Math.abs(Math.floor((angleRadians - angle1) / Math.PI) % 2); if (Blockly.FieldAngle.CLOCKWISE) { largeFlag = 1 - largeFlag; } diff --git a/core/field_checkbox.js b/core/field_checkbox.js index a48a76215..545dd320f 100644 --- a/core/field_checkbox.js +++ b/core/field_checkbox.js @@ -32,17 +32,15 @@ goog.require('Blockly.Field'); /** * Class for a checkbox field. * @param {string} state The initial state of the field ('TRUE' or 'FALSE'). - * @param {Function=} opt_changeHandler A function that is executed when a new + * @param {Function=} opt_validator A function that is executed when a new * option is selected. Its sole argument is the new checkbox state. If * it returns a value, this becomes the new checkbox state, unless the * value is null, in which case the change is aborted. * @extends {Blockly.Field} * @constructor */ -Blockly.FieldCheckbox = function(state, opt_changeHandler) { - Blockly.FieldCheckbox.superClass_.constructor.call(this, ''); - - this.setChangeHandler(opt_changeHandler); +Blockly.FieldCheckbox = function(state, opt_validator) { + Blockly.FieldCheckbox.superClass_.constructor.call(this, '', opt_validator); // Set the initial state. this.setValue(state); }; @@ -100,9 +98,9 @@ Blockly.FieldCheckbox.prototype.setValue = function(strBool) { */ Blockly.FieldCheckbox.prototype.showEditor_ = function() { var newState = !this.state_; - if (this.sourceBlock_ && this.changeHandler_) { - // Call any change handler, and allow it to override. - var override = this.changeHandler_(newState); + if (this.sourceBlock_ && this.validator_) { + // Call any validation function, and allow it to override. + var override = this.validator_(newState); if (override !== undefined) { newState = override; } diff --git a/core/field_colour.js b/core/field_colour.js index b1b58c519..5ee1357ed 100644 --- a/core/field_colour.js +++ b/core/field_colour.js @@ -36,7 +36,7 @@ goog.require('goog.ui.ColorPicker'); /** * Class for a colour input field. * @param {string} colour The initial colour in '#rrggbb' format. - * @param {Function=} opt_changeHandler A function that is executed when a new + * @param {Function=} opt_validator A function that is executed when a new * colour is selected. Its sole argument is the new colour value. Its * return value becomes the selected colour, unless it is undefined, in * which case the new colour stands, or it is null, in which case the change @@ -44,10 +44,9 @@ goog.require('goog.ui.ColorPicker'); * @extends {Blockly.Field} * @constructor */ -Blockly.FieldColour = function(colour, opt_changeHandler) { - Blockly.FieldColour.superClass_.constructor.call(this, colour); +Blockly.FieldColour = function(colour, opt_validator) { + Blockly.FieldColour.superClass_.constructor.call(this, colour, opt_validator); this.setText(Blockly.Field.NBSP + Blockly.Field.NBSP + Blockly.Field.NBSP); - this.setChangeHandler(opt_changeHandler); }; goog.inherits(Blockly.FieldColour, Blockly.Field); @@ -215,9 +214,9 @@ Blockly.FieldColour.prototype.showEditor_ = function() { function(event) { var colour = event.target.getSelectedColor() || '#000000'; Blockly.WidgetDiv.hide(); - if (thisField.sourceBlock_ && thisField.changeHandler_) { - // Call any change handler, and allow it to override. - var override = thisField.changeHandler_(colour); + if (thisField.sourceBlock_ && thisField.validator_) { + // Call any validation function, and allow it to override. + var override = thisField.validator_(colour); if (override !== undefined) { colour = override; } diff --git a/core/field_date.js b/core/field_date.js index 11f47d976..e2a203a2f 100644 --- a/core/field_date.js +++ b/core/field_date.js @@ -39,7 +39,7 @@ goog.require('goog.ui.DatePicker'); /** * Class for a date input field. * @param {string} date The initial date. - * @param {Function=} opt_changeHandler A function that is executed when a new + * @param {Function=} opt_validator A function that is executed when a new * date is selected. Its sole argument is the new date value. Its * return value becomes the selected date, unless it is undefined, in * which case the new date stands, or it is null, in which case the change @@ -47,13 +47,12 @@ goog.require('goog.ui.DatePicker'); * @extends {Blockly.Field} * @constructor */ -Blockly.FieldDate = function(date, opt_changeHandler) { +Blockly.FieldDate = function(date, opt_validator) { if (!date) { date = new goog.date.Date().toIsoString(true); } - Blockly.FieldDate.superClass_.constructor.call(this, date); + Blockly.FieldDate.superClass_.constructor.call(this, date, opt_validator); this.setValue(date); - this.setChangeHandler(opt_changeHandler); }; goog.inherits(Blockly.FieldDate, Blockly.Field); @@ -83,8 +82,8 @@ Blockly.FieldDate.prototype.getValue = function() { * @param {string} date The new date. */ Blockly.FieldDate.prototype.setValue = function(date) { - if (this.sourceBlock_ && this.changeHandler_) { - var validated = this.changeHandler_(date); + if (this.sourceBlock_ && this.validator_) { + var validated = this.validator_(date); // If the new date is invalid, validation returns null. // In this case we still want to display the illegal result. if (validated !== null && validated !== undefined) { @@ -150,9 +149,9 @@ Blockly.FieldDate.prototype.showEditor_ = function() { function(event) { var date = event.date ? event.date.toIsoString(true) : ''; Blockly.WidgetDiv.hide(); - if (thisField.sourceBlock_ && thisField.changeHandler_) { - // Call any change handler, and allow it to override. - var override = thisField.changeHandler_(date); + if (thisField.sourceBlock_ && thisField.validator_) { + // Call any validation function, and allow it to override. + var override = thisField.validator_(date); if (override !== undefined) { date = override; } diff --git a/core/field_dropdown.js b/core/field_dropdown.js index 76525ca99..72b6c9867 100644 --- a/core/field_dropdown.js +++ b/core/field_dropdown.js @@ -41,7 +41,7 @@ goog.require('goog.userAgent'); * Class for an editable dropdown field. * @param {(!Array.>|!Function)} menuGenerator An array of options * for a dropdown list, or a function which generates these options. - * @param {Function=} opt_changeHandler A function that is executed when a new + * @param {Function=} opt_validator A function that is executed when a new * option is selected, with the newly selected value as its sole argument. * If it returns a value, that value (which must be one of the options) will * become selected in place of the newly selected option, unless the return @@ -49,14 +49,14 @@ goog.require('goog.userAgent'); * @extends {Blockly.Field} * @constructor */ -Blockly.FieldDropdown = function(menuGenerator, opt_changeHandler) { +Blockly.FieldDropdown = function(menuGenerator, opt_validator) { this.menuGenerator_ = menuGenerator; - this.setChangeHandler(opt_changeHandler); this.trimOptions_(); var firstTuple = this.getOptions_()[0]; // Call parent's constructor. - Blockly.FieldDropdown.superClass_.constructor.call(this, firstTuple[1]); + Blockly.FieldDropdown.superClass_.constructor.call(this, firstTuple[1], + opt_validator); }; goog.inherits(Blockly.FieldDropdown, Blockly.Field); @@ -110,9 +110,9 @@ Blockly.FieldDropdown.prototype.showEditor_ = function() { var menuItem = e.target; if (menuItem) { var value = menuItem.getValue(); - if (thisField.sourceBlock_ && thisField.changeHandler_) { - // Call any change handler, and allow it to override. - var override = thisField.changeHandler_(value); + if (thisField.sourceBlock_ && thisField.validator_) { + // Call any validation function, and allow it to override. + var override = thisField.validator_(value); if (override !== undefined) { value = override; } diff --git a/core/field_textinput.js b/core/field_textinput.js index 3891471eb..ad358348a 100644 --- a/core/field_textinput.js +++ b/core/field_textinput.js @@ -36,16 +36,16 @@ goog.require('goog.userAgent'); /** * Class for an editable text field. * @param {string} text The initial content of the field. - * @param {Function=} opt_changeHandler An optional function that is called + * @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. * @extends {Blockly.Field} * @constructor */ -Blockly.FieldTextInput = function(text, opt_changeHandler) { - Blockly.FieldTextInput.superClass_.constructor.call(this, text); - this.setChangeHandler(opt_changeHandler); +Blockly.FieldTextInput = function(text, opt_validator) { + Blockly.FieldTextInput.superClass_.constructor.call(this, text, + opt_validator); }; goog.inherits(Blockly.FieldTextInput, Blockly.Field); @@ -82,8 +82,8 @@ Blockly.FieldTextInput.prototype.setValue = function(text) { if (text === null) { return; // No change if null. } - if (this.sourceBlock_ && this.changeHandler_) { - var validated = this.changeHandler_(text); + if (this.sourceBlock_ && this.validator_) { + var validated = this.validator_(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) { @@ -114,8 +114,8 @@ Blockly.FieldTextInput.prototype.showEditor_ = function(opt_quietInput) { goog.userAgent.IPAD)) { // Mobile browsers have issues with in-line textareas (focus & keyboards). var newValue = window.prompt(Blockly.Msg.CHANGE_VALUE_TITLE, this.text_); - if (this.sourceBlock_ && this.changeHandler_) { - var override = this.changeHandler_(newValue); + if (this.sourceBlock_ && this.validator_) { + var override = this.validator_(newValue); if (override !== undefined) { newValue = override; } @@ -210,8 +210,8 @@ Blockly.FieldTextInput.prototype.validate_ = function() { var valid = true; goog.asserts.assertObject(Blockly.FieldTextInput.htmlInput_); var htmlInput = Blockly.FieldTextInput.htmlInput_; - if (this.sourceBlock_ && this.changeHandler_) { - valid = this.changeHandler_(htmlInput.value); + if (this.sourceBlock_ && this.validator_) { + valid = this.validator_(htmlInput.value); } if (valid === null) { Blockly.addClass_(htmlInput, 'blocklyInvalidInput'); @@ -264,13 +264,13 @@ Blockly.FieldTextInput.prototype.widgetDispose_ = function() { var htmlInput = Blockly.FieldTextInput.htmlInput_; // Save the edit (if it validates). var text = htmlInput.value; - if (thisField.sourceBlock_ && thisField.changeHandler_) { - var text1 = thisField.changeHandler_(text); + if (thisField.sourceBlock_ && thisField.validator_) { + var text1 = thisField.validator_(text); if (text1 === null) { // Invalid edit. text = htmlInput.defaultValue; } else if (text1 !== undefined) { - // Change handler has changed the text. + // Validation function has changed the text. text = text1; } } diff --git a/core/field_variable.js b/core/field_variable.js index f6bb2b782..3cd4c5deb 100644 --- a/core/field_variable.js +++ b/core/field_variable.js @@ -36,15 +36,14 @@ goog.require('goog.string'); * Class for a variable's dropdown field. * @param {?string} varname The default name for the variable. If null, * a unique variable name will be generated. - * @param {Function=} opt_changeHandler A function that is executed when a new + * @param {Function=} opt_validator A function that is executed when a new * option is selected. Its sole argument is the new option value. * @extends {Blockly.FieldDropdown} * @constructor */ -Blockly.FieldVariable = function(varname, opt_changeHandler) { +Blockly.FieldVariable = function(varname, opt_validator) { Blockly.FieldVariable.superClass_.constructor.call(this, - Blockly.FieldVariable.dropdownCreate, null); - this.setChangeHandler(opt_changeHandler); + Blockly.FieldVariable.dropdownCreate, opt_validator); this.setValue(varname || ''); }; goog.inherits(Blockly.FieldVariable, Blockly.FieldDropdown); @@ -53,7 +52,7 @@ goog.inherits(Blockly.FieldVariable, Blockly.FieldDropdown); * Sets a new change handler for angle field. * @param {Function} handler New change handler, or null. */ -Blockly.FieldVariable.prototype.setChangeHandler = function(handler) { +Blockly.FieldVariable.prototype.setValidator = function(handler) { var wrappedHandler; if (handler) { // Wrap the user's change handler together with the variable rename handler. @@ -75,7 +74,7 @@ Blockly.FieldVariable.prototype.setChangeHandler = function(handler) { } else { wrappedHandler = Blockly.FieldVariable.dropdownChange; } - Blockly.FieldVariable.superClass_.setChangeHandler.call(this, wrappedHandler); + Blockly.FieldVariable.superClass_.setValidator.call(this, wrappedHandler); }; /**