diff --git a/awx/ui/client/src/credential-types/add/add.controller.js b/awx/ui/client/src/credential-types/add/add.controller.js index d5302bdf61..c8d2d3d38f 100644 --- a/awx/ui/client/src/credential-types/add/add.controller.js +++ b/awx/ui/client/src/credential-types/add/add.controller.js @@ -18,17 +18,17 @@ export default ['Rest', 'Wait', function init() { // Load the list of options for Kind - GetChoices({ - scope: $scope, - url: url, - field: 'kind', - variable: 'credential_kind_options' - }); + $scope.$parent.optionsDefer.promise + .then(function(options) { + GetChoices({ + scope: $scope, + url: url, + field: 'kind', + variable: 'credential_kind_options', + options: options + }); - Rest.setUrl(url); - Rest.options() - .success(function(data) { - if (!data.actions.POST) { + if (!options.actions.POST) { $state.go("^"); Alert('Permission Error', 'You do not have permission to add a credential type.', 'alert-info'); } diff --git a/awx/ui/client/src/credential-types/edit/edit.controller.js b/awx/ui/client/src/credential-types/edit/edit.controller.js index 8861c647ba..2808ef3249 100644 --- a/awx/ui/client/src/credential-types/edit/edit.controller.js +++ b/awx/ui/client/src/credential-types/edit/edit.controller.js @@ -6,14 +6,15 @@ export default ['Rest', 'Wait', 'CredentialTypesForm', 'ProcessErrors', 'GetBasePath', - 'GenerateForm', 'credential_typeData', + 'GenerateForm', 'resourceData', '$scope', '$state', 'GetChoices', 'ParseTypeChange', 'ToJSON', 'ParseVariableString', 'CreateSelect2', function( Rest, Wait, CredentialTypesForm, ProcessErrors, GetBasePath, - GenerateForm, credential_typeData, + GenerateForm, resourceData, $scope, $state, GetChoices, ParseTypeChange, ToJSON, ParseVariableString, CreateSelect2 ) { - var generator = GenerateForm, + var credential_typeData = resourceData.data, + generator = GenerateForm, data = credential_typeData, id = credential_typeData.id, form = CredentialTypesForm, @@ -24,13 +25,17 @@ export default ['Rest', 'Wait', function init() { // Load the list of options for Kind - GetChoices({ - scope: $scope, - url: url, - field: 'kind', - variable: 'credential_kind_options', - callback: 'choicesReadyCredentialTypes' - }); + $scope.$parent.optionsDefer.promise + .then(function(options) { + GetChoices({ + scope: $scope, + url: url, + field: 'kind', + variable: 'credential_kind_options', + options: options, + callback: 'choicesReadyCredentialTypes' + }); + }); } if ($scope.removeChoicesReady) { @@ -38,6 +43,7 @@ export default ['Rest', 'Wait', } $scope.removeChoicesReady = $scope.$on('choicesReadyCredentialTypes', function() { + $scope.credential_type = credential_typeData; $scope.$watch('credential_type.summary_fields.user_capabilities.edit', function(val) { diff --git a/awx/ui/client/src/credential-types/list/list.controller.js b/awx/ui/client/src/credential-types/list/list.controller.js index 27c167923d..0b16ca23d5 100644 --- a/awx/ui/client/src/credential-types/list/list.controller.js +++ b/awx/ui/client/src/credential-types/list/list.controller.js @@ -5,10 +5,10 @@ *************************************************/ export default ['$rootScope', '$scope', 'Wait', 'CredentialTypesList', - 'GetBasePath', 'Rest', 'ProcessErrors', 'Prompt', '$state', '$filter', 'Dataset', 'rbacUiControlService', 'Alert', + 'GetBasePath', 'Rest', 'ProcessErrors', 'Prompt', '$state', '$filter', 'Dataset', 'rbacUiControlService', 'Alert', '$q', function( $rootScope, $scope, Wait, CredentialTypesList, - GetBasePath, Rest, ProcessErrors, Prompt, $state, $filter, Dataset, rbacUiControlService, Alert + GetBasePath, Rest, ProcessErrors, Prompt, $state, $filter, Dataset, rbacUiControlService, Alert, $q ) { var defaultUrl = GetBasePath('credential_types'), list = CredentialTypesList; @@ -16,6 +16,8 @@ export default ['$rootScope', '$scope', 'Wait', 'CredentialTypesList', init(); function init() { + $scope.optionsDefer = $q.defer(); + if (!($rootScope.user_is_superuser || $rootScope.user_is_system_auditor)) { $state.go("setup"); Alert('Permission Error', 'You do not have permission to view, edit or create custom credential types.', 'alert-info'); @@ -27,6 +29,7 @@ export default ['$rootScope', '$scope', 'Wait', 'CredentialTypesList', .then(function(params) { $scope.canAdd = params.canAdd; $scope.options = params.options; + $scope.optionsDefer.resolve(params.options); optionsRequestDataProcessing(); }); diff --git a/awx/ui/client/src/credential-types/main.js b/awx/ui/client/src/credential-types/main.js index 5215f8e139..2ccb9607ed 100644 --- a/awx/ui/client/src/credential-types/main.js +++ b/awx/ui/client/src/credential-types/main.js @@ -36,27 +36,6 @@ angular.module('credentialTypes', [ add: 'CredentialTypesAddController', edit: 'CredentialTypesEditController' }, - resolve: { - edit: { - credential_typeData: ['$state', '$stateParams', 'Rest', 'GetBasePath', 'ProcessErrors', - function($state, $stateParams, rest, getBasePath, ProcessErrors) { - var credentialTypeId = $stateParams.credential_type_id; - var url = getBasePath('credential_types') + credentialTypeId + '/'; - rest.setUrl(url); - return rest.get() - .then(function(data) { - return data.data; - }).catch(function(response) { - ProcessErrors(null, response.data, response.status, null, { - hdr: 'Error!', - msg: 'Failed to get credential type info. GET returned status: ' + - response.status - }); - }); - } - ] - } - }, data: { activityStream: true, activityStreamTarget: 'custom_inventory_script' // TODO: change to 'credential_type'...there's probably more work that needs to be done to hook up activity stream diff --git a/awx/ui/client/src/rest/get-choices.factory.js b/awx/ui/client/src/rest/get-choices.factory.js index 33537d5ab4..e9f7e4398d 100644 --- a/awx/ui/client/src/rest/get-choices.factory.js +++ b/awx/ui/client/src/rest/get-choices.factory.js @@ -17,27 +17,41 @@ return function (params) { var scope = params.scope, url = params.url, - field = params.field; + field = params.field, + options = params.options; - // Auto populate the field if there is only one result - Rest.setUrl(url); - return Rest.options() - .then(function (data) { - data = data.data; - var choices = data.actions.GET[field].choices; + if (!options) { + // Auto populate the field if there is only one result + Rest.setUrl(url); + return Rest.options() + .then(function (data) { + data = data.data; + var choices = data.actions.GET[field].choices; - // manually add the adhoc label to the choices object if - // the permission_type field - if (field === "permission_type") { - choices.push(["adhoc", - data.actions.GET.run_ad_hoc_commands.help_text]); - } + // manually add the adhoc label to the choices object if + // the permission_type field + if (field === "permission_type") { + choices.push(["adhoc", + data.actions.GET.run_ad_hoc_commands.help_text]); + } - return choices; - }) - .catch(function (data, status) { - ProcessErrors(scope, data, status, null, { hdr: 'Error!', - msg: 'Failed to get ' + field + ' labels. Options requrest returned status: ' + status }); - }); + return choices; + }) + .catch(function (data, status) { + ProcessErrors(scope, data, status, null, { hdr: 'Error!', + msg: 'Failed to get ' + field + ' labels. Options requrest returned status: ' + status }); + }); + } else { + var choices = options.actions.GET[field].choices; + + // manually add the adhoc label to the choices object if + // the permission_type field + if (field === "permission_type") { + choices.push(["adhoc", + options.actions.GET.run_ad_hoc_commands.help_text]); + } + + return choices; + } }; }]; diff --git a/awx/ui/client/src/shared/Utilities.js b/awx/ui/client/src/shared/Utilities.js index 0b7a86970f..de5dad33a3 100644 --- a/awx/ui/client/src/shared/Utilities.js +++ b/awx/ui/client/src/shared/Utilities.js @@ -720,7 +720,8 @@ angular.module('Utilities', ['RestServices', 'Utilities']) field = params.field, variable = params.variable, callback = params.callback, - choice_name = params.choice_name; + choice_name = params.choice_name, + options = params.options if (scope[variable]) { scope[variable].length = 0; @@ -728,42 +729,50 @@ angular.module('Utilities', ['RestServices', 'Utilities']) scope[variable] = []; } - Rest.setUrl(url); - Rest.options() - .success(function(data) { - var choices, defaultChoice; - choices = (choice_name) ? data.actions.GET[field][choice_name] : data.actions.GET[field].choices; - if (data && data.actions && data.actions.POST && data.actions.POST[field]) { - defaultChoice = data.actions.POST[field].default; - } - if (choices) { - // including 'name' property so list can be used by search - choices.forEach(function(choice) { - scope[variable].push({ - label: choice[1], - value: choice[0], - name: choice[1] - }); + var withOptions = function(options) { + var choices, defaultChoice; + choices = (choice_name) ? options.actions.GET[field][choice_name] : options.actions.GET[field].choices; + if (options && options.actions && options.actions.POST && options.actions.POST[field]) { + defaultChoice = options.actions.POST[field].default; + } + if (choices) { + // including 'name' property so list can be used by search + choices.forEach(function(choice) { + scope[variable].push({ + label: choice[1], + value: choice[0], + name: choice[1] }); - } - if (defaultChoice !== undefined) { - var val; - for (val in scope[variable]) { - if (scope[variable][val].value === defaultChoice) { - scope[variable][val].isDefault = true; - } + }); + } + if (defaultChoice !== undefined) { + var val; + for (val in scope[variable]) { + if (scope[variable][val].value === defaultChoice) { + scope[variable][val].isDefault = true; } } - if (callback) { - scope.$emit(callback); - } - }) - .error(function(data, status) { - ProcessErrors(scope, data, status, null, { - hdr: 'Error!', - msg: 'Failed to get ' + url + '. GET status: ' + status - }); - }); + } + if (callback) { + scope.$emit(callback); + } + } + + if (!options) { + Rest.setUrl(url); + Rest.options() + .success(function(data) { + withOptions(data); + }) + .error(function(data, status) { + ProcessErrors(scope, data, status, null, { + hdr: 'Error!', + msg: 'Failed to get ' + url + '. GET status: ' + status + }); + }); + } else { + withOptions(options); + } }; } ])