Skip to content

Commit bc29b25

Browse files
committed
fix(form) Don't populate select options when the select is hidden
1 parent 72e920b commit bc29b25

File tree

4 files changed

+112
-111
lines changed

4 files changed

+112
-111
lines changed

dist/forms-angular.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1821,6 +1821,14 @@ var fng;
18211821
}
18221822
}
18231823
function handleFieldType(formInstructions, mongooseType, mongooseOptions, $scope, ctrlState) {
1824+
function performLookupSelect() {
1825+
formInstructions.options = recordHandler.suffixCleanId(formInstructions, 'Options');
1826+
formInstructions.ids = recordHandler.suffixCleanId(formInstructions, '_ids');
1827+
if (!formInstructions.hidden) {
1828+
recordHandler.setUpSelectOptions(mongooseOptions.ref, formInstructions, $scope, ctrlState, handleSchema);
1829+
}
1830+
;
1831+
}
18241832
var select2ajaxName;
18251833
if (mongooseType.caster) {
18261834
formInstructions.array = true;
@@ -2036,15 +2044,11 @@ var fng;
20362044
}
20372045
};
20382046
_.extend($scope[formInstructions.select2.s2query], formInstructions.select2);
2039-
formInstructions.options = recordHandler.suffixCleanId(formInstructions, 'Options');
2040-
formInstructions.ids = recordHandler.suffixCleanId(formInstructions, '_ids');
2041-
recordHandler.setUpSelectOptions(mongooseOptions.ref, formInstructions, $scope, ctrlState, handleSchema);
2047+
performLookupSelect();
20422048
}
20432049
}
20442050
else if (!formInstructions.directive || !formInstructions[$filter('camelCase')(formInstructions.directive)] || !formInstructions[$filter('camelCase')(formInstructions.directive)].fngAjax) {
2045-
formInstructions.options = recordHandler.suffixCleanId(formInstructions, 'Options');
2046-
formInstructions.ids = recordHandler.suffixCleanId(formInstructions, '_ids');
2047-
recordHandler.setUpSelectOptions(mongooseOptions.ref, formInstructions, $scope, ctrlState, handleSchema);
2051+
performLookupSelect();
20482052
}
20492053
}
20502054
}
@@ -3156,7 +3160,7 @@ var fng;
31563160
ctrlState.master = JSON.parse($location.$$search.r);
31573161
}
31583162
catch (e) {
3159-
console.log('Error parsing specified record ' + e.message);
3163+
console.log('Error parsing specified record : ' + e.message);
31603164
}
31613165
}
31623166
$scope.phase = 'ready';

dist/forms-angular.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/services/form-generator.ts

Lines changed: 97 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ module fng.services {
111111

112112
function handleFieldType(formInstructions, mongooseType, mongooseOptions, $scope, ctrlState) {
113113

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+
114122
var select2ajaxName;
115123
if (mongooseType.caster) {
116124
formInstructions.array = true;
@@ -232,111 +240,106 @@ module fng.services {
232240
delete formInstructions.link;
233241
} else {
234242
formInstructions.type = 'select';
235-
236243
// Support both of the syntaxes below
237244
// team : [ { type: Schema.Types.ObjectId , ref: 'f_nested_schema', form: {select2: {fngAjax: true}}} ],
238245
// 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;
254249
}
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 {
302250
if (formInstructions.select2 === true) {
303251
formInstructions.select2 = {};
304252
}
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};
325303
}
326304
}
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 = {};
328311
}
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();
334342
}
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-
}
340343
}
341344
} else if (mongooseType.instance === 'Date') {
342345
if (!formInstructions.type) {

test/unit/baseControllerSpec.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -640,10 +640,7 @@ describe('BaseCtrl', function () {
640640
'setters': [],
641641
'getters': [],
642642
'options': {
643-
'ref': 'organisation',
644-
'form': {
645-
'hidden': true
646-
}
643+
'ref': 'organisation'
647644
},
648645
'_index': null
649646
},
@@ -654,10 +651,7 @@ describe('BaseCtrl', function () {
654651
'options': {
655652
'type': [
656653
{
657-
'ref': 'organisation',
658-
'form': {
659-
'hidden': true
660-
}
654+
'ref': 'organisation'
661655
}
662656
],
663657
'form': {'label': 'Organisations'}

0 commit comments

Comments
 (0)