reduce options reqests and get requests further

This commit is contained in:
John Mitchell
2017-04-25 18:58:12 -04:00
parent 2b57027067
commit 3731c8b72f
6 changed files with 107 additions and 96 deletions

View File

@@ -18,17 +18,17 @@ export default ['Rest', 'Wait',
function init() { function init() {
// Load the list of options for Kind // Load the list of options for Kind
GetChoices({ $scope.$parent.optionsDefer.promise
scope: $scope, .then(function(options) {
url: url, GetChoices({
field: 'kind', scope: $scope,
variable: 'credential_kind_options' url: url,
}); field: 'kind',
variable: 'credential_kind_options',
options: options
});
Rest.setUrl(url); if (!options.actions.POST) {
Rest.options()
.success(function(data) {
if (!data.actions.POST) {
$state.go("^"); $state.go("^");
Alert('Permission Error', 'You do not have permission to add a credential type.', 'alert-info'); Alert('Permission Error', 'You do not have permission to add a credential type.', 'alert-info');
} }

View File

@@ -6,14 +6,15 @@
export default ['Rest', 'Wait', export default ['Rest', 'Wait',
'CredentialTypesForm', 'ProcessErrors', 'GetBasePath', 'CredentialTypesForm', 'ProcessErrors', 'GetBasePath',
'GenerateForm', 'credential_typeData', 'GenerateForm', 'resourceData',
'$scope', '$state', 'GetChoices', 'ParseTypeChange', 'ToJSON', 'ParseVariableString', 'CreateSelect2', '$scope', '$state', 'GetChoices', 'ParseTypeChange', 'ToJSON', 'ParseVariableString', 'CreateSelect2',
function( function(
Rest, Wait, CredentialTypesForm, ProcessErrors, GetBasePath, Rest, Wait, CredentialTypesForm, ProcessErrors, GetBasePath,
GenerateForm, credential_typeData, GenerateForm, resourceData,
$scope, $state, GetChoices, ParseTypeChange, ToJSON, ParseVariableString, CreateSelect2 $scope, $state, GetChoices, ParseTypeChange, ToJSON, ParseVariableString, CreateSelect2
) { ) {
var generator = GenerateForm, var credential_typeData = resourceData.data,
generator = GenerateForm,
data = credential_typeData, data = credential_typeData,
id = credential_typeData.id, id = credential_typeData.id,
form = CredentialTypesForm, form = CredentialTypesForm,
@@ -24,13 +25,17 @@ export default ['Rest', 'Wait',
function init() { function init() {
// Load the list of options for Kind // Load the list of options for Kind
GetChoices({ $scope.$parent.optionsDefer.promise
scope: $scope, .then(function(options) {
url: url, GetChoices({
field: 'kind', scope: $scope,
variable: 'credential_kind_options', url: url,
callback: 'choicesReadyCredentialTypes' field: 'kind',
}); variable: 'credential_kind_options',
options: options,
callback: 'choicesReadyCredentialTypes'
});
});
} }
if ($scope.removeChoicesReady) { if ($scope.removeChoicesReady) {
@@ -38,6 +43,7 @@ export default ['Rest', 'Wait',
} }
$scope.removeChoicesReady = $scope.$on('choicesReadyCredentialTypes', $scope.removeChoicesReady = $scope.$on('choicesReadyCredentialTypes',
function() { function() {
$scope.credential_type = credential_typeData; $scope.credential_type = credential_typeData;
$scope.$watch('credential_type.summary_fields.user_capabilities.edit', function(val) { $scope.$watch('credential_type.summary_fields.user_capabilities.edit', function(val) {

View File

@@ -5,10 +5,10 @@
*************************************************/ *************************************************/
export default ['$rootScope', '$scope', 'Wait', 'CredentialTypesList', 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( function(
$rootScope, $scope, Wait, CredentialTypesList, $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'), var defaultUrl = GetBasePath('credential_types'),
list = CredentialTypesList; list = CredentialTypesList;
@@ -16,6 +16,8 @@ export default ['$rootScope', '$scope', 'Wait', 'CredentialTypesList',
init(); init();
function init() { function init() {
$scope.optionsDefer = $q.defer();
if (!($rootScope.user_is_superuser || $rootScope.user_is_system_auditor)) { if (!($rootScope.user_is_superuser || $rootScope.user_is_system_auditor)) {
$state.go("setup"); $state.go("setup");
Alert('Permission Error', 'You do not have permission to view, edit or create custom credential types.', 'alert-info'); 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) { .then(function(params) {
$scope.canAdd = params.canAdd; $scope.canAdd = params.canAdd;
$scope.options = params.options; $scope.options = params.options;
$scope.optionsDefer.resolve(params.options);
optionsRequestDataProcessing(); optionsRequestDataProcessing();
}); });

View File

@@ -36,27 +36,6 @@ angular.module('credentialTypes', [
add: 'CredentialTypesAddController', add: 'CredentialTypesAddController',
edit: 'CredentialTypesEditController' 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: { data: {
activityStream: true, 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 activityStreamTarget: 'custom_inventory_script' // TODO: change to 'credential_type'...there's probably more work that needs to be done to hook up activity stream

View File

@@ -17,27 +17,41 @@
return function (params) { return function (params) {
var scope = params.scope, var scope = params.scope,
url = params.url, url = params.url,
field = params.field; field = params.field,
options = params.options;
// Auto populate the field if there is only one result if (!options) {
Rest.setUrl(url); // Auto populate the field if there is only one result
return Rest.options() Rest.setUrl(url);
.then(function (data) { return Rest.options()
data = data.data; .then(function (data) {
var choices = data.actions.GET[field].choices; data = data.data;
var choices = data.actions.GET[field].choices;
// manually add the adhoc label to the choices object if // manually add the adhoc label to the choices object if
// the permission_type field // the permission_type field
if (field === "permission_type") { if (field === "permission_type") {
choices.push(["adhoc", choices.push(["adhoc",
data.actions.GET.run_ad_hoc_commands.help_text]); data.actions.GET.run_ad_hoc_commands.help_text]);
} }
return choices; return choices;
}) })
.catch(function (data, status) { .catch(function (data, status) {
ProcessErrors(scope, data, status, null, { hdr: 'Error!', ProcessErrors(scope, data, status, null, { hdr: 'Error!',
msg: 'Failed to get ' + field + ' labels. Options requrest returned status: ' + status }); 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;
}
}; };
}]; }];

View File

@@ -720,7 +720,8 @@ angular.module('Utilities', ['RestServices', 'Utilities'])
field = params.field, field = params.field,
variable = params.variable, variable = params.variable,
callback = params.callback, callback = params.callback,
choice_name = params.choice_name; choice_name = params.choice_name,
options = params.options
if (scope[variable]) { if (scope[variable]) {
scope[variable].length = 0; scope[variable].length = 0;
@@ -728,42 +729,50 @@ angular.module('Utilities', ['RestServices', 'Utilities'])
scope[variable] = []; scope[variable] = [];
} }
Rest.setUrl(url); var withOptions = function(options) {
Rest.options() var choices, defaultChoice;
.success(function(data) { choices = (choice_name) ? options.actions.GET[field][choice_name] : options.actions.GET[field].choices;
var choices, defaultChoice; if (options && options.actions && options.actions.POST && options.actions.POST[field]) {
choices = (choice_name) ? data.actions.GET[field][choice_name] : data.actions.GET[field].choices; defaultChoice = options.actions.POST[field].default;
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
if (choices) { choices.forEach(function(choice) {
// including 'name' property so list can be used by search scope[variable].push({
choices.forEach(function(choice) { label: choice[1],
scope[variable].push({ value: choice[0],
label: choice[1], name: choice[1]
value: choice[0],
name: choice[1]
});
}); });
} });
if (defaultChoice !== undefined) { }
var val; if (defaultChoice !== undefined) {
for (val in scope[variable]) { var val;
if (scope[variable][val].value === defaultChoice) { for (val in scope[variable]) {
scope[variable][val].isDefault = true; if (scope[variable][val].value === defaultChoice) {
} scope[variable][val].isDefault = true;
} }
} }
if (callback) { }
scope.$emit(callback); if (callback) {
} scope.$emit(callback);
}) }
.error(function(data, status) { }
ProcessErrors(scope, data, status, null, {
hdr: 'Error!', if (!options) {
msg: 'Failed to get ' + url + '. GET status: ' + status 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);
}
}; };
} }
]) ])