]>
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<VideoComment>() | |
29 | @Output() cancel = new EventEmitter() | |
30 | ||
31 | @ViewChild('visitorModal', { static: true }) visitorModal: NgbModal | |
32 | @ViewChild('textarea', { static: true }) textareaElement: ElementRef | |
33 | ||
34 | addingComment = false | |
35 | ||
36 | constructor ( | |
37 | protected formValidatorService: FormValidatorService, | |
38 | private videoCommentValidatorsService: VideoCommentValidatorsService, | |
39 | private notifier: Notifier, | |
40 | private videoCommentService: VideoCommentService, | |
41 | private authService: AuthService, | |
42 | private modalService: NgbModal, | |
43 | private router: Router | |
44 | ) { | |
45 | super() | |
46 | } | |
47 | ||
48 | ngOnInit () { | |
49 | this.buildForm({ | |
50 | text: this.videoCommentValidatorsService.VIDEO_COMMENT_TEXT | |
51 | }) | |
52 | ||
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 && c.account.id !== this.user.account.id) // Don't add mention of ourselves | |
61 | .map(c => '@' + c.by) | |
62 | ||
63 | const mentionsSet = new Set(mentions) | |
64 | const mentionsText = Array.from(mentionsSet).join(' ') + ' ' | |
65 | ||
66 | this.form.patchValue({ text: mentionsText }) | |
67 | } | |
68 | } | |
69 | } | |
70 | ||
71 | onValidKey () { | |
72 | this.check() | |
73 | if (!this.form.valid) return | |
74 | ||
75 | this.formValidated() | |
76 | } | |
77 | ||
78 | openVisitorModal (event: any) { | |
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 | ||
92 | formValidated () { | |
93 | // If we validate very quickly the comment form, we might comment twice | |
94 | if (this.addingComment) return | |
95 | ||
96 | this.addingComment = true | |
97 | ||
98 | const commentCreate: VideoCommentCreate = this.form.value | |
99 | let obs: Observable<VideoComment> | |
100 | ||
101 | if (this.parentComment) { | |
102 | obs = this.addCommentReply(commentCreate) | |
103 | } else { | |
104 | obs = this.addCommentThread(commentCreate) | |
105 | } | |
106 | ||
107 | obs.subscribe( | |
108 | comment => { | |
109 | this.addingComment = false | |
110 | this.commentCreated.emit(comment) | |
111 | this.form.reset() | |
112 | }, | |
113 | ||
114 | err => { | |
115 | this.addingComment = false | |
116 | ||
117 | this.notifier.error(err.text) | |
118 | } | |
119 | ) | |
120 | } | |
121 | ||
122 | isAddButtonDisplayed () { | |
123 | return this.form.value['text'] | |
124 | } | |
125 | ||
126 | getUri () { | |
127 | return window.location.href | |
128 | } | |
129 | ||
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() | |
137 | this.router.navigate([ '/login' ]) | |
138 | } | |
139 | ||
140 | cancelCommentReply () { | |
141 | this.cancel.emit(null) | |
142 | this.form.value['text'] = this.textareaElement.nativeElement.value = '' | |
143 | } | |
144 | ||
145 | private addCommentReply (commentCreate: VideoCommentCreate) { | |
146 | return this.videoCommentService | |
147 | .addCommentReply(this.video.id, this.parentComment.id, commentCreate) | |
148 | } | |
149 | ||
150 | private addCommentThread (commentCreate: VideoCommentCreate) { | |
151 | return this.videoCommentService | |
152 | .addCommentThread(this.video.id, commentCreate) | |
153 | } | |
154 | } |