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