]>
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>() | |
88adad2d | 29 | @Output() cancel = new EventEmitter() |
4635f59d | 30 | |
f36da21e C |
31 | @ViewChild('visitorModal', { static: true }) visitorModal: NgbModal |
32 | @ViewChild('textarea', { static: true }) textareaElement: ElementRef | |
eacf925e | 33 | |
2fbe7f19 | 34 | addingComment = false |
ff336427 | 35 | |
4635f59d | 36 | constructor ( |
d18d6478 | 37 | protected formValidatorService: FormValidatorService, |
e309822b | 38 | private videoCommentValidatorsService: VideoCommentValidatorsService, |
f8b2c1b4 | 39 | private notifier: Notifier, |
b1d40cff | 40 | private videoCommentService: VideoCommentService, |
660d11e9 RK |
41 | private authService: AuthService, |
42 | private modalService: NgbModal, | |
3ddb1ec5 | 43 | private router: Router |
4635f59d C |
44 | ) { |
45 | super() | |
46 | } | |
47 | ||
4635f59d | 48 | ngOnInit () { |
d18d6478 | 49 | this.buildForm({ |
e309822b | 50 | text: this.videoCommentValidatorsService.VIDEO_COMMENT_TEXT |
d18d6478 | 51 | }) |
eacf925e | 52 | |
660d11e9 RK |
53 | if (this.user) { |
54 | if (this.focusOnInit === true) { | |
55 | this.textareaElement.nativeElement.focus() | |
56 | } | |
57 | ||
58 | if (this.parentComment) { | |
59 | const mentions = this.parentComments | |
60 | .filter(c => c.account.id !== this.user.account.id) // Don't add mention of ourselves | |
61 | .map(c => '@' + c.by) | |
d7e70384 | 62 | |
660d11e9 RK |
63 | const mentionsSet = new Set(mentions) |
64 | const mentionsText = Array.from(mentionsSet).join(' ') + ' ' | |
d7e70384 | 65 | |
660d11e9 RK |
66 | this.form.patchValue({ text: mentionsText }) |
67 | } | |
d7e70384 | 68 | } |
4635f59d C |
69 | } |
70 | ||
1263fc4e | 71 | onValidKey () { |
3866f1a0 | 72 | this.check() |
1263fc4e C |
73 | if (!this.form.valid) return |
74 | ||
75 | this.formValidated() | |
76 | } | |
77 | ||
244b4ae3 | 78 | openVisitorModal (event: any) { |
660d11e9 RK |
79 | if (this.user === null) { // we only open it for visitors |
80 | // fixing ng-bootstrap ModalService and the "Expression Changed After It Has Been Checked" Error | |
81 | event.srcElement.blur() | |
82 | event.preventDefault() | |
83 | ||
84 | this.modalService.open(this.visitorModal) | |
85 | } | |
86 | } | |
87 | ||
88 | hideVisitorModal () { | |
89 | this.modalService.dismissAll() | |
90 | } | |
91 | ||
4635f59d | 92 | formValidated () { |
ff336427 C |
93 | // If we validate very quickly the comment form, we might comment twice |
94 | if (this.addingComment) return | |
95 | ||
96 | this.addingComment = true | |
97 | ||
4635f59d C |
98 | const commentCreate: VideoCommentCreate = this.form.value |
99 | let obs: Observable<any> | |
100 | ||
101 | if (this.parentComment) { | |
102 | obs = this.addCommentReply(commentCreate) | |
103 | } else { | |
104 | obs = this.addCommentThread(commentCreate) | |
105 | } | |
106 | ||
107 | obs.subscribe( | |
108 | comment => { | |
ff336427 | 109 | this.addingComment = false |
4635f59d C |
110 | this.commentCreated.emit(comment) |
111 | this.form.reset() | |
112 | }, | |
113 | ||
ff336427 C |
114 | err => { |
115 | this.addingComment = false | |
116 | ||
f8b2c1b4 | 117 | this.notifier.error(err.text) |
ff336427 | 118 | } |
4635f59d | 119 | ) |
d50acfab | 120 | } |
4635f59d C |
121 | |
122 | isAddButtonDisplayed () { | |
123 | return this.form.value['text'] | |
124 | } | |
125 | ||
3ddb1ec5 | 126 | getUri () { |
6d5973fa RK |
127 | return window.location.href |
128 | } | |
129 | ||
660d11e9 RK |
130 | getAvatarUrl () { |
131 | if (this.user) return this.user.accountAvatarUrl | |
132 | return window.location.origin + '/client/assets/images/default-avatar.png' | |
133 | } | |
134 | ||
135 | gotoLogin () { | |
136 | this.hideVisitorModal() | |
660d11e9 RK |
137 | this.router.navigate([ '/login' ]) |
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 | } | |
88adad2d RK |
149 | |
150 | private cancelCommentReply () { | |
151 | this.cancel.emit(null) | |
152 | } | |
4635f59d | 153 | } |