1
0
mirror of https://github.com/ansible/awx.git synced 2024-11-02 01:21:21 +03:00

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

@ -3,7 +3,7 @@
*
* LookupHelper
* Build a lookup dialog
*
*
* LookUpInit( {
* scope: <form scope>,
* form: <form object>,
@ -13,141 +13,156 @@
* hdr: <optional. modal dialog header>
* })
*/
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',
function(Alert, Rest, GenerateList, SearchInit, PaginateInit, GetBasePath, FormatDate, Empty) {
return function(params) {
var scope = params.scope; // form scope
var form = params.form; // form object
var list = params.list; // list object
var field = params.field; // form field
var postAction = params.postAction //action to perform post user selection
function (Alert, Rest, GenerateList, SearchInit, PaginateInit, GetBasePath, FormatDate, Empty) {
return function (params) {
var defaultUrl;
if (params.url) {
// pass in a url value to override the default
defaultUrl = params.url;
}
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();
}
}
var scope = params.scope, // form scope
form = params.form, // form object
list = params.list, // list object
field = params.field, // form field
postAction = params.postAction, //action to perform post user selection
defaultUrl, name, hdr, watchUrl;
if (params.url) {
// pass in a url value to override the default
defaultUrl = params.url;
} else {
defaultUrl = (list.name === 'inventories') ? GetBasePath('inventory') : GetBasePath(list.name);
}
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() {
for (var fld in list.fields) {
if (list.fields[fld].type && list.fields[fld].type == 'date') {
//convert dates to our standard format
for (var 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.
// Put it back based on the value of sourceModel_sourceName
if (scope[form.fields[field].sourceModel + '_' + form.fields[field].sourceField] !== '' &&
scope[form.fields[field].sourceModel + '_' + form.fields[field].sourceField] !== null) {
for (var 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;
//scope[form.fields[field].sourceModel + '_' + form.fields[field].sourceField] =
// listScope[list.name][i][form.fields[field].sourceField];
break;
// Show pop-up
name = list.iterator.charAt(0).toUpperCase() + list.iterator.substring(1);
hdr = (params.hdr) ? params.hdr : 'Select ' + name;
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 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 () {
// Restore search settings
if (listScope.searchCleanup) {
listScope.searchCleanup();
}
// 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();
}
}
if (!Empty(scope[field])) {
listScope['toggle_' + list.iterator](scope[field]);
}
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;
}
}
});
listScope.search(list.iterator);
}
}
if (!Empty(scope[field])) {
listScope['toggle_' + list.iterator](scope[field]);
}
});
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_size'] = pageSize;
}
console.log('setting ' + relatedSets[key].iterator + ' page_size: ' + pageSize);
}
scope.getPage = function(page, set, iterator) {