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