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