fix for autopopulation

This commit is contained in:
jaredevantabor 2017-01-18 20:49:12 -08:00
parent 67f282f64d
commit 61ad4e3793
3 changed files with 56 additions and 19 deletions

View File

@ -48,6 +48,7 @@ export default
ngShow: 'canShareCredential',
label: i18n._('Organization'),
type: 'lookup',
autopopulateLookup: false,
list: 'OrganizationList',
sourceModel: 'organization',
sourceField: 'name',

View File

@ -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

View File

@ -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 <resource>-<mode>.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 += "</div>\n";