1 import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
2 import { FormBuilder, FormGroup } from '@angular/forms'
3 import { NotificationsService } from 'angular2-notifications'
4 import { Observable } from 'rxjs/Observable'
5 import { VideoCommentCreate } from '../../../../../../shared/models/videos/video-comment.model'
6 import { FormReactive } from '../../../shared'
7 import { VIDEO_COMMENT_TEXT } from '../../../shared/forms/form-validators/video-comment'
8 import { User } from '../../../shared/users'
9 import { Video } from '../../../shared/video/video.model'
10 import { VideoComment } from './video-comment.model'
11 import { VideoCommentService } from './video-comment.service'
14 selector: 'my-video-comment-add',
15 templateUrl: './video-comment-add.component.html',
16 styleUrls: ['./video-comment-add.component.scss']
18 export class VideoCommentAddComponent extends FormReactive implements OnInit {
21 @Input() parentComment: VideoComment
22 @Input() parentComments: VideoComment[]
23 @Input() focusOnInit = false
25 @Output() commentCreated = new EventEmitter<VideoCommentCreate>()
31 validationMessages = {
32 'text': VIDEO_COMMENT_TEXT.MESSAGES
35 @ViewChild('textarea') private textareaElement: ElementRef
38 private formBuilder: FormBuilder,
39 private notificationsService: NotificationsService,
40 private videoCommentService: VideoCommentService
46 this.form = this.formBuilder.group({
47 text: [ '', VIDEO_COMMENT_TEXT.VALIDATORS ]
50 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
56 if (this.focusOnInit === true) {
57 this.textareaElement.nativeElement.focus()
60 if (this.parentComment) {
61 const mentions = this.parentComments
62 .filter(c => c.account.id !== this.user.account.id) // Don't add mention of ourselves
65 const mentionsSet = new Set(mentions)
66 const mentionsText = Array.from(mentionsSet).join(' ') + ' '
68 this.form.patchValue({ text: mentionsText })
74 if (!this.form.valid) return
80 const commentCreate: VideoCommentCreate = this.form.value
81 let obs: Observable<any>
83 if (this.parentComment) {
84 obs = this.addCommentReply(commentCreate)
86 obs = this.addCommentThread(commentCreate)
91 this.commentCreated.emit(comment)
95 err => this.notificationsService.error('Error', err.text)
99 isAddButtonDisplayed () {
100 return this.form.value['text']
103 getUserAvatarUrl () {
104 return this.user.getAvatarUrl()
107 private addCommentReply (commentCreate: VideoCommentCreate) {
108 return this.videoCommentService
109 .addCommentReply(this.video.id, this.parentComment.id, commentCreate)
112 private addCommentThread (commentCreate: VideoCommentCreate) {
113 return this.videoCommentService
114 .addCommentThread(this.video.id, commentCreate)