]>
Commit | Line | Data |
---|---|---|
1 | import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core' | |
2 | import { Router } from '@angular/router' | |
3 | import { Notifier } from '@app/core' | |
4 | import { Observable } from 'rxjs' | |
5 | import { VideoCommentCreate } from '../../../../../../shared/models/videos/video-comment.model' | |
6 | import { FormReactive } from '../../../shared' | |
7 | import { User } from '../../../shared/users' | |
8 | import { Video } from '../../../shared/video/video.model' | |
9 | import { VideoComment } from './video-comment.model' | |
10 | import { VideoCommentService } from './video-comment.service' | |
11 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' | |
12 | import { VideoCommentValidatorsService } from '@app/shared/forms/form-validators/video-comment-validators.service' | |
13 | import { NgbModal } from '@ng-bootstrap/ng-bootstrap' | |
14 | import { AuthService } from '@app/core/auth' | |
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 { | |
22 | @Input() user: User | |
23 | @Input() video: Video | |
24 | @Input() parentComment: VideoComment | |
25 | @Input() parentComments: VideoComment[] | |
26 | @Input() focusOnInit = false | |
27 | ||
28 | @Output() commentCreated = new EventEmitter<VideoCommentCreate>() | |
29 | ||
30 | @ViewChild('visitorModal', { static: true }) visitorModal: NgbModal | |
31 | @ViewChild('textarea', { static: true }) textareaElement: ElementRef | |
32 | ||
33 | addingComment = false | |
34 | ||
35 | constructor ( | |
36 | protected formValidatorService: FormValidatorService, | |
37 | private videoCommentValidatorsService: VideoCommentValidatorsService, | |
38 | private notifier: Notifier, | |
39 | private videoCommentService: VideoCommentService, | |
40 | private authService: AuthService, | |
41 | private modalService: NgbModal, | |
42 | private router: Router | |
43 | ) { | |
44 | super() | |
45 | } | |
46 | ||
47 | ngOnInit () { | |
48 | this.buildForm({ | |
49 | text: this.videoCommentValidatorsService.VIDEO_COMMENT_TEXT | |
50 | }) | |
51 | ||
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) | |
61 | ||
62 | const mentionsSet = new Set(mentions) | |
63 | const mentionsText = Array.from(mentionsSet).join(' ') + ' ' | |
64 | ||
65 | this.form.patchValue({ text: mentionsText }) | |
66 | } | |
67 | } | |
68 | } | |
69 | ||
70 | onValidKey () { | |
71 | this.check() | |
72 | if (!this.form.valid) return | |
73 | ||
74 | this.formValidated() | |
75 | } | |
76 | ||
77 | openVisitorModal (event: any) { | |
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 | ||
91 | formValidated () { | |
92 | // If we validate very quickly the comment form, we might comment twice | |
93 | if (this.addingComment) return | |
94 | ||
95 | this.addingComment = true | |
96 | ||
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 => { | |
108 | this.addingComment = false | |
109 | this.commentCreated.emit(comment) | |
110 | this.form.reset() | |
111 | }, | |
112 | ||
113 | err => { | |
114 | this.addingComment = false | |
115 | ||
116 | this.notifier.error(err.text) | |
117 | } | |
118 | ) | |
119 | } | |
120 | ||
121 | isAddButtonDisplayed () { | |
122 | return this.form.value['text'] | |
123 | } | |
124 | ||
125 | getUri () { | |
126 | return window.location.href | |
127 | } | |
128 | ||
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() | |
136 | this.router.navigate([ '/login' ]) | |
137 | } | |
138 | ||
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 | } |