From 0476725d61d9b337c4ffb17581759655c8438a8c Mon Sep 17 00:00:00 2001 From: John Mitchell Date: Mon, 4 May 2015 10:08:29 -0400 Subject: [PATCH 1/2] Pared back autopopulation of lookups to be only on add of a form and only for required fields. --- awx/ui/static/js/helpers/Lookup.js | 66 ++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 22 deletions(-) diff --git a/awx/ui/static/js/helpers/Lookup.js b/awx/ui/static/js/helpers/Lookup.js index b71d7ffff5..f001c8aa3c 100644 --- a/awx/ui/static/js/helpers/Lookup.js +++ b/awx/ui/static/js/helpers/Lookup.js @@ -63,30 +63,52 @@ export default $('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); - // Auto populate the field if there is only one result - Rest.setUrl(defaultUrl); - Rest.get() - .success(function (data) { - if (data.count === 1) { - parent_scope[field] = data.results[0].id; - if (parent_scope[form.name + '_form'] && form.fields[field] && form.fields[field].sourceModel) { - parent_scope[form.fields[field].sourceModel + '_' + form.fields[field].sourceField] = - data.results[0][form.fields[field].sourceField]; - if (parent_scope[form.name + '_form'][form.fields[field].sourceModel + '_' + form.fields[field].sourceField]) { - parent_scope[form.name + '_form'][form.fields[field].sourceModel + '_' + form.fields[field].sourceField] - .$setValidity('awlookup', true); + var sourceModel = form.fields[field].sourceModel, + sourceField = form.fields[field].sourceField; + + // this logic makes sure that the form is being added, and that the lookup to be autopopulated is required + function fieldIsAutopopulatable() { + if (parent_scope.mode === "add") { + if (parent_scope[form.fields[field].sourceModel + "_field"].awRequiredWhen && + parent_scope[form.fields[field].sourceModel + "_field"].awRequiredWhen.variable && + parent_scope[parent_scope[form.fields[field].sourceModel + "_field"].awRequiredWhen.variable]) { + return true; + } else if (parent_scope[form.fields[field].sourceModel + "_field"].addRequired === true) { + return true; + } else { + return false; + } + } else { + return false; + } + } + + if (fieldIsAutopopulatable()) { + // Auto populate the field if there is only one result + Rest.setUrl(defaultUrl); + Rest.get() + .success(function (data) { + if (data.count === 1) { + parent_scope[field] = data.results[0].id; + if (parent_scope[form.name + '_form'] && form.fields[field] && sourceModel) { + parent_scope[sourceModel + '_' + sourceField] = + data.results[0][sourceField]; + if (parent_scope[form.name + '_form'][sourceModel + '_' + sourceField]) { + parent_scope[form.name + '_form'][sourceModel + '_' + sourceField] + .$setValidity('awlookup', true); + } + } + if (parent_scope[form.name + '_form']) { + parent_scope[form.name + '_form'].$setDirty(); } } - if (parent_scope[form.name + '_form']) { - parent_scope[form.name + '_form'].$setDirty(); - } - } - }) - .error(function (data, status) { - ProcessErrors(parent_scope, data, status, form, { hdr: 'Error!', - msg: 'Failed to launch adhoc command. POST returned status: ' + - status }); - }); + }) + .error(function (data, status) { + ProcessErrors(parent_scope, data, status, form, { hdr: 'Error!', + msg: 'Failed to launch adhoc command. POST returned status: ' + + status }); + }); + } parent_scope['lookUp' + name] = function () { From 2ee2563e61e0f3ec250d621936ad1f5309f9ac40 Mon Sep 17 00:00:00 2001 From: John Mitchell Date: Wed, 6 May 2015 14:39:16 -0400 Subject: [PATCH 2/2] fixed variable name in fieldIsAutopopulatable function --- awx/ui/static/js/helpers/Lookup.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/awx/ui/static/js/helpers/Lookup.js b/awx/ui/static/js/helpers/Lookup.js index f001c8aa3c..0dceb91511 100644 --- a/awx/ui/static/js/helpers/Lookup.js +++ b/awx/ui/static/js/helpers/Lookup.js @@ -69,11 +69,11 @@ export default // this logic makes sure that the form is being added, and that the lookup to be autopopulated is required function fieldIsAutopopulatable() { if (parent_scope.mode === "add") { - if (parent_scope[form.fields[field].sourceModel + "_field"].awRequiredWhen && - parent_scope[form.fields[field].sourceModel + "_field"].awRequiredWhen.variable && - parent_scope[parent_scope[form.fields[field].sourceModel + "_field"].awRequiredWhen.variable]) { + if (parent_scope[sourceModel + "_field"].awRequiredWhen && + parent_scope[sourceModel + "_field"].awRequiredWhen.variable && + parent_scope[parent_scope[sourceModel + "_field"].awRequiredWhen.variable]) { return true; - } else if (parent_scope[form.fields[field].sourceModel + "_field"].addRequired === true) { + } else if (parent_scope[sourceModel + "_field"].addRequired === true) { return true; } else { return false;