1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/frontend/app/ui/gallery/grid/photo/photo.grid.gallery.component.ts

139 lines
4.3 KiB
TypeScript
Raw Normal View History

2018-03-31 03:30:30 +08:00
import {Component, ElementRef, Input, OnDestroy, OnInit, ViewChild} from '@angular/core';
import {Dimension, IRenderable} from '../../../../model/IRenderable';
2018-11-05 02:28:32 +08:00
import {GridMedia} from '../GridMedia';
import {SearchTypes} from '../../../../../../common/entities/AutoCompleteItem';
2018-03-31 03:30:30 +08:00
import {RouterLink} from '@angular/router';
2018-12-09 01:17:33 +08:00
import {Thumbnail, ThumbnailManagerService} from '../../thumbnailManager.service';
import {Config} from '../../../../../../common/config/public/Config';
import {PageHelper} from '../../../../model/page.helper';
import {PhotoDTO, PhotoMetadata} from '../../../../../../common/entities/PhotoDTO';
2017-07-20 04:40:27 +08:00
2016-05-12 17:00:46 +08:00
@Component({
2018-05-04 07:17:08 +08:00
selector: 'app-gallery-grid-photo',
templateUrl: './photo.grid.gallery.component.html',
styleUrls: ['./photo.grid.gallery.component.css'],
2017-07-16 02:23:02 +08:00
providers: [RouterLink]
2016-05-12 17:00:46 +08:00
})
export class GalleryPhotoComponent implements IRenderable, OnInit, OnDestroy {
2019-07-20 06:29:16 +08:00
@Input() gridMedia: GridMedia;
2019-07-19 06:12:22 +08:00
@ViewChild('img', {static: false}) imageRef: ElementRef;
@ViewChild('photoContainer', {static: true}) container: ElementRef;
thumbnail: Thumbnail;
2019-01-14 03:18:18 +08:00
keywords: { value: string, type: SearchTypes }[] = null;
2019-07-20 06:29:16 +08:00
infoBarVisible = false;
2018-11-29 06:49:33 +08:00
animationTimer: number = null;
2018-11-29 06:49:33 +08:00
readonly SearchTypes: typeof SearchTypes;
2018-05-04 07:17:08 +08:00
searchEnabled = true;
wasInView: boolean = null;
2019-01-14 03:18:18 +08:00
constructor(private thumbnailService: ThumbnailManagerService) {
this.SearchTypes = SearchTypes;
2017-07-14 05:39:09 +08:00
this.searchEnabled = Config.Client.Search.enabled;
}
2019-01-14 03:18:18 +08:00
get ScrollListener(): boolean {
return !this.thumbnail.Available && !this.thumbnail.Error;
}
get Title(): string {
if (Config.Client.Other.captionFirstNaming === false) {
2019-07-20 06:29:16 +08:00
return this.gridMedia.media.name;
2019-01-14 03:18:18 +08:00
}
2019-07-20 06:29:16 +08:00
if ((<PhotoDTO>this.gridMedia.media).metadata.caption) {
if ((<PhotoDTO>this.gridMedia.media).metadata.caption.length > 20) {
return (<PhotoDTO>this.gridMedia.media).metadata.caption.substring(0, 17) + '...';
2019-01-14 03:18:18 +08:00
}
2019-07-20 06:29:16 +08:00
return (<PhotoDTO>this.gridMedia.media).metadata.caption;
2019-01-14 03:18:18 +08:00
}
2019-07-20 06:29:16 +08:00
return this.gridMedia.media.name;
2019-01-14 03:18:18 +08:00
}
ngOnInit() {
2019-07-20 06:29:16 +08:00
this.thumbnail = this.thumbnailService.getThumbnail(this.gridMedia);
const metadata = this.gridMedia.media.metadata as PhotoMetadata;
2019-01-14 03:18:18 +08:00
if ((metadata.keywords && metadata.keywords.length > 0) ||
(metadata.faces && metadata.faces.length > 0)) {
const names: string[] = (metadata.faces || []).map(f => f.name);
this.keywords = names.filter((name, index) => names.indexOf(name) === index)
.map(n => ({value: n, type: SearchTypes.person}))
2019-01-14 03:18:18 +08:00
.concat((metadata.keywords || []).map(k => ({value: k, type: SearchTypes.keyword})));
}
2019-01-14 03:18:18 +08:00
}
ngOnDestroy() {
this.thumbnail.destroy();
2017-07-20 04:40:27 +08:00
if (this.animationTimer != null) {
clearTimeout(this.animationTimer);
}
}
2016-05-12 17:00:46 +08:00
isInView(): boolean {
2018-05-17 06:52:08 +08:00
return PageHelper.ScrollY < this.container.nativeElement.offsetTop + this.container.nativeElement.clientHeight
&& PageHelper.ScrollY + window.innerHeight > this.container.nativeElement.offsetTop;
}
2016-05-12 17:00:46 +08:00
onScroll() {
2018-05-17 06:52:08 +08:00
if (this.thumbnail.Available === true || this.thumbnail.Error === true) {
2017-06-22 03:16:04 +08:00
return;
}
2018-05-04 07:17:08 +08:00
const isInView = this.isInView();
if (this.wasInView !== isInView) {
this.wasInView = isInView;
this.thumbnail.Visible = isInView;
2016-05-16 00:52:07 +08:00
}
}
2016-05-16 17:03:11 +08:00
getPositionText(): string {
2019-07-20 06:29:16 +08:00
if (!this.gridMedia || !this.gridMedia.isPhoto()) {
2018-03-31 03:30:30 +08:00
return '';
2016-05-12 17:00:46 +08:00
}
2019-07-20 06:29:16 +08:00
return (<PhotoDTO>this.gridMedia.media).metadata.positionData.city ||
(<PhotoDTO>this.gridMedia.media).metadata.positionData.state ||
(<PhotoDTO>this.gridMedia.media).metadata.positionData.country;
}
2017-07-16 23:15:42 +08:00
mouseOver() {
2019-07-20 06:29:16 +08:00
this.infoBarVisible = true;
2017-07-16 02:23:02 +08:00
if (this.animationTimer != null) {
clearTimeout(this.animationTimer);
2019-07-20 06:29:16 +08:00
this.animationTimer = null;
2017-07-16 02:23:02 +08:00
}
}
mouseOut() {
2017-07-16 02:23:02 +08:00
if (this.animationTimer != null) {
clearTimeout(this.animationTimer);
}
2018-11-29 06:49:33 +08:00
this.animationTimer = window.setTimeout(() => {
2019-07-20 06:29:16 +08:00
this.animationTimer = null;
this.infoBarVisible = false;
}, 500);
}
public getDimension(): Dimension {
2017-06-23 04:07:47 +08:00
if (!this.imageRef) {
return <Dimension>{
top: 0,
left: 0,
width: 0,
height: 0
};
}
return <Dimension>{
top: this.imageRef.nativeElement.offsetTop,
left: this.imageRef.nativeElement.offsetLeft,
width: this.imageRef.nativeElement.width,
height: this.imageRef.nativeElement.height
};
}
2016-05-12 17:00:46 +08:00
}