From 61ad4e3793e4d72346ad81b4c5ef28cd3fcffb03 Mon Sep 17 00:00:00 2001 From: jaredevantabor Date: Wed, 18 Jan 2017 20:49:12 -0800 Subject: [PATCH] fix for autopopulation --- awx/ui/client/src/forms/Credentials.js | 1 + awx/ui/client/src/shared/directives.js | 71 ++++++++++++++++------ awx/ui/client/src/shared/form-generator.js | 3 +- 3 files changed, 56 insertions(+), 19 deletions(-) diff --git a/awx/ui/client/src/forms/Credentials.js b/awx/ui/client/src/forms/Credentials.js index 1ec2628cfd..4ebfa20170 100644 --- a/awx/ui/client/src/forms/Credentials.js +++ b/awx/ui/client/src/forms/Credentials.js @@ -48,6 +48,7 @@ export default ngShow: 'canShareCredential', label: i18n._('Organization'), type: 'lookup', + autopopulateLookup: false, list: 'OrganizationList', sourceModel: 'organization', sourceField: 'name', diff --git a/awx/ui/client/src/shared/directives.js b/awx/ui/client/src/shared/directives.js index d0435e1fc0..ade342c479 100644 --- a/awx/ui/client/src/shared/directives.js +++ b/awx/ui/client/src/shared/directives.js @@ -465,19 +465,21 @@ angular.module('AWDirectives', ['RestServices', 'Utilities', 'JobsHelper']) link: function(scope, elm, attrs, fieldCtrl) { let query, basePath, - defer = $q.defer(); + defer = $q.defer(), + autopopulateLookup, + modelKey = attrs.ngModel, + modelName = attrs.source, + watcher = attrs.awRequiredWhen || undefined; + + if (attrs.autopopulateLookup !== undefined) { + autopopulateLookup = attrs.autopopulateLookup; + } else { + autopopulateLookup = true; + } - // Auto-populating related fields when there is only 1 relatable entry isn't mission critical. - // Therefore, don't display an error message if the get request fails. function _doAutoPopulate() { - let modelKey = attrs.ngModel; - let modelName = attrs.source; let query = ''; - // TODO: We can't fully rely on basePath. For example, the "Add Credentials" form sets the basePath - // to something like admin_of_organizations. Instead, we should probably just use the model name. - // At the time, we need to account for admin_of_organizations to determine if we auto-populate org - // on the cred add page. basePath = GetBasePath(elm.attr('data-basePath')) || elm.attr('data-basePath'); switch(modelName) { @@ -492,22 +494,55 @@ angular.module('AWDirectives', ['RestServices', 'Utilities', 'JobsHelper']) Rest.setUrl(`${basePath}` + query); Rest.get() .success(function (data) { - // TODO: Obviously, add back in the data count - /* - if (data.count == 1) { + if (data.count === 1) { + scope[modelKey] = data.results[0].name; + scope[modelName] = data.results[0].id; } - */ - scope[modelKey] = data.results[0].name; - scope[modelName] = data.results[0].id; }); } - // TODO: Add more logic checks to see if this field needs to be auto-populated. Checks similar to the below linked code. - // https://github.com/ansible/ansible-tower/blob/release_3.0.3/awx/ui/client/src/lookup/lookup.factory.js#L94 - if (scope.mode === 'add') { + if (fieldIsAutopopulatable()) { _doAutoPopulate(); } + // This checks to see if the field meets the criteria to + // autopopulate: + // Population rules: + // - add form only + // - lookup is required + // - lookup is not promptable + // - user must only have access to 1 item the lookup is for + function fieldIsAutopopulatable() { + if (autopopulateLookup === false) { + return false; + } + if (scope.mode === "add") { + if(watcher){ + scope.$watch(watcher, (newValue, oldValue, scope2) => { + console.log('success', watcher, newValue , oldValue, scope2); + + if(Boolean(scope.$eval(watcher)) === true){ + + // if we get here then the field is required + // by way of awRequiredWhen + // and is a candidate for autopopulation + + _doAutoPopulate(); + } + }); + } + else if (attrs.required === true) { + return true; + } + else { + return false; + } + } + else { + return false; + } + } + // query the API to see if field value corresponds to a valid resource // .ng-pending will be applied to the directive element while the request is outstanding // form.$pending will contain object reference to any ngModelControllers with outstanding requests diff --git a/awx/ui/client/src/shared/form-generator.js b/awx/ui/client/src/shared/form-generator.js index b257197030..52e0eccfc4 100644 --- a/awx/ui/client/src/shared/form-generator.js +++ b/awx/ui/client/src/shared/form-generator.js @@ -377,7 +377,7 @@ angular.module('FormGenerator', [GeneratorHelpers.name, 'Utilities', listGenerat applyDefaults: function (form, scope) { // Note: This is a hack. Ideally, mode should be set in each -.controller.js - // The mode is needed by the awlookup directive to auto-populate form fields when there is a + // The mode is needed by the awlookup directive to auto-populate form fields when there is a // single related resource. scope.mode = this.mode; @@ -1368,6 +1368,7 @@ angular.module('FormGenerator', [GeneratorHelpers.name, 'Utilities', listGenerat html += `data-basePath="${field.basePath}"`; html += `data-source="${field.sourceModel}"`; html += `data-query="?${field.sourceField}__iexact=:value"`; + html += (field.autopopulateLookup) ? `autopopulateLookup=${field.autopopulateLookup}` : ""; html += `ng-model-options="{ updateOn: 'default blur', debounce: { 'default': 300, 'blur': 0 } }"`; html += " awlookup >\n"; html += "\n";