]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-watch/comment/video-comment-add.component.ts
Comment textarea focus on init
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / comment / video-comment-add.component.ts
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 { Video } from '../../../shared/video/video.model'
9 import { VideoComment } from './video-comment.model'
10 import { VideoCommentService } from './video-comment.service'
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 OnInit {
18 @Input() video: Video
19 @Input() parentComment: VideoComment
20 @Input() focusOnInit = false
21
22 @Output() commentCreated = new EventEmitter<VideoCommentCreate>()
23
24 form: FormGroup
25 formErrors = {
26 'text': ''
27 }
28 validationMessages = {
29 'text': VIDEO_COMMENT_TEXT.MESSAGES
30 }
31
32 @ViewChild('textarea') private textareaElement: ElementRef
33
34 constructor (
35 private formBuilder: FormBuilder,
36 private notificationsService: NotificationsService,
37 private videoCommentService: VideoCommentService
38 ) {
39 super()
40 }
41
42 buildForm () {
43 this.form = this.formBuilder.group({
44 text: [ '', VIDEO_COMMENT_TEXT.VALIDATORS ]
45 })
46
47 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
48 }
49
50 ngOnInit () {
51 this.buildForm()
52
53 if (this.focusOnInit === true) {
54 this.textareaElement.nativeElement.focus()
55 }
56 }
57
58 formValidated () {
59 const commentCreate: VideoCommentCreate = this.form.value
60 let obs: Observable<any>
61
62 if (this.parentComment) {
63 obs = this.addCommentReply(commentCreate)
64 } else {
65 obs = this.addCommentThread(commentCreate)
66 }
67
68 obs.subscribe(
69 comment => {
70 this.commentCreated.emit(comment)
71 this.form.reset()
72 },
73
74 err => this.notificationsService.error('Error', err.text)
75 )
76 }
77
78 isAddButtonDisplayed () {
79 return this.form.value['text']
80 }
81
82 private addCommentReply (commentCreate: VideoCommentCreate) {
83 return this.videoCommentService
84 .addCommentReply(this.video.id, this.parentComment.id, commentCreate)
85 }
86
87 private addCommentThread (commentCreate: VideoCommentCreate) {
88 return this.videoCommentService
89 .addCommentThread(this.video.id, commentCreate)
90 }
91 }