@@ -111,6 +111,14 @@ module fng.services {
111
111
112
112
function handleFieldType ( formInstructions , mongooseType , mongooseOptions , $scope , ctrlState ) {
113
113
114
+ function performLookupSelect ( ) {
115
+ formInstructions . options = recordHandler . suffixCleanId ( formInstructions , 'Options' ) ;
116
+ formInstructions . ids = recordHandler . suffixCleanId ( formInstructions , '_ids' ) ;
117
+ if ( ! formInstructions . hidden ) {
118
+ recordHandler . setUpSelectOptions ( mongooseOptions . ref , formInstructions , $scope , ctrlState , handleSchema ) ;
119
+ } ;
120
+ }
121
+
114
122
var select2ajaxName ;
115
123
if ( mongooseType . caster ) {
116
124
formInstructions . array = true ;
@@ -232,111 +240,106 @@ module fng.services {
232
240
delete formInstructions . link ;
233
241
} else {
234
242
formInstructions . type = 'select' ;
235
-
236
243
// Support both of the syntaxes below
237
244
// team : [ { type: Schema.Types.ObjectId , ref: 'f_nested_schema', form: {select2: {fngAjax: true}}} ],
238
245
// team2: { type:[Schema.Types.ObjectId], ref: 'f_nested_schema', form: {select2: {fngAjax: true}}},
239
- if ( formInstructions . select2 || ( mongooseOptions . form && mongooseOptions . form . select2 ) ) {
240
- if ( ! formInstructions . select2 ) {
241
- formInstructions . select2 = mongooseOptions . form . select2 ;
242
- }
243
- if ( formInstructions . select2 === true ) {
244
- formInstructions . select2 = { } ;
245
- }
246
- $scope . select2List . push ( formInstructions . name ) ;
247
- $scope . conversions [ formInstructions . name ] = formInstructions . select2 ;
248
- if ( formInstructions . select2 . fngAjax ) {
249
- // create the instructions for select2
250
- select2ajaxName = 'ajax' + formInstructions . name . replace ( / \. / g, '' ) ;
251
- // If not required then generate a place holder if none specified (see https://github.com/forms-angular/forms-angular/issues/53)
252
- if ( ! mongooseOptions . required && ! formInstructions . placeHolder ) {
253
- formInstructions . placeHolder = 'Select...' ;
246
+ if ( formInstructions . select2 || ( mongooseOptions . form && mongooseOptions . form . select2 ) ) {
247
+ if ( ! formInstructions . select2 ) {
248
+ formInstructions . select2 = mongooseOptions . form . select2 ;
254
249
}
255
- $scope [ select2ajaxName ] = {
256
- allowClear : ! mongooseOptions . required ,
257
- minimumInputLength : 2 ,
258
- initSelection : function ( element , callback ) {
259
- var theId = element . val ( ) ;
260
- if ( theId && theId !== '' ) {
261
- SubmissionsService . getListAttributes ( mongooseOptions . ref , theId )
262
- . success ( function ( data ) {
263
- if ( data . success === false ) {
264
- $location . path ( '/404' ) ;
265
- }
266
- var display = { id : theId , text : data . list } ;
267
- recordHandler . preservePristine ( element , function ( ) {
268
- callback ( display ) ;
269
- } ) ;
270
- } ) . error ( $scope . handleHttpError ) ;
271
- // } else {
272
- // throw new Error('select2 initSelection called without a value');
273
- }
274
- } ,
275
- ajax : {
276
- url : '/api/search/' + mongooseOptions . ref ,
277
- data : function ( term , page ) { // page is the one-based page number tracked by Select2
278
- var queryList = {
279
- q : term , //search term
280
- pageLimit : 10 , // page size
281
- page : page // page number
282
- } ;
283
- var queryListExtension ;
284
- if ( typeof mongooseOptions . form . searchQuery === 'object' ) {
285
- queryListExtension = mongooseOptions . form . searchQuery ;
286
- } else if ( typeof mongooseOptions . form . searchQuery === 'function' ) {
287
- queryListExtension = mongooseOptions . form . searchQuery ( $scope , mongooseOptions ) ;
288
- }
289
- if ( queryListExtension ) {
290
- _ . extend ( queryList , queryListExtension ) ;
291
- }
292
- return queryList ;
293
- } ,
294
- results : function ( data ) {
295
- return { results : data . results , more : data . moreCount > 0 } ;
296
- }
297
- }
298
- } ;
299
- _ . extend ( $scope [ select2ajaxName ] , formInstructions . select2 ) ;
300
- formInstructions . select2 . fngAjax = select2ajaxName ;
301
- } else {
302
250
if ( formInstructions . select2 === true ) {
303
251
formInstructions . select2 = { } ;
304
252
}
305
- formInstructions . select2 . s2query = 'select2' + formInstructions . name . replace ( / \. / g, '_' ) ;
306
- $scope [ 'select2' + formInstructions . select2 . s2query ] = {
307
- allowClear : ! mongooseOptions . required ,
308
- initSelection : function ( element , callback ) {
309
- var myId = element . val ( ) ;
310
- if ( myId !== '' && $scope [ formInstructions . ids ] . length > 0 ) {
311
- var myVal = recordHandler . convertIdToListValue ( myId , $scope [ formInstructions . ids ] , $scope [ formInstructions . options ] , formInstructions . name ) ;
312
- var display = { id : myId , text : myVal } ;
313
- callback ( display ) ;
314
- }
315
- } ,
316
- query : function ( query ) {
317
- var data = { results : [ ] } ,
318
- searchString = query . term . toUpperCase ( ) ;
319
- for ( var i = 0 ; i < $scope [ formInstructions . options ] . length ; i ++ ) {
320
- if ( $scope [ formInstructions . options ] [ i ] . toUpperCase ( ) . indexOf ( searchString ) !== - 1 ) {
321
- data . results . push ( {
322
- id : $scope [ formInstructions . ids ] [ i ] ,
323
- text : $scope [ formInstructions . options ] [ i ]
324
- } ) ;
253
+ $scope . select2List . push ( formInstructions . name ) ;
254
+ $scope . conversions [ formInstructions . name ] = formInstructions . select2 ;
255
+ if ( formInstructions . select2 . fngAjax ) {
256
+ // create the instructions for select2
257
+ select2ajaxName = 'ajax' + formInstructions . name . replace ( / \. / g, '' ) ;
258
+ // If not required then generate a place holder if none specified (see https://github.com/forms-angular/forms-angular/issues/53)
259
+ if ( ! mongooseOptions . required && ! formInstructions . placeHolder ) {
260
+ formInstructions . placeHolder = 'Select...' ;
261
+ }
262
+ $scope [ select2ajaxName ] = {
263
+ allowClear : ! mongooseOptions . required ,
264
+ minimumInputLength : 2 ,
265
+ initSelection : function ( element , callback ) {
266
+ var theId = element . val ( ) ;
267
+ if ( theId && theId !== '' ) {
268
+ SubmissionsService . getListAttributes ( mongooseOptions . ref , theId )
269
+ . success ( function ( data ) {
270
+ if ( data . success === false ) {
271
+ $location . path ( '/404' ) ;
272
+ }
273
+ var display = { id : theId , text : data . list } ;
274
+ recordHandler . preservePristine ( element , function ( ) {
275
+ callback ( display ) ;
276
+ } ) ;
277
+ } ) . error ( $scope . handleHttpError ) ;
278
+ // } else {
279
+ // throw new Error('select2 initSelection called without a value');
280
+ }
281
+ } ,
282
+ ajax : {
283
+ url : '/api/search/' + mongooseOptions . ref ,
284
+ data : function ( term , page ) { // page is the one-based page number tracked by Select2
285
+ var queryList = {
286
+ q : term , //search term
287
+ pageLimit : 10 , // page size
288
+ page : page // page number
289
+ } ;
290
+ var queryListExtension ;
291
+ if ( typeof mongooseOptions . form . searchQuery === 'object' ) {
292
+ queryListExtension = mongooseOptions . form . searchQuery ;
293
+ } else if ( typeof mongooseOptions . form . searchQuery === 'function' ) {
294
+ queryListExtension = mongooseOptions . form . searchQuery ( $scope , mongooseOptions ) ;
295
+ }
296
+ if ( queryListExtension ) {
297
+ _ . extend ( queryList , queryListExtension ) ;
298
+ }
299
+ return queryList ;
300
+ } ,
301
+ results : function ( data ) {
302
+ return { results : data . results , more : data . moreCount > 0 } ;
325
303
}
326
304
}
327
- query . callback ( data ) ;
305
+ } ;
306
+ _ . extend ( $scope [ select2ajaxName ] , formInstructions . select2 ) ;
307
+ formInstructions . select2 . fngAjax = select2ajaxName ;
308
+ } else {
309
+ if ( formInstructions . select2 === true ) {
310
+ formInstructions . select2 = { } ;
328
311
}
329
- } ;
330
- _ . extend ( $scope [ formInstructions . select2 . s2query ] , formInstructions . select2 ) ;
331
- formInstructions . options = recordHandler . suffixCleanId ( formInstructions , 'Options' ) ;
332
- formInstructions . ids = recordHandler . suffixCleanId ( formInstructions , '_ids' ) ;
333
- recordHandler . setUpSelectOptions ( mongooseOptions . ref , formInstructions , $scope , ctrlState , handleSchema ) ;
312
+ formInstructions . select2 . s2query = 'select2' + formInstructions . name . replace ( / \. / g, '_' ) ;
313
+ $scope [ 'select2' + formInstructions . select2 . s2query ] = {
314
+ allowClear : ! mongooseOptions . required ,
315
+ initSelection : function ( element , callback ) {
316
+ var myId = element . val ( ) ;
317
+ if ( myId !== '' && $scope [ formInstructions . ids ] . length > 0 ) {
318
+ var myVal = recordHandler . convertIdToListValue ( myId , $scope [ formInstructions . ids ] , $scope [ formInstructions . options ] , formInstructions . name ) ;
319
+ var display = { id : myId , text : myVal } ;
320
+ callback ( display ) ;
321
+ }
322
+ } ,
323
+ query : function ( query ) {
324
+ var data = { results : [ ] } ,
325
+ searchString = query . term . toUpperCase ( ) ;
326
+ for ( var i = 0 ; i < $scope [ formInstructions . options ] . length ; i ++ ) {
327
+ if ( $scope [ formInstructions . options ] [ i ] . toUpperCase ( ) . indexOf ( searchString ) !== - 1 ) {
328
+ data . results . push ( {
329
+ id : $scope [ formInstructions . ids ] [ i ] ,
330
+ text : $scope [ formInstructions . options ] [ i ]
331
+ } ) ;
332
+ }
333
+ }
334
+ query . callback ( data ) ;
335
+ }
336
+ } ;
337
+ _ . extend ( $scope [ formInstructions . select2 . s2query ] , formInstructions . select2 ) ;
338
+ performLookupSelect ( ) ;
339
+ }
340
+ } else if ( ! formInstructions . directive || ! formInstructions [ $filter ( 'camelCase' ) ( formInstructions . directive ) ] || ! formInstructions [ $filter ( 'camelCase' ) ( formInstructions . directive ) ] . fngAjax ) {
341
+ performLookupSelect ( ) ;
334
342
}
335
- } else if ( ! formInstructions . directive || ! formInstructions [ $filter ( 'camelCase' ) ( formInstructions . directive ) ] || ! formInstructions [ $filter ( 'camelCase' ) ( formInstructions . directive ) ] . fngAjax ) {
336
- formInstructions . options = recordHandler . suffixCleanId ( formInstructions , 'Options' ) ;
337
- formInstructions . ids = recordHandler . suffixCleanId ( formInstructions , '_ids' ) ;
338
- recordHandler . setUpSelectOptions ( mongooseOptions . ref , formInstructions , $scope , ctrlState , handleSchema ) ;
339
- }
340
343
}
341
344
} else if ( mongooseType . instance === 'Date' ) {
342
345
if ( ! formInstructions . type ) {
0 commit comments