Fixed issues with Lookup.js helper. With multipe pages of data available to a lookup dialog, original selection was lost if user clicked cancel. Removed console.log statement left in pagination helper.

This commit is contained in:
Chris Houseknecht
2014-02-07 17:20:12 -05:00
parent a1e7b28380
commit cac49c199c
2 changed files with 146 additions and 132 deletions

View File

@@ -14,140 +14,155 @@
* }) * })
*/ */
angular.module('LookUpHelper', [ 'RestServices', 'Utilities', 'SearchHelper', 'PaginationHelpers', 'ListGenerator', 'ApiLoader' ]) 'use strict';
angular.module('LookUpHelper', ['RestServices', 'Utilities', 'SearchHelper', 'PaginationHelpers', 'ListGenerator', 'ApiLoader'])
.factory('LookUpInit', ['Alert', 'Rest', 'GenerateList', 'SearchInit', 'PaginateInit', 'GetBasePath', 'FormatDate', 'Empty', .factory('LookUpInit', ['Alert', 'Rest', 'GenerateList', 'SearchInit', 'PaginateInit', 'GetBasePath', 'FormatDate', 'Empty',
function(Alert, Rest, GenerateList, SearchInit, PaginateInit, GetBasePath, FormatDate, Empty) { function (Alert, Rest, GenerateList, SearchInit, PaginateInit, GetBasePath, FormatDate, Empty) {
return function(params) { return function (params) {
var scope = params.scope; // form scope var scope = params.scope, // form scope
var form = params.form; // form object form = params.form, // form object
var list = params.list; // list object list = params.list, // list object
var field = params.field; // form field field = params.field, // form field
var postAction = params.postAction //action to perform post user selection postAction = params.postAction, //action to perform post user selection
defaultUrl, name, hdr, watchUrl;
var defaultUrl; if (params.url) {
if (params.url) { // pass in a url value to override the default
// pass in a url value to override the default defaultUrl = params.url;
defaultUrl = params.url; } else {
} defaultUrl = (list.name === 'inventories') ? GetBasePath('inventory') : GetBasePath(list.name);
else {
defaultUrl = (list.name == 'inventories') ? GetBasePath('inventory') : GetBasePath(list.name);
}
// Show pop-up
var name = list.iterator.charAt(0).toUpperCase() + list.iterator.substring(1);
var hdr = (params.hdr) ? params.hdr : 'Select ' + name;
var watchUrl = (/\/$/.test(defaultUrl)) ? defaultUrl + '?' : defaultUrl + '&';
watchUrl += form.fields[field].sourceField + '__' + 'iexact=:value';
$('input[name="' + form.fields[field].sourceModel + '_' + form.fields[field].sourceField + '"]').attr('data-url', watchUrl);
$('input[name="' + form.fields[field].sourceModel + '_' + form.fields[field].sourceField + '"]').attr('data-source',field);
scope['lookUp' + name] = function() {
var listGenerator = GenerateList;
var listScope = listGenerator.inject(list, { mode: 'lookup', hdr: hdr });
$('#lookup-modal').on('hidden.bs.modal', function() {
// If user clicks cancel without making a selection, make sure that field values are
// in synch.
if (listScope.searchCleanup) {
listScope.searchCleanup();
}
if (scope[field] == '' || scope[field] == null) {
scope[form.fields[field].sourceModel + '_' + form.fields[field].sourceField] = '';
if (!scope.$$phase) {
scope.$digest();
}
}
});
listScope.selectAction = function() {
var found = false;
var name;
for (var i=0; i < listScope[list.name].length; i++) {
if (listScope[list.name][i]['checked'] == '1') {
found = true;
scope[field] = listScope[list.name][i].id;
if (scope[form.name + '_form'] && form.fields[field] && form.fields[field].sourceModel) {
scope[form.fields[field].sourceModel + '_' + form.fields[field].sourceField] =
listScope[list.name][i][form.fields[field].sourceField];
if (scope[form.name + '_form'][form.fields[field].sourceModel + '_' + form.fields[field].sourceField]) {
scope[form.name + '_form'][form.fields[field].sourceModel + '_' + form.fields[field].sourceField]
.$setValidity('awlookup',true);
}
}
if (scope[form.name + '_form']) {
scope[form.name + '_form'].$setDirty();
}
listGenerator.hide();
}
}
if (found == false) {
Alert('Missing Selection', 'Oops, you failed to make a selection. Click on a row to make your selection, ' +
'and then click the Select button.');
}
else {
if (postAction) {
postAction();
}
}
} }
listScope['toggle_' + list.iterator] = function(id) { // Show pop-up
for (var i=0; i < scope[list.name].length; i++) { name = list.iterator.charAt(0).toUpperCase() + list.iterator.substring(1);
if (listScope[list.name][i]['id'] == id) { hdr = (params.hdr) ? params.hdr : 'Select ' + name;
listScope[list.name][i]['checked'] = '1';
listScope[list.name][i]['success_class'] = 'success';
}
else {
listScope[list.name][i]['checked'] = '0';
listScope[list.name][i]['success_class'] = '';
}
}
}
SearchInit({ scope: listScope, set: list.name, list: list, url: defaultUrl });
PaginateInit({ scope: listScope, list: list, url: defaultUrl, mode: 'lookup' });
// If user made a selection previously, mark it as selected when modal loads watchUrl = (/\/$/.test(defaultUrl)) ? defaultUrl + '?' : defaultUrl + '&';
if (listScope.lookupPostRefreshRemove) { watchUrl += form.fields[field].sourceField + '__' + 'iexact=:value';
listScope.lookupPostRefreshRemove();
} $('input[name="' + form.fields[field].sourceModel + '_' + form.fields[field].sourceField + '"]').attr('data-url', watchUrl);
listScope.lookupPostRefreshRemove = scope.$on('PostRefresh', function() { $('input[name="' + form.fields[field].sourceModel + '_' + form.fields[field].sourceField + '"]').attr('data-source', field);
for (var fld in list.fields) {
if (list.fields[fld].type && list.fields[fld].type == 'date') { scope['lookUp' + name] = function () {
//convert dates to our standard format
for (var i=0; i < scope[list.name].length; i++) { var master = {}, listGenerator, listScope;
scope[list.name][i][fld] = FormatDate(new Date(scope[list.name][i][fld]));
} // Generating the search list potentially kills the values held in scope for the field.
} // We'll keep a copy in master{} that we can revert back to on cancel;
} master[field] = scope[field];
// List generator creates the form, resetting it and losing the previously selected value. master[form.fields[field].sourceModel + '_' + form.fields[field].sourceField] =
// Put it back based on the value of sourceModel_sourceName scope[form.fields[field].sourceModel + '_' + form.fields[field].sourceField];
if (scope[form.fields[field].sourceModel + '_' + form.fields[field].sourceField] !== '' &&
scope[form.fields[field].sourceModel + '_' + form.fields[field].sourceField] !== null) { listGenerator = GenerateList;
for (var i=0; i < listScope[list.name].length; i++) { listScope = listGenerator.inject(list, { mode: 'lookup', hdr: hdr });
if (listScope[list.name][i][form.fields[field].sourceField] ==
scope[form.fields[field].sourceModel + '_' + form.fields[field].sourceField]) { $('#lookup-modal').on('hidden.bs.modal', function () {
scope[field] = listScope[list.name][i].id; // Restore search settings
//scope[form.fields[field].sourceModel + '_' + form.fields[field].sourceField] = if (listScope.searchCleanup) {
// listScope[list.name][i][form.fields[field].sourceField]; listScope.searchCleanup();
break;
} }
// If user clicks cancel without making a selection, restore original values
if (Empty(scope[field])) {
scope[field] = master[field];
scope[form.fields[field].sourceModel + '_' + form.fields[field].sourceField] =
master[form.fields[field].sourceModel + '_' + form.fields[field].sourceField];
}
});
listScope.selectAction = function () {
var i, found = false;
for (i = 0; i < listScope[list.name].length; i++) {
if (listScope[list.name][i].checked === '1') {
found = true;
scope[field] = listScope[list.name][i].id;
if (scope[form.name + '_form'] && form.fields[field] && form.fields[field].sourceModel) {
scope[form.fields[field].sourceModel + '_' + form.fields[field].sourceField] =
listScope[list.name][i][form.fields[field].sourceField];
if (scope[form.name + '_form'][form.fields[field].sourceModel + '_' + form.fields[field].sourceField]) {
scope[form.name + '_form'][form.fields[field].sourceModel + '_' + form.fields[field].sourceField]
.$setValidity('awlookup', true);
}
}
if (scope[form.name + '_form']) {
scope[form.name + '_form'].$setDirty();
}
listGenerator.hide();
}
}
if (found === false) {
Alert('Missing Selection', 'Oops, you failed to make a selection. Click on a row to make your selection, ' +
'and then click the Select button.');
} else {
if (postAction) {
postAction();
}
}
};
listScope['toggle_' + list.iterator] = function (id) {
for (var i = 0; i < scope[list.name].length; i++) {
if (listScope[list.name][i].id === id) {
listScope[list.name][i].checked = '1';
listScope[list.name][i].success_class = 'success';
} else {
listScope[list.name][i].checked = '0';
listScope[list.name][i].success_class = '';
}
}
};
SearchInit({
scope: listScope,
set: list.name,
list: list,
url: defaultUrl
});
PaginateInit({
scope: listScope,
list: list,
url: defaultUrl,
mode: 'lookup'
});
// If user made a selection previously, mark it as selected when modal loads
if (listScope.lookupPostRefreshRemove) {
listScope.lookupPostRefreshRemove();
} }
listScope.lookupPostRefreshRemove = scope.$on('PostRefresh', function () {
var fld, i;
for (fld in list.fields) {
if (list.fields[fld].type === 'date') {
//convert dates to our standard format
for (i = 0; i < scope[list.name].length; i++) {
scope[list.name][i][fld] = FormatDate(new Date(scope[list.name][i][fld]));
}
}
}
// List generator creates the form, resetting it and losing the previously selected value.
// If it's in the current set, find it and marke it as selected.
if (scope[form.fields[field].sourceModel + '_' + form.fields[field].sourceField] !== '' &&
scope[form.fields[field].sourceModel + '_' + form.fields[field].sourceField] !== null) {
for (i = 0; i < listScope[list.name].length; i++) {
if (listScope[list.name][i][form.fields[field].sourceField] ===
scope[form.fields[field].sourceModel + '_' + form.fields[field].sourceField]) {
scope[field] = listScope[list.name][i].id;
break;
}
}
} }
if (!Empty(scope[field])) { if (!Empty(scope[field])) {
listScope['toggle_' + list.iterator](scope[field]); listScope['toggle_' + list.iterator](scope[field]);
} }
}); });
listScope.search(list.iterator); listScope.search(list.iterator);
} };
};
} }
}]); ]);

View File

@@ -71,7 +71,6 @@ angular.module('PaginationHelpers', ['Utilities', 'RefreshHelper', 'RefreshRelat
scope[relatedSets[key].iterator + '_page'] = 1; scope[relatedSets[key].iterator + '_page'] = 1;
scope[relatedSets[key].iterator + '_page_size'] = pageSize; scope[relatedSets[key].iterator + '_page_size'] = pageSize;
} }
console.log('setting ' + relatedSets[key].iterator + ' page_size: ' + pageSize);
} }
scope.getPage = function(page, set, iterator) { scope.getPage = function(page, set, iterator) {