From bdcd2c593874f2739cda5785ad7f386cbe73df72 Mon Sep 17 00:00:00 2001 From: John Mitchell Date: Fri, 24 Apr 2015 14:00:27 -0400 Subject: [PATCH] autopopulate lookups if there is only one option for that lookup type --- awx/ui/static/js/helpers/Lookup.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/awx/ui/static/js/helpers/Lookup.js b/awx/ui/static/js/helpers/Lookup.js index a476b1fd6c..178b66d13f 100644 --- a/awx/ui/static/js/helpers/Lookup.js +++ b/awx/ui/static/js/helpers/Lookup.js @@ -63,6 +63,31 @@ 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); + } + } + 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 }); + }); + parent_scope['lookUp' + name] = function () {