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;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,14 @@
|
||||
import {Component, OnInit} from '@angular/core';
|
||||
import {Component, OnInit, ViewEncapsulation} from '@angular/core';
|
||||
import {DeviceDetectorService} from '../shared/services/device-detector.service';
|
||||
import { CommonModule, NgStyle } from '@angular/common';
|
||||
import {Title} from '@angular/platform-browser';
|
||||
import {AudioPlayerComponent} from '../shared/audio-player/audio-player.component';
|
||||
import {TranslatePipe} from '@ngx-translate/core';
|
||||
import {animate, stagger, style, transition, trigger, query} from '@angular/animations';
|
||||
import {ParallaxComponent} from '../shared/parallax/parallax.component';
|
||||
|
||||
@Component({
|
||||
selector: 'app-portfolio',
|
||||
encapsulation: ViewEncapsulation.None,
|
||||
imports: [
|
||||
NgStyle,
|
||||
AudioPlayerComponent,
|
||||
@@ -17,30 +17,7 @@ import {ParallaxComponent} from '../shared/parallax/parallax.component';
|
||||
ParallaxComponent
|
||||
],
|
||||
templateUrl: './portfolio.component.html',
|
||||
styleUrl: './portfolio.component.css',
|
||||
animations: [
|
||||
trigger('fadeInOut', [
|
||||
transition(':enter', [
|
||||
style({ opacity: 0, transform: 'translateY(20px)' }),
|
||||
animate('0.5s ease-out', style({ opacity: 1, transform: 'translateY(0)' }))
|
||||
]),
|
||||
transition(':leave', [
|
||||
animate('0.3s ease-in', style({ opacity: 0, transform: 'translateY(20px)' }))
|
||||
])
|
||||
]),
|
||||
trigger('staggerGallery', [
|
||||
transition(':enter', [
|
||||
query('.gallery-item', [
|
||||
style({ opacity: 0, transform: 'translateY(20px)' }),
|
||||
stagger(100, [
|
||||
animate('0.5s ease-out', style({ opacity: 1, transform: 'translateY(0)' }))
|
||||
])
|
||||
], {
|
||||
optional: true
|
||||
})
|
||||
])
|
||||
])
|
||||
]
|
||||
styleUrl: './portfolio.component.css'
|
||||
})
|
||||
export class PortfolioComponent implements OnInit {
|
||||
galleryItems = [
|
||||
|
||||
Reference in new issue
Block a user