Skip to content

Commit 7dec25e

Browse files
committed
Change typeName function to member variable with accessor.
1 parent 4ecd827 commit 7dec25e

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

blockly/core/type.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@ goog.require('goog.asserts');
1616

1717
/**
1818
* Blockly Type class constructor.
19-
* @param {Object} args Object/dictionary with typeId, and compatibleTypes.
19+
* @param {Object} args Object/dictionary with typeId, typeNameMsgId, and
20+
* compatibleTypes.
2021
* @constructor
2122
*/
2223
Blockly.Type = function(args) {
23-
if ((args.typeId === undefined) || (args.typeName === undefined) ||
24+
if ((args.typeId === undefined) || (args.typeNameMsgId === undefined) ||
2425
(args.compatibleTypes === undefined)) {
2526
throw new Error('Creating a Type requires the following format:\n{\n' +
2627
' typeId: string,\n' +
27-
' typeName: function returning a string,\n' +
28+
' typeNameMsgId: Blockly.Msg string member name,\n' +
2829
' compatibleTypes: [Blockly.Type,]\n}');
2930
}
3031
if (!goog.isArray(args.compatibleTypes)) {
@@ -33,11 +34,11 @@ Blockly.Type = function(args) {
3334
}
3435
/** @type {string} */
3536
this.typeId = args.typeId;
36-
/** @type {function}
37-
This is the translatable name. As translation is only present past build,
38-
pass a function to return translated string.
39-
*/
40-
this.typeName = args.typeName;
37+
/** @type {string}
38+
* This is the translatable Blockly.Msg member string name.
39+
* @private
40+
*/
41+
this.typeNameStrId_ = args.typeNameMsgId;
4142
/**
4243
* @type {Array<Blockly.Type>}
4344
* @private
@@ -52,6 +53,16 @@ Blockly.Type = function(args) {
5253
this.generateCheckList_();
5354
};
5455

56+
/** Getter for the typeName property, used for translatable Type naming. */
57+
Object.defineProperty(Blockly.Type.prototype, 'typeName', {
58+
get: function() {
59+
return Blockly.Msg[this.typeNameStrId_] || this.typeId;
60+
},
61+
set: function(value) {
62+
console.warn('"Blockly.Type" property "typeName" is not allowed to be set.');
63+
}
64+
});
65+
5566
/** Getter for the output property, used for block output types. */
5667
Object.defineProperty(Blockly.Type.prototype, 'output', {
5768
get: function() {

blockly/core/types.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,49 +16,49 @@ goog.require('Blockly.Type');
1616
/** Single character. */
1717
Blockly.Types.CHARACTER = new Blockly.Type({
1818
typeId: 'Character',
19-
typeName: function() {return Blockly.Msg.ARD_TYPE_CHAR;},
19+
typeNameMsgId: 'ARD_TYPE_CHAR',
2020
compatibleTypes: []
2121
});
2222

2323
/** Text string. */
2424
Blockly.Types.TEXT = new Blockly.Type({
2525
typeId: 'Text',
26-
typeName: function() {return Blockly.Msg.ARD_TYPE_TEXT;},
26+
typeNameMsgId: 'ARD_TYPE_TEXT',
2727
compatibleTypes: [Blockly.Types.CHARACTER]
2828
});
2929

3030
/** Boolean. */
3131
Blockly.Types.BOOLEAN = new Blockly.Type({
3232
typeId: 'Boolean',
33-
typeName: function() {return Blockly.Msg.ARD_TYPE_BOOL;},
33+
typeNameMsgId: 'ARD_TYPE_BOOL',
3434
compatibleTypes: []
3535
});
3636

3737
/** Short integer number. */
3838
Blockly.Types.SHORT_NUMBER = new Blockly.Type({
3939
typeId: 'Short Positive Number',
40-
typeName: function() {return Blockly.Msg.ARD_TYPE_SHORTPOS;},
40+
typeNameMsgId: 'ARD_TYPE_SHORTPOS',
4141
compatibleTypes: [] // Circular dependencies, add after all declarations
4242
});
4343

4444
/** Integer number. */
4545
Blockly.Types.NUMBER = new Blockly.Type({
4646
typeId: 'Number',
47-
typeName: function() {return Blockly.Msg.ARD_TYPE_NUMBER;},
47+
typeNameMsgId: 'ARD_TYPE_NUMBER',
4848
compatibleTypes: [] // Circular dependencies, add after all declarations
4949
});
5050

5151
/** Large integer number. */
5252
Blockly.Types.LARGE_NUMBER = new Blockly.Type({
5353
typeId: 'Large Number',
54-
typeName: function() {return Blockly.Msg.ARD_TYPE_LONG;},
54+
typeNameMsgId: 'ARD_TYPE_LONG',
5555
compatibleTypes: [] // Circular dependencies, add after all declarations
5656
});
5757

5858
/** Decimal/floating point number. */
5959
Blockly.Types.DECIMAL = new Blockly.Type({
6060
typeId: 'Decimal',
61-
typeName: function() {return Blockly.Msg.ARD_TYPE_DECIMAL;},
61+
typeNameMsgId: 'ARD_TYPE_DECIMAL',
6262
compatibleTypes: [Blockly.Types.BOOLEAN,
6363
Blockly.Types.SHORT_NUMBER,
6464
Blockly.Types.NUMBER,
@@ -68,28 +68,28 @@ Blockly.Types.DECIMAL = new Blockly.Type({
6868
/** Array/List of items. */
6969
Blockly.Types.ARRAY = new Blockly.Type({
7070
typeId: 'Array',
71-
typeName: function() {return Blockly.Msg.ARD_TYPE_ARRAY},
71+
typeNameMsgId: 'ARD_TYPE_ARRAY',
7272
compatibleTypes: []
7373
});
7474

7575
/** Null indicate there is no type. */
7676
Blockly.Types.NULL = new Blockly.Type({
7777
typeId: 'Null',
78-
typeName: function() {return Blockly.Msg.ARD_TYPE_NULL;},
78+
typeNameMsgId: 'ARD_TYPE_NULL',
7979
compatibleTypes: []
8080
});
8181

8282
/** Type not defined, or not yet defined. */
8383
Blockly.Types.UNDEF = new Blockly.Type({
8484
typeId: 'Undefined',
85-
typeName: function() {return Blockly.Msg.ARD_TYPE_UNDEF;},
85+
typeNameMsgId: 'ARD_TYPE_UNDEF',
8686
compatibleTypes: []
8787
});
8888

8989
/** Set when no child block (meant to define the variable type) is connected. */
9090
Blockly.Types.CHILD_BLOCK_MISSING = new Blockly.Type({
9191
typeId: 'ChildBlockMissing',
92-
typeName: function() {return Blockly.Msg.ARD_TYPE_CHILDBLOCKMISSING;},
92+
typeNameMsgId: 'ARD_TYPE_CHILDBLOCKMISSING',
9393
compatibleTypes: []
9494
});
9595

@@ -119,19 +119,20 @@ Blockly.Types.LARGE_NUMBER.addCompatibleTypes([
119119
/**
120120
* Adds another type to the Blockly.Types collection.
121121
* @param {string} typeId_ Identifiable name of the type.
122-
* @param {string} typeName_ Descriptive name of the type for use in the UI.
122+
* @param {string} typeNameMsgId_ Name of the member variable from Blockly.Msg
123+
* object to identify the translateble string.for the Type name.
123124
* @param {Array<Blockly.Type>} compatibleTypes_ List of types this Type is
124125
* compatible with.
125126
*/
126-
Blockly.Types.addType = function(typeId_, typeName_, compatibleTypes_) {
127+
Blockly.Types.addType = function(typeId_, typeNameMsgId_, compatibleTypes_) {
127128
// The Id is used as the key from the value pair in the BlocklyTypes object
128129
var key = typeId_.toUpperCase().replace(/ /g, '_');
129130
if (Blockly.Types[key] !== undefined) {
130131
throw 'The Blockly type ' + key + ' already exists.';
131132
}
132133
Blockly.Types[key] = new Blockly.Type({
133134
typeId: typeId_,
134-
typeName: function() {return typeName_;},
135+
typeName: typeNameMsgId_,
135136
compatibleTypes: compatibleTypes_
136137
});
137138
};
@@ -148,7 +149,7 @@ Blockly.Types.getValidTypeArray = function() {
148149
(typeKey !== 'NULL') && (typeKey !== 'ARRAY') &&
149150
(typeof Blockly.Types[typeKey] !== 'function') &&
150151
!(Blockly.Types[typeKey] instanceof RegExp)) {
151-
typesArray.push([Blockly.Types[typeKey].typeName(), typeKey]);
152+
typesArray.push([Blockly.Types[typeKey].typeName, typeKey]);
152153
}
153154
}
154155
return typesArray;

0 commit comments

Comments
 (0)