You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
4.3 KiB

import { Component, OnInit, ViewChild, ViewEncapsulation} from '@angular/core'
import { Router, NavigationEnd, NavigationStart, ActivatedRoute } from '@angular/router'
5 years ago
import { DomSanitizer } from '@angular/platform-browser'
5 years ago
import { Location } from '@angular/common'
import { NgxImageGalleryComponent, GALLERY_IMAGE, GALLERY_CONF } from "ngx-image-gallery"
5 years ago
import { ApisService } from '../services/apis.service'
5 years ago
import { environment } from '../../environments/environment'
5 years ago
@Component({
selector: 'app-detail',
templateUrl: './detail.component.html',
styleUrls: ['./detail.component.scss'],
encapsulation: ViewEncapsulation.None,
5 years ago
})
export class DetailComponent implements OnInit {
5 years ago
public basePath = `${environment.BASE_PATH}`
@ViewChild(NgxImageGalleryComponent) ngxImageGallery: NgxImageGalleryComponent
5 years ago
public details: any = {}
public section: string = ''
public id: number = 0
private history: string[] = []
5 years ago
public conf: GALLERY_CONF = {
imageOffset: '0px',
showDeleteControl: false,
showImageTitle: false,
}
public galleryImages: GALLERY_IMAGE[] = []
5 years ago
constructor(
private apisService: ApisService,
private router: Router,
private location: Location,
5 years ago
private activeRoute: ActivatedRoute,
private sanitizer: DomSanitizer
) { }
5 years ago
ngOnInit(): void {
this.section = this.router.url.split('/')[2]
this.id = parseInt(this.router.url.split('/')[3])
this.showDetails(this.section, this.id)
5 years ago
}
showDetails(section, id): void {
this.galleryImages = []
this.apisService.getDetails(section, id).toPromise().then((response) => {
if(this.history[this.history.length - 1] != `/detail/${section}/${id}`) {
this.history.push(`/detail/${section}/${id}`)
}
5 years ago
const detail = response.item
detail.videos = detail.videos ? JSON.parse(detail.videos) : []
detail.videos.forEach((e) => {
e.code = e.url.split('/').pop()
switch(e.type) {
case 'youtube':
e.embed = this.sanitizer.bypassSecurityTrustResourceUrl(`https://www.youtube.com/embed/${e.code}`)
break
case 'vimeo':
e.embed = this.sanitizer.bypassSecurityTrustResourceUrl(`https://player.vimeo.com/video/${e.code}`)
break
case 'embed':
e.embed = this.sanitizer.bypassSecurityTrustHtml(e.url)
break
}
5 years ago
})
5 years ago
detail.gallery = detail.gallery ? JSON.parse(detail.gallery) : []
detail.gallery.forEach((e) => {
if(!e.main) {
this.galleryImages.push({
url: `${e.url}`,
altText: e.title,
title: e.title,
thumbnailUrl: `${this.basePath}${e.url}`
})
}
})
5 years ago
this.details = detail
5 years ago
},(error) => {
console.error('getPortfolio ERROR', error)
}).catch((e) => {
console.error('getPortfolio CATCH', e)
})
}
back(): void {
this.history.pop()
if(this.history.length > 0) {
const last = this.history[this.history.length - 1]
this.section = last.split('/')[2]
this.id = parseInt(last.split('/')[3])
this.showDetails(this.section, this.id)
this.location.back()
} else {
this.location.back()
}
5 years ago
}
openGallery(index: number = 0) {
5 years ago
this.ngxImageGallery.open(index)
}
// close gallery
closeGallery() {
5 years ago
this.ngxImageGallery.close()
}
// set new active(visible) image in gallery
newImage(index: number = 0) {
5 years ago
this.ngxImageGallery.setActiveImage(index)
}
// next image in gallery
nextImage(index: number = 0) {
5 years ago
this.ngxImageGallery.next()
}
// prev image in gallery
prevImage(index: number = 0) {
5 years ago
this.ngxImageGallery.prev()
}
/**************************************************/
// EVENTS
// callback on gallery opened
galleryOpened(index) {
5 years ago
console.info('Gallery opened at index ', index)
}
// callback on gallery closed
galleryClosed() {
5 years ago
console.info('Gallery closed.')
}
// callback on gallery image clicked
galleryImageClicked(index) {
5 years ago
console.info('Gallery image clicked with index ', index)
}
// callback on gallery image changed
galleryImageChanged(index) {
5 years ago
console.info('Gallery image changed to index ', index)
}
// callback on user clicked delete button
deleteImage(index) {
5 years ago
console.info('Delete image at index ', index)
}
5 years ago
}