-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDefault.js
More file actions
457 lines (385 loc) · 18.7 KB
/
Default.js
File metadata and controls
457 lines (385 loc) · 18.7 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
(function ($) {
$(function () {
loadCardsFromService();
const NotificationUiElements = {
settingForm: $("#NotificationsSettingsForm"),
saveButton: $("#NotificationsSaveButton"),
discardButton: $("#NotificationsDiscardButton")
}
let initialFormState = NotificationUiElements.settingForm.serialize();
function checkFormChanges() {
let currentFormState = NotificationUiElements.settingForm.serialize();
let isFormChanged = currentFormState !== initialFormState;
NotificationUiElements.saveButton.prop('disabled', !isFormChanged);
NotificationUiElements.discardButton.prop('disabled', !isFormChanged);
}
NotificationUiElements.settingForm.on('input change', function () {
checkFormChanges();
});
NotificationUiElements.settingForm.on('submit', function (event) {
event.preventDefault();
if (!$(this).valid()) {
return;
}
let form = $(this).serializeFormToObject();
unity.notifications.emailNotifications.emailNotification.updateSettings(form).then(function (result) {
$(document).trigger("AbpSettingSaved");
initialFormState = NotificationUiElements.settingForm.serialize();
checkFormChanges();
});
});
NotificationUiElements.discardButton.on('click', function () {
NotificationUiElements.settingForm[0].reset();
initialFormState = NotificationUiElements.settingForm.serialize();
checkFormChanges();
});
checkFormChanges();
let editorInstances = {};
function createCard(data = null) {
const isPopulated = data !== null;
const id = data?.id?.toString() || generateTempId();
const cardId = `collapseDetails-${id}`;
const formId = `form-${id}`;
const wrapperId = `cardWrapper-${id}`;
const editorId = `editor-${id}`;
const type = data?.type || 'Automatic';
let lastEdited;
const dropdownItems = [];
getTemplateVariables();
if (data?.lastModificationTime) {
lastEdited = new Date(data.lastModificationTime).toLocaleDateString('en-CA');
} else if (data?.creationTime) {
lastEdited = new Date(data.creationTime).toLocaleDateString('en-CA');
} else {
lastEdited = new Date().toLocaleDateString('en-CA');
}
const disabled = isPopulated ? 'disabled' : '';
const cardHtml = `
<div class="card mb-3 shadow-sm" id="${wrapperId}" data-id="${id}">
<div class="card-header d-flex justify-content-between align-items-center">
<strong class="template-title">${data?.name || 'Untitled Template'}</strong>
<div class="d-flex align-items-center gap-3">
<div class="text-muted small text-end me-3">
<div>Type<br /><span class="fw-normal">${type}</span></div>
</div>
<div class="text-muted small text-end me-3">
<div>Last Edited<br /><span class="fw-normal">${lastEdited}</span></div>
</div>
<button class="btn btn-sm btn-link text-decoration-none" type="button"
data-bs-toggle="collapse" data-bs-target="#${cardId}" aria-expanded="false"
aria-controls="${cardId}">
<i class="unt-icon-sm fa-solid fa-chevron-down"></i>
</button>
</div>
</div>
<div class="collapse" id="${cardId}">
<div class="card-body">
<form id="${formId}">
<div class="mb-3">
<label class="form-label">Template Name</label>
<input type="text" class="form-control form-input" name="templateName" value="${data?.name || ''}" ${disabled} required>
</div>
<div class="mb-3">
<label class="form-label">Send From</label>
<input type="text" class="form-control form-input" name="sendFrom" value="${data?.sendFrom || ''}" ${disabled} required>
</div>
<div class="mb-3">
<label class="form-label">Subject</label>
<input type="text" class="form-control form-input" name="subject" value="${data?.subject || ''}" ${disabled} required>
</div>
<div class="mb-3">
<label class="form-label">Body</label>
<textarea id="${editorId}"></textarea>
</div>
<div class="mb-3">
<p><b>NOTE</b>:<span class="note-text"> Selecting text will let your customize it: replace it with a variable, make it bold, italic, change the alignment, add a link, create a list, etc.</span></p>
</div>
<div class="mb-3">
<hr>
<button type="button" class="btn btn-outline-primary deleteCardBtn">X DELETE THIS TEMPLATE</button>
<hr>
</div>
<div class="d-flex gap-2">
${isPopulated ? `<button type="submit" class="btn btn-primary saveBtn" disabled>SAVE CHANGES</button>` : `<button type="submit" class="btn btn-primary saveBtn">SAVE CHANGES</button>`}
<button type="button" class="btn btn-outline-secondary discardBtn d-none">DISCARD CHANGES</button>
${isPopulated ? `<button type="button" class="btn btn-secondary editBtn">EDIT THIS TEMPLATE</button>` : ''}
</div>
</form>
</div>
</div>
</div>
`;
$("#cardContainer").append(cardHtml);
$(`#${wrapperId}`).data('original-name', data?.name || '');
// editorInstances[id] =
console.log("tinymce", tinymce)
if (tinymce.get(editorId)) {
tinymce.get(editorId).remove(); // remove existing instance
}
function getToolbarOptions() {
return 'undo redo | styles | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist | link image | code preview | variablesDropdownButton';
}
function getPlugins() {
return 'lists link image preview code';
}
function setupEditor(editor, id, editorId, data, isPopulated) {
editor.ui.registry.addMenuButton('variablesDropdownButton', {
text: 'VARIABLES',
fetch: function (callback) {
const items = dropdownItems.map(item => ({
type: 'menuitem',
text: item.text,
onAction: () => {
editor.insertContent(`{{${item.value}}}`);
}
}));
callback(items);
}
});
editor.on('init', function () {
editor.mode.set(isPopulated ? 'readonly' : 'design');
if (data?.bodyHTML) {
editor.setContent(data.bodyHTML);
}
editorInstances[id] = editor;
console.log(`Editor initialized: ${editorId}`);
});
}
function initTinyMCE(editorId, id, data, isPopulated) {
tinymce.init({
license_key: 'gpl',
selector: `#${editorId}`,
plugins: getPlugins(),
toolbar: getToolbarOptions(),
statusbar: false,
promotion: false,
content_css: false,
skin: false,
setup: function (editor) {
setupEditor(editor, id, editorId, data, isPopulated);
}
});
}
initTinyMCE(editorId, id, data, isPopulated)
function extractFormData(formDataArray) {
return {
name: formDataArray[0].value,
sendFrom: formDataArray[1].value,
subject: formDataArray[2].value
};
}
function buildTemplatePayload(data, editor) {
return JSON.stringify({
name: data.name,
description: "",
subject: data.subject,
bodyText: editor.getContent({ format: 'text' }),
bodyHTML: editor.getContent(),
sendFrom: data.sendFrom
});
}
function handleSuccess(message) {
abp.notify.success(message);
$("#cardContainer").empty();
loadCardsFromService();
return true;
}
function handleError(message) {
abp.notify.error(message);
return false;
}
function onSaveTemplateSuccess() {
handleSuccess('Template saved successfully.');
}
function onSaveTemplateError() {
handleError('Failed to save template.');
}
function saveTemplate(payload) {
$.ajax({
url: `/api/app/template`,
method: 'POST',
contentType: 'application/json',
data: payload,
success: onSaveTemplateSuccess,
error: onSaveTemplateError
});
}
function updateTemplate(id, payload) {
$.ajax({
url: `/api/app/template/${id}/template`,
method: 'PUT',
contentType: 'application/json',
data: payload,
success: onSaveTemplateSuccess,
error: onSaveTemplateError
});
}
$(`#${formId} input[name="templateName"]`).on('input', function () {
const templateInput = $(this);
const newTitle = templateInput.val().trim() || 'Untitled Template';
$(`#${wrapperId} .template-title`).text(newTitle);
// Check if name is unique
checkTemplateNameUnique(newTitle, id, function (isUnique) {
if (!isUnique) {
templateInput.addClass("is-invalid");
if (!$(`#${formId} .template-name-feedback`).length) {
templateInput.after(`<div class="invalid-feedback template-name-feedback">Template name must be unique.</div>`);
}
$(`#${formId} .saveBtn`).prop("disabled", true);
} else {
templateInput.removeClass("is-invalid");
$(`#${formId} .template-name-feedback`).remove();
$(`#${formId} .saveBtn`).prop("disabled", false);
}
});
});
$(`#${formId}`).on("submit", function (e) {
e.preventDefault();
const formDataArray = $(this).serializeArray();
const formData = extractFormData(formDataArray);
const editor = editorInstances[id];
const payload = buildTemplatePayload(formData, editor);
if (id.includes("temp")) {
saveTemplate(payload);
} else {
updateTemplate(id, payload);
}
});
$(`#${cardId}`).on('show.bs.collapse', function () {
$(`#${wrapperId} .btn[data-bs-target="#${cardId}"] i`)
.removeClass('fa-chevron-down')
.addClass('fa-chevron-up');
});
$(`#${cardId}`).on('hide.bs.collapse', function () {
$(`#${wrapperId} .btn[data-bs-target="#${cardId}"] i`)
.removeClass('fa-chevron-up')
.addClass('fa-chevron-down');
});
$(`#${wrapperId}`).on("click", ".editBtn", function () {
const currentEditor = editorInstances[id];
currentEditor.destroy();
initTinyMCE(editorId, id, data, false)
const card = $(`#${wrapperId}`);
card.find(".form-input").prop('disabled', false);
card.find(".saveBtn").prop('disabled', false);
card.find(".discardBtn").removeClass("d-none");
$(this).addClass("d-none");
});
$(`#${wrapperId}`).on("click", ".discardBtn", function () {
const form = $(`#${formId}`)[0];
form.reset();
const currentEditor = editorInstances[id];
currentEditor.destroy();
initTinyMCE(editorId, id, data, true)
$(`#${wrapperId} .form-input`).prop('disabled', true);
$(`#${wrapperId} .saveBtn`).prop('disabled', true);
$(`#${wrapperId} .discardBtn`).addClass("d-none");
$(`#${wrapperId} .editBtn`).removeClass("d-none");
});
$(`#${formId} input[name="templateName"]`).on('input', function () {
const newTitle = $(this).val().trim() || 'Untitled Template';
$(`#${wrapperId} .template-title`).text(newTitle);
});
$(`#${wrapperId}`).on("click", ".deleteCardBtn", function () {
if (isPopulated) {
showDeleteConfirmation(id, wrapperId);
} else {
$(`#${wrapperId}`).remove();
}
});
function showDeleteConfirmation(id, wrapperId) {
const swalOptions = {
title: "Delete Template",
text: "Are you sure you want to delete this template?",
showCancelButton: true,
confirmButtonText: "Confirm",
customClass: {
confirmButton: 'btn btn-primary',
cancelButton: 'btn btn-secondary'
}
};
Swal.fire(swalOptions).then(handleResult.bind(null, id, wrapperId));
}
function handleResult(id, wrapperId, result) {
handleDeleteConfirmation(result, id, wrapperId);
}
function handleDeleteConfirmation(result, id, wrapperId) {
if (!result.isConfirmed) return;
deleteTemplate(id, wrapperId);
}
function handleDeleteSuccess() {
$(`#${wrapperId}`).remove();
abp.notify.success('Template deleted successfully.');
}
function handleDeleteError() {
abp.notify.error('Error deleting the template.');
}
function deleteTemplate(id, wrapperId) {
$.ajax({
url: `/api/app/template/${id}/template`,
type: 'DELETE',
success: handleDeleteSuccess,
error: handleDeleteError
});
}
function checkTemplateNameUnique(name, currentId, callback) {
$.ajax({
url: `/api/app/template/template-by-name?name=${encodeURIComponent(name)}`,
type: 'GET',
success: function (response) {
// Assume response: { isUnique: true/false }
// If editing an existing template, allow current name
const isSameAsCurrent = !currentId.includes('temp') && name === $(`#${wrapperId}`).data('original-name');
let isExist = false;
if (response?.id) {
isExist = true;
}
callback(!isExist || isSameAsCurrent);
},
error: function () {
callback(false); // Assume not unique if error
}
});
}
function getTemplateVariables() {
$.ajax({
url: `/api/app/template/template-variables`,
type: 'GET',
success: function (response) {
$.map(response, function (item) {
dropdownItems.push( {
text: item.name,
value: item.token
});
});
},
error: function () {
}
});
}
}
function loadCardsFromService() {
$.ajax({
url: `/api/app/template/templates-by-tenent`,
type: 'GET',
success: handleLoadCardsSuccess,
error: handleLoadCardsError
});
}
function handleLoadCardsSuccess(response) {
editorInstances = {};
response.forEach(item => createCard(item));
}
function handleLoadCardsError() {
abp.notify.error('Unable to load the templates.');
}
function generateTempId() {
const array = new Uint32Array(1);
window.crypto.getRandomValues(array);
return `temp-${array[0].toString(36)}`;
}
$("#CreateNewTemplate").on("click", function () {
createCard();
});
});
})(jQuery);