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

187 lines
4.6 KiB
TypeScript
Raw Normal View History

2018-11-12 02:22:46 +08:00
import {Component, ElementRef, Input, Output, OnChanges, ViewChild} from '@angular/core';
2018-11-05 02:28:32 +08:00
import {GridMedia} from '../../grid/GridMedia';
2018-11-02 17:40:09 +08:00
import {FixOrientationPipe} from '../../FixOrientationPipe';
2018-11-05 02:28:32 +08:00
import {MediaDTO} from '../../../../../common/entities/MediaDTO';
2016-05-13 20:27:00 +08:00
@Component({
selector: 'app-gallery-lightbox-media',
styleUrls: ['./media.lightbox.gallery.component.css'],
templateUrl: './media.lightbox.gallery.component.html'
2016-05-13 20:27:00 +08:00
})
export class GalleryLightboxMediaComponent implements OnChanges {
2016-05-13 20:27:00 +08:00
2018-11-05 02:28:32 +08:00
@Input() gridMedia: GridMedia;
@Input() loadMedia = false;
2018-05-04 06:23:48 +08:00
@Input() windowAspect = 1;
2018-11-12 02:22:46 +08:00
@ViewChild('video') video: ElementRef<HTMLVideoElement>;
2018-11-29 06:49:33 +08:00
prevGirdPhoto: GridMedia = null;
2016-05-13 20:27:00 +08:00
2018-03-31 03:30:30 +08:00
public imageSize = {width: 'auto', height: '100'};
2016-05-13 20:27:00 +08:00
2018-11-02 17:40:09 +08:00
private imageLoaded = false;
2018-05-04 06:23:48 +08:00
public imageLoadFinished = false;
2016-05-13 20:27:00 +08:00
2018-11-02 17:40:09 +08:00
thumbnailSrc: string = null;
photoSrc: string = null;
private videoProgress = 0;
2018-11-02 17:40:09 +08:00
constructor(public elementRef: ElementRef) {
}
ngOnChanges() {
this.imageLoaded = false;
2017-07-28 06:04:19 +08:00
this.imageLoadFinished = false;
this.setImageSize();
2018-11-05 02:28:32 +08:00
if (this.prevGirdPhoto !== this.gridMedia) {
this.prevGirdPhoto = this.gridMedia;
2018-11-02 17:40:09 +08:00
this.thumbnailSrc = null;
this.photoSrc = null;
}
2018-11-05 02:28:32 +08:00
if (this.thumbnailSrc == null && this.gridMedia && this.ThumbnailUrl !== null) {
FixOrientationPipe.transform(this.ThumbnailUrl, this.gridMedia.Orientation)
2018-11-02 17:40:09 +08:00
.then((src) => this.thumbnailSrc = src);
}
2018-11-05 02:28:32 +08:00
if (this.photoSrc == null && this.gridMedia && this.loadMedia) {
FixOrientationPipe.transform(this.gridMedia.getPhotoPath(), this.gridMedia.Orientation)
.then((src) => this.photoSrc = src);
2018-11-02 17:40:09 +08:00
}
2018-11-12 02:22:46 +08:00
}
/** Video **/
2018-11-12 02:22:46 +08:00
private onVideoProgress() {
this.videoProgress = (100 / this.video.nativeElement.duration) * this.video.nativeElement.currentTime;
}
public get VideoProgress(): number {
return this.videoProgress;
}
public get VideoVolume(): number {
if (!this.video) {
2018-11-18 03:15:48 +08:00
return 1;
2018-11-12 02:22:46 +08:00
}
return this.video.nativeElement.volume;
}
public set VideoVolume(value: number) {
if (!this.video) {
return;
}
this.video.nativeElement.muted = false;
this.video.nativeElement.volume = value;
}
public set VideoProgress(value: number) {
if (!this.video && value === null && typeof value === 'undefined') {
return;
}
this.video.nativeElement.currentTime = this.video.nativeElement.duration * (value / 100);
if (this.video.nativeElement.paused) {
this.video.nativeElement.play().catch(console.error);
}
}
public get Muted(): boolean {
if (!this.video) {
2018-11-18 03:15:48 +08:00
return false;
2018-11-12 02:22:46 +08:00
}
return this.video.nativeElement.muted;
}
public mute() {
if (!this.video) {
return;
}
this.video.nativeElement.muted = !this.video.nativeElement.muted;
}
public playPause() {
if (!this.video) {
return;
}
if (this.video.nativeElement.paused) {
this.video.nativeElement.play().catch(console.error);
} else {
this.video.nativeElement.pause();
}
}
public get Paused(): boolean {
if (!this.video) {
return true;
}
return this.video.nativeElement.paused;
}
public getVideoType(): string {
switch (this.gridMedia.getExtension().toLowerCase()) {
case 'webm':
return 'video/webm';
case 'ogv':
case 'ogg':
return 'video/ogg';
default:
return 'video/mp4';
}
}
2018-03-31 03:30:30 +08:00
onImageError() {
2018-05-04 06:23:48 +08:00
// TODO:handle error
2018-03-31 03:30:30 +08:00
this.imageLoadFinished = true;
console.error('Error: cannot load media for lightbox url: ' + this.gridMedia.getPhotoPath());
}
2016-05-13 20:27:00 +08:00
onImageLoad() {
2017-07-28 06:04:19 +08:00
this.imageLoadFinished = true;
this.imageLoaded = true;
}
2018-11-02 17:40:09 +08:00
private get ThumbnailUrl(): string {
2018-11-05 02:28:32 +08:00
if (this.gridMedia.isThumbnailAvailable() === true) {
return this.gridMedia.getThumbnailPath();
2018-05-04 06:23:48 +08:00
}
2018-03-31 03:30:30 +08:00
2018-11-05 02:28:32 +08:00
if (this.gridMedia.isReplacementThumbnailAvailable() === true) {
return this.gridMedia.getReplacementThumbnailPath();
2018-05-04 06:23:48 +08:00
}
2018-03-31 03:30:30 +08:00
return null;
}
2018-11-02 17:40:09 +08:00
public get PhotoSrc(): string {
2018-11-05 02:28:32 +08:00
return this.gridMedia.getPhotoPath();
2018-11-02 17:40:09 +08:00
}
public showThumbnail(): boolean {
2018-11-05 02:28:32 +08:00
return this.gridMedia &&
2018-11-02 17:40:09 +08:00
!this.imageLoaded &&
this.thumbnailSrc !== null &&
2018-11-05 02:28:32 +08:00
(this.gridMedia.isThumbnailAvailable() || this.gridMedia.isReplacementThumbnailAvailable());
}
2018-03-31 03:30:30 +08:00
private setImageSize() {
2018-11-05 02:28:32 +08:00
if (!this.gridMedia) {
2018-03-31 03:30:30 +08:00
return;
}
2018-03-31 03:30:30 +08:00
2018-11-05 02:28:32 +08:00
const photoAspect = MediaDTO.calcRotatedAspectRatio(this.gridMedia.media);
2018-03-31 03:30:30 +08:00
if (photoAspect < this.windowAspect) {
this.imageSize.height = '100';
this.imageSize.width = null;
} else {
this.imageSize.height = null;
this.imageSize.width = '100';
}
}
2016-05-13 20:27:00 +08:00
}