diff options
Diffstat (limited to 'client/src/app/videos/+video-watch/comment/video-comments.component.ts')
-rw-r--r-- | client/src/app/videos/+video-watch/comment/video-comments.component.ts | 76 |
1 files changed, 42 insertions, 34 deletions
diff --git a/client/src/app/videos/+video-watch/comment/video-comments.component.ts b/client/src/app/videos/+video-watch/comment/video-comments.component.ts index 7970a5dcf..aada9554d 100644 --- a/client/src/app/videos/+video-watch/comment/video-comments.component.ts +++ b/client/src/app/videos/+video-watch/comment/video-comments.component.ts | |||
@@ -1,7 +1,9 @@ | |||
1 | import { Component, Input, OnChanges, SimpleChanges } from '@angular/core' | 1 | import { Component, Input, OnChanges, OnDestroy, OnInit, SimpleChanges } from '@angular/core' |
2 | import { ActivatedRoute } from '@angular/router' | ||
2 | import { ConfirmService } from '@app/core' | 3 | import { ConfirmService } from '@app/core' |
3 | import { NotificationsService } from 'angular2-notifications' | 4 | import { NotificationsService } from 'angular2-notifications' |
4 | import { VideoComment as VideoCommentInterface, VideoCommentThreadTree } from '../../../../../../shared/models/videos/video-comment.model' | 5 | import { Subscription } from 'rxjs/Subscription' |
6 | import { VideoCommentThreadTree } from '../../../../../../shared/models/videos/video-comment.model' | ||
5 | import { AuthService } from '../../../core/auth' | 7 | import { AuthService } from '../../../core/auth' |
6 | import { ComponentPagination } from '../../../shared/rest/component-pagination.model' | 8 | import { ComponentPagination } from '../../../shared/rest/component-pagination.model' |
7 | import { User } from '../../../shared/users' | 9 | import { User } from '../../../shared/users' |
@@ -9,18 +11,18 @@ import { SortField } from '../../../shared/video/sort-field.type' | |||
9 | import { VideoDetails } from '../../../shared/video/video-details.model' | 11 | import { VideoDetails } from '../../../shared/video/video-details.model' |
10 | import { VideoComment } from './video-comment.model' | 12 | import { VideoComment } from './video-comment.model' |
11 | import { VideoCommentService } from './video-comment.service' | 13 | import { VideoCommentService } from './video-comment.service' |
12 | import { ActivatedRoute } from '@angular/router' | ||
13 | 14 | ||
14 | @Component({ | 15 | @Component({ |
15 | selector: 'my-video-comments', | 16 | selector: 'my-video-comments', |
16 | templateUrl: './video-comments.component.html', | 17 | templateUrl: './video-comments.component.html', |
17 | styleUrls: ['./video-comments.component.scss'] | 18 | styleUrls: ['./video-comments.component.scss'] |
18 | }) | 19 | }) |
19 | export class VideoCommentsComponent implements OnChanges { | 20 | export class VideoCommentsComponent implements OnInit, OnChanges, OnDestroy { |
20 | @Input() video: VideoDetails | 21 | @Input() video: VideoDetails |
21 | @Input() user: User | 22 | @Input() user: User |
22 | 23 | ||
23 | comments: VideoComment[] = [] | 24 | comments: VideoComment[] = [] |
25 | highlightedComment: VideoComment | ||
24 | sort: SortField = '-createdAt' | 26 | sort: SortField = '-createdAt' |
25 | componentPagination: ComponentPagination = { | 27 | componentPagination: ComponentPagination = { |
26 | currentPage: 1, | 28 | currentPage: 1, |
@@ -30,7 +32,8 @@ export class VideoCommentsComponent implements OnChanges { | |||
30 | inReplyToCommentId: number | 32 | inReplyToCommentId: number |
31 | threadComments: { [ id: number ]: VideoCommentThreadTree } = {} | 33 | threadComments: { [ id: number ]: VideoCommentThreadTree } = {} |
32 | threadLoading: { [ id: number ]: boolean } = {} | 34 | threadLoading: { [ id: number ]: boolean } = {} |
33 | markedCommentID: number | 35 | |
36 | private sub: Subscription | ||
34 | 37 | ||
35 | constructor ( | 38 | constructor ( |
36 | private authService: AuthService, | 39 | private authService: AuthService, |
@@ -40,20 +43,38 @@ export class VideoCommentsComponent implements OnChanges { | |||
40 | private activatedRoute: ActivatedRoute | 43 | private activatedRoute: ActivatedRoute |
41 | ) {} | 44 | ) {} |
42 | 45 | ||
46 | ngOnInit () { | ||
47 | // Find highlighted comment in params | ||
48 | this.sub = this.activatedRoute.params.subscribe( | ||
49 | params => { | ||
50 | if (params['commentId']) { | ||
51 | const highlightedCommentId = +params['commentId'] | ||
52 | this.processHighlightedComment(highlightedCommentId) | ||
53 | } | ||
54 | } | ||
55 | ) | ||
56 | } | ||
57 | |||
43 | ngOnChanges (changes: SimpleChanges) { | 58 | ngOnChanges (changes: SimpleChanges) { |
44 | if (changes['video']) { | 59 | if (changes['video']) { |
45 | this.loadVideoComments() | 60 | this.resetVideo() |
46 | } | 61 | } |
47 | } | 62 | } |
48 | 63 | ||
49 | viewReplies (comment: VideoCommentInterface) { | 64 | ngOnDestroy () { |
50 | this.threadLoading[comment.id] = true | 65 | if (this.sub) this.sub.unsubscribe() |
66 | } | ||
67 | |||
68 | viewReplies (commentId: number, highlightComment = false) { | ||
69 | this.threadLoading[commentId] = true | ||
51 | 70 | ||
52 | this.videoCommentService.getVideoThreadComments(this.video.id, comment.id) | 71 | this.videoCommentService.getVideoThreadComments(this.video.id, commentId) |
53 | .subscribe( | 72 | .subscribe( |
54 | res => { | 73 | res => { |
55 | this.threadComments[comment.id] = res | 74 | this.threadComments[commentId] = res |
56 | this.threadLoading[comment.id] = false | 75 | this.threadLoading[commentId] = false |
76 | |||
77 | if (highlightComment) this.highlightedComment = new VideoComment(res.comment) | ||
57 | }, | 78 | }, |
58 | 79 | ||
59 | err => this.notificationsService.error('Error', err.message) | 80 | err => this.notificationsService.error('Error', err.message) |
@@ -66,18 +87,6 @@ export class VideoCommentsComponent implements OnChanges { | |||
66 | res => { | 87 | res => { |
67 | this.comments = this.comments.concat(res.comments) | 88 | this.comments = this.comments.concat(res.comments) |
68 | this.componentPagination.totalItems = res.totalComments | 89 | this.componentPagination.totalItems = res.totalComments |
69 | |||
70 | if (this.markedCommentID) { | ||
71 | // If there is a marked comment, retrieve it separately as it may not be on this page, filter to prevent duplicate | ||
72 | this.comments = this.comments.filter(value => value.id !== this.markedCommentID) | ||
73 | this.videoCommentService.getVideoThreadComments(this.video.id, this.markedCommentID).subscribe( | ||
74 | res => { | ||
75 | let comment = new VideoComment(res.comment) | ||
76 | comment.marked = true | ||
77 | this.comments.unshift(comment) // Insert marked comment at the beginning | ||
78 | } | ||
79 | ) | ||
80 | } | ||
81 | }, | 90 | }, |
82 | 91 | ||
83 | err => this.notificationsService.error('Error', err.message) | 92 | err => this.notificationsService.error('Error', err.message) |
@@ -97,7 +106,7 @@ export class VideoCommentsComponent implements OnChanges { | |||
97 | } | 106 | } |
98 | 107 | ||
99 | onThreadCreated (commentTree: VideoCommentThreadTree) { | 108 | onThreadCreated (commentTree: VideoCommentThreadTree) { |
100 | this.viewReplies(commentTree.comment) | 109 | this.viewReplies(commentTree.comment.id) |
101 | } | 110 | } |
102 | 111 | ||
103 | onWantedToDelete (commentToDelete: VideoComment) { | 112 | onWantedToDelete (commentToDelete: VideoComment) { |
@@ -168,9 +177,10 @@ export class VideoCommentsComponent implements OnChanges { | |||
168 | } | 177 | } |
169 | } | 178 | } |
170 | 179 | ||
171 | private loadVideoComments () { | 180 | private resetVideo () { |
172 | if (this.video.commentsEnabled === true) { | 181 | if (this.video.commentsEnabled === true) { |
173 | // Reset all our fields | 182 | // Reset all our fields |
183 | this.highlightedComment = null | ||
174 | this.comments = [] | 184 | this.comments = [] |
175 | this.threadComments = {} | 185 | this.threadComments = {} |
176 | this.threadLoading = {} | 186 | this.threadLoading = {} |
@@ -178,16 +188,14 @@ export class VideoCommentsComponent implements OnChanges { | |||
178 | this.componentPagination.currentPage = 1 | 188 | this.componentPagination.currentPage = 1 |
179 | this.componentPagination.totalItems = null | 189 | this.componentPagination.totalItems = null |
180 | 190 | ||
181 | // Find marked comment in params | ||
182 | this.activatedRoute.params.subscribe( | ||
183 | params => { | ||
184 | if (params['commentId']) { | ||
185 | this.markedCommentID = +params['commentId'] | ||
186 | } | ||
187 | } | ||
188 | ) | ||
189 | |||
190 | this.loadMoreComments() | 191 | this.loadMoreComments() |
191 | } | 192 | } |
192 | } | 193 | } |
194 | |||
195 | private processHighlightedComment (highlightedCommentId: number) { | ||
196 | this.highlightedComment = this.comments.find(c => c.id === highlightedCommentId) | ||
197 | |||
198 | const highlightComment = true | ||
199 | this.viewReplies(highlightedCommentId, highlightComment) | ||
200 | } | ||
193 | } | 201 | } |