-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDefault.js
More file actions
572 lines (505 loc) · 21.3 KB
/
Default.js
File metadata and controls
572 lines (505 loc) · 21.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
$(function () {
const emptyGuid = '00000000-0000-0000-0000-000000000000';
const UIElements = {
applicationId: $('#DetailsViewApplicationId')[0].value,
btnSend: $('#btn-send'),
btnSave: $('#btn-save'),
btnDiscard: $('#btn-send-discard'),
btnConfirmSend: $('#btn-confirm-send'),
btnCancelEmail: $('#btn-cancel-email'),
btnNewEmail: $('#btn-new-email'),
btnSendClose: $('#btn-send-close'),
emailForm: $('#EmailForm'),
inputEmailId: $('#EmailId'),
inputEmailTo: $($('#EmailTo')[0]),
inputEmailToField: $('#EmailTo')[0],
inputEmailFrom: $($('#EmailFrom')[0]),
inputEmailSubject: $($('#EmailSubject')[0]),
inputEmailBody: $($('#EmailBody')[0]),
inputOriginalEmailTo: $($('#OriginalDraftEmailTo')[0]),
inputOriginalEmailFrom: $($('#OriginalDraftEmailFrom')[0]),
inputOriginalEmailSubject: $($('#OriginalDraftEmailSubject')[0]),
inputOriginalEmailBody: $($('#OriginalDraftEmailBody')[0]),
emailSpinner: $('#spinner-modal'),
confirmationModal: $('#confirmation-modal'),
alertEmailReadonly: $('#email-alert-readonly')
};
let defaultValues = {
emailTo: '',
emailFrom: ''
};
let applicationDetails;
let mappingConfig;
let editorInstance;
function bindUIEvents() {
UIElements.btnNewEmail.on('click', handleNewEmail);
UIElements.btnSend.on('click', handleSendEmail);
UIElements.btnSave.on('click', handleSaveEmail);
UIElements.btnDiscard.on('click', handleDiscardEmail);
UIElements.btnConfirmSend.on('click', handleConfirmSendEmail);
UIElements.btnCancelEmail.on('click', handleCancelEmailSend);
UIElements.btnSendClose.on('click', handleCloseEmail);
UIElements.inputEmailSubject.on('change', handleKeyUpTrim);
UIElements.inputEmailFrom.on('change', handleKeyUpTrim);
UIElements.inputEmailBody.on('change', handleKeyUpTrim);
UIElements.inputEmailTo.on('change', validateEmailTo);
UIElements.inputEmailTo.on('input', handleDraftChange);
UIElements.inputEmailFrom.on('input', handleDraftChange);
UIElements.inputEmailSubject.on('input', handleDraftChange);
UIElements.inputEmailBody.on('input', handleDraftChange);
}
init();
function init() {
bindUIEvents();
defaultValues.emailTo = UIElements.inputOriginalEmailTo.val();
defaultValues.emailFrom = UIElements.inputOriginalEmailFrom.val();
toastr.options.positionClass = 'toast-top-center';
initTemplateDetails();
$('#templateTextContainer').hide();
}
async function initTemplateDetails() {
applicationDetails = await loadApplicationDetails();
mappingConfig = await getTemplateVariables();
}
function disableEmail() {
UIElements.btnSend.attr('disabled', true);
UIElements.btnSave.attr('disabled', true);
UIElements.btnDiscard.attr('disabled', true);
UIElements.inputEmailTo.attr('disabled', true);
UIElements.inputEmailFrom.attr('disabled', true);
UIElements.inputEmailSubject.attr('disabled', true);
UIElements.inputEmailBody.attr('disabled', true);
}
function enableEmail() {
UIElements.btnSend.attr('disabled', false);
UIElements.btnSave.attr('disabled', false);
UIElements.btnDiscard.attr('disabled', false);
UIElements.inputEmailTo.attr('disabled', false);
UIElements.inputEmailFrom.attr('disabled', false);
UIElements.inputEmailSubject.attr('disabled', false);
UIElements.inputEmailBody.attr('disabled', false);
}
function handleKeyUpTrim(e) {
let trimmedString = e.currentTarget.value.trim();
e.currentTarget.value = trimmedString;
}
function handleCloseEmail() {
$('#modal-content, #modal-background').removeClass('active');
UIElements.emailForm.removeClass('active');
UIElements.btnNewEmail.removeClass('hide');
UIElements.alertEmailReadonly.removeClass('hide');
UIElements.emailForm.trigger("reset");
enableEmail();
}
function handleDiscardEmail() {
UIElements.inputEmailTo.val(UIElements.inputOriginalEmailTo.val());
UIElements.inputEmailFrom.val(UIElements.inputOriginalEmailFrom.val());
UIElements.inputEmailSubject.val(UIElements.inputOriginalEmailSubject.val());
UIElements.inputEmailBody.val(UIElements.inputOriginalEmailBody.val());
}
function handleCancelEmailSend() {
$('#modal-content, #modal-background').removeClass('active');
}
function getToolbarOptions() {
return 'undo redo | styles | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist | link image | code preview';
}
function getPlugins() {
return 'lists link image preview code';
}
function resetEmailBody() {
const id = 'EmailBody';
// 1. Remove any existing editor instance
if (tinymce.get(id)) {
tinymce.get(id).remove();
}
// 2. Clear the underlying <textarea>
$('#' + id).val(''); // ← this line prevents the old HTML from coming back
}
function handleNewEmail() {
resetEmailBody();
$('#templateListContainer').show();
$('#templateTextContainer').hide();
UIElements.inputEmailId.val(emptyGuid);
// Support discard to empty email template for new emails
UIElements.inputOriginalEmailTo.val(defaultValues.emailTo);
UIElements.inputOriginalEmailFrom.val(defaultValues.emailFrom);
UIElements.inputOriginalEmailSubject.val("");
UIElements.inputOriginalEmailBody.val("");
if (tinymce.get("EmailBody")) {
tinymce.get("EmailBody").remove(); // remove existing instance
}
tinymce.init({
license_key: 'gpl',
selector: `#EmailBody`,
plugins: getPlugins(),
toolbar: getToolbarOptions(),
statusbar: false,
promotion: false,
content_css: false,
skin: false,
setup: function (editor) {
editor.on("input", (e) => {
UIElements.inputEmailBody.val(editor.getContent())
handleDraftChange();
});
editorInstance = editor;
editorInstance.setContent('');
}
});
console.log(editorInstance.getContent())
handleDraftChange();
showModalEmail();
resetValidationErrors();
}
function showModalEmail() {
UIElements.emailForm.addClass('active');
UIElements.btnNewEmail.addClass('hide');
UIElements.alertEmailReadonly.addClass('hide');
}
function validateEmail(email) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.exec(String(email).toLowerCase()) !== null;
}
function validateEmailTo() {
let emailValue = UIElements.inputEmailToField.value.trim(); // Trim leading/trailing whitespace
// Remove trailing commas, semicolons, or spaces (safe regex)
emailValue = emailValue.replace(/[;,\s]+$/, ''); // Remove only trailing semicolons, commas, or spaces
let emails = emailValue.split(/[,;]/g).map(email => email.trim()); // Split by comma or semicolon, and trim each email
let emailToErrorSpan = $("span[data-valmsg-for*='EmailTo']")[0];
// Initialize as valid
let isValid = true;
let emailToError = ''; // Initialize error message variable
// Iterate through the list of emails using for...of loop
for (let emailStr of emails) {
// Check if the email is empty or invalid
if (emailStr === '' || !validateEmail(emailStr)) {
// Handle empty email input
if (emailStr === '') {
emailToError = emailValue.length > 0
? 'An email is required after each comma or semicolon.'
: 'The Email To field is required.';
} else {
// Handle invalid email format
emailToError = `Please enter a valid Email To: ${emailStr}`;
}
// Display the error message
$(emailToErrorSpan).addClass('field-validation-error').removeClass('field-validation-valid');
$(emailToErrorSpan).html(emailToError);
// Mark the validation as invalid and exit the loop
isValid = false;
break; // No need to continue checking further emails
}
}
// Clear error message if all emails are valid
if (isValid) {
$(emailToErrorSpan).addClass('field-validation-valid').removeClass('field-validation-error');
$(emailToErrorSpan).html(''); // Clear any existing error message
}
return isValid;
}
function handleConfirmSendEmail() {
UIElements.confirmationModal.hide();
UIElements.emailSpinner.show();
let templateName = '';
if (!UIElements.inputEmailId[0].value || UIElements.inputEmailId[0].value === emptyGuid) {
// id is either null/undefined/"" OR the empty GUID
templateName = $("#template option:selected").text();
if (!templateName || templateName == '' || templateName == 'Please select') {
// id is either null/undefined/"" OR the empty GUID
templateName = "No Template Selected"
}
}
else {
templateName = $('#templateText').val();
}
unity.grantManager.emails.email
.create({
emailId: UIElements.inputEmailId[0].value,
applicationId: UIElements.applicationId,
emailTo: UIElements.inputEmailTo[0].value,
emailFrom: UIElements.inputEmailFrom[0].value,
emailBody: editorInstance.getContent(),
emailSubject: UIElements.inputEmailSubject[0].value,
currentUserId: decodeURIComponent(abp.currentUser.id),
emailTemplateName: templateName,
})
.then(function () {
hideConfirmation();
handleCloseEmail();
abp.notify.success('Your email is being sent');
PubSub.publish('refresh_application_emails');
}).catch(function () {
hideConfirmation();
abp.notify.error('An error ocurred your email could not be sent.');
});
}
function hideConfirmation() {
UIElements.confirmationModal.hide();
UIElements.emailSpinner.hide();
$('#modal-content, #modal-background').removeClass('active');
}
function showConfirmation() {
UIElements.confirmationModal.show();
$('#modal-content, #modal-background').addClass('active');
}
function handleSaveEmail(e) {
if (validateEmailForm(e)) {
let templateName = '';
if (!UIElements.inputEmailId[0].value || UIElements.inputEmailId[0].value === emptyGuid) {
// id is either null/undefined/"" OR the empty GUID
templateName = $("#template option:selected").text();
if (!templateName || templateName == '' || templateName == 'Please select') {
// id is either null/undefined/"" OR the empty GUID
templateName = "No Template Selected"
}
}
else {
templateName = $('#templateText').val();
}
unity.grantManager.emails.email
.saveDraft({
emailId: UIElements.inputEmailId[0].value,
applicationId: UIElements.applicationId,
emailTo: UIElements.inputEmailTo[0].value,
emailFrom: UIElements.inputEmailFrom[0].value,
emailBody: editorInstance.getContent(),
emailSubject: UIElements.inputEmailSubject[0].value,
currentUserId: decodeURIComponent(abp.currentUser.id),
emailTemplateName: templateName,
})
.then(function () {
handleCloseEmail();
abp.notify.success('Your email has been saved.');
PubSub.publish('refresh_application_emails');
}).catch(function () {
abp.notify.error('An error ocurred your email could not be saved.');
});
}
else {
return false;
}
}
function validateEmailForm(e) {
// Prevent form submission and stop propagation
e.stopPropagation();
e.preventDefault();
let isValid = UIElements.emailForm.valid();
let validator = UIElements.emailForm.validate();
// Validate the "Email To" field
if (!validateEmailTo()) {
return false; // If validation fails, stop further processing
}
let fieldName = 'EmailBody';
let errorList = validator.errorList;
let tinymceContent = tinymce.get(fieldName).getContent({ format: 'text' }).trim();
let onlyErrorIsTinyMCE = (
errorList.length === 1 &&
errorList[0].element.name === fieldName &&
tinymceContent.length > 0
);
if (!isValid && onlyErrorIsTinyMCE) {
let fieldElement = $('textarea[name="' + fieldName + '"]');
// Remove error message (if inserted by validator)
fieldElement.removeClass('input-validation-error'); // if ABP uses this
fieldElement.siblings('.field-validation-error').remove(); // for inline messages
return true;
// Proceed with your form logic here
// ...
} else if (isValid) {
return true;
} else {
return false;
}
}
function handleSendEmail(e) {
// Check if the form is valid
if (validateEmailForm(e)) {
showConfirmation(); // Show confirmation if the form is valid
return true; // Return true to indicate success
}
// If form is not valid, do not show confirmation
return false; // Return false if validation or other conditions fail
}
function handleDraftChange() {
const isDraftChanged = checkDraftChanges();
UIElements.btnSave.attr('disabled', !isDraftChanged);
UIElements.btnDiscard.attr('disabled', !isDraftChanged);
}
function checkDraftChanges() {
return UIElements.inputEmailTo.val() !== UIElements.inputOriginalEmailTo.val() ||
UIElements.inputEmailFrom.val() !== UIElements.inputOriginalEmailFrom.val() ||
UIElements.inputEmailSubject.val() !== UIElements.inputOriginalEmailSubject.val() ||
UIElements.inputEmailBody.val() !== UIElements.inputOriginalEmailBody.val();
}
function resetValidationErrors() {
UIElements.emailForm.find('.field-validation-error').each(function() {
$(this).removeClass('field-validation-error').addClass('field-validation-valid').html('');
});
}
$('#template').on('change', async function () {
console.log(this.value);
try {
resetValidationErrors();
let templateDetails = await loadTemplateDetails(this.value);
const templateData = extractTemplateData(applicationDetails, mappingConfig);
const template = Handlebars.compile(templateDetails.bodyHTML);
// Render the compiled template with data
const renderedHtml = template(templateData);
$('#EmailFrom').val(templateDetails.sendFrom)
$('#EmailSubject').val(templateDetails.subject)
editorInstance.setContent(renderedHtml);
UIElements.btnSave.attr('disabled', false);
} catch (error) {
console.error("Error loading data:", error);
}
});
function getTemplateVariables() {
return new Promise((resolve, reject) => {
$.ajax({
url: `/api/app/template/template-variables`,
type: 'GET',
success: function (response) {
resolve(response);
},
error: function (xhr, status, error) {
reject(error);
}
});
});
}
function loadTemplateDetails(templateId) {
return new Promise((resolve, reject) => {
$.ajax({
url: `/api/app/template/${templateId}/template-by-id`,
type: 'GET',
success: function (response) {
resolve(response);
},
error: function (xhr, status, error) {
reject(error);
}
});
});
}
function loadApplicationDetails() {
return new Promise((resolve, reject) => {
$.ajax({
url: `/api/app/grant-application/${UIElements.applicationId}`,
type: 'GET',
success: function (response) {
resolve(response);
},
error: function (xhr, status, error) {
reject(error);
}
});
});
}
function toTitleCase(str) {
return str.replace(/\b\w/g, function (char) {
return char.toUpperCase();
}).replace(/\B\w/g, function (char) {
return char.toLowerCase();
});
}
function processString(token,inputString) {
let lookupArray = ['category', 'status', 'decline_rationale'];
let datesArray = ['submission_date', 'approval_date', 'project_start_date', 'project_end_date']
if(typeof inputString !== 'string') {
return inputString;
}
if (datesArray.includes(token)) {
let date = new Date(inputString);
if (!isNaN(date.getTime())) {
return luxon.DateTime.fromISO(inputString, {
locale: abp.localization.currentCulture.name,
}).toUTC().toLocaleString()
}
else {
return '';
}
}
else {
if (lookupArray.includes(token)) {
inputString = inputString.replace(/_/g, ' ');
return toTitleCase(inputString);
}
return inputString;
}
}
function extractTemplateData(apiResponse, mappingConfig) {
const templateData = {};
mappingConfig.forEach(mapping => {
const { token, mapTo } = mapping;
if (!mapTo) {
templateData[token] = ""; // handle empty MapTo
return;
}
const value = getValueByPath(apiResponse, mapTo);
let formatValue = processString(token,value);
templateData[token] = formatValue !== undefined ? formatValue : "";
});
return templateData;
}
// Helper to resolve nested properties from a string path (e.g., "applicant.applicantName")
function getValueByPath(obj, path) {
return path.split('.').reduce((acc, key) => acc?.[key], obj);
}
PubSub.subscribe('email_selected', (msg, data) => {
resetValidationErrors();
console.log("data", data)
$('#templateListContainer').hide();
$('#templateTextContainer').show();
$('#templateText').val(data.templateName);
$('#templateText').prop('disabled', true);
UIElements.inputEmailId.val(data.id);
UIElements.inputOriginalEmailTo.val(data.toAddress);
UIElements.inputOriginalEmailFrom.val(data.fromAddress);
UIElements.inputOriginalEmailSubject.val(data.subject);
resetEmailBody();
if (tinymce.get("EmailBody")) {
tinymce.get("EmailBody").remove(); // remove existing instance
}
tinymce.init({
license_key: 'gpl',
selector: `#EmailBody`,
plugins: getPlugins(),
toolbar: getToolbarOptions(),
statusbar: false,
promotion: false,
content_css: false,
skin: false,
setup: function (editor) {
editor.on("input", (e) => {
UIElements.inputEmailBody.val(editor.getContent())
handleDraftChange();
});
editorInstance = editor;
editorInstance.setContent(data.body);
}
});
UIElements.inputEmailTo.val(data.toAddress);
UIElements.inputEmailFrom.val(data.fromAddress);
UIElements.inputEmailSubject.val(data.subject);
UIElements.inputEmailBody.val(data.body);
if (data && data.status === 'Draft') {
// Must run after form inputs are assigned
enableEmail();
handleDraftChange();
} else {
disableEmail();
}
showModalEmail();
resetValidationErrors();
});
PubSub.subscribe(
'applicant_info_updated',
(_, ApplicantInfoObj) => {
if(ApplicantInfoObj+"" !== "undefined"
&& ApplicantInfoObj.ContactEmail+"" != "undefined"
&& ApplicantInfoObj.ContactEmail !== "") {
UIElements.inputEmailTo[0].value = ApplicantInfoObj.ContactEmail;
}
}
);
});