mirror of
https://github.com/google/blockly.git
synced 2026-01-08 01:20:12 +01:00
Changed utils.fields -> fieldRegistry. Also removed useless tooltip requires.
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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<string, !{fromJson: Function}>}
|
||||
* @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' +
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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());
|
||||
});
|
||||
@@ -48,7 +48,7 @@
|
||||
<script src="procedures_test.js"></script>
|
||||
<script src="trashcan_test.js"></script>
|
||||
<script src="utils_test.js"></script>
|
||||
<script src="utils_field_test.js"></script>
|
||||
<script src="field_registry_test.js"></script>
|
||||
<script src="xml_test.js"></script>
|
||||
|
||||
<div id="blocklyDiv"></div>
|
||||
|
||||
Reference in New Issue
Block a user