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

73 lines
2.1 KiB
TypeScript
Raw Normal View History

import {Utils} from '../../../../common/Utils';
2018-11-05 02:28:32 +08:00
import {MediaIcon} from './MediaIcon';
import {Config} from '../../../../common/config/public/Config';
import {MediaDTO} from '../../../../common/entities/MediaDTO';
2018-03-31 03:30:30 +08:00
2018-11-05 02:28:32 +08:00
export class Media extends MediaIcon {
2017-03-18 07:11:53 +08:00
2018-11-05 02:28:32 +08:00
constructor(media: MediaDTO, public renderWidth: number, public renderHeight: number) {
super(media);
}
2017-03-18 07:11:53 +08:00
thumbnailLoaded() {
if (!this.isThumbnailAvailable()) {
2018-11-05 02:28:32 +08:00
this.media.readyThumbnails = this.media.readyThumbnails || [];
this.media.readyThumbnails.push(this.getThumbnailSize());
2017-03-18 07:11:53 +08:00
}
}
2017-03-18 07:11:53 +08:00
getThumbnailSize() {
const renderSize = Math.sqrt(this.renderWidth * this.renderHeight);
2017-07-15 18:47:11 +08:00
return Utils.findClosest(renderSize, Config.Client.Thumbnail.thumbnailSizes);
}
2017-03-18 07:11:53 +08:00
getReplacementThumbnailSize(): number {
2017-03-18 07:11:53 +08:00
if (this.replacementSizeCache === false) {
this.replacementSizeCache = null;
2017-03-18 07:11:53 +08:00
const size = this.getThumbnailSize();
2018-11-05 02:28:32 +08:00
if (!!this.media.readyThumbnails) {
for (let i = 0; i < this.media.readyThumbnails.length; i++) {
if (this.media.readyThumbnails[i] < size) {
this.replacementSizeCache = this.media.readyThumbnails[i];
break;
}
2017-03-18 07:11:53 +08:00
}
}
2017-03-18 07:11:53 +08:00
}
return <number>this.replacementSizeCache;
}
2017-03-18 07:11:53 +08:00
isReplacementThumbnailAvailable() {
return this.getReplacementThumbnailSize() !== null;
}
2017-03-18 07:11:53 +08:00
isThumbnailAvailable() {
2018-11-05 02:28:32 +08:00
return this.media.readyThumbnails && this.media.readyThumbnails.indexOf(this.getThumbnailSize()) !== -1;
}
2017-03-18 07:11:53 +08:00
getReplacementThumbnailPath() {
const size = this.getReplacementThumbnailSize();
return Utils.concatUrls(Config.Client.urlBase,
'/api/gallery/content/',
2018-11-05 02:28:32 +08:00
this.media.directory.path, this.media.directory.name, this.media.name, 'thumbnail', size.toString());
2017-03-18 07:11:53 +08:00
}
2017-03-18 07:11:53 +08:00
2018-05-14 04:59:57 +08:00
hasPositionData(): boolean {
2018-11-05 02:28:32 +08:00
return MediaDTO.hasPositionData(this.media);
2018-05-14 04:59:57 +08:00
}
getThumbnailPath() {
const size = this.getThumbnailSize();
return Utils.concatUrls(Config.Client.urlBase,
'/api/gallery/content/',
2018-11-05 02:28:32 +08:00
this.media.directory.path, this.media.directory.name, this.media.name, 'thumbnail', size.toString());
}
2017-03-18 07:11:53 +08:00
}