55 */
66angular . module ( 'schemaForm' ) . provider ( 'schemaForm' , [ 'sfPathProvider' , function ( sfPathProvider ) {
77
8+ //Creates an default titleMap list from an enum, i.e. a list of strings.
9+ var enumToTitleMap = function ( enm ) {
10+ var titleMap = [ ] ; //canonical titleMap format is a list.
11+ enm . forEach ( function ( name ) {
12+ titleMap . push ( { name : name , value : name } ) ;
13+ } ) ;
14+ return titleMap ;
15+ } ;
16+
17+ // Takes a titleMap in either object or list format and returns one in
18+ // in the list format.
19+ var canonicalTitleMap = function ( titleMap ) {
20+ if ( ! angular . isArray ( titleMap ) ) {
21+ var canonical = [ ] ;
22+ angular . forEach ( titleMap , function ( name , value ) {
23+ canonical . push ( { name : name , value : value } ) ;
24+ } ) ;
25+ return canonical ;
26+ }
27+ return titleMap ;
28+ } ;
29+
830 var defaultFormDefinition = function ( name , schema , options ) {
931 var rules = defaults [ schema . type ] ;
1032 if ( rules ) {
@@ -34,7 +56,7 @@ angular.module('schemaForm').provider('schemaForm',['sfPathProvider', function(s
3456
3557 //Non standard attributes
3658 if ( schema . validationMessage ) f . validationMessage = schema . validationMessage ;
37- if ( schema . enumNames ) f . titleMap = schema . enumNames ;
59+ if ( schema . enumNames ) f . titleMap = canonicalTitleMap ( schema . enumNames ) ;
3860 f . schema = schema ;
3961 return f ;
4062 } ;
@@ -89,10 +111,7 @@ angular.module('schemaForm').provider('schemaForm',['sfPathProvider', function(s
89111 f . key = options . path ;
90112 f . type = 'select' ;
91113 if ( ! f . titleMap ) {
92- f . titleMap = { } ;
93- schema . enum . forEach ( function ( name ) {
94- f . titleMap [ name ] = name ;
95- } ) ;
114+ f . titleMap = enumToTitleMap ( schema . enum ) ;
96115 }
97116 options . lookup [ sfPathProvider . stringify ( options . path ) ] = f ;
98117 return f ;
@@ -105,10 +124,7 @@ angular.module('schemaForm').provider('schemaForm',['sfPathProvider', function(s
105124 f . key = options . path ;
106125 f . type = 'checkboxes' ;
107126 if ( ! f . titleMap ) {
108- f . titleMap = { } ;
109- schema . items . enum . forEach ( function ( name ) {
110- f . titleMap [ name ] = name ;
111- } ) ;
127+ f . titleMap = enumToTitleMap ( schema . items . enum ) ;
112128 }
113129 options . lookup [ sfPathProvider . stringify ( options . path ) ] = f ;
114130 return f ;
@@ -254,7 +270,6 @@ angular.module('schemaForm').provider('schemaForm',['sfPathProvider', function(s
254270 form = form || [ "*" ] ;
255271
256272 var stdForm = service . defaults ( schema , ignore ) ;
257-
258273 //simple case, we have a "*", just put the stdForm there
259274 var idx = form . indexOf ( "*" ) ;
260275 if ( idx !== - 1 ) {
@@ -276,6 +291,11 @@ angular.module('schemaForm').provider('schemaForm',['sfPathProvider', function(s
276291 obj = { key : obj } ;
277292 }
278293
294+ //If it has a titleMap make sure it's a list
295+ if ( obj . titleMap ) {
296+ obj . titleMap = canonicalTitleMap ( obj . titleMap ) ;
297+ }
298+
279299 //if it's a type with items, merge 'em!
280300 if ( obj . items ) {
281301 obj . items = service . merge ( schema , obj . items , ignore ) ;
@@ -293,6 +313,7 @@ angular.module('schemaForm').provider('schemaForm',['sfPathProvider', function(s
293313 if ( typeof obj . key == 'string' ) {
294314 obj . key = sfPathProvider . parse ( obj . key ) ;
295315 }
316+
296317 var str = sfPathProvider . stringify ( obj . key ) ;
297318 if ( lookup [ str ] ) {
298319 return angular . extend ( lookup [ str ] , obj ) ;
0 commit comments