note-editor.component.ts 6.68 KB
Newer Older
1
import { Component, Input, EventEmitter, Output, OnInit } from '@angular/core';
2
import { HttpClient } from '@angular/common/http';
3
import { TranslateService } from '@ngx-translate/core';
4
import { NotificationService } from '@service/notification/notification.service';
5
import { catchError, tap, debounceTime, filter } from 'rxjs/operators';
6
import { HeaderService } from '@service/header.service';
7
import { of } from 'rxjs';
8
import { FunctionsService } from '@service/functions.service';
9
import { FormControl } from '@angular/forms';
10
import { LatinisePipe } from 'ngx-pipes';
11
12
13
14
15
16

@Component({
    selector: 'app-note-editor',
    templateUrl: 'note-editor.component.html',
    styleUrls: ['note-editor.component.scss'],
})
17
export class NoteEditorComponent implements OnInit {
18

19
20
21
22
23
24
25
26
27
28
29
    @Input() title: string = this.translate.instant('lang.addNote');
    @Input() content: string = '';
    @Input() resIds: any[];
    @Input() addMode: boolean;
    @Input() upMode: boolean;
    @Input() noteContent: string;
    @Input() entitiesNoteRestriction: string[];
    @Input() noteId: number;
    @Input() defaultRestriction: boolean;
    @Input() disableRestriction: boolean = false;
    @Output() refreshNotes = new EventEmitter<string>();
30

31
32
33
34
35
36
37
38
    notes: any;
    loading: boolean = false;
    templatesNote: any = [];
    entities: any = [];

    entitiesRestriction: string[] = [];


39
40
41
    searchTerm: FormControl = new FormControl();
    entitiesList: any[] = [];

42
    constructor(
43
        public translate: TranslateService,
44
45
        public http: HttpClient,
        private notify: NotificationService,
46
        public headerService: HeaderService,
47
48
        public functions: FunctionsService,
        private latinisePipe: LatinisePipe) { }
49
50
51
52
53
54
55

    async ngOnInit() {
        await this.getEntities();

        if (this.defaultRestriction) {
            this.setDefaultRestriction();
        }
56

57
58
        if (this.upMode) {
            this.content = this.noteContent;
Hamza HRAMCHI's avatar
Hamza HRAMCHI committed
59
            if (this.content.startsWith(`[${this.translate.instant('lang.opinionUserState')}]`) || this.content.startsWith(`[${this.translate.instant('lang.avisUserAsk').toUpperCase()}]`)) {
60
61
                this.disableRestriction = true;
            }
62
            this.entitiesRestriction = this.entitiesNoteRestriction;
63
        }
64
65
66
67
68

        this.entitiesList = this.entities;

        this.searchTerm.valueChanges.pipe(
            debounceTime(300),
69
            // distinctUntilChanged(),
70
71
            tap((data: any) => {
                if (data.length > 0) {
72
73
74
                    const filterValue = this.latinisePipe.transform(data.toLowerCase());
                    this.entitiesList = this.entities.filter( (item: any) => (
                        this.latinisePipe.transform(item.entity_label.toLowerCase()).includes(filterValue)
75
                                || this.latinisePipe.transform(item.entity_id.toLowerCase()).includes(filterValue)
76
                    ));
77
78
79
80
81
82
83
84
85
                } else {
                    this.entitiesList = this.entities;
                }
            }),
            catchError((err) => {
                this.notify.handleErrors(err);
                return of(false);
            })
        ).subscribe();
86
    }
Alex ORLUC's avatar
Alex ORLUC committed
87

88
89
    setDefaultRestriction() {
        this.entitiesRestriction = [];
90
        this.http.get(`../rest/resources/${this.resIds[0]}/fields/destination`).pipe(
91
92
            tap((data: any) => {
                this.entitiesRestriction = this.headerService.user.entities.map((entity: any) => entity.entity_id);
93
                if (this.entitiesRestriction.indexOf(data.field) === -1 && !this.functions.empty(data.field)) {
94
95
                    this.entitiesRestriction.push(data.field);
                }
96
97
98
99
100
101
102
103
104
                this.entities.filter((entity: any) => this.entitiesRestriction.indexOf(entity.id) > -1).forEach((element: any) => {
                    element.selected = true;
                });
            }),
            catchError((err: any) => {
                this.notify.handleSoftErrors(err);
                return of(false);
            })
        ).subscribe();
105
    }
106
107

    addNote() {
108
        this.loading = true;
109
        this.http.post('../rest/notes', { value: this.content, resId: this.resIds[0], entities: this.entitiesRestriction })
Alex ORLUC's avatar
Alex ORLUC committed
110
111
            .subscribe((data: any) => {
                this.refreshNotes.emit(this.resIds[0]);
112
                this.loading = false;
Alex ORLUC's avatar
Alex ORLUC committed
113
            });
114
    }
115

116
117
    updateNote() {
        this.loading = true;
118
        this.http.put('../rest/notes/' + this.noteId, { value: this.content, resId: this.resIds[0], entities: this.entitiesRestriction })
119
120
121
122
123
124
            .subscribe((data: any) => {
                this.refreshNotes.emit(this.resIds[0]);
                this.loading = false;
            });
    }

125
126
127
    getNoteContent() {
        return this.content;
    }
128

129
130
131
132
133
134
135
136
137
    setNoteContent(content: string) {
        this.content = content;
    }


    getNote() {
        return {content: this.content, entities: this.entitiesRestriction};
    }

138
139
140
141
142
143
    selectTemplate(template: any) {
        if (this.content.length > 0) {
            this.content = this.content + ' ' + template.template_content;
        } else {
            this.content = template.template_content;
        }
144
    }
Alex ORLUC's avatar
Alex ORLUC committed
145

146
147
    selectEntity(entity: any) {
        entity.selected = true;
148
        this.entitiesRestriction.push(entity.id);
149
150
151
152
    }

    getTemplatesNote() {
        if (this.templatesNote.length == 0) {
153
            const params = {};
154
            if (!this.functions.empty(this.resIds) && this.resIds.length == 1) {
Damien's avatar
Damien committed
155
                params['resId'] = this.resIds[0];
156
            }
157
            this.http.get('../rest/notesTemplates', { params: params })
Alex ORLUC's avatar
Alex ORLUC committed
158
159
160
                .subscribe((data: any) => {
                    this.templatesNote = data['templates'];
                });
Damien's avatar
Damien committed
161

162
163
        }
    }
164
165

    getEntities() {
166
167
        return new Promise((resolve, reject) => {
            if (this.entities.length == 0) {
168
                const params = {};
169
                if (!this.functions.empty(this.resIds) && this.resIds.length == 1) {
170
171
                    params['resId'] = this.resIds[0];
                }
172
                this.http.get('../rest/entities').pipe(
173
174
175
176
177
178
179
180
181
182
                    tap((data: any) => {
                        this.entities = data['entities'];
                        resolve(true);
                    }),
                    catchError((err: any) => {
                        this.notify.handleSoftErrors(err);
                        resolve(false);
                        return of(false);
                    })
                ).subscribe();
183

184
185
            }
        });
186
187
188
189
    }

    removeEntityRestriction(index: number, realIndex: number) {
        this.entities[realIndex].selected = false;
190
        this.entitiesRestriction.splice(index, 1);
191
    }
192
193
194
195

    isWritingNote() {
        return this.content !== '';
    }
196
}