wire in custom template messages on edit form

This commit is contained in:
Keith Grant
2019-06-21 11:50:29 -07:00
committed by Jim Ladd
parent b80ca62072
commit a56a6d7158
5 changed files with 89 additions and 42 deletions

View File

@@ -53,7 +53,7 @@ function atSyntaxHighlightController ($scope, AngularCodeMirror) {
init(); init();
}); });
$scope.$on('reset-code-mirror', () => { $scope.$on('reset-code-mirror', () => {
initCodeMirror(); setImmediate(initCodeMirror);
}); });
} }

View File

@@ -7,12 +7,13 @@
export default ['Rest', 'Wait', 'NotificationsFormObject', export default ['Rest', 'Wait', 'NotificationsFormObject',
'ProcessErrors', 'GetBasePath', 'Alert', 'ProcessErrors', 'GetBasePath', 'Alert',
'GenerateForm', '$scope', '$state', 'CreateSelect2', 'GetChoices', 'GenerateForm', '$scope', '$state', 'CreateSelect2', 'GetChoices',
'NotificationsTypeChange', 'ParseTypeChange', 'i18n', 'NotificationsTypeChange', 'ParseTypeChange', 'i18n', 'MessageUtils',
function( function(
Rest, Wait, NotificationsFormObject, Rest, Wait, NotificationsFormObject,
ProcessErrors, GetBasePath, Alert, ProcessErrors, GetBasePath, Alert,
GenerateForm, $scope, $state, CreateSelect2, GetChoices, GenerateForm, $scope, $state, CreateSelect2, GetChoices,
NotificationsTypeChange, ParseTypeChange, i18n NotificationsTypeChange, ParseTypeChange, i18n,
MessageUtils
) { ) {
var generator = GenerateForm, var generator = GenerateForm,
@@ -31,9 +32,11 @@ export default ['Rest', 'Wait', 'NotificationsFormObject',
Alert('Permission Error', 'You do not have permission to add a notification template.', 'alert-info'); Alert('Permission Error', 'You do not have permission to add a notification template.', 'alert-info');
} }
// TODO: get default messages off data // TODO: get default messages off data
$scope.start_message = defaultMessages.start_message = 'It started'; console.log('OPTIONS:', data);
$scope.success_message = defaultMessages.success_message = 'It succeeded'; defaultMessages.start_message = 'It started';
$scope.error_message = defaultMessages.error_message = 'It failed'; defaultMessages.success_message = 'It succeeded';
defaultMessages.error_message = 'It failed';
MessageUtils.setMessagesOnScope($scope, null, defaultMessages);
}); });
// apply form definition's default field values // apply form definition's default field values
GenerateForm.applyDefaults(form, $scope); GenerateForm.applyDefaults(form, $scope);
@@ -189,29 +192,6 @@ export default ['Rest', 'Wait', 'NotificationsFormObject',
} }
}; };
function getMessages () {
return {
start: {
message: $scope.start_message === defaultMessages.start_message
? null : $scope.start_message,
body: $scope.start_body === defaultMessages.start_body
? null : $scope.start_body,
},
success: {
message: $scope.success_message === defaultMessages.success_message
? null : $scope.success_message,
body: $scope.success_body === defaultMessages.success_body
? null : $scope.success_body,
},
error: {
message: $scope.error_message === defaultMessages.error_message
? null : $scope.error_message,
body: $scope.error_body === defaultMessages.error_body
? null : $scope.error_body,
}
};
}
// Save // Save
$scope.formSave = function() { $scope.formSave = function() {
var params, var params,
@@ -222,7 +202,7 @@ export default ['Rest', 'Wait', 'NotificationsFormObject',
"name": $scope.name, "name": $scope.name,
"description": $scope.description, "description": $scope.description,
"organization": $scope.organization, "organization": $scope.organization,
"messages": $scope.customize_messages ? getMessages() : null, "messages": MessageUtils.getMessagesObj($scope, defaultMessages),
"notification_type": v, "notification_type": v,
"notification_configuration": {} "notification_configuration": {}
}; };

View File

@@ -10,19 +10,22 @@ export default ['Rest', 'Wait',
'notification_template', 'notification_template',
'$scope', '$state', 'GetChoices', 'CreateSelect2', 'Empty', '$scope', '$state', 'GetChoices', 'CreateSelect2', 'Empty',
'NotificationsTypeChange', 'ParseTypeChange', 'i18n', 'NotificationsTypeChange', 'ParseTypeChange', 'i18n',
'MessageUtils',
function( function(
Rest, Wait, Rest, Wait,
NotificationsFormObject, ProcessErrors, GetBasePath, NotificationsFormObject, ProcessErrors, GetBasePath,
GenerateForm, GenerateForm,
notification_template, notification_template,
$scope, $state, GetChoices, CreateSelect2, Empty, $scope, $state, GetChoices, CreateSelect2, Empty,
NotificationsTypeChange, ParseTypeChange, i18n NotificationsTypeChange, ParseTypeChange, i18n,
MessageUtils
) { ) {
var generator = GenerateForm, var generator = GenerateForm,
id = notification_template.id, id = notification_template.id,
form = NotificationsFormObject, form = NotificationsFormObject,
master = {}, master = {},
url = GetBasePath('notification_templates'); url = GetBasePath('notification_templates'),
defaultMessages = {};
init(); init();
@@ -35,6 +38,11 @@ export default ['Rest', 'Wait',
} }
}); });
// TODO: get OPTIONS for defaultMessages
defaultMessages.start_message = 'It started';
defaultMessages.success_message = 'It succeeded';
defaultMessages.error_message = 'It failed';
GetChoices({ GetChoices({
scope: $scope, scope: $scope,
url: url, url: url,
@@ -165,6 +173,9 @@ export default ['Rest', 'Wait',
field_id: 'notification_template_headers', field_id: 'notification_template_headers',
readOnly: !$scope.notification_template.summary_fields.user_capabilities.edit readOnly: !$scope.notification_template.summary_fields.user_capabilities.edit
}); });
MessageUtils.setMessagesOnScope($scope, data.messages, defaultMessages);
Wait('stop'); Wait('stop');
}) })
.catch(({data, status}) => { .catch(({data, status}) => {
@@ -175,8 +186,6 @@ export default ['Rest', 'Wait',
}); });
}); });
$scope.$watch('headers', function validate_headers(str) { $scope.$watch('headers', function validate_headers(str) {
try { try {
let headers = JSON.parse(str); let headers = JSON.parse(str);
@@ -277,14 +286,7 @@ export default ['Rest', 'Wait',
"name": $scope.name, "name": $scope.name,
"description": $scope.description, "description": $scope.description,
"organization": $scope.organization, "organization": $scope.organization,
"messages": $scope.customize_messages ? { "messages": MessageUtils.getMessagesObj($scope, defaultMessages),
start_message: $scope.start_message,
start_body: $scope.start_body,
success_message: $scope.success_message,
success_body: $scope.success_body,
error_message: $scope.error_message,
error_body: $scope.error_body,
} : null,
"notification_type": v, "notification_type": v,
"notification_configuration": {} "notification_configuration": {}
}; };

View File

@@ -15,6 +15,7 @@ import notificationsList from './notifications.list';
import toggleNotification from './shared/toggle-notification.factory'; import toggleNotification from './shared/toggle-notification.factory';
import notificationsListInit from './shared/notification-list-init.factory'; import notificationsListInit from './shared/notification-list-init.factory';
import typeChange from './shared/type-change.service'; import typeChange from './shared/type-change.service';
import messageUtils from './shared/message-utils.service';
import { N_ } from '../i18n'; import { N_ } from '../i18n';
export default export default
@@ -29,6 +30,7 @@ angular.module('notifications', [
.factory('ToggleNotification', toggleNotification) .factory('ToggleNotification', toggleNotification)
.factory('NotificationsListInit', notificationsListInit) .factory('NotificationsListInit', notificationsListInit)
.service('NotificationsTypeChange', typeChange) .service('NotificationsTypeChange', typeChange)
.service('MessageUtils', messageUtils)
.config(['$stateProvider', 'stateDefinitionsProvider', .config(['$stateProvider', 'stateDefinitionsProvider',
function($stateProvider, stateDefinitionsProvider) { function($stateProvider, stateDefinitionsProvider) {
let stateDefinitions = stateDefinitionsProvider.$get(); let stateDefinitions = stateDefinitionsProvider.$get();

View File

@@ -0,0 +1,63 @@
export default [function () {
return {
getMessagesObj: function ($scope, defaultMessages) {
if (!$scope.customize_messages) {
return null;
}
return {
start: {
message: $scope.start_message === defaultMessages.start_message
? null : $scope.start_message,
body: $scope.start_body === defaultMessages.start_body
? null : $scope.start_body,
},
success: {
message: $scope.success_message === defaultMessages.success_message
? null : $scope.success_message,
body: $scope.success_body === defaultMessages.success_body
? null : $scope.success_body,
},
error: {
message: $scope.error_message === defaultMessages.error_message
? null : $scope.error_message,
body: $scope.error_body === defaultMessages.error_body
? null : $scope.error_body,
}
};
},
setMessagesOnScope: function ($scope, messages, defaultMessages) {
$scope.start_message = defaultMessages.start_message;
$scope.success_message = defaultMessages.success_message;
$scope.error_message = defaultMessages.error_message;
if (!messages) {
return;
}
let customized = false;
if (messages.start.message) {
customized = true;
$scope.start_message = messages.start.message;
}
if (messages.start.body) {
customized = true;
$scope.start_body = messages.start.body;
}
if (messages.success.message) {
customized = true;
$scope.success_message = messages.success.message;
}
if (messages.success.body) {
customized = true;
$scope.success_body = messages.success.body;
}
if (messages.error.message) {
customized = true;
$scope.error_message = messages.error.message;
}
if (messages.error.body) {
customized = true;
$scope.error_body = messages.error.body;
}
$scope.customize_messages = customized;
}
}
}]