]>
Commit | Line | Data |
---|---|---|
67ed6552 | 1 | import { Observable } from 'rxjs' |
f63c03fb | 2 | import { Component, ElementRef, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges, ViewChild } from '@angular/core' |
660d11e9 | 3 | import { Router } from '@angular/router' |
67ed6552 C |
4 | import { Notifier, User } from '@app/core' |
5 | import { FormReactive, FormValidatorService, VideoCommentValidatorsService } from '@app/shared/shared-forms' | |
6 | import { Video } from '@app/shared/shared-main' | |
cfde28ba | 7 | import { VideoComment, VideoCommentService } from '@app/shared/shared-video-comment' |
67ed6552 C |
8 | import { NgbModal } from '@ng-bootstrap/ng-bootstrap' |
9 | import { VideoCommentCreate } from '@shared/models' | |
cb54210c | 10 | import { I18n } from '@ngx-translate/i18n-polyfill' |
4635f59d C |
11 | |
12 | @Component({ | |
13 | selector: 'my-video-comment-add', | |
14 | templateUrl: './video-comment-add.component.html', | |
15 | styleUrls: ['./video-comment-add.component.scss'] | |
16 | }) | |
f63c03fb | 17 | export class VideoCommentAddComponent extends FormReactive implements OnChanges, 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 |
f63c03fb | 23 | @Input() textValue?: string |
cb54210c | 24 | @Input() commentThread?: boolean |
4635f59d | 25 | |
be27ef3b | 26 | @Output() commentCreated = new EventEmitter<VideoComment>() |
88adad2d | 27 | @Output() cancel = new EventEmitter() |
4635f59d | 28 | |
f36da21e C |
29 | @ViewChild('visitorModal', { static: true }) visitorModal: NgbModal |
30 | @ViewChild('textarea', { static: true }) textareaElement: ElementRef | |
eacf925e | 31 | |
2fbe7f19 | 32 | addingComment = false |
cb54210c | 33 | addingCommentButtonValue: string |
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, |
cb54210c | 41 | private router: Router, |
42 | private i18n: I18n | |
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 | 52 | if (this.user) { |
cb54210c | 53 | if (this.commentThread) { |
54 | this.addingCommentButtonValue = this.i18n('Comment') | |
55 | } else { | |
56 | this.addingCommentButtonValue = this.i18n('Reply') | |
57 | } | |
58 | ||
f63c03fb | 59 | if (this.textValue) { |
60 | this.patchTextValue(this.textValue, this.focusOnInit) | |
61 | return | |
660d11e9 RK |
62 | } |
63 | ||
64 | if (this.parentComment) { | |
65 | const mentions = this.parentComments | |
6cb55644 | 66 | .filter(c => c.account && c.account.id !== this.user.account.id) // Don't add mention of ourselves |
660d11e9 | 67 | .map(c => '@' + c.by) |
d7e70384 | 68 | |
660d11e9 RK |
69 | const mentionsSet = new Set(mentions) |
70 | const mentionsText = Array.from(mentionsSet).join(' ') + ' ' | |
d7e70384 | 71 | |
f63c03fb | 72 | this.patchTextValue(mentionsText, this.focusOnInit) |
660d11e9 | 73 | } |
d7e70384 | 74 | } |
4635f59d C |
75 | } |
76 | ||
f63c03fb | 77 | ngOnChanges (changes: SimpleChanges) { |
78 | if (changes.textValue && changes.textValue.currentValue && changes.textValue.currentValue !== changes.textValue.previousValue) { | |
79 | this.patchTextValue(changes.textValue.currentValue, true) | |
80 | } | |
81 | } | |
82 | ||
1263fc4e | 83 | onValidKey () { |
3866f1a0 | 84 | this.check() |
1263fc4e C |
85 | if (!this.form.valid) return |
86 | ||
87 | this.formValidated() | |
88 | } | |
89 | ||
244b4ae3 | 90 | openVisitorModal (event: any) { |
660d11e9 RK |
91 | if (this.user === null) { // we only open it for visitors |
92 | // fixing ng-bootstrap ModalService and the "Expression Changed After It Has Been Checked" Error | |
93 | event.srcElement.blur() | |
94 | event.preventDefault() | |
95 | ||
96 | this.modalService.open(this.visitorModal) | |
97 | } | |
98 | } | |
99 | ||
100 | hideVisitorModal () { | |
101 | this.modalService.dismissAll() | |
102 | } | |
103 | ||
4635f59d | 104 | formValidated () { |
ff336427 C |
105 | // If we validate very quickly the comment form, we might comment twice |
106 | if (this.addingComment) return | |
107 | ||
108 | this.addingComment = true | |
109 | ||
4635f59d | 110 | const commentCreate: VideoCommentCreate = this.form.value |
be27ef3b | 111 | let obs: Observable<VideoComment> |
4635f59d C |
112 | |
113 | if (this.parentComment) { | |
114 | obs = this.addCommentReply(commentCreate) | |
115 | } else { | |
116 | obs = this.addCommentThread(commentCreate) | |
117 | } | |
118 | ||
119 | obs.subscribe( | |
120 | comment => { | |
ff336427 | 121 | this.addingComment = false |
4635f59d C |
122 | this.commentCreated.emit(comment) |
123 | this.form.reset() | |
124 | }, | |
125 | ||
ff336427 C |
126 | err => { |
127 | this.addingComment = false | |
128 | ||
f8b2c1b4 | 129 | this.notifier.error(err.text) |
ff336427 | 130 | } |
4635f59d | 131 | ) |
d50acfab | 132 | } |
4635f59d C |
133 | |
134 | isAddButtonDisplayed () { | |
135 | return this.form.value['text'] | |
136 | } | |
137 | ||
3ddb1ec5 | 138 | getUri () { |
6d5973fa RK |
139 | return window.location.href |
140 | } | |
141 | ||
660d11e9 RK |
142 | getAvatarUrl () { |
143 | if (this.user) return this.user.accountAvatarUrl | |
144 | return window.location.origin + '/client/assets/images/default-avatar.png' | |
145 | } | |
146 | ||
147 | gotoLogin () { | |
148 | this.hideVisitorModal() | |
660d11e9 RK |
149 | this.router.navigate([ '/login' ]) |
150 | } | |
151 | ||
3f9c4955 C |
152 | cancelCommentReply () { |
153 | this.cancel.emit(null) | |
79671021 | 154 | this.form.value['text'] = this.textareaElement.nativeElement.value = '' |
3f9c4955 C |
155 | } |
156 | ||
4635f59d C |
157 | private addCommentReply (commentCreate: VideoCommentCreate) { |
158 | return this.videoCommentService | |
159 | .addCommentReply(this.video.id, this.parentComment.id, commentCreate) | |
160 | } | |
161 | ||
162 | private addCommentThread (commentCreate: VideoCommentCreate) { | |
163 | return this.videoCommentService | |
164 | .addCommentThread(this.video.id, commentCreate) | |
165 | } | |
f63c03fb | 166 | |
167 | private patchTextValue (text: string, focus: boolean) { | |
168 | setTimeout(() => { | |
169 | if (focus) { | |
170 | this.textareaElement.nativeElement.focus() | |
171 | } | |
172 | ||
173 | this.textareaElement.nativeElement.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'nearest' }) | |
174 | }) | |
175 | ||
176 | this.form.patchValue({ text }) | |
177 | } | |
4635f59d | 178 | } |