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