]>
Commit | Line | Data |
---|---|---|
1 | import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core' | |
2 | import { Router } from '@angular/router' | |
3 | import { NotificationsService } from 'angular2-notifications' | |
4 | import { Observable } from 'rxjs' | |
5 | import { VideoCommentCreate } from '../../../../../../shared/models/videos/video-comment.model' | |
6 | import { FormReactive } from '../../../shared' | |
7 | import { User } from '../../../shared/users' | |
8 | import { Video } from '../../../shared/video/video.model' | |
9 | import { VideoComment } from './video-comment.model' | |
10 | import { VideoCommentService } from './video-comment.service' | |
11 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
12 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' | |
13 | import { VideoCommentValidatorsService } from '@app/shared/forms/form-validators/video-comment-validators.service' | |
14 | import { NgbModal } from '@ng-bootstrap/ng-bootstrap' | |
15 | import { AuthService } from '@app/core/auth' | |
16 | ||
17 | @Component({ | |
18 | selector: 'my-video-comment-add', | |
19 | templateUrl: './video-comment-add.component.html', | |
20 | styleUrls: ['./video-comment-add.component.scss'] | |
21 | }) | |
22 | export class VideoCommentAddComponent extends FormReactive implements OnInit { | |
23 | @Input() user: User | |
24 | @Input() video: Video | |
25 | @Input() parentComment: VideoComment | |
26 | @Input() parentComments: VideoComment[] | |
27 | @Input() focusOnInit = false | |
28 | ||
29 | @Output() commentCreated = new EventEmitter<VideoCommentCreate>() | |
30 | ||
31 | @ViewChild('visitorModal') visitorModal: NgbModal | |
32 | @ViewChild('textarea') textareaElement: ElementRef | |
33 | ||
34 | addingComment = false | |
35 | ||
36 | constructor ( | |
37 | protected formValidatorService: FormValidatorService, | |
38 | private videoCommentValidatorsService: VideoCommentValidatorsService, | |
39 | private notificationsService: NotificationsService, | |
40 | private videoCommentService: VideoCommentService, | |
41 | private authService: AuthService, | |
42 | private modalService: NgbModal, | |
43 | private router: Router, | |
44 | private i18n: I18n | |
45 | ) { | |
46 | super() | |
47 | } | |
48 | ||
49 | ngOnInit () { | |
50 | this.buildForm({ | |
51 | text: this.videoCommentValidatorsService.VIDEO_COMMENT_TEXT | |
52 | }) | |
53 | ||
54 | if (this.user) { | |
55 | if (this.focusOnInit === true) { | |
56 | this.textareaElement.nativeElement.focus() | |
57 | } | |
58 | ||
59 | if (this.parentComment) { | |
60 | const mentions = this.parentComments | |
61 | .filter(c => c.account.id !== this.user.account.id) // Don't add mention of ourselves | |
62 | .map(c => '@' + c.by) | |
63 | ||
64 | const mentionsSet = new Set(mentions) | |
65 | const mentionsText = Array.from(mentionsSet).join(' ') + ' ' | |
66 | ||
67 | this.form.patchValue({ text: mentionsText }) | |
68 | } | |
69 | } | |
70 | } | |
71 | ||
72 | onValidKey () { | |
73 | this.onValueChanged() | |
74 | if (!this.form.valid) return | |
75 | ||
76 | this.formValidated() | |
77 | } | |
78 | ||
79 | openVisitorModal (event: any) { | |
80 | if (this.user === null) { // we only open it for visitors | |
81 | // fixing ng-bootstrap ModalService and the "Expression Changed After It Has Been Checked" Error | |
82 | event.srcElement.blur() | |
83 | event.preventDefault() | |
84 | ||
85 | this.modalService.open(this.visitorModal) | |
86 | } | |
87 | } | |
88 | ||
89 | hideVisitorModal () { | |
90 | this.modalService.dismissAll() | |
91 | } | |
92 | ||
93 | formValidated () { | |
94 | // If we validate very quickly the comment form, we might comment twice | |
95 | if (this.addingComment) return | |
96 | ||
97 | this.addingComment = true | |
98 | ||
99 | const commentCreate: VideoCommentCreate = this.form.value | |
100 | let obs: Observable<any> | |
101 | ||
102 | if (this.parentComment) { | |
103 | obs = this.addCommentReply(commentCreate) | |
104 | } else { | |
105 | obs = this.addCommentThread(commentCreate) | |
106 | } | |
107 | ||
108 | obs.subscribe( | |
109 | comment => { | |
110 | this.addingComment = false | |
111 | this.commentCreated.emit(comment) | |
112 | this.form.reset() | |
113 | }, | |
114 | ||
115 | err => { | |
116 | this.addingComment = false | |
117 | ||
118 | this.notificationsService.error(this.i18n('Error'), err.text) | |
119 | } | |
120 | ) | |
121 | } | |
122 | ||
123 | isAddButtonDisplayed () { | |
124 | return this.form.value['text'] | |
125 | } | |
126 | ||
127 | getUrl () { | |
128 | return window.location.href | |
129 | } | |
130 | ||
131 | getAvatarUrl () { | |
132 | if (this.user) return this.user.accountAvatarUrl | |
133 | return window.location.origin + '/client/assets/images/default-avatar.png' | |
134 | } | |
135 | ||
136 | gotoLogin () { | |
137 | this.hideVisitorModal() | |
138 | this.router.navigate([ '/login' ]) | |
139 | } | |
140 | ||
141 | private addCommentReply (commentCreate: VideoCommentCreate) { | |
142 | return this.videoCommentService | |
143 | .addCommentReply(this.video.id, this.parentComment.id, commentCreate) | |
144 | } | |
145 | ||
146 | private addCommentThread (commentCreate: VideoCommentCreate) { | |
147 | return this.videoCommentService | |
148 | .addCommentThread(this.video.id, commentCreate) | |
149 | } | |
150 | } |