diff --git a/awx/ui/client/src/notifications/add/add.controller.js b/awx/ui/client/src/notifications/add/add.controller.js index b7fc7b6d0e..58dacc5405 100644 --- a/awx/ui/client/src/notifications/add/add.controller.js +++ b/awx/ui/client/src/notifications/add/add.controller.js @@ -32,7 +32,7 @@ export default ['Rest', 'Wait', 'NotificationsFormObject', $state.go("^"); Alert('Permission Error', 'You do not have permission to add a notification template.', 'alert-info'); } - defaultMessages = data.actions.POST.messages.default; + defaultMessages = data.actions.GET.messages; MessageUtils.setMessagesOnScope($scope, null, defaultMessages); }); // apply form definition's default field values @@ -168,8 +168,13 @@ export default ['Rest', 'Wait', 'NotificationsFormObject', $scope.toggleForm = function(key) { $scope[key] = !$scope[key]; }; - $scope.$watch('notification_type', (value) => { - if (value) { + $scope.$watch('notification_type', (newValue, oldValue = {}) => { + if (newValue) { + MessageUtils.updateDefaultsOnScope( + $scope, + defaultMessages[oldValue.value], + defaultMessages[newValue.value] + ); $scope.$broadcast('reset-code-mirror', { customize_messages: $scope.customize_messages, }); diff --git a/awx/ui/client/src/notifications/edit/edit.controller.js b/awx/ui/client/src/notifications/edit/edit.controller.js index 2f5796c98a..8128cff488 100644 --- a/awx/ui/client/src/notifications/edit/edit.controller.js +++ b/awx/ui/client/src/notifications/edit/edit.controller.js @@ -41,7 +41,7 @@ export default ['Rest', 'Wait', Rest.setUrl(GetBasePath('notification_templates')); Rest.options() .then(({data}) => { - defaultMessages = data.actions.GET.messages.default; + defaultMessages = data.actions.GET.messages; }); GetChoices({ @@ -257,8 +257,13 @@ export default ['Rest', 'Wait', $scope.toggleForm = function(key) { $scope[key] = !$scope[key]; }; - $scope.$watch('notification_type', (value) => { - if (value) { + $scope.$watch('notification_type', (newValue, oldValue = {}) => { + if (newValue) { + MessageUtils.updateDefaultsOnScope( + $scope, + defaultMessages[oldValue.value], + defaultMessages[newValue.value] + ); $scope.$broadcast('reset-code-mirror', { customize_messages: $scope.customize_messages, }); diff --git a/awx/ui/client/src/notifications/shared/message-utils.service.js b/awx/ui/client/src/notifications/shared/message-utils.service.js index 52076626a1..fff8de847b 100644 --- a/awx/ui/client/src/notifications/shared/message-utils.service.js +++ b/awx/ui/client/src/notifications/shared/message-utils.service.js @@ -1,66 +1,111 @@ -export default [function () { + +const emptyDefaults = { + started: { + message: '', + body: '', + }, + success: { + message: '', + body: '', + }, + error: { + message: '', + body: '', + }, +} + +export default [function() { return { getMessagesObj: function ($scope, defaultMessages) { if (!$scope.customize_messages) { return null; } + const defaults = defaultMessages[$scope.notification_type.value] || {}; return { started: { - message: $scope.started_message === defaultMessages.started.message ? + message: $scope.started_message === defaults.started.message ? null : $scope.started_message, - body: $scope.started_body === defaultMessages.started.body ? + body: $scope.started_body === defaults.started.body ? null : $scope.started_body, }, success: { - message: $scope.success_message === defaultMessages.success.message ? + message: $scope.success_message === defaults.success.message ? null : $scope.success_message, - body: $scope.success_body === defaultMessages.success.body ? + body: $scope.success_body === defaults.success.body ? null : $scope.success_body, }, error: { - message: $scope.error_message === defaultMessages.error.message ? + message: $scope.error_message === defaults.error.message ? null : $scope.error_message, - body: $scope.error_body === defaultMessages.error.body ? + body: $scope.error_body === defaults.error.body ? null : $scope.error_body, } }; }, + setMessagesOnScope: function ($scope, messages, defaultMessages) { - $scope.started_message = defaultMessages.started.message; - $scope.started_body = defaultMessages.started.body; - $scope.success_message = defaultMessages.success.message; - $scope.success_body = defaultMessages.success.body; - $scope.error_message = defaultMessages.error.message; - $scope.error_body = defaultMessages.error.body; - if (!messages) { - return; - } - let isCustomized = false; - if (messages.started.message) { - isCustomized = true; - $scope.started_message = messages.started.message; - } - if (messages.started.body) { - isCustomized = true; - $scope.started_body = messages.started.body; - } - if (messages.success.message) { - isCustomized = true; - $scope.success_message = messages.success.message; - } - if (messages.success.body) { - isCustomized = true; - $scope.success_body = messages.success.body; - } - if (messages.error.message) { - isCustomized = true; - $scope.error_message = messages.error.message; - } - if (messages.error.body) { - isCustomized = true; - $scope.error_body = messages.error.body; - } - $scope.customize_messages = isCustomized; + let defaults; + if ($scope.notification_type) { + defaults = defaultMessages[$scope.notification_type.value] || emptyDefaults; + } else { + defaults = emptyDefaults; + } + $scope.started_message = defaults.started.message; + $scope.started_body = defaults.started.body; + $scope.success_message = defaults.success.message; + $scope.success_body = defaults.success.body; + $scope.error_message = defaults.error.message; + $scope.error_body = defaults.error.body; + if (!messages) { + return; + } + let isCustomized = false; + if (messages.started.message) { + isCustomized = true; + $scope.started_message = messages.started.message; + } + if (messages.started.body) { + isCustomized = true; + $scope.started_body = messages.started.body; + } + if (messages.success.message) { + isCustomized = true; + $scope.success_message = messages.success.message; + } + if (messages.success.body) { + isCustomized = true; + $scope.success_body = messages.success.body; + } + if (messages.error.message) { + isCustomized = true; + $scope.error_message = messages.error.message; + } + if (messages.error.body) { + isCustomized = true; + $scope.error_body = messages.error.body; + } + $scope.customize_messages = isCustomized; + }, + + updateDefaultsOnScope: function($scope, oldDefaults = emptyDefaults, newDefaults) { + if ($scope.started_message === oldDefaults.started.message) { + $scope.started_message = newDefaults.started.message; + } + if ($scope.started_body === oldDefaults.started.body) { + $scope.started_body = newDefaults.started.body; + } + if ($scope.success_message === oldDefaults.success.message) { + $scope.success_message = newDefaults.success.message; + } + if ($scope.success_body === oldDefaults.success.body) { + $scope.success_body = newDefaults.success.body; + } + if ($scope.error_message === oldDefaults.error.message) { + $scope.error_message = newDefaults.error.message; + } + if ($scope.error_body === oldDefaults.error.body) { + $scope.error_body = newDefaults.error.body; + } } }; }];