Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Make the dialogUserController a little bit more API driven #6400

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,30 +1,47 @@
ManageIQ.angular.app.controller('dialogUserController', ['API', 'dialogFieldRefreshService', 'miqService', 'dialogUserSubmitErrorHandlerService', 'dialogId', 'apiSubmitEndpoint', 'apiAction', 'finishSubmitEndpoint', 'cancelEndpoint', 'resourceActionId', 'targetId', 'targetType', 'realTargetType', 'openUrl', '$http', '$window', 'dialogReplaceData', 'DialogData', function(API, dialogFieldRefreshService, miqService, dialogUserSubmitErrorHandlerService, dialogId, apiSubmitEndpoint, apiAction, finishSubmitEndpoint, cancelEndpoint, resourceActionId, targetId, targetType, realTargetType, openUrl, $http, $window, dialogReplaceData, DialogData) {
ManageIQ.angular.app.controller('dialogUserController', ['API', 'dialogFieldRefreshService', 'miqService', 'dialogUserSubmitErrorHandlerService', 'dialogId', 'apiSubmitEndpoint', 'apiAction', 'finishSubmitEndpoint', 'cancelEndpoint', 'resourceActionId', 'targetId', 'targetType', 'realTargetType', 'openUrl', '$http', '$window', 'dialogReplaceRequestId', 'DialogData', function(API, dialogFieldRefreshService, miqService, dialogUserSubmitErrorHandlerService, dialogId, apiSubmitEndpoint, apiAction, finishSubmitEndpoint, cancelEndpoint, resourceActionId, targetId, targetType, realTargetType, openUrl, $http, $window, dialogReplaceRequestId, DialogData) {
var vm = this;

vm.$onInit = function() {
var apiCall = new Promise(function(resolve) {
var url = '/api/service_dialogs/' + dialogId +
'?resource_action_id=' + resourceActionId +
'&target_id=' + targetId +
'&target_type=' + targetType;

resolve(API.get(url, {expand: 'resources', attributes: 'content'}).then(init));
});

Promise.resolve(apiCall).then(miqService.refreshSelectpicker);
var url = '/api/service_dialogs/' + dialogId +
'?resource_action_id=' + resourceActionId +
'&target_id=' + targetId +
'&target_type=' + targetType;

API.get(url, {expand: 'resources', attributes: 'content'})
.then(function (data) { vm._dialog = data.content[0] })
.then(loadServiceRequest)
.then(postprocess)
.then(function () {
vm.dialog = vm._dialog;
delete vm._dialog;
vm.dialogLoaded = true;
});
};

function init(dialog) {
vm.dialog = dialog.content[0];
vm.dialogLoaded = true;
function postprocess(replace) {
var promises = [];

_.forEach(vm.dialog.dialog_tabs, function(tab) {
_.forEach(vm._dialog.dialog_tabs, function(tab) {
_.forEach(tab.dialog_groups, function(group) {
_.forEach(group.dialog_fields, function(field) {
const replaceField = dialogReplaceData ? JSON.parse(dialogReplaceData).find(function (replace) { return replace.name === field.name }) : false;
if (replaceField) {
field.default_value = replaceField.value;
// If the service request has been copied, replace the fields' default values from the original service request
if (dialogReplaceRequestId) {
var replaceField = replace.find(function (item) { return item.name === field.name });
if (replaceField) {
field.default_value = replaceField.value;
}
}

// Load tag values from the API
if (field.type == 'DialogFieldTagControl') {
promises.push(API.get('/api/categories/' + field.options.category_id + '/tags?expand=resources&attributes=categorization').then(function(response) {
field.values = response.resources.map(function(tag) {
return { id: tag.id, name: tag.name, description: tag.categorization.description };
});
}));
}

// Translate default fields in all dropdowns
if (field.type === 'DialogFieldDropDownList') {
_.forEach(field.values, function(value) {
if (value[0] === null) {
Expand All @@ -35,7 +52,20 @@ ManageIQ.angular.app.controller('dialogUserController', ['API', 'dialogFieldRefr
});
});
});
}

// Wait for all promises to be resolved before returning
return Promise.all(promises);
};

function loadServiceRequest(data) {
if (dialogReplaceRequestId) {
return API.get('/api/service_requests/' + dialogReplaceRequestId + '?attributes=options').then(function(data) {
return _.map(data.options.dialog, function (val, key) {
return { name: key.split('dialog_').pop(), value: val };
});
});
}
};

vm.refreshField = refreshField;
vm.setDialogData = setDialogData;
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/miq_request_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def prov_copy
)

if req.kind_of?(ServiceTemplateProvisionRequest)
@dialog_replace_data = req.options[:dialog].map { |key, val| {:name => key.split('dialog_').last, :value => val } }.to_json
@dialog_replace_request_id = org_req.id
@new_dialog = true
template = find_record_with_rbac(ServiceTemplate, req.source_id)
resource_action = template.resource_actions.find { |r| r.action.downcase == 'provision' && r.dialog_id }
Expand Down
2 changes: 1 addition & 1 deletion app/views/shared/dialogs/_dialog_user.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@
ManageIQ.angular.app.value('apiAction', '#{j_str(api_action)}');
ManageIQ.angular.app.value('cancelEndpoint', '#{cancel_endpoint}');
ManageIQ.angular.app.value('openUrl', '#{open_url}');
ManageIQ.angular.app.value('dialogReplaceData', '#{@dialog_replace_data}');
ManageIQ.angular.app.value('dialogReplaceRequestId', '#{@dialog_replace_request_id}');
miq_bootstrap('.wrapper');
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('dialogUserController', function() {
targetType: 'targettype',
realTargetType: 'targettype',
openUrl: false,
dialogReplaceData: null,
dialogReplaceRequestId: null,
});

DialogData.data = {
Expand Down Expand Up @@ -129,7 +129,7 @@ describe('dialogUserController', function() {
realTargetType: 'targettype',
saveable: true,
openUrl: false,
dialogReplaceData: null,
dialogReplaceRequestId: null,
});

$controller.setDialogData({data: {field1: 'field1'}, validations: { isValid : true}});
Expand Down Expand Up @@ -165,7 +165,7 @@ describe('dialogUserController', function() {
realTargetType: 'targettype',
saveable: true,
openUrl: false,
dialogReplaceData: null,
dialogReplaceRequestId: null,
});

$controller.setDialogData({data: {field1: 'field1'}, validations: { isValid : true}});
Expand Down