refactor of image modal code
This commit is contained in:
1 parent
293a06e9a4
commit
357e1b6cf3
2 files changed
+58
-97
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import {AfterViewInit, Component, ElementRef, OnInit, ViewChild} from '@angular/core';
|
||||
import {Component, OnInit, ViewEncapsulation} from '@angular/core';
|
||||
import { DatePipe, NgStyle } from '@angular/common';
|
||||
import {ArticleService} from '../../shared/services/article.service';
|
||||
import {ActivatedRoute} from '@angular/router';
|
||||
@@ -20,6 +20,7 @@ interface Article {
|
||||
|
||||
@Component({
|
||||
selector: 'app-article-page',
|
||||
encapsulation: ViewEncapsulation.None,
|
||||
imports: [
|
||||
DatePipe,
|
||||
NgStyle,
|
||||
@@ -29,16 +30,14 @@ interface Article {
|
||||
templateUrl: './article-page.component.html',
|
||||
styleUrl: './article-page.component.css'
|
||||
})
|
||||
export class ArticlePageComponent implements OnInit, AfterViewInit {
|
||||
@ViewChild('articleContent') articleContent!: ElementRef<HTMLDivElement>;
|
||||
export class ArticlePageComponent implements OnInit {
|
||||
|
||||
selectedImage: string | null = null;
|
||||
private imagesProcessed = false;
|
||||
article: Article | undefined;
|
||||
currentArticle: any;
|
||||
article: Article | null = null;
|
||||
error: boolean = false;
|
||||
copied: boolean = false;
|
||||
|
||||
loading: boolean = true;
|
||||
|
||||
constructor(
|
||||
private articleService: ArticleService,
|
||||
private titleService: Title,
|
||||
@@ -49,28 +48,57 @@ export class ArticlePageComponent implements OnInit, AfterViewInit {
|
||||
ngOnInit() {
|
||||
this.route.params.subscribe((params) => {
|
||||
const slug = params['slug'];
|
||||
if (slug) {
|
||||
this.loadTitleBySlug(slug);
|
||||
} else {
|
||||
this.titleService.setTitle('Article | itzMiney');
|
||||
}
|
||||
});
|
||||
|
||||
const slug = this.route.snapshot.paramMap.get('slug') || '';
|
||||
this.articleService.getArticleBySlug(slug).subscribe(
|
||||
(article) => {
|
||||
this.article = article;
|
||||
this.loading = false;
|
||||
console.log('Article found!')
|
||||
},
|
||||
(error) => {
|
||||
console.error('Error fetching article:', error);
|
||||
this.loading = false;
|
||||
if (error.status === 404) {
|
||||
this.error = true;
|
||||
}
|
||||
this.loading = true;
|
||||
this.error = false;
|
||||
this.article = null;
|
||||
|
||||
if (!slug) {
|
||||
this.titleService.setTitle('Error | itzMiney');
|
||||
return;
|
||||
}
|
||||
);
|
||||
|
||||
this.articleService.getArticleBySlug(slug).subscribe({
|
||||
next: (article: Article | undefined) => {
|
||||
if (!article) {
|
||||
this.article = null;
|
||||
this.loading = false;
|
||||
this.error = true;
|
||||
return;
|
||||
}
|
||||
|
||||
this.article = article;
|
||||
this.titleService.setTitle(`${article.title} | itzMiney`);
|
||||
this.loading = false;
|
||||
},
|
||||
error: (error) => {
|
||||
console.error('Error fetching article:', error);
|
||||
this.article = null;
|
||||
this.loading = false;
|
||||
|
||||
if (error.status === 404) {
|
||||
this.error = true;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
onArticleContentClick(event: MouseEvent) {
|
||||
const target = event.target as HTMLElement;
|
||||
|
||||
if (target.tagName === 'IMG') {
|
||||
const img = target as HTMLImageElement;
|
||||
this.openPreview(img.src);
|
||||
}
|
||||
}
|
||||
|
||||
openPreview(src: string) {
|
||||
this.selectedImage = src;
|
||||
}
|
||||
|
||||
closePreview() {
|
||||
this.selectedImage = null;
|
||||
}
|
||||
|
||||
copyLink() {
|
||||
@@ -84,48 +112,4 @@ export class ArticlePageComponent implements OnInit, AfterViewInit {
|
||||
|
||||
navigator.clipboard.writeText(shareLink).then()
|
||||
}
|
||||
|
||||
ngAfterViewInit() {
|
||||
this.makeImagesClickable();
|
||||
this.imagesProcessed = true;
|
||||
}
|
||||
|
||||
private makeImagesClickable() {
|
||||
const imgs: NodeListOf<HTMLImageElement> = this.articleContent.nativeElement.querySelectorAll('img');
|
||||
imgs.forEach(img => {
|
||||
img.style.cursor = 'pointer';
|
||||
if (!img.dataset['clickable']) {
|
||||
img.addEventListener('click', () => this.openPreview(img.src));
|
||||
img.dataset['clickable'] = 'true';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
openPreview(src: string) {
|
||||
this.selectedImage = src;
|
||||
}
|
||||
|
||||
closePreview() {
|
||||
this.selectedImage = null;
|
||||
}
|
||||
|
||||
loadTitleBySlug(slug: string) {
|
||||
this.articleService.getArticleBySlug(slug).subscribe(
|
||||
(article) => {
|
||||
this.article = article;
|
||||
this.loading = false;
|
||||
|
||||
setTimeout(() => this.makeImagesClickable(), 0);
|
||||
|
||||
console.log('Article found!')
|
||||
},
|
||||
(error) => {
|
||||
console.error('Error fetching article:', error);
|
||||
this.loading = false;
|
||||
if (error.status === 404) {
|
||||
this.error = true;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user