Newer
Older

Alex ORLUC
committed
import { COMMA, ENTER } from '@angular/cdk/keycodes';
import { Component, OnInit, ViewChild, EventEmitter, ElementRef, Input, Inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { LANG } from '../../translate.component';
import { NotificationService } from '../../notification.service';
import { Observable, merge, Subject, of as observableOf, of } from 'rxjs';
import { MatPaginator, MatSort, MatDialog, MatTableDataSource, MAT_DIALOG_DATA, MatDialogRef, MatChipInputEvent, MatSelect } from '@angular/material';

Alex ORLUC
committed
import { takeUntil, startWith, switchMap, map, catchError, filter, exhaustMap, tap, debounceTime, distinctUntilChanged, finalize } from 'rxjs/operators';
import { FormControl } from '@angular/forms';
import { FunctionsService } from '../../../service/functions.service';
import { CdkDragDrop, transferArrayItem } from '@angular/cdk/drag-drop';

Alex ORLUC
committed
@Component({
selector: 'app-sended-resource-page',
templateUrl: "sended-resource-page.component.html",
styleUrls: ['sended-resource-page.component.scss'],
})
export class SendedResourcePageComponent implements OnInit {
lang: any = LANG;
loading: boolean = true;
readonly separatorKeysCodes: number[] = [COMMA];

Alex ORLUC
committed
availableEmailModels: any[] = [];
availableSignEmailModels: any[] = [];
mainDocList: any[] = [];
notesList: any[] = [];
attachmentsList: any[] = [];
resourceData: any = null;
availableSenders: any[] = [];
currentSender: any = {};

Alex ORLUC
committed
recipients: any[] = [];
copies: any[] = [];
invisibleCopies: any[] = [];
fruits: any[] = [];
recipientsInput: FormControl = new FormControl();
tinymceInput: string = '';
filteredEmails: Observable<string[]>;
emailsList: any[] = [];
currentSelected: any = null;
showCopies: boolean = false;
showInvisibleCopies: boolean = false;
tinyMCEConfig = {
language: this.lang.langISO.replace('-', '_'),
language_url: `../../node_modules/tinymce-i18n/langs/${this.lang.langISO.replace('-', '_')}.js`,
menubar: false,

Alex ORLUC
committed
'autolink', 'autoresize'

Alex ORLUC
committed
external_plugins: {
'maarch_b64image': "../../src/frontend/plugins/tinymce/maarch_b64image/plugin.min.js"
},

Alex ORLUC
committed
toolbar_drawer: 'floating',

Alex ORLUC
committed
'undo redo | fontselect fontsizeselect | bold italic underline strikethrough forecolor | maarch_b64image | \
alignleft aligncenter alignright alignjustify \
bullist numlist outdent indent | removeformat'
}
emailsubject: string = '';
emailAttach: any = {
document: {
isLinked: false,
original: false
},
notes: [],
attachments: []
};

Alex ORLUC
committed
constructor(
public http: HttpClient,
private notify: NotificationService,
@Inject(MAT_DIALOG_DATA) public data: any,
public dialogRef: MatDialogRef<SendedResourcePageComponent>,
private functions: FunctionsService

Alex ORLUC
committed
) { }
async ngOnInit(): Promise<void> {
this.emailAttach.document.id = this.data.resId;

Alex ORLUC
committed
this.loading = false;
this.getResourceData();
this.initEmailModelsList();
this.getUserEmails();

Alex ORLUC
committed
this.initEmailsList();
this.initSignEmailModelsList();

Alex ORLUC
committed
}
add(event: MatChipInputEvent, type: string): void {

Alex ORLUC
committed
const input = event.input;
const value = event.value;
if ((value || '').trim()) {
this[type].push(
{
label: '',
email: value.trim()
});

Alex ORLUC
committed
}
if (input) {
input.value = '';
}
}
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
addEmail(item: any, type: string) {
this[type].splice(this[type].length - 1, 1);
if (item.type === 'contactGroup') {
this.http.get(`../../rest/contactsGroups/${item.id}`).pipe(
map((data: any) => {
data = data.contactsGroup.contacts.filter((contact: any) => !this.functions.empty(contact.email)).map((contact: any) => {
return {
label: contact.contact,
email: contact.email
}
});
return data;
}),
tap((data: any) => {
this[type] = this[type].concat(data);
}),
catchError((err) => {
this.notify.handleSoftErrors(err);
return of(false);
})
).subscribe();
} else {
this[type].push({
label: 'toto',
email: item.email
});
}
}
mergeEmailTemplate(templateId: number) {
this.currentSelected = '';
this.http.post(`../../rest/templates/${templateId}/mergeEmail`, { data: { resId: this.data.resId } }).pipe(
tap((data: any) => {
var div = document.createElement('div');
div.innerHTML = this.tinymceInput.trim();
if (div.getElementsByClassName('signature').length > 0) {
const signatureContent = div.getElementsByClassName('signature')[0].innerHTML;
div.getElementsByClassName('signature')[0].remove();
this.tinymceInput = `${div.innerHTML}${data.mergedDocument}<div class="signature">${signatureContent}</div>`;
} else {
this.tinymceInput += data.mergedDocument;
}
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
}),
catchError((err) => {
this.notify.handleSoftErrors(err);
return of(false);
})
).subscribe();
}
mergeSignEmailTemplate(templateId: number) {
this.currentSelected = '';
this.http.get(`../../rest/currentUser/emailSignatures/${templateId}`).pipe(
tap((data: any) => {
var div = document.createElement('div');
div.innerHTML = this.tinymceInput.trim();
if (div.getElementsByClassName('signature').length > 0) {
div.getElementsByClassName('signature')[0].remove();
this.tinymceInput = `${div.innerHTML}<div class="signature">${data.emailSignature.content}</div>`;
} else {
this.tinymceInput += `<div class="signature">${data.emailSignature.content}</div>`;
}
}),
catchError((err) => {
this.notify.handleSoftErrors(err);
return of(false);
})
).subscribe();
}

Alex ORLUC
committed
remove(item: any, type: string): void {
const index = this[type].indexOf(item);
if (index >= 0) {
this[type].splice(index, 1);
}
}
getResourceData() {
this.http.get(`../../rest/resources/${this.data.resId}?light=true`).pipe(
tap((data: any) => {
this.resourceData = data;
this.emailsubject = `[${this.resourceData.chrono}] ${this.resourceData.subject}`;
this.emailAttach.document.label = this.resourceData.subject;
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
if (!this.functions.empty(this.resourceData.senders)) {
this.resourceData.senders.forEach((sender: any) => {
this.setSender(sender.id);
});
}
}),
catchError((err) => {
this.notify.handleSoftErrors(err);
return of(false);
})
).subscribe();
}
setSender(id: number) {
this.http.get(`../../rest/contacts/${id}`).pipe(
tap((data: any) => {
this.recipients.push(
{
email: data.email
}
)
}),
catchError((err) => {
this.notify.handleSoftErrors(err);
return of(false);
})
).subscribe();
}
getUserEmails() {
this.http.get('../../rest/users/21/availableEmails').pipe(
tap((data: any) => {
this.availableSenders = data.emails;
this.currentSender = this.availableSenders[0];
}),
catchError((err) => {
this.notify.handleSoftErrors(err);
return of(false);
})
).subscribe();
}
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
getAttachElements() {
this.mainDocList = [
{
id: 100,
label: 'Réservation Bal',
typeLabel: 'Document principal',
pdfVersion: 131,
creator: 'Bernard Blier',
size: '40ko'
}
];
this.attachmentsList = [
{
id: 100,
label: 'je suis une pj',
typeLabel: 'Projet de réponse',
pdfVersion: 131,
creator: 'Bernard Blier',
size: '40ko'
},
{
id: 102,
label: 'je suis une pj 2',
typeLabel: 'Projet de réponse',
pdfVersion: 131,
creator: 'Bernard Blier',
size: '40ko'
}
];
this.notesList = [
{
id: 100,
label: 'Je suis une note',
typeLabel: 'Note',
pdfVersion: null,
creator: 'Bernard Blier',
size: null
}
];
}

Alex ORLUC
committed
initEmailsList() {
this.recipientsInput.valueChanges.pipe(
debounceTime(300),
filter(value => value.length > 2),
tap(() => this.loading = true),
switchMap(data => this.http.get('../../rest/autocomplete/correspondents', { params: { "search": data, "searchEmails": 'true' } })),

Alex ORLUC
committed
tap((data: any) => {
data = data.filter((contact: any) => !this.functions.empty(contact.email) || contact.type === 'contactGroup').map((contact: any) => {
let label = '';
if (contact.type === 'user') {
label = `${contact.firstname} ${contact.lastname} (${contact.email})`;
} else if (contact.type === 'contactGroup') {
label = `${contact.firstname} ${contact.lastname}`;
} else {
label = `${contact.lastname} (${contact.email})`;
}
return {
id: contact.id,
type: contact.type,
label: label,
email: contact.email
}
});
this.filteredEmails = of(data);

Alex ORLUC
committed
}),
catchError((err) => {
this.notify.handleSoftErrors(err);

Alex ORLUC
committed
return of(false);
})
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
).subscribe();
}
initEmailModelsList() {
this.http.get(`../../rest/resources/${this.data.resId}/emailTemplates`).pipe(
tap((data: any) => {
this.availableEmailModels = data.templates;
}),
catchError((err) => {
this.notify.handleSoftErrors(err);
return of(false);
})
).subscribe();
}
initSignEmailModelsList() {
this.http.get(`../../rest/currentUser/emailSignatures`).pipe(
tap((data: any) => {
this.availableSignEmailModels = data.emailSignatures;
}),
catchError((err) => {
this.notify.handleSoftErrors(err);
return of(false);
})
).subscribe();
}
resetAutocomplete() {
this.filteredEmails = of([]);
}
onSubmit() {
console.log(this.formatEmail());
/*this.http.post(`../../rest/emails`, this.formatEmail()).pipe(
tap(() => {
this.notify.success("Email en cours d'envoi...")
this.dialogRef.close('success');
}),
catchError((err) => {
this.notify.handleSoftErrors(err);
return of(false);
})
}
drop(event: CdkDragDrop<string[]>) {
if (event.previousContainer !== event.container) {
transferArrayItem(event.previousContainer.data,
event.container.data,
event.previousIndex,
event.currentIndex);
}

Alex ORLUC
committed
}
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
toggleAttachMail(item: any, type: string, mode: string) {
if (type === 'maindocument') {
if (this.emailAttach.document.isLinked === false) {
this.emailAttach.document.isLinked = true;
this.emailAttach.document.original = mode === 'pdf' ? false : true;
}
} else if (type === 'attachment') {
if (this.emailAttach.attachments.filter((attach: any) => attach.id === item.id).length === 0) {
this.emailAttach.attachments.push({
id: item.id,
label: item.label,
original: mode === 'pdf' ? false : true
});
}
} else if (type === 'note') {
if (this.emailAttach.notes.filter((noteId: any) => noteId === item.id).length === 0) {
this.emailAttach.notes.push({
id: item.id,
label: item.label
});
}
}
}
removeAttachMail(index: number, type: string) {
console.log(index);
console.log(type);
if (type === 'document') {
this.emailAttach.document.isLinked = true;
this.emailAttach.document.original = false;
} else if (type === 'attachments') {
this.emailAttach.attachments.splice(index, 1);
} else if (type === 'notes') {
this.emailAttach.notes.splice(index, 1);
}
}
formatEmail() {
Object.keys(this.emailAttach).forEach(element => {
if (this.functions.empty(this.emailAttach[element])) {
delete this.emailAttach[element];
}
});
const data = {
sender: this.currentSender,
recipients: this.recipients.map(recipient => recipient.email),
cc: this.copies.map(copy => copy.email),
cci: this.invisibleCopies.map((invCopy => invCopy.email)),
object: this.emailsubject,
body: this.tinymceInput,
isHtml: true,
status: 'WAITING'
};
return Object.assign({}, this.emailAttach, data);
}