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'
12 selector: 'my-video-comment-add',
13 templateUrl: './video-comment-add.component.html',
14 styleUrls: ['./video-comment-add.component.scss']
16 export class VideoCommentAddComponent extends FormReactive implements OnChanges, OnInit {
19 @Input() parentComment: VideoComment
20 @Input() parentComments: VideoComment[]
21 @Input() focusOnInit = false
22 @Input() textValue?: string
24 @Output() commentCreated = new EventEmitter<VideoComment>()
25 @Output() cancel = new EventEmitter()
27 @ViewChild('visitorModal', { static: true }) visitorModal: NgbModal
28 @ViewChild('textarea', { static: true }) textareaElement: ElementRef
33 protected formValidatorService: FormValidatorService,
34 private videoCommentValidatorsService: VideoCommentValidatorsService,
35 private notifier: Notifier,
36 private videoCommentService: VideoCommentService,
37 private modalService: NgbModal,
38 private router: Router
45 text: this.videoCommentValidatorsService.VIDEO_COMMENT_TEXT
50 this.patchTextValue(this.textValue, this.focusOnInit)
54 if (this.parentComment) {
55 const mentions = this.parentComments
56 .filter(c => c.account && c.account.id !== this.user.account.id) // Don't add mention of ourselves
59 const mentionsSet = new Set(mentions)
60 const mentionsText = Array.from(mentionsSet).join(' ') + ' '
62 this.patchTextValue(mentionsText, this.focusOnInit)
67 ngOnChanges (changes: SimpleChanges) {
68 if (changes.textValue && changes.textValue.currentValue && changes.textValue.currentValue !== changes.textValue.previousValue) {
69 this.patchTextValue(changes.textValue.currentValue, true)
75 if (!this.form.valid) return
80 openVisitorModal (event: any) {
81 if (this.user === null) { // we only open it for visitors
82 // fixing ng-bootstrap ModalService and the "Expression Changed After It Has Been Checked" Error
83 event.srcElement.blur()
84 event.preventDefault()
86 this.modalService.open(this.visitorModal)
91 this.modalService.dismissAll()
95 // If we validate very quickly the comment form, we might comment twice
96 if (this.addingComment) return
98 this.addingComment = true
100 const commentCreate: VideoCommentCreate = this.form.value
101 let obs: Observable<VideoComment>
103 if (this.parentComment) {
104 obs = this.addCommentReply(commentCreate)
106 obs = this.addCommentThread(commentCreate)
111 this.addingComment = false
112 this.commentCreated.emit(comment)
117 this.addingComment = false
119 this.notifier.error(err.text)
124 isAddButtonDisplayed () {
125 return this.form.value['text']
129 return window.location.href
133 if (this.user) return this.user.accountAvatarUrl
134 return window.location.origin + '/client/assets/images/default-avatar.png'
138 this.hideVisitorModal()
139 this.router.navigate([ '/login' ])
142 cancelCommentReply () {
143 this.cancel.emit(null)
144 this.form.value['text'] = this.textareaElement.nativeElement.value = ''
147 private addCommentReply (commentCreate: VideoCommentCreate) {
148 return this.videoCommentService
149 .addCommentReply(this.video.id, this.parentComment.id, commentCreate)
152 private addCommentThread (commentCreate: VideoCommentCreate) {
153 return this.videoCommentService
154 .addCommentThread(this.video.id, commentCreate)
157 private patchTextValue (text: string, focus: boolean) {
160 this.textareaElement.nativeElement.focus()
163 this.textareaElement.nativeElement.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'nearest' })
166 this.form.patchValue({ text })