Changeset View
Changeset View
Standalone View
Standalone View
src/app/_shared/myPosts/myPosts.component.ts
- This file was copied from src/app/notificationArchive/notificationArchive.component.ts.
import { Component, OnInit, OnDestroy, NgModule, ChangeDetectorRef } from '@angular/core'; | import { Component, OnInit, OnDestroy, ChangeDetectorRef } from '@angular/core'; | ||||
import { FormGroup } from '@angular/forms'; | |||||
import { MatSnackBar } from '@angular/material'; | |||||
import { Router, ActivatedRoute } from '@angular/router'; | import { Router, ActivatedRoute } from '@angular/router'; | ||||
import { Subject } from 'rxjs/Subject'; | import { Subject } from 'rxjs/Subject'; | ||||
import { Area } from '../_models/area'; | import { Account } from '../../_models/account'; | ||||
import * as C from '../_models/constants'; | import { Area } from '../../_models/area'; | ||||
import { Post } from '../_models/post'; | import { Author } from '../../_models/author'; | ||||
import { NavBarService } from '../_services/navBar.service'; | import { Ban } from '../../_models/ban'; | ||||
import { NotificationService } from '../_services/notification.service'; | import { Choice } from '../../_models/choice'; | ||||
import { RouteService } from '../_services/route.service'; | import * as C from '../../_models/constants'; | ||||
import { NgxMasonryModule } from '../_modules/ngx-masonry/ngx-masonry.module'; | import { Post } from '../../_models/post'; | ||||
import { AreaService } from '../../_services/area.service'; | |||||
import { NavBarService } from '../../_services/navBar.service'; | |||||
import { PostService } from '../../_services/post.service'; | |||||
import { RouteService } from '../../_services/route.service'; | |||||
@Component({ | @Component({ | ||||
templateUrl: 'notificationArchive.component.html' | templateUrl: 'myPosts.component.html', | ||||
styleUrls: ['./myPosts.component.scss'] | |||||
}) | }) | ||||
export class NotificationArchiveComponent implements OnInit, OnDestroy { | export class MyPostsComponent implements OnInit, OnDestroy { | ||||
account: Account; | |||||
author: Author; | |||||
backupPosts: { [area: string]: Post[]; } = {}; | backupPosts: { [area: string]: Post[]; } = {}; | ||||
bans: Ban[] = []; | |||||
bioForm: FormGroup; | |||||
choices: Choice[]; | |||||
componentDestroyed: Subject<boolean> = new Subject(); | componentDestroyed: Subject<boolean> = new Subject(); | ||||
currentArea: string; | currentArea: Area; | ||||
funPosts: Post[] = []; | data: any; | ||||
editBio = false; | |||||
emailForm: FormGroup; | |||||
errors: any; | |||||
imageArray: { [area: string]: string[]; } = {}; | imageArray: { [area: string]: string[]; } = {}; | ||||
index = 1; | index = 1; | ||||
infoPosts: Post[] = []; | |||||
limit = 10; | limit = 10; | ||||
loading = true; | loading = true; | ||||
model: any = {}; | |||||
offset = 10; | offset = 10; | ||||
searchArray: Post[] = []; | self: boolean; | ||||
searching = false; | |||||
superPosts: { [area: string]: Post[]; } = {}; | superPosts: { [area: string]: Post[]; } = {}; | ||||
totalCount = 0; | totalCount = 0; | ||||
url: string; | |||||
constructor( | constructor( | ||||
private cdRef: ChangeDetectorRef, | private cdRef: ChangeDetectorRef, | ||||
private route: ActivatedRoute, | private route: ActivatedRoute, | ||||
private router: Router, | private router: Router, | ||||
public snackBar: MatSnackBar, | |||||
private areaService: AreaService, | |||||
private navBarService: NavBarService, | private navBarService: NavBarService, | ||||
private notificationService: NotificationService, | private postService: PostService, | ||||
private routeService: RouteService | private routeService: RouteService | ||||
) { } | ) { } | ||||
private imageInPosts(posts: Post[], area: string) { | private imageInPosts(posts: Post[], area: string) { | ||||
this.imageArray[area] = []; | this.imageArray[area] = []; | ||||
for (let i = 0; i <= posts.length - 1; i++) { | for (let i = 0; i <= posts.length - 1; i++) { | ||||
// Find image markdown data in post.text - Guarenteed by Regex | // Find image markdown data in post.text - Guarenteed by Regex | ||||
const indexOfStart = posts[i].text.search(C.WF_IMAGE_REGEX); | const indexOfStart = posts[i].text.search(C.WF_IMAGE_REGEX); | ||||
if (indexOfStart !== -1) { | if (indexOfStart !== -1) { | ||||
// Start at index and parse until we find a closing ')' char | // Start at index and parse until we find a closing ')' char | ||||
for (let j = indexOfStart; j <= posts[i].text.length; j++) { | for (let j = indexOfStart; j <= posts[i].text.length; j++) { | ||||
if (posts[i].text.charAt(j) === ']') { | if (posts[i].text.charAt(j) === ']') { | ||||
// Add data to image array in specific area | // Add data to image array in specific area | ||||
this.imageArray[area][i] = posts[i].text.slice(indexOfStart, j + 1); | this.imageArray[area][i] = posts[i].text.slice(indexOfStart, j + 1); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
this.loading = false; | |||||
} | } | ||||
private removeMarkdown(input: string) { | private removeMarkdown(input: string) { | ||||
input = input | input = input | ||||
// Remove horizontal rules (stripListHeaders conflict with this rule, which is why it has been moved to the top) | // Remove horizontal rules (stripListHeaders conflict with this rule, which is why it has been moved to the top) | ||||
.replace(/^(-\s*?|\*\s*?|_\s*?){3,}\s*$/gm, '[Horizontal-Rule]') | .replace(/^(-\s*?|\*\s*?|_\s*?){3,}\s*$/gm, '[Horizontal-Rule]') | ||||
// Remove horizontal rules | // Remove horizontal rules | ||||
.replace(/^(-\s*?|\*\s*?|_\s*?){3,}\s*$/gm, '') | .replace(/^(-\s*?|\*\s*?|_\s*?){3,}\s*$/gm, '') | ||||
// Header | // Header | ||||
.replace(/\n={2,}/g, '\n') | .replace(/\n={2,}/g, '\n') | ||||
// Strikethrough | // Strikethrough | ||||
.replace(/~~/g, '') | .replace(/~~/g, '') | ||||
// Fenced codeblocks | // Fenced codeblocks | ||||
.replace(/`{3}.*\n/g, '') | .replace(/`{3}.*\n/g, '') | ||||
// Remove HTML tags | // Remove HTML tags | ||||
.replace(/<[^>]*>/g, '') | .replace(/<[^>]*>/g, '') | ||||
// Remove setext-style headers | // Remove setext-style headers | ||||
.replace(/^[=\-]{2,}\s*$/g, '') | .replace(/^[=\-]{2,}\s*$/g, '') | ||||
// Remove footnotes? | // Remove footnotes? | ||||
.replace(/\[\^.+?\](\: .*?$)?/g, '') | .replace(/\[\^.+?\](\: .*?$)?/g, '') | ||||
.replace(/\s{0,2}\[.*?\]: .*?$/g, '') | .replace(/\s{0,2}\[.*?\]: .*?$/g, '') | ||||
// Remove images | // Remove images | ||||
.replace(/\!\[.*?\][\[\(].*?[\]\)]/g, '') | .replace(C.WF_IMAGE_REGEX, '') | ||||
// Remove wildfyre images | // Remove wildfyre images | ||||
.replace(/(\[img: \d\])/gm, '') | .replace(/(\[img: \d\])/gm, '') | ||||
// Remove inline links | // Remove inline links | ||||
.replace(/\[(.*?)\][\[\(].*?[\]\)]/g, '$1') | .replace(/\[(.*?)\][\[\(].*?[\]\)]/g, '$1') | ||||
// Remove blockquotes | // Remove blockquotes | ||||
.replace(/^\s{0,3}>\s?/g, '') | .replace(/^\s{0,3}>\s?/g, '') | ||||
// Remove reference-style links? | // Remove reference-style links? | ||||
.replace(/^\s{1,2}\[(.*?)\]: (\S+)( ".*?")?\s*$/g, '') | .replace(/^\s{1,2}\[(.*?)\]: (\S+)( ".*?")?\s*$/g, '') | ||||
// Remove atx-style headers | // Remove atx-style headers | ||||
.replace(/^(\n)?\s{0,}#{1,6}\s+| {0,}(\n)?\s{0,}#{0,} {0,}(\n)?\s{0,}$/gm, '$1$2$3') | .replace(/^(\n)?\s{0,}#{1,6}\s+| {0,}(\n)?\s{0,}#{0,} {0,}(\n)?\s{0,}$/gm, '$1$2$3') | ||||
// Remove emphasis (repeat the line to remove double emphasis) | // Remove emphasis (repeat the line to remove double emphasis) | ||||
.replace(/([\*_]{1,3})(\S.*?\S{0,1})\1/g, '$2') | .replace(/([\*_]{1,3})(\S.*?\S{0,1})\1/g, '$2') | ||||
.replace(/([\*_]{1,3})(\S.*?\S{0,1})\1/g, '$2') | .replace(/([\*_]{1,3})(\S.*?\S{0,1})\1/g, '$2') | ||||
// Remove code blocks | // Remove code blocks | ||||
.replace(/(`{3,})(.*?)\1/gm, '$2') | .replace(/(`{3,})(.*?)\1/gm, '$2') | ||||
// Remove inline code | // Remove inline code | ||||
.replace(/`(.+?)`/g, '$1') | .replace(/`(.+?)`/g, '$1') | ||||
// Replace two or more newlines with exactly two? Not entirely sure this belongs here... | // Replace two or more newlines with exactly two? Not entirely sure this belongs here... | ||||
.replace(/\n{2,}/g, '\n\n'); | .replace(/\n{2,}/g, '\n\n'); | ||||
return input; | return input; | ||||
} | } | ||||
ngOnInit() { | ngOnInit() { | ||||
this.route.params | this.route.params | ||||
.takeUntil(this.componentDestroyed) | .takeUntil(this.componentDestroyed) | ||||
.subscribe(params => { | .subscribe(params => { | ||||
if (params['index'] !== undefined) { | if (params['area'] !== undefined) { | ||||
this.index = params['index']; | this.areaService.getAreas() | ||||
.takeUntil(this.componentDestroyed) | |||||
.subscribe((areas) => { | |||||
for (let i = 0; i <= areas.length - 1; i++) { | |||||
if (areas[i].name === params['area']) { | |||||
this.currentArea = areas[i]; | |||||
} | |||||
} | } | ||||
}); | |||||
this.navBarService.currentArea | if (!this.superPosts[this.currentArea.name]) { | ||||
.takeUntil(this.componentDestroyed) | this.superPosts[this.currentArea.name] = []; | ||||
.subscribe((currentArea: Area) => { | |||||
if (currentArea.name !== '') { | |||||
this.currentArea = currentArea.name; | |||||
if (!this.superPosts[currentArea.name]) { | |||||
this.superPosts[currentArea.name] = []; | |||||
} | } | ||||
if (!this.backupPosts[currentArea.name]) { | if (!this.backupPosts[this.currentArea.name]) { | ||||
this.backupPosts[currentArea.name] = []; | this.backupPosts[this.currentArea.name] = []; | ||||
} | } | ||||
this.loading = true; | |||||
const posts: Post[] = []; | const posts: Post[] = []; | ||||
this.notificationService.getArchive(currentArea.name, this.limit, 0) | this.postService.getOwnPosts(this.currentArea.name, this.limit, 0) | ||||
.takeUntil(this.componentDestroyed) | .takeUntil(this.componentDestroyed) | ||||
.subscribe(superPost => { | .subscribe(superPost => { | ||||
superPost.results.forEach((obj: any) => { | superPost.results.forEach((obj: any) => { | ||||
posts.push(Post.parse(obj)); | posts.push(Post.parse(obj)); | ||||
}); | }); | ||||
// Removes binding to original 'superPost' variable | // Removes binding to original 'superPost' variable | ||||
this.superPosts[currentArea.name] = JSON.parse(JSON.stringify(posts)); | this.superPosts[this.currentArea.name] = JSON.parse(JSON.stringify(posts)); | ||||
this.backupPosts[currentArea.name] = posts; | this.backupPosts[this.currentArea.name] = posts; | ||||
this.totalCount = superPost.count; | this.totalCount = superPost.count; | ||||
this.imageInPosts(this.superPosts[currentArea.name], currentArea.name); | this.imageInPosts(this.superPosts[this.currentArea.name], this.currentArea.name); | ||||
for (let i = 0; i <= this.backupPosts[currentArea.name].length - 1; i++) { | for (let i = 0; i <= this.backupPosts[this.currentArea.name].length - 1; i++) { | ||||
this.backupPosts[currentArea.name][i].text = this.removeMarkdown(this.backupPosts[currentArea.name][i].text); | this.backupPosts[this.currentArea.name][i].text = this.removeMarkdown(this.backupPosts[this.currentArea.name][i].text); | ||||
} | } | ||||
this.cdRef.detectChanges(); | this.cdRef.detectChanges(); | ||||
this.loading = false; | this.loading = false; | ||||
}); | }); | ||||
this.loading = false; | |||||
}); | |||||
} | } | ||||
}); | }); | ||||
} | } | ||||
ngOnDestroy() { | ngOnDestroy() { | ||||
this.cdRef.detach(); | this.cdRef.detach(); | ||||
this.componentDestroyed.next(true); | this.componentDestroyed.next(true); | ||||
this.componentDestroyed.complete(); | this.componentDestroyed.complete(); | ||||
} | } | ||||
back() { | back() { | ||||
if (this.routeService.routes.length === 0) { | if (this.routeService.routes.length === 0) { | ||||
this.router.navigateByUrl(''); | this.router.navigateByUrl(''); | ||||
} else { | } else { | ||||
this.router.navigateByUrl(this.routeService.getNextRoute()); | this.router.navigateByUrl(this.routeService.getNextRoute()); | ||||
} | } | ||||
} | } | ||||
getPosts(page: number) { | getPosts(page: any) { | ||||
this.loading = true; | this.loading = true; | ||||
const posts: Post[] = []; | const posts: Post[] = []; | ||||
this.notificationService.getArchive(this.currentArea, this.limit, (this.offset * page) - this.limit) | this.postService.getOwnPosts(this.currentArea.name, this.limit, (this.offset * page) - this.limit) | ||||
.takeUntil(this.componentDestroyed) | .takeUntil(this.componentDestroyed) | ||||
.subscribe(superPost => { | .subscribe(superPost => { | ||||
superPost.results.forEach((obj: any) => { | superPost.results.forEach((obj: any) => { | ||||
posts.push(Post.parse(obj)); | posts.push(Post.parse(obj)); | ||||
}); | }); | ||||
// Removes binding to original 'superPost' variable | // Removes binding to original 'superPost' variable | ||||
this.superPosts[this.currentArea] = JSON.parse(JSON.stringify(posts)); | this.superPosts[this.currentArea.name] = JSON.parse(JSON.stringify(posts)); | ||||
this.backupPosts[this.currentArea] = posts; | this.backupPosts[this.currentArea.name] = posts; | ||||
this.imageInPosts(this.superPosts[this.currentArea], this.currentArea); | this.imageInPosts(this.superPosts[this.currentArea.name], this.currentArea.name); | ||||
for (let i = 0; i <= this.backupPosts[this.currentArea].length - 1; i++) { | for (let i = 0; i <= this.backupPosts[this.currentArea.name].length - 1; i++) { | ||||
this.backupPosts[this.currentArea][i].text = this.removeMarkdown(this.backupPosts[this.currentArea][i].text); | this.backupPosts[this.currentArea.name][i].text = this.removeMarkdown(this.backupPosts[this.currentArea.name][i].text); | ||||
} | } | ||||
this.index = page; | this.index = page; | ||||
this.totalCount = superPost.count; | this.totalCount = superPost.count; | ||||
this.cdRef.detectChanges(); | this.cdRef.detectChanges(); | ||||
this.loading = false; | this.loading = false; | ||||
}); | }); | ||||
} | } | ||||
goto(postID: string) { | goto(postID: string) { | ||||
this.routeService.addNextRouteByIndex(this.index); | this.routeService.addNextRouteByIndex(this.index); | ||||
this.router.navigateByUrl('/areas/' + this.currentArea + '/' + postID); | this.router.navigateByUrl('/areas/' + this.currentArea + '/' + postID); | ||||
} | } | ||||
/* Removed as of D114 | |||||
searchInput() { | |||||
if (this.areaService.currentAreaName === 'fun') { | |||||
this.searchArray = []; | |||||
if (this.model.postText === '') { | |||||
this.searching = false; | |||||
this.cdRef.detectChanges(); | |||||
} else { | |||||
this.searching = true; | |||||
for (let i = 0; i <= this.funPosts.length - 1; i++) { | |||||
if (this.funPosts[i].text.toLowerCase().includes(this.model.postText.toLowerCase())) { | |||||
this.searchArray.push(this.backupFunPosts[i]); | |||||
} | |||||
} | |||||
this.cdRef.detectChanges(); | |||||
} | |||||
} else { | |||||
this.searchArray = []; | |||||
if (this.model.postText === '') { | |||||
this.searching = false; | |||||
this.cdRef.detectChanges(); | |||||
} else { | |||||
this.searching = true; | |||||
for (let i = 0; i <= this.infoPosts.length - 1; i++) { | |||||
if (this.infoPosts[i].text.toLowerCase().includes(this.model.postText.toLowerCase())) { | |||||
this.searchArray.push(this.backupInfoPosts[i]); | |||||
} | |||||
} | |||||
this.cdRef.detectChanges(); | |||||
} | |||||
} | |||||
} | |||||
*/ | |||||
} | } |