From 0872d022e1a48b4aacfbb7295bcad0c184e3922d Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Thu, 8 Aug 2019 19:42:16 -0700 Subject: [PATCH] Changed utils.fields -> fieldRegistry. Also removed useless tooltip requires. --- core/block.js | 4 +- core/field_angle.js | 4 +- core/field_checkbox.js | 4 +- core/field_colour.js | 4 +- core/field_date.js | 4 +- core/field_dropdown.js | 4 +- core/field_image.js | 5 +-- core/field_label.js | 5 +-- core/field_label_serializable.js | 4 +- core/field_number.js | 4 +- core/{utils/field.js => field_registry.js} | 20 +++++----- core/field_textinput.js | 4 +- core/field_variable.js | 4 +- ...s_field_test.js => field_registry_test.js} | 38 +++++++++---------- tests/mocha/index.html | 2 +- 15 files changed, 53 insertions(+), 57 deletions(-) rename core/{utils/field.js => field_registry.js} (83%) rename tests/mocha/{utils_field_test.js => field_registry_test.js} (71%) diff --git a/core/block.js b/core/block.js index 43fe84f8d..d63612aaf 100644 --- a/core/block.js +++ b/core/block.js @@ -39,7 +39,7 @@ goog.require('Blockly.Input'); goog.require('Blockly.Mutator'); goog.require('Blockly.utils'); goog.require('Blockly.utils.Coordinate'); -goog.require('Blockly.utils.fields'); +goog.require('Blockly.fieldRegistry'); goog.require('Blockly.utils.string'); goog.require('Blockly.Warning'); goog.require('Blockly.Workspace'); @@ -1637,7 +1637,7 @@ Blockly.Block.prototype.interpolate_ = function(message, args, lastDummyAlign) { default: // This should handle all field JSON parsing, including // options that can be applied to any field type. - field = Blockly.utils.fields.fromJson(element); + field = Blockly.fieldRegistry.fromJson(element); // Unknown field. if (!field) { diff --git a/core/field_angle.js b/core/field_angle.js index aeb91e536..f1d54af76 100644 --- a/core/field_angle.js +++ b/core/field_angle.js @@ -27,9 +27,9 @@ goog.provide('Blockly.FieldAngle'); goog.require('Blockly.DropDownDiv'); +goog.require('Blockly.fieldRegistry'); goog.require('Blockly.FieldTextInput'); goog.require('Blockly.utils.dom'); -goog.require('Blockly.utils.fields'); goog.require('Blockly.utils.math'); goog.require('Blockly.utils.userAgent'); @@ -354,4 +354,4 @@ Blockly.FieldAngle.prototype.doClassValidation_ = function(opt_newValue) { return n; }; -Blockly.utils.fields.register('field_angle', Blockly.FieldAngle); +Blockly.fieldRegistry.register('field_angle', Blockly.FieldAngle); diff --git a/core/field_checkbox.js b/core/field_checkbox.js index b8f9f1d82..7f4ec4caf 100644 --- a/core/field_checkbox.js +++ b/core/field_checkbox.js @@ -29,8 +29,8 @@ goog.provide('Blockly.FieldCheckbox'); goog.require('Blockly.Events'); goog.require('Blockly.Events.BlockChange'); goog.require('Blockly.Field'); +goog.require('Blockly.fieldRegistry'); goog.require('Blockly.utils.dom'); -goog.require('Blockly.utils.fields'); goog.require('Blockly.utils.Size'); @@ -211,4 +211,4 @@ Blockly.FieldCheckbox.prototype.convertValueToBool_ = function(value) { } }; -Blockly.utils.fields.register('field_checkbox', Blockly.FieldCheckbox); +Blockly.fieldRegistry.register('field_checkbox', Blockly.FieldCheckbox); diff --git a/core/field_colour.js b/core/field_colour.js index 13eff115e..059fcb756 100644 --- a/core/field_colour.js +++ b/core/field_colour.js @@ -30,8 +30,8 @@ goog.require('Blockly.DropDownDiv'); goog.require('Blockly.Events'); goog.require('Blockly.Events.BlockChange'); goog.require('Blockly.Field'); +goog.require('Blockly.fieldRegistry'); goog.require('Blockly.utils.colour'); -goog.require('Blockly.utils.fields'); goog.require('Blockly.utils.Size'); @@ -347,4 +347,4 @@ Blockly.FieldColour.prototype.dropdownDispose_ = function() { Blockly.unbindEvent_(this.onUpWrapper_); }; -Blockly.utils.fields.register('field_colour', Blockly.FieldColour); +Blockly.fieldRegistry.register('field_colour', Blockly.FieldColour); diff --git a/core/field_date.js b/core/field_date.js index 9b2ab30f4..3ab5d0a30 100644 --- a/core/field_date.js +++ b/core/field_date.js @@ -28,8 +28,8 @@ goog.provide('Blockly.FieldDate'); goog.require('Blockly.Events'); goog.require('Blockly.Field'); +goog.require('Blockly.fieldRegistry'); goog.require('Blockly.utils.dom'); -goog.require('Blockly.utils.fields'); goog.require('Blockly.utils.string'); goog.require('goog.date'); @@ -324,4 +324,4 @@ Blockly.FieldDate.CSS = [ '}' ]; -Blockly.utils.fields.register('field_date', Blockly.FieldDate); +Blockly.fieldRegistry.register('field_date', Blockly.FieldDate); diff --git a/core/field_dropdown.js b/core/field_dropdown.js index 989523b85..70c00bd08 100644 --- a/core/field_dropdown.js +++ b/core/field_dropdown.js @@ -31,9 +31,9 @@ goog.provide('Blockly.FieldDropdown'); goog.require('Blockly.Events'); goog.require('Blockly.Events.BlockChange'); goog.require('Blockly.Field'); +goog.require('Blockly.fieldRegistry'); goog.require('Blockly.utils'); goog.require('Blockly.utils.dom'); -goog.require('Blockly.utils.fields'); goog.require('Blockly.utils.Size'); goog.require('Blockly.utils.string'); goog.require('Blockly.utils.uiMenu'); @@ -566,4 +566,4 @@ Blockly.FieldDropdown.validateOptions_ = function(options) { } }; -Blockly.utils.fields.register('field_dropdown', Blockly.FieldDropdown); +Blockly.fieldRegistry.register('field_dropdown', Blockly.FieldDropdown); diff --git a/core/field_image.js b/core/field_image.js index 37dbf5765..22685f560 100644 --- a/core/field_image.js +++ b/core/field_image.js @@ -27,10 +27,9 @@ goog.provide('Blockly.FieldImage'); goog.require('Blockly.Field'); -goog.require('Blockly.Tooltip'); +goog.require('Blockly.fieldRegistry'); goog.require('Blockly.utils'); goog.require('Blockly.utils.dom'); -goog.require('Blockly.utils.fields'); goog.require('Blockly.utils.Size'); @@ -223,4 +222,4 @@ Blockly.FieldImage.prototype.setOnClickHandler = function(func) { this.clickHandler_ = func; }; -Blockly.utils.fields.register('field_image', Blockly.FieldImage); +Blockly.fieldRegistry.register('field_image', Blockly.FieldImage); diff --git a/core/field_label.js b/core/field_label.js index 8134feb09..c89b574d0 100644 --- a/core/field_label.js +++ b/core/field_label.js @@ -28,10 +28,9 @@ goog.provide('Blockly.FieldLabel'); goog.require('Blockly.Field'); -goog.require('Blockly.Tooltip'); +goog.require('Blockly.fieldRegistry'); goog.require('Blockly.utils'); goog.require('Blockly.utils.dom'); -goog.require('Blockly.utils.fields'); goog.require('Blockly.utils.Size'); @@ -101,4 +100,4 @@ Blockly.FieldLabel.prototype.doClassValidation_ = function(opt_newValue) { return String(opt_newValue); }; -Blockly.utils.fields.register('field_label', Blockly.FieldLabel); +Blockly.fieldRegistry.register('field_label', Blockly.FieldLabel); diff --git a/core/field_label_serializable.js b/core/field_label_serializable.js index 38f8ddb05..f6fa0ee33 100644 --- a/core/field_label_serializable.js +++ b/core/field_label_serializable.js @@ -28,8 +28,8 @@ goog.provide('Blockly.FieldLabelSerializable'); goog.require('Blockly.FieldLabel'); +goog.require('Blockly.fieldRegistry'); goog.require('Blockly.utils'); -goog.require('Blockly.utils.fields'); /** @@ -76,5 +76,5 @@ Blockly.FieldLabelSerializable.prototype.EDITABLE = false; */ Blockly.FieldLabelSerializable.prototype.SERIALIZABLE = true; -Blockly.utils.fields.register( +Blockly.fieldRegistry.register( 'field_label_serializable', Blockly.FieldLabelSerializable); diff --git a/core/field_number.js b/core/field_number.js index 347ae60d7..496ee66f6 100644 --- a/core/field_number.js +++ b/core/field_number.js @@ -26,8 +26,8 @@ goog.provide('Blockly.FieldNumber'); +goog.require('Blockly.fieldRegistry'); goog.require('Blockly.FieldTextInput'); -goog.require('Blockly.utils.fields'); /** @@ -139,4 +139,4 @@ Blockly.FieldNumber.prototype.doClassValidation_ = function(opt_newValue) { return n; }; -Blockly.utils.fields.register('field_number', Blockly.FieldNumber); +Blockly.fieldRegistry.register('field_number', Blockly.FieldNumber); diff --git a/core/utils/field.js b/core/field_registry.js similarity index 83% rename from core/utils/field.js rename to core/field_registry.js index 531fad10c..15a66e042 100644 --- a/core/utils/field.js +++ b/core/field_registry.js @@ -18,8 +18,6 @@ * limitations under the License. */ -// TODO: Files in this directory seem to not be specific to Blockly, yet -// this one is. Should it be moved to the core? /** * @fileoverview Utility methods for handling fields. * @author bekawestberg@gmail.com (Beka Westberg) @@ -27,10 +25,10 @@ 'use strict'; /** - * @name Blockly.utils + * @name Blockly.fieldRegistry * @namespace */ -goog.provide('Blockly.utils.fields'); +goog.provide('Blockly.fieldRegistry'); /** @@ -39,11 +37,11 @@ goog.provide('Blockly.utils.fields'); * @type {!Object} * @private */ -Blockly.utils.fields.typeMap_ = {}; +Blockly.fieldRegistry.typeMap_ = {}; /** * Registers a field type. May also override an existing field type. - * Blockly.utils.fields.fromJson uses this registry to + * Blockly.fieldRegistry.fromJson uses this registry to * find the appropriate field type. * @param {string} type The field type name as used in the JSON definition. * @param {!{fromJson: Function}} fieldClass The field class containing a @@ -51,7 +49,7 @@ Blockly.utils.fields.typeMap_ = {}; * @throws {Error} if the type name is empty, or the fieldClass is not an * object containing a fromJson function. */ -Blockly.utils.fields.register = function(type, fieldClass) { +Blockly.fieldRegistry.register = function(type, fieldClass) { if ((typeof type != 'string') || (type.trim() == '')) { throw Error('Invalid field type "' + type + '". The type must be a' + ' non-empty string.'); @@ -59,21 +57,21 @@ Blockly.utils.fields.register = function(type, fieldClass) { if (!fieldClass || (typeof fieldClass.fromJson != 'function')) { throw Error('Field "' + fieldClass + '" must have a fromJson function'); } - Blockly.utils.fields.typeMap_[type] = fieldClass; + Blockly.fieldRegistry.typeMap_[type] = fieldClass; }; /** * Construct a Field from a JSON arg object. * Finds the appropriate registered field by the type name as registered using - * Blockly.utils.fields.register. + * Blockly.fieldRegistry.register. * @param {!Object} options A JSON object with a type and options specific * to the field type. * @return {Blockly.Field} The new field instance or null if a field wasn't * found with the given type name * @package */ -Blockly.utils.fields.fromJson = function(options) { - var fieldClass = Blockly.utils.fields.typeMap_[options['type']]; +Blockly.fieldRegistry.fromJson = function(options) { + var fieldClass = Blockly.fieldRegistry.typeMap_[options['type']]; if (!fieldClass) { console.warn('Blockly could not create a field of type ' + options['type'] + '. The field is probably not being registered. This may be because the' + diff --git a/core/field_textinput.js b/core/field_textinput.js index f9fa93f25..a8f9b6f3b 100644 --- a/core/field_textinput.js +++ b/core/field_textinput.js @@ -29,12 +29,12 @@ goog.provide('Blockly.FieldTextInput'); goog.require('Blockly.Events'); goog.require('Blockly.Events.BlockChange'); goog.require('Blockly.Field'); +goog.require('Blockly.fieldRegistry'); goog.require('Blockly.Msg'); goog.require('Blockly.utils'); goog.require('Blockly.utils.aria'); goog.require('Blockly.utils.Coordinate'); goog.require('Blockly.utils.dom'); -goog.require('Blockly.utils.fields'); goog.require('Blockly.utils.Size'); goog.require('Blockly.utils.userAgent'); @@ -449,4 +449,4 @@ Blockly.FieldTextInput.nonnegativeIntegerValidator = function(text) { return n; }; -Blockly.utils.fields.register('field_input', Blockly.FieldTextInput); +Blockly.fieldRegistry.register('field_input', Blockly.FieldTextInput); diff --git a/core/field_variable.js b/core/field_variable.js index c4af8fb0d..853460e25 100644 --- a/core/field_variable.js +++ b/core/field_variable.js @@ -29,9 +29,9 @@ goog.provide('Blockly.FieldVariable'); goog.require('Blockly.Events'); goog.require('Blockly.Events.BlockChange'); goog.require('Blockly.FieldDropdown'); +goog.require('Blockly.fieldRegistry'); goog.require('Blockly.Msg'); goog.require('Blockly.utils'); -goog.require('Blockly.utils.fields'); goog.require('Blockly.utils.Size'); goog.require('Blockly.VariableModel'); goog.require('Blockly.Variables'); @@ -431,4 +431,4 @@ Blockly.FieldVariable.prototype.referencesVariables = function() { return true; }; -Blockly.utils.fields.register('field_variable', Blockly.FieldVariable); +Blockly.fieldRegistry.register('field_variable', Blockly.FieldVariable); diff --git a/tests/mocha/utils_field_test.js b/tests/mocha/field_registry_test.js similarity index 71% rename from tests/mocha/utils_field_test.js rename to tests/mocha/field_registry_test.js index 367ee7579..0d51b83f8 100644 --- a/tests/mocha/utils_field_test.js +++ b/tests/mocha/field_registry_test.js @@ -19,12 +19,12 @@ */ /** - * @fileoverview Tests for Blockly.utils.fields - * @author bekawestberg@gmail.com (Beka WEstberg) + * @fileoverview Tests for Blockly.fieldRegistry + * @author bekawestberg@gmail.com (Beka Westberg) */ 'use strict'; -suite('Field Utils', function() { +suite('Field Registry', function() { function CustomFieldType(value) { CustomFieldType.superClass_.constructor.call(this, value); } @@ -34,45 +34,45 @@ suite('Field Utils', function() { }; teardown(function() { - if (Blockly.utils.fields.typeMap_['field_custom_test']) { - delete Blockly.utils.fields.typeMap_['field_custom_test']; + if (Blockly.fieldRegistry.typeMap_['field_custom_test']) { + delete Blockly.fieldRegistry.typeMap_['field_custom_test']; } }); suite('Registration', function() { test('Simple', function() { - Blockly.utils.fields.register('field_custom_test', CustomFieldType); + Blockly.fieldRegistry.register('field_custom_test', CustomFieldType); }); test('Empty String Key', function() { chai.assert.throws(function() { - Blockly.utils.fields.register('', CustomFieldType); + Blockly.fieldRegistry.register('', CustomFieldType); }.bind(this), 'Invalid field type'); }); test('Class as Key', function() { chai.assert.throws(function() { - Blockly.utils.fields.register(CustomFieldType, ''); + Blockly.fieldRegistry.register(CustomFieldType, ''); }.bind(this), 'Invalid field type'); }); test('fromJson as Key', function() { chai.assert.throws(function() { - Blockly.utils.fields.register(CustomFieldType.fromJson, ''); + Blockly.fieldRegistry.register(CustomFieldType.fromJson, ''); }.bind(this), 'Invalid field type'); }); // TODO (#2788): What do you want it to do if you overwrite a key? test('Overwrite a Key', function() { - Blockly.utils.fields.register('field_custom_test', CustomFieldType); + Blockly.fieldRegistry.register('field_custom_test', CustomFieldType); - Blockly.utils.fields.register('field_custom_test', CustomFieldType); + Blockly.fieldRegistry.register('field_custom_test', CustomFieldType); }); test('Null Value', function() { chai.assert.throws(function() { - Blockly.utils.fields.register('field_custom_test', null); + Blockly.fieldRegistry.register('field_custom_test', null); }.bind(this), 'fromJson function'); }); test('No fromJson', function() { var fromJson = CustomFieldType.fromJson; delete CustomFieldType.fromJson; chai.assert.throws(function() { - Blockly.utils.fields.register('field_custom_test', CustomFieldType); + Blockly.fieldRegistry.register('field_custom_test', CustomFieldType); }.bind(this), 'fromJson function'); CustomFieldType.fromJson = fromJson; }); @@ -80,21 +80,21 @@ suite('Field Utils', function() { var fromJson = CustomFieldType.fromJson; CustomFieldType.fromJson = true; chai.assert.throws(function() { - Blockly.utils.fields.register('field_custom_test', CustomFieldType); + Blockly.fieldRegistry.register('field_custom_test', CustomFieldType); }.bind(this), 'fromJson function'); CustomFieldType.fromJson = fromJson; }); }); suite('Retrieval', function() { test('Simple', function() { - Blockly.utils.fields.register('field_custom_test', CustomFieldType); + Blockly.fieldRegistry.register('field_custom_test', CustomFieldType); var json = { type: 'field_custom_test', value: 'ok' }; - var field = Blockly.utils.fields.fromJson(json); + var field = Blockly.fieldRegistry.fromJson(json); chai.assert.isNotNull(field); chai.assert.equal('ok', field.getValue()); }); @@ -105,21 +105,21 @@ suite('Field Utils', function() { }; var spy = sinon.stub(console, 'warn'); - var field = Blockly.utils.fields.fromJson(json); + var field = Blockly.fieldRegistry.fromJson(json); chai.assert.isNull(field); chai.assert.isTrue(spy.called); spy.restore(); }); // TODO: Is this supposed to be case sensitive? test.skip('Case Different', function() { - Blockly.utils.fields.register('field_custom_test', CustomFieldType); + Blockly.fieldRegistry.register('field_custom_test', CustomFieldType); var json = { type: 'FIELD_CUSTOM_TEST', value: 'ok' }; - var field = Blockly.utils.fields.fromJson(json); + var field = Blockly.fieldRegistry.fromJson(json); chai.assert.isNotNull(field); chai.assert.equal('ok', field.getValue()); }); diff --git a/tests/mocha/index.html b/tests/mocha/index.html index 26a5ce34d..cbceb33ca 100644 --- a/tests/mocha/index.html +++ b/tests/mocha/index.html @@ -48,7 +48,7 @@ - +