]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/+video-watch/comment/video-comment-add.component.ts
Lazy load all routes
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-watch / comment / video-comment-add.component.ts
CommitLineData
67ed6552 1import { Observable } from 'rxjs'
eacf925e 2import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
660d11e9 3import { Router } from '@angular/router'
67ed6552
C
4import { Notifier, User } from '@app/core'
5import { FormReactive, FormValidatorService, VideoCommentValidatorsService } from '@app/shared/shared-forms'
6import { Video } from '@app/shared/shared-main'
7import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
8import { VideoCommentCreate } from '@shared/models'
4635f59d
C
9import { VideoComment } from './video-comment.model'
10import { 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})
17export class VideoCommentAddComponent extends FormReactive implements OnInit {
cf117aaa 18 @Input() user: User
4635f59d
C
19 @Input() video: Video
20 @Input() parentComment: VideoComment
d7e70384 21 @Input() parentComments: VideoComment[]
eacf925e 22 @Input() focusOnInit = false
4635f59d 23
be27ef3b 24 @Output() commentCreated = new EventEmitter<VideoComment>()
88adad2d 25 @Output() cancel = new EventEmitter()
4635f59d 26
f36da21e
C
27 @ViewChild('visitorModal', { static: true }) visitorModal: NgbModal
28 @ViewChild('textarea', { static: true }) textareaElement: ElementRef
eacf925e 29
2fbe7f19 30 addingComment = false
ff336427 31
4635f59d 32 constructor (
d18d6478 33 protected formValidatorService: FormValidatorService,
e309822b 34 private videoCommentValidatorsService: VideoCommentValidatorsService,
f8b2c1b4 35 private notifier: Notifier,
b1d40cff 36 private videoCommentService: VideoCommentService,
660d11e9 37 private modalService: NgbModal,
3ddb1ec5 38 private router: Router
4635f59d
C
39 ) {
40 super()
41 }
42
4635f59d 43 ngOnInit () {
d18d6478 44 this.buildForm({
e309822b 45 text: this.videoCommentValidatorsService.VIDEO_COMMENT_TEXT
d18d6478 46 })
eacf925e 47
660d11e9
RK
48 if (this.user) {
49 if (this.focusOnInit === true) {
50 this.textareaElement.nativeElement.focus()
51 }
52
53 if (this.parentComment) {
54 const mentions = this.parentComments
6cb55644 55 .filter(c => c.account && c.account.id !== this.user.account.id) // Don't add mention of ourselves
660d11e9 56 .map(c => '@' + c.by)
d7e70384 57
660d11e9
RK
58 const mentionsSet = new Set(mentions)
59 const mentionsText = Array.from(mentionsSet).join(' ') + ' '
d7e70384 60
660d11e9
RK
61 this.form.patchValue({ text: mentionsText })
62 }
d7e70384 63 }
4635f59d
C
64 }
65
1263fc4e 66 onValidKey () {
3866f1a0 67 this.check()
1263fc4e
C
68 if (!this.form.valid) return
69
70 this.formValidated()
71 }
72
244b4ae3 73 openVisitorModal (event: any) {
660d11e9
RK
74 if (this.user === null) { // we only open it for visitors
75 // fixing ng-bootstrap ModalService and the "Expression Changed After It Has Been Checked" Error
76 event.srcElement.blur()
77 event.preventDefault()
78
79 this.modalService.open(this.visitorModal)
80 }
81 }
82
83 hideVisitorModal () {
84 this.modalService.dismissAll()
85 }
86
4635f59d 87 formValidated () {
ff336427
C
88 // If we validate very quickly the comment form, we might comment twice
89 if (this.addingComment) return
90
91 this.addingComment = true
92
4635f59d 93 const commentCreate: VideoCommentCreate = this.form.value
be27ef3b 94 let obs: Observable<VideoComment>
4635f59d
C
95
96 if (this.parentComment) {
97 obs = this.addCommentReply(commentCreate)
98 } else {
99 obs = this.addCommentThread(commentCreate)
100 }
101
102 obs.subscribe(
103 comment => {
ff336427 104 this.addingComment = false
4635f59d
C
105 this.commentCreated.emit(comment)
106 this.form.reset()
107 },
108
ff336427
C
109 err => {
110 this.addingComment = false
111
f8b2c1b4 112 this.notifier.error(err.text)
ff336427 113 }
4635f59d 114 )
d50acfab 115 }
4635f59d
C
116
117 isAddButtonDisplayed () {
118 return this.form.value['text']
119 }
120
3ddb1ec5 121 getUri () {
6d5973fa
RK
122 return window.location.href
123 }
124
660d11e9
RK
125 getAvatarUrl () {
126 if (this.user) return this.user.accountAvatarUrl
127 return window.location.origin + '/client/assets/images/default-avatar.png'
128 }
129
130 gotoLogin () {
131 this.hideVisitorModal()
660d11e9
RK
132 this.router.navigate([ '/login' ])
133 }
134
3f9c4955
C
135 cancelCommentReply () {
136 this.cancel.emit(null)
79671021 137 this.form.value['text'] = this.textareaElement.nativeElement.value = ''
3f9c4955
C
138 }
139
4635f59d
C
140 private addCommentReply (commentCreate: VideoCommentCreate) {
141 return this.videoCommentService
142 .addCommentReply(this.video.id, this.parentComment.id, commentCreate)
143 }
144
145 private addCommentThread (commentCreate: VideoCommentCreate) {
146 return this.videoCommentService
147 .addCommentThread(this.video.id, commentCreate)
148 }
149}