From 8dfc9e482e4fc03bbad2f7fb9dc60e4e32659bf2 Mon Sep 17 00:00:00 2001 From: John Mitchell Date: Wed, 21 Oct 2015 20:31:21 -0700 Subject: [PATCH] Revamp options request helper to work for other choice fields This moves the get-choices and get-labels services to the rest module The get-choices service is also revamped so that a field name can be passed to check the options request json All uses of get choices were revamped to utilize the new naming convention --- awx/ui/client/src/controllers/Teams.js | 11 ++++++----- awx/ui/client/src/controllers/Users.js | 11 ++++++----- .../client/src/permissions/add/add.controller.js | 11 ++++++----- .../src/permissions/edit/edit.controller.js | 11 ++++++----- .../src/permissions/list/list.controller.js | 11 ++++++----- awx/ui/client/src/permissions/main.js | 4 ---- .../shared => rest}/get-choices.factory.js | 16 ++++++++++------ .../shared => rest}/get-labels.factory.js | 0 awx/ui/client/src/rest/main.js | 6 +++++- 9 files changed, 45 insertions(+), 36 deletions(-) rename awx/ui/client/src/{permissions/shared => rest}/get-choices.factory.js (66%) rename awx/ui/client/src/{permissions/shared => rest}/get-labels.factory.js (100%) diff --git a/awx/ui/client/src/controllers/Teams.js b/awx/ui/client/src/controllers/Teams.js index ab5dc9298f..6fe42bc955 100644 --- a/awx/ui/client/src/controllers/Teams.js +++ b/awx/ui/client/src/controllers/Teams.js @@ -176,7 +176,7 @@ TeamsAdd.$inject = ['$scope', '$rootScope', '$compile', '$location', '$log', '$r export function TeamsEdit($scope, $rootScope, $compile, $location, $log, $routeParams, TeamForm, GenerateForm, Rest, Alert, ProcessErrors, LoadBreadCrumbs, RelatedSearchInit, RelatedPaginateInit, ReturnToCaller, ClearScope, LookUpInit, Prompt, GetBasePath, CheckAccess, - OrganizationList, Wait, Stream, permissionsChoices, permissionsLabel, permissionsSearchSelect) { + OrganizationList, Wait, Stream, fieldChoices, fieldLabels, permissionsSearchSelect) { ClearScope(); @@ -192,16 +192,17 @@ export function TeamsEdit($scope, $rootScope, $compile, $location, $log, $routeP $scope.permission_search_select = []; // return a promise from the options request with the permission type choices (including adhoc) as a param - var permissionsChoice = permissionsChoices({ + var permissionsChoice = fieldChoices({ scope: $scope, - url: 'api/v1/' + base + '/' + id + '/permissions/' + url: 'api/v1/' + base + '/' + id + '/permissions/', + field: 'permission_type' }); // manipulate the choices from the options request to be set on // scope and be usable by the list form permissionsChoice.then(function (choices) { choices = - permissionsLabel({ + fieldLabels({ choices: choices }); _.map(choices, function(n, key) { @@ -431,5 +432,5 @@ export function TeamsEdit($scope, $rootScope, $compile, $location, $log, $routeP TeamsEdit.$inject = ['$scope', '$rootScope', '$compile', '$location', '$log', '$routeParams', 'TeamForm', 'GenerateForm', 'Rest', 'Alert', 'ProcessErrors', 'LoadBreadCrumbs', 'RelatedSearchInit', 'RelatedPaginateInit', - 'ReturnToCaller', 'ClearScope', 'LookUpInit', 'Prompt', 'GetBasePath', 'CheckAccess', 'OrganizationList', 'Wait', 'Stream', 'permissionsChoices', 'permissionsLabel', 'permissionsSearchSelect' + 'ReturnToCaller', 'ClearScope', 'LookUpInit', 'Prompt', 'GetBasePath', 'CheckAccess', 'OrganizationList', 'Wait', 'Stream', 'fieldChoices', 'fieldLabels', 'permissionsSearchSelect' ]; diff --git a/awx/ui/client/src/controllers/Users.js b/awx/ui/client/src/controllers/Users.js index 2a37115a16..3aa0b98662 100644 --- a/awx/ui/client/src/controllers/Users.js +++ b/awx/ui/client/src/controllers/Users.js @@ -208,7 +208,7 @@ UsersAdd.$inject = ['$scope', '$rootScope', '$compile', '$location', '$log', '$r export function UsersEdit($scope, $rootScope, $compile, $location, $log, $routeParams, UserForm, GenerateForm, Rest, Alert, ProcessErrors, LoadBreadCrumbs, RelatedSearchInit, RelatedPaginateInit, ReturnToCaller, ClearScope, GetBasePath, - Prompt, CheckAccess, ResetForm, Wait, Stream, permissionsChoices, permissionsLabel, permissionsSearchSelect) { + Prompt, CheckAccess, ResetForm, Wait, Stream, fieldChoices, fieldLabels, permissionsSearchSelect) { ClearScope(); @@ -224,16 +224,17 @@ export function UsersEdit($scope, $rootScope, $compile, $location, $log, $routeP $scope.permission_search_select = []; // return a promise from the options request with the permission type choices (including adhoc) as a param - var permissionsChoice = permissionsChoices({ + var permissionsChoice = fieldChoices({ scope: $scope, - url: 'api/v1/' + base + '/' + id + '/permissions/' + url: 'api/v1/' + base + '/' + id + '/permissions/', + field: 'permission_type' }); // manipulate the choices from the options request to be set on // scope and be usable by the list form permissionsChoice.then(function (choices) { choices = - permissionsLabel({ + fieldLabels({ choices: choices }); _.map(choices, function(n, key) { @@ -519,5 +520,5 @@ export function UsersEdit($scope, $rootScope, $compile, $location, $log, $routeP UsersEdit.$inject = ['$scope', '$rootScope', '$compile', '$location', '$log', '$routeParams', 'UserForm', 'GenerateForm', 'Rest', 'Alert', 'ProcessErrors', 'LoadBreadCrumbs', 'RelatedSearchInit', 'RelatedPaginateInit', 'ReturnToCaller', 'ClearScope', - 'GetBasePath', 'Prompt', 'CheckAccess', 'ResetForm', 'Wait', 'Stream', 'permissionsChoices', 'permissionsLabel', 'permissionsSearchSelect' + 'GetBasePath', 'Prompt', 'CheckAccess', 'ResetForm', 'Wait', 'Stream', 'fieldChoices', 'fieldLabels', 'permissionsSearchSelect' ]; diff --git a/awx/ui/client/src/permissions/add/add.controller.js b/awx/ui/client/src/permissions/add/add.controller.js index 0dad88f9db..f90e9120a7 100644 --- a/awx/ui/client/src/permissions/add/add.controller.js +++ b/awx/ui/client/src/permissions/add/add.controller.js @@ -10,8 +10,8 @@ * @description This controller for permissions add */ export default - ['$scope', '$rootScope', '$compile', '$location', '$log', '$routeParams', 'permissionsForm', 'GenerateForm', 'Rest', 'Alert', 'ProcessErrors', 'LoadBreadCrumbs', 'ClearScope', 'GetBasePath', 'ReturnToCaller', 'InventoryList', 'ProjectList', 'LookUpInit', 'CheckAccess', 'Wait', 'permissionsCategoryChange', 'permissionsChoices', 'permissionsLabel', - function($scope, $rootScope, $compile, $location, $log, $routeParams, permissionsForm, GenerateForm, Rest, Alert, ProcessErrors, LoadBreadCrumbs, ClearScope, GetBasePath, ReturnToCaller, InventoryList, ProjectList, LookUpInit, CheckAccess, Wait, permissionsCategoryChange, permissionsChoices, permissionsLabel) { + ['$scope', '$rootScope', '$compile', '$location', '$log', '$routeParams', 'permissionsForm', 'GenerateForm', 'Rest', 'Alert', 'ProcessErrors', 'LoadBreadCrumbs', 'ClearScope', 'GetBasePath', 'ReturnToCaller', 'InventoryList', 'ProjectList', 'LookUpInit', 'CheckAccess', 'Wait', 'permissionsCategoryChange', 'fieldChoices', 'fieldLabels', + function($scope, $rootScope, $compile, $location, $log, $routeParams, permissionsForm, GenerateForm, Rest, Alert, ProcessErrors, LoadBreadCrumbs, ClearScope, GetBasePath, ReturnToCaller, InventoryList, ProjectList, LookUpInit, CheckAccess, Wait, permissionsCategoryChange, fieldChoices, fieldLabels) { ClearScope(); @@ -22,13 +22,14 @@ export default base = $location.path().replace(/^\//, '').split('/')[0], master = {}; - var permissionsChoice = permissionsChoices({ + var permissionsChoice = fieldChoices({ scope: $scope, - url: 'api/v1/' + base + '/' + id + '/permissions/' + url: 'api/v1/' + base + '/' + id + '/permissions/', + field: 'permission_type' }); permissionsChoice.then(function (choices) { - return permissionsLabel({ + return fieldLabels({ choices: choices }); }).then(function (choices) { diff --git a/awx/ui/client/src/permissions/edit/edit.controller.js b/awx/ui/client/src/permissions/edit/edit.controller.js index 1b1c6a552b..e209a309e2 100644 --- a/awx/ui/client/src/permissions/edit/edit.controller.js +++ b/awx/ui/client/src/permissions/edit/edit.controller.js @@ -10,8 +10,8 @@ * @description This controller for permissions edit */ export default - ['$scope', '$rootScope', '$compile', '$location', '$log', '$routeParams', 'permissionsForm', 'GenerateForm', 'Rest', 'Alert', 'ProcessErrors', 'LoadBreadCrumbs', 'ReturnToCaller', 'ClearScope', 'Prompt', 'GetBasePath', 'InventoryList', 'ProjectList', 'LookUpInit', 'CheckAccess', 'Wait', 'permissionsCategoryChange', 'permissionsChoices', 'permissionsLabel', - function($scope, $rootScope, $compile, $location, $log, $routeParams, permissionsForm, GenerateForm, Rest, Alert, ProcessErrors, LoadBreadCrumbs, ReturnToCaller, ClearScope, Prompt, GetBasePath, InventoryList, ProjectList, LookUpInit, CheckAccess, Wait, permissionsCategoryChange, permissionsChoices, permissionsLabel) { + ['$scope', '$rootScope', '$compile', '$location', '$log', '$routeParams', 'permissionsForm', 'GenerateForm', 'Rest', 'Alert', 'ProcessErrors', 'LoadBreadCrumbs', 'ReturnToCaller', 'ClearScope', 'Prompt', 'GetBasePath', 'InventoryList', 'ProjectList', 'LookUpInit', 'CheckAccess', 'Wait', 'permissionsCategoryChange', 'fieldChoices', 'fieldLabels', + function($scope, $rootScope, $compile, $location, $log, $routeParams, permissionsForm, GenerateForm, Rest, Alert, ProcessErrors, LoadBreadCrumbs, ReturnToCaller, ClearScope, Prompt, GetBasePath, InventoryList, ProjectList, LookUpInit, CheckAccess, Wait, permissionsCategoryChange, fieldChoices, fieldLabels) { ClearScope(); @@ -25,13 +25,14 @@ export default $scope.permission_label = {}; - var permissionsChoice = permissionsChoices({ + var permissionsChoice = fieldChoices({ scope: $scope, - url: 'api/v1/' + base + '/' + base_id + '/permissions/' + url: 'api/v1/' + base + '/' + base_id + '/permissions/', + field: 'permission_type' }); permissionsChoice.then(function (choices) { - return permissionsLabel({ + return fieldLabels({ choices: choices }); }).then(function (choices) { diff --git a/awx/ui/client/src/permissions/list/list.controller.js b/awx/ui/client/src/permissions/list/list.controller.js index 4e9ea4c716..341150b71b 100644 --- a/awx/ui/client/src/permissions/list/list.controller.js +++ b/awx/ui/client/src/permissions/list/list.controller.js @@ -12,8 +12,8 @@ export default - ['$scope', '$rootScope', '$location', '$log', '$routeParams', 'Rest', 'Alert', 'permissionsList', 'generateList', 'LoadBreadCrumbs', 'Prompt', 'SearchInit', 'PaginateInit', 'ReturnToCaller', 'ClearScope', 'ProcessErrors', 'GetBasePath', 'CheckAccess', 'Wait', 'permissionsChoices', 'permissionsLabel', 'permissionsSearchSelect', - function ($scope, $rootScope, $location, $log, $routeParams, Rest, Alert, permissionsList, GenerateList, LoadBreadCrumbs, Prompt, SearchInit, PaginateInit, ReturnToCaller, ClearScope, ProcessErrors, GetBasePath, CheckAccess, Wait, permissionsChoices, permissionsLabel, permissionsSearchSelect) { + ['$scope', '$rootScope', '$location', '$log', '$routeParams', 'Rest', 'Alert', 'permissionsList', 'generateList', 'LoadBreadCrumbs', 'Prompt', 'SearchInit', 'PaginateInit', 'ReturnToCaller', 'ClearScope', 'ProcessErrors', 'GetBasePath', 'CheckAccess', 'Wait', 'fieldChoices', 'fieldLabels', 'permissionsSearchSelect', + function ($scope, $rootScope, $location, $log, $routeParams, Rest, Alert, permissionsList, GenerateList, LoadBreadCrumbs, Prompt, SearchInit, PaginateInit, ReturnToCaller, ClearScope, ProcessErrors, GetBasePath, CheckAccess, Wait, fieldChoices, fieldLabels, permissionsSearchSelect) { ClearScope(); @@ -27,16 +27,17 @@ export default $scope.permission_search_select = []; // return a promise from the options request with the permission type choices (including adhoc) as a param - var permissionsChoice = permissionsChoices({ + var permissionsChoice = fieldChoices({ scope: $scope, - url: 'api/v1/' + base + '/' + base_id + '/permissions/' + url: 'api/v1/' + base + '/' + base_id + '/permissions/', + field: 'permission_type' }); // manipulate the choices from the options request to be set on // scope and be usable by the list form permissionsChoice.then(function (choices) { choices = - permissionsLabel({ + fieldLabels({ choices: choices }); _.map(choices, function(n, key) { diff --git a/awx/ui/client/src/permissions/main.js b/awx/ui/client/src/permissions/main.js index fef273cfa6..0bb913370d 100644 --- a/awx/ui/client/src/permissions/main.js +++ b/awx/ui/client/src/permissions/main.js @@ -12,8 +12,6 @@ import list from './shared/permissions.list'; import form from './shared/permissions.form'; import permissionsCategoryChange from './shared/category-change.factory'; -import permissionsChoices from './shared/get-choices.factory'; -import permissionsLabel from './shared/get-labels.factory'; import permissionsSearchSelect from './shared/get-search-select.factory'; export default @@ -25,6 +23,4 @@ export default .factory('permissionsList', list) .factory('permissionsForm', form) .factory('permissionsCategoryChange', permissionsCategoryChange) - .factory('permissionsChoices', permissionsChoices) - .factory('permissionsLabel', permissionsLabel) .factory('permissionsSearchSelect', permissionsSearchSelect); diff --git a/awx/ui/client/src/permissions/shared/get-choices.factory.js b/awx/ui/client/src/rest/get-choices.factory.js similarity index 66% rename from awx/ui/client/src/permissions/shared/get-choices.factory.js rename to awx/ui/client/src/rest/get-choices.factory.js index 8ad55907a5..33537d5ab4 100644 --- a/awx/ui/client/src/permissions/shared/get-choices.factory.js +++ b/awx/ui/client/src/rest/get-choices.factory.js @@ -16,24 +16,28 @@ ['Rest', 'ProcessErrors', function(Rest, ProcessErrors) { return function (params) { var scope = params.scope, - url = params.url; + url = params.url, + field = params.field; // 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.permission_type.choices; + var choices = data.actions.GET[field].choices; - // manually add the adhoc label to the choices object - 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 permission type labels. Options requrest returned status: ' + status }); + msg: 'Failed to get ' + field + ' labels. Options requrest returned status: ' + status }); }); }; }]; diff --git a/awx/ui/client/src/permissions/shared/get-labels.factory.js b/awx/ui/client/src/rest/get-labels.factory.js similarity index 100% rename from awx/ui/client/src/permissions/shared/get-labels.factory.js rename to awx/ui/client/src/rest/get-labels.factory.js diff --git a/awx/ui/client/src/rest/main.js b/awx/ui/client/src/rest/main.js index 335106af5d..5466355a12 100644 --- a/awx/ui/client/src/rest/main.js +++ b/awx/ui/client/src/rest/main.js @@ -6,6 +6,8 @@ import restServicesFactory from './restServices.factory'; import interceptors from './interceptors.service'; +import fieldChoices from './get-choices.factory'; +import fieldLabels from './get-labels.factory'; export default angular.module('RestServices', []) @@ -13,4 +15,6 @@ export default $httpProvider.interceptors.push('RestInterceptor'); }]) .factory('Rest', restServicesFactory) - .service('RestInterceptor', interceptors); + .service('RestInterceptor', interceptors) + .factory('fieldChoices', fieldChoices) + .factory('fieldLabels', fieldLabels);