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