Skip to content

Commit 294b5c7

Browse files
committed
Extract common code in prop-types
1 parent 100b8b9 commit 294b5c7

File tree

1 file changed

+35
-42
lines changed

1 file changed

+35
-42
lines changed

js/scripts/prop-types.js

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,31 @@ _.extend(BaseType.prototype, {
2929
},
3030
})
3131

32+
function genInstanceTraitlet(typeName, nullable, args, kwargs) {
33+
var nullableStr = nullable ? 'True' : 'False';
34+
// allow type unions
35+
if (typeName instanceof Array) {
36+
var instances = typeName.map(function(tname) {
37+
return ` Instance(${tname}, allow_none=${nullableStr})`;
38+
});
39+
return 'Union([\n' + instances.join(',\n') + '\n ]).tag(sync=True, **widget_serialization)';
40+
}
41+
42+
if (typeName.toLowerCase() === 'this') {
43+
return 'This().tag(sync=True, **widget_serialization)';
44+
}
45+
46+
var ret = `Instance(${typeName}`;
47+
if (args !== undefined) {
48+
ret += `, args=${args}`;
49+
}
50+
if (kwargs !== undefined) {
51+
ret += `, kw=${kwargs}`;
52+
}
53+
ret += `, allow_none=${nullableStr}).tag(sync=True, **widget_serialization)`;
54+
return ret;
55+
}
56+
3257
function ThreeType(typeName, options={}) {
3358
this.typeName = typeName || '';
3459
this.defaultValue = null;
@@ -39,28 +64,16 @@ function ThreeType(typeName, options={}) {
3964
}
4065
_.extend(ThreeType.prototype, BaseType.prototype, {
4166
getTraitlet: function() {
42-
var nullableStr = this.nullable ? 'True' : 'False';
43-
// allow type unions
44-
if (this.typeName instanceof Array) {
45-
var instances = this.typeName.map(function(typeName) {
46-
return ` Instance(${typeName || 'ThreeWidget'}, allow_none=${nullableStr})`;
67+
var typeName = this.typeName;
68+
if (typeName instanceof Array) {
69+
typeName = _.each(typeName, function(tname) {
70+
return `${tname || 'ThreeWidget'}`;
4771
});
48-
return 'Union([\n' + instances.join(',\n') + '\n ]).tag(sync=True, **widget_serialization)';
49-
}
50-
51-
if (this.typeName.toLowerCase() === 'this') {
52-
return 'This().tag(sync=True, **widget_serialization)';
53-
}
54-
55-
var ret = `Instance(${this.typeName || 'ThreeWidget'}`;
56-
if (this.args !== undefined) {
57-
ret += `, args=${this.args}`;
58-
}
59-
if (this.kwargs !== undefined) {
60-
ret += `, kw=${this.kwargs}`;
72+
} else {
73+
typeName = `${typeName || 'ThreeWidget'}`;
6174
}
62-
ret += `, allow_none=${nullableStr}).tag(sync=True, **widget_serialization)`;
63-
return ret;
75+
return genInstanceTraitlet(
76+
typeName, this.nullable, this.args, this.kwargs);
6477
},
6578
getPropArrayName: function() {
6679
return 'three_properties';
@@ -79,28 +92,8 @@ _.extend(ForwardDeclaredThreeType.prototype, ThreeType.prototype, {
7992
return this.modulePath + '.' + this.typeName;
8093
},
8194
getTraitlet: function() {
82-
var nullableStr = this.nullable ? 'True' : 'False';
83-
// allow type unions
84-
if (this.typeName instanceof Array) {
85-
var instances = this.typeName.map(function(typeName) {
86-
return ` Instance('${this.forwardType()}', allow_none=${nullableStr})`;
87-
});
88-
return 'Union([\n' + instances.join(',\n') + '\n ]).tag(sync=True, **widget_serialization)';
89-
}
90-
91-
if (this.typeName.toLowerCase() === 'this') {
92-
return 'This().tag(sync=True, **widget_serialization)';
93-
}
94-
95-
var ret = `Instance('${this.forwardType()}'`;
96-
if (this.args !== undefined) {
97-
ret += `, args=${this.args}`;
98-
}
99-
if (this.kwargs !== undefined) {
100-
ret += `, kw=${this.kwargs}`;
101-
}
102-
ret += `, allow_none=${nullableStr}).tag(sync=True, **widget_serialization)`;
103-
return ret;
95+
return genInstanceTraitlet(
96+
this.forwardType(), this.nullable, this.args, this.kwargs);
10497
},
10598
});
10699

0 commit comments

Comments
 (0)