]>
Commit | Line | Data |
---|---|---|
1 | import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core' | |
2 | import { Router } from '@angular/router' | |
3 | import { NotificationsService } from 'angular2-notifications' | |
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 { I18n } from '@ngx-translate/i18n-polyfill' | |
12 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' | |
13 | import { VideoCommentValidatorsService } from '@app/shared/forms/form-validators/video-comment-validators.service' | |
14 | import { NgbModal } from '@ng-bootstrap/ng-bootstrap' | |
15 | import { AuthService } from '@app/core/auth' | |
16 | ||
17 | @Component({ | |
18 | selector: 'my-video-comment-add', | |
19 | templateUrl: './video-comment-add.component.html', | |
20 | styleUrls: ['./video-comment-add.component.scss'] | |
21 | }) | |
22 | export class VideoCommentAddComponent extends FormReactive implements OnInit { | |
23 | @Input() user: User | |
24 | @Input() video: Video | |
25 | @Input() parentComment: VideoComment | |
26 | @Input() parentComments: VideoComment[] | |
27 | @Input() focusOnInit = false | |
28 | ||
29 | @Output() commentCreated = new EventEmitter<VideoCommentCreate>() | |
30 | ||
31 | @ViewChild('visitorModal') visitorModal: NgbModal | |
32 | @ViewChild('textarea') private textareaElement: ElementRef | |
33 | ||
34 | private addingComment = false | |
35 | private uri: string | |
36 | ||
37 | constructor ( | |
38 | protected formValidatorService: FormValidatorService, | |
39 | private videoCommentValidatorsService: VideoCommentValidatorsService, | |
40 | private notificationsService: NotificationsService, | |
41 | private videoCommentService: VideoCommentService, | |
42 | private authService: AuthService, | |
43 | private modalService: NgbModal, | |
44 | private router: Router, | |
45 | private i18n: I18n | |
46 | ) { | |
47 | super() | |
48 | } | |
49 | ||
50 | ngOnInit () { | |
51 | this.buildForm({ | |
52 | text: this.videoCommentValidatorsService.VIDEO_COMMENT_TEXT | |
53 | }) | |
54 | ||
55 | this.uri = this.router.url | |
56 | ||
57 | if (this.user) { | |
58 | if (this.focusOnInit === true) { | |
59 | this.textareaElement.nativeElement.focus() | |
60 | } | |
61 | ||
62 | if (this.parentComment) { | |
63 | const mentions = this.parentComments | |
64 | .filter(c => c.account.id !== this.user.account.id) // Don't add mention of ourselves | |
65 | .map(c => '@' + c.by) | |
66 | ||
67 | const mentionsSet = new Set(mentions) | |
68 | const mentionsText = Array.from(mentionsSet).join(' ') + ' ' | |
69 | ||
70 | this.form.patchValue({ text: mentionsText }) | |
71 | } | |
72 | } | |
73 | } | |
74 | ||
75 | onValidKey () { | |
76 | this.onValueChanged() | |
77 | if (!this.form.valid) return | |
78 | ||
79 | this.formValidated() | |
80 | } | |
81 | ||
82 | openVisitorModal (event) { | |
83 | if (this.user === null) { // we only open it for visitors | |
84 | // fixing ng-bootstrap ModalService and the "Expression Changed After It Has Been Checked" Error | |
85 | event.srcElement.blur() | |
86 | event.preventDefault() | |
87 | ||
88 | this.modalService.open(this.visitorModal) | |
89 | } | |
90 | } | |
91 | ||
92 | hideVisitorModal () { | |
93 | this.modalService.dismissAll() | |
94 | } | |
95 | ||
96 | formValidated () { | |
97 | // If we validate very quickly the comment form, we might comment twice | |
98 | if (this.addingComment) return | |
99 | ||
100 | this.addingComment = true | |
101 | ||
102 | const commentCreate: VideoCommentCreate = this.form.value | |
103 | let obs: Observable<any> | |
104 | ||
105 | if (this.parentComment) { | |
106 | obs = this.addCommentReply(commentCreate) | |
107 | } else { | |
108 | obs = this.addCommentThread(commentCreate) | |
109 | } | |
110 | ||
111 | obs.subscribe( | |
112 | comment => { | |
113 | this.addingComment = false | |
114 | this.commentCreated.emit(comment) | |
115 | this.form.reset() | |
116 | }, | |
117 | ||
118 | err => { | |
119 | this.addingComment = false | |
120 | ||
121 | this.notificationsService.error(this.i18n('Error'), err.text) | |
122 | } | |
123 | ) | |
124 | } | |
125 | ||
126 | isAddButtonDisplayed () { | |
127 | return this.form.value['text'] | |
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.authService.redirectUrl = this.router.url | |
138 | this.router.navigate([ '/login' ]) | |
139 | } | |
140 | ||
141 | private addCommentReply (commentCreate: VideoCommentCreate) { | |
142 | return this.videoCommentService | |
143 | .addCommentReply(this.video.id, this.parentComment.id, commentCreate) | |
144 | } | |
145 | ||
146 | private addCommentThread (commentCreate: VideoCommentCreate) { | |
147 | return this.videoCommentService | |
148 | .addCommentThread(this.video.id, commentCreate) | |
149 | } | |
150 | } |