2016-05-29 12:28:52 +02:00
|
|
|
'use strict';
|
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
const settings = require('../models/settings.js');
|
2016-05-29 12:28:52 +02:00
|
|
|
const views = require('../util/views.js');
|
|
|
|
const optimizedResize = require('../util/optimized_resize.js');
|
|
|
|
|
|
|
|
class PostContentControl {
|
|
|
|
constructor(containerNode, post, viewportSizeCalculator) {
|
|
|
|
this._post = post;
|
|
|
|
this._viewportSizeCalculator = viewportSizeCalculator;
|
|
|
|
this._containerNode = containerNode;
|
|
|
|
this._template = views.getTemplate('post-content');
|
|
|
|
|
2016-06-29 18:54:49 +02:00
|
|
|
this._currentFitFunction = {
|
|
|
|
'fit-both': this.fitBoth,
|
|
|
|
'fit-original': this.fitOriginal,
|
|
|
|
'fit-width': this.fitWidth,
|
|
|
|
'fit-height': this.fitHeight,
|
|
|
|
}[settings.get().fitMode] || this.fitBoth;
|
2016-07-27 22:27:33 +02:00
|
|
|
|
|
|
|
this._install();
|
|
|
|
|
|
|
|
this._post.addEventListener(
|
|
|
|
'changeContent', e => this._evtPostContentChange(e));
|
2016-05-29 12:28:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fitWidth() {
|
|
|
|
this._currentFitFunction = this.fitWidth;
|
|
|
|
const mul = this._post.canvasHeight / this._post.canvasWidth;
|
2016-06-01 22:37:12 +02:00
|
|
|
let width = this._viewportWidth;
|
2016-06-14 10:31:48 +02:00
|
|
|
if (!settings.get().upscaleSmallPosts) {
|
2016-06-01 22:37:12 +02:00
|
|
|
width = Math.min(this._post.canvasWidth, width);
|
|
|
|
}
|
|
|
|
this._resize(width, width * mul);
|
2016-05-29 12:28:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fitHeight() {
|
|
|
|
this._currentFitFunction = this.fitHeight;
|
|
|
|
const mul = this._post.canvasWidth / this._post.canvasHeight;
|
2016-06-01 22:37:12 +02:00
|
|
|
let height = this._viewportHeight;
|
2016-06-14 10:31:48 +02:00
|
|
|
if (!settings.get().upscaleSmallPosts) {
|
2016-06-01 22:37:12 +02:00
|
|
|
height = Math.min(this._post.canvasHeight, height);
|
|
|
|
}
|
|
|
|
this._resize(height * mul, height);
|
2016-05-29 12:28:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fitBoth() {
|
|
|
|
this._currentFitFunction = this.fitBoth;
|
|
|
|
let mul = this._post.canvasHeight / this._post.canvasWidth;
|
|
|
|
if (this._viewportWidth * mul < this._viewportHeight) {
|
2016-06-06 20:57:22 +02:00
|
|
|
let width = this._viewportWidth;
|
2016-06-14 10:31:48 +02:00
|
|
|
if (!settings.get().upscaleSmallPosts) {
|
2016-06-06 20:57:22 +02:00
|
|
|
width = Math.min(this._post.canvasWidth, width);
|
|
|
|
}
|
|
|
|
this._resize(width, width * mul);
|
2016-05-29 12:28:52 +02:00
|
|
|
} else {
|
2016-06-06 20:57:22 +02:00
|
|
|
let height = this._viewportHeight;
|
2016-06-14 10:31:48 +02:00
|
|
|
if (!settings.get().upscaleSmallPosts) {
|
2016-06-06 20:57:22 +02:00
|
|
|
height = Math.min(this._post.canvasHeight, height);
|
|
|
|
}
|
|
|
|
this._resize(height / mul, height);
|
2016-05-29 12:28:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-06 20:57:22 +02:00
|
|
|
fitOriginal() {
|
|
|
|
this._currentFitFunction = this.fitOriginal;
|
|
|
|
this._resize(this._post.canvasWidth, this._post.canvasHeight);
|
|
|
|
}
|
|
|
|
|
2016-05-29 12:28:52 +02:00
|
|
|
get _viewportWidth() {
|
|
|
|
return this._viewportSizeCalculator()[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
get _viewportHeight() {
|
|
|
|
return this._viewportSizeCalculator()[1];
|
|
|
|
}
|
|
|
|
|
2016-07-27 22:27:33 +02:00
|
|
|
_evtPostContentChange(e) {
|
|
|
|
this._post = e.detail.post;
|
|
|
|
this._post.mutateContentUrl();
|
|
|
|
this._reinstall();
|
|
|
|
}
|
|
|
|
|
2016-05-29 12:28:52 +02:00
|
|
|
_resize(width, height) {
|
2016-07-27 22:27:33 +02:00
|
|
|
this._postContentNode.style.width = width + 'px';
|
|
|
|
this._postContentNode.style.height = height + 'px';
|
2016-05-29 12:28:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_refreshSize() {
|
|
|
|
this._currentFitFunction();
|
|
|
|
}
|
|
|
|
|
|
|
|
_install() {
|
2016-07-27 22:27:33 +02:00
|
|
|
this._reinstall();
|
2016-05-29 12:28:52 +02:00
|
|
|
optimizedResize.add(() => this._refreshSize());
|
|
|
|
views.monitorNodeRemoval(
|
|
|
|
this._containerNode, () => { this._uninstall(); });
|
|
|
|
}
|
|
|
|
|
2016-07-27 22:27:33 +02:00
|
|
|
_reinstall() {
|
|
|
|
const newNode = this._template({post: this._post});
|
|
|
|
if (settings.get().transparencyGrid) {
|
|
|
|
newNode.classList.add('transparency-grid');
|
|
|
|
}
|
|
|
|
if (this._postContentNode) {
|
|
|
|
this._containerNode.replaceChild(newNode, this._postContentNode);
|
|
|
|
} else {
|
|
|
|
this._containerNode.appendChild(newNode);
|
|
|
|
}
|
|
|
|
this._postContentNode = newNode;
|
|
|
|
this._refreshSize();
|
|
|
|
}
|
|
|
|
|
2016-05-29 12:28:52 +02:00
|
|
|
_uninstall() {
|
|
|
|
optimizedResize.remove(() => this._refreshSize());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = PostContentControl;
|