]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+videos/+video-watch/comment/video-comment-add.component.ts
c6e9c73b8f2357c1cbe357dfb05521e55e4c806e
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-watch / comment / video-comment-add.component.ts
1 import { Observable } from 'rxjs'
2 import { Component, ElementRef, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges, ViewChild } from '@angular/core'
3 import { Router } from '@angular/router'
4 import { Notifier, User } from '@app/core'
5 import { FormReactive, FormValidatorService, VideoCommentValidatorsService } from '@app/shared/shared-forms'
6 import { Video } from '@app/shared/shared-main'
7 import { VideoComment, VideoCommentService } from '@app/shared/shared-video-comment'
8 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
9 import { VideoCommentCreate } from '@shared/models'
10 import { I18n } from '@ngx-translate/i18n-polyfill'
11
12 @Component({
13 selector: 'my-video-comment-add',
14 templateUrl: './video-comment-add.component.html',
15 styleUrls: ['./video-comment-add.component.scss']
16 })
17 export class VideoCommentAddComponent extends FormReactive implements OnChanges, OnInit {
18 @Input() user: User
19 @Input() video: Video
20 @Input() parentComment: VideoComment
21 @Input() parentComments: VideoComment[]
22 @Input() focusOnInit = false
23 @Input() textValue?: string
24 @Input() commentThread?: boolean
25
26 @Output() commentCreated = new EventEmitter<VideoComment>()
27 @Output() cancel = new EventEmitter()
28
29 @ViewChild('visitorModal', { static: true }) visitorModal: NgbModal
30 @ViewChild('textarea', { static: true }) textareaElement: ElementRef
31
32 addingComment = false
33 addingCommentButtonValue: string
34
35 constructor (
36 protected formValidatorService: FormValidatorService,
37 private videoCommentValidatorsService: VideoCommentValidatorsService,
38 private notifier: Notifier,
39 private videoCommentService: VideoCommentService,
40 private modalService: NgbModal,
41 private router: Router,
42 private i18n: I18n
43 ) {
44 super()
45 }
46
47 ngOnInit () {
48 this.buildForm({
49 text: this.videoCommentValidatorsService.VIDEO_COMMENT_TEXT
50 })
51
52 if (this.user) {
53 if (this.commentThread) {
54 this.addingCommentButtonValue = this.i18n('Comment')
55 } else {
56 this.addingCommentButtonValue = this.i18n('Reply')
57 }
58
59 if (this.textValue) {
60 this.patchTextValue(this.textValue, this.focusOnInit)
61 return
62 }
63
64 if (this.parentComment) {
65 const mentions = this.parentComments
66 .filter(c => c.account && c.account.id !== this.user.account.id) // Don't add mention of ourselves
67 .map(c => '@' + c.by)
68
69 const mentionsSet = new Set(mentions)
70 const mentionsText = Array.from(mentionsSet).join(' ') + ' '
71
72 this.patchTextValue(mentionsText, this.focusOnInit)
73 }
74 }
75 }
76
77 ngOnChanges (changes: SimpleChanges) {
78 if (changes.textValue && changes.textValue.currentValue && changes.textValue.currentValue !== changes.textValue.previousValue) {
79 this.patchTextValue(changes.textValue.currentValue, true)
80 }
81 }
82
83 onValidKey () {
84 this.check()
85 if (!this.form.valid) return
86
87 this.formValidated()
88 }
89
90 openVisitorModal (event: any) {
91 if (this.user === null) { // we only open it for visitors
92 // fixing ng-bootstrap ModalService and the "Expression Changed After It Has Been Checked" Error
93 event.srcElement.blur()
94 event.preventDefault()
95
96 this.modalService.open(this.visitorModal)
97 }
98 }
99
100 hideVisitorModal () {
101 this.modalService.dismissAll()
102 }
103
104 formValidated () {
105 // If we validate very quickly the comment form, we might comment twice
106 if (this.addingComment) return
107
108 this.addingComment = true
109
110 const commentCreate: VideoCommentCreate = this.form.value
111 let obs: Observable<VideoComment>
112
113 if (this.parentComment) {
114 obs = this.addCommentReply(commentCreate)
115 } else {
116 obs = this.addCommentThread(commentCreate)
117 }
118
119 obs.subscribe(
120 comment => {
121 this.addingComment = false
122 this.commentCreated.emit(comment)
123 this.form.reset()
124 },
125
126 err => {
127 this.addingComment = false
128
129 this.notifier.error(err.text)
130 }
131 )
132 }
133
134 isAddButtonDisplayed () {
135 return this.form.value['text']
136 }
137
138 getUri () {
139 return window.location.href
140 }
141
142 getAvatarUrl () {
143 if (this.user) return this.user.accountAvatarUrl
144 return window.location.origin + '/client/assets/images/default-avatar.png'
145 }
146
147 gotoLogin () {
148 this.hideVisitorModal()
149 this.router.navigate([ '/login' ])
150 }
151
152 cancelCommentReply () {
153 this.cancel.emit(null)
154 this.form.value['text'] = this.textareaElement.nativeElement.value = ''
155 }
156
157 private addCommentReply (commentCreate: VideoCommentCreate) {
158 return this.videoCommentService
159 .addCommentReply(this.video.id, this.parentComment.id, commentCreate)
160 }
161
162 private addCommentThread (commentCreate: VideoCommentCreate) {
163 return this.videoCommentService
164 .addCommentThread(this.video.id, commentCreate)
165 }
166
167 private patchTextValue (text: string, focus: boolean) {
168 setTimeout(() => {
169 if (focus) {
170 this.textareaElement.nativeElement.focus()
171 }
172
173 this.textareaElement.nativeElement.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'nearest' })
174 })
175
176 this.form.patchValue({ text })
177 }
178 }