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'
18 selector: 'my-video-comment-add',
19 templateUrl: './video-comment-add.component.html',
20 styleUrls: ['./video-comment-add.component.scss']
22 export class VideoCommentAddComponent extends FormReactive implements OnInit {
25 @Input() parentComment: VideoComment
26 @Input() parentComments: VideoComment[]
27 @Input() focusOnInit = false
29 @Output() commentCreated = new EventEmitter<VideoCommentCreate>()
31 @ViewChild('visitorModal') visitorModal: NgbModal
32 @ViewChild('textarea') private textareaElement: ElementRef
34 private addingComment = false
38 protected formValidatorService: FormValidatorService,
39 private videoCommentValidatorsService: VideoCommentValidatorsService,
40 private notificationsService: NotificationsService,
41 private videoCommentService: VideoCommentService,
42 private authService: AuthService,
43 private modalService: NgbModal,
44 private router: Router,
52 text: this.videoCommentValidatorsService.VIDEO_COMMENT_TEXT
55 this.uri = this.router.url
58 if (this.focusOnInit === true) {
59 this.textareaElement.nativeElement.focus()
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
67 const mentionsSet = new Set(mentions)
68 const mentionsText = Array.from(mentionsSet).join(' ') + ' '
70 this.form.patchValue({ text: mentionsText })
77 if (!this.form.valid) return
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()
88 this.modalService.open(this.visitorModal)
93 this.modalService.dismissAll()
97 // If we validate very quickly the comment form, we might comment twice
98 if (this.addingComment) return
100 this.addingComment = true
102 const commentCreate: VideoCommentCreate = this.form.value
103 let obs: Observable<any>
105 if (this.parentComment) {
106 obs = this.addCommentReply(commentCreate)
108 obs = this.addCommentThread(commentCreate)
113 this.addingComment = false
114 this.commentCreated.emit(comment)
119 this.addingComment = false
121 this.notificationsService.error(this.i18n('Error'), err.text)
126 isAddButtonDisplayed () {
127 return this.form.value['text']
131 return window.location.href
135 if (this.user) return this.user.accountAvatarUrl
136 return window.location.origin + '/client/assets/images/default-avatar.png'
140 this.hideVisitorModal()
141 this.authService.redirectUrl = this.router.url
142 this.router.navigate([ '/login' ])
145 private addCommentReply (commentCreate: VideoCommentCreate) {
146 return this.videoCommentService
147 .addCommentReply(this.video.id, this.parentComment.id, commentCreate)
150 private addCommentThread (commentCreate: VideoCommentCreate) {
151 return this.videoCommentService
152 .addCommentThread(this.video.id, commentCreate)