szurubooru/public_html/js/Presenters/PostContentPresenter.js

166 lines
5.1 KiB
JavaScript
Raw Normal View History

2014-10-22 10:42:15 +02:00
var App = App || {};
App.Presenters = App.Presenters || {};
App.Presenters.PostContentPresenter = function(
2015-06-28 10:07:11 +02:00
jQuery,
util,
promise,
keyboard,
2015-06-28 10:07:11 +02:00
presenterManager,
2015-07-19 11:53:34 +02:00
postNotesPresenter,
browsingSettings) {
2014-10-22 10:42:15 +02:00
2015-06-28 10:07:11 +02:00
var post;
var templates = {};
var $target;
2015-07-19 18:30:20 +02:00
var $wrapper;
2014-10-22 10:42:15 +02:00
2015-06-28 10:07:11 +02:00
function init(params, loaded) {
$target = params.$target;
post = params.post;
2014-10-22 10:42:15 +02:00
2015-06-28 10:07:11 +02:00
promise.wait(util.promiseTemplate('post-content'))
.then(function(postContentTemplate) {
templates.postContent = postContentTemplate;
render();
loaded();
}).fail(function() {
console.log(arguments);
loaded();
});
}
2014-10-22 10:42:15 +02:00
2015-07-19 18:30:20 +02:00
function getFitters() {
var originalWidth = $wrapper.attr('data-width');
var originalHeight = $wrapper.attr('data-height');
var ratio = originalWidth / originalHeight;
var containerHeight = jQuery(window).height() - $wrapper.offset().top - 10;
var containerWidth = $wrapper.parent().outerWidth() - 10;
return {
2015-07-19 18:50:36 +02:00
'fit-both': function(allowUpscale) {
var width = containerWidth;
var height = containerWidth / ratio;
if (height > containerHeight) {
width = containerHeight * ratio;
height = containerHeight;
}
if (!allowUpscale) {
if (width > originalWidth) {
width = originalWidth;
height = originalWidth / ratio;
}
if (height > originalHeight) {
width = originalHeight * ratio;
height = originalHeight;
}
}
2015-07-29 19:39:10 +02:00
$wrapper.css({maxWidth: width + 'px'});
2015-07-19 18:50:36 +02:00
},
2015-07-19 18:30:20 +02:00
'fit-height': function(allowUpscale) {
var width = containerHeight * ratio;
if (width > originalWidth && !allowUpscale) {
width = originalWidth;
}
2015-07-29 19:39:10 +02:00
$wrapper.css({maxWidth: width + 'px'});
2015-07-19 18:30:20 +02:00
},
'fit-width': function(allowUpscale) {
if (allowUpscale) {
2015-07-29 19:39:10 +02:00
$wrapper.css({maxWidth: containerWidth + 'px'});
2015-07-19 18:30:20 +02:00
} else {
2015-07-29 19:39:10 +02:00
$wrapper.css({maxWidth: originalWidth + 'px'});
2015-07-19 18:30:20 +02:00
}
},
'original': function(allowUpscale) {
2015-07-29 19:39:10 +02:00
$wrapper.css({
minWidth: originalWidth + 'px',
width: originalWidth + 'px'});
}
2015-07-19 18:30:20 +02:00
};
}
2015-07-19 11:27:36 +02:00
function getFitMode() {
2015-07-19 18:30:20 +02:00
return $wrapper.data('fit-mode');
}
2015-07-19 18:30:20 +02:00
function changeFitMode(fitMode) {
$wrapper.data('fit-mode', fitMode);
2015-07-29 19:39:10 +02:00
$wrapper.css({
width: '', height: '',
minWidth: '', minHeight: '',
maxWidth: '', maxHeight: '',
});
2015-07-19 18:30:20 +02:00
getFitters()[fitMode.style](fitMode.upscale);
updatePostNotesSize();
2015-07-19 11:27:36 +02:00
}
function cycleFitMode() {
var oldMode = getFitMode();
2015-07-19 18:30:20 +02:00
var fitterNames = Object.keys(getFitters());
var newMode = {
style: fitterNames[(fitterNames.indexOf(oldMode.style) + 1) % fitterNames.length],
upscale: oldMode.upscale,
};
changeFitMode(newMode);
2015-07-19 11:27:36 +02:00
}
2015-06-28 10:07:11 +02:00
function render() {
$target.html(templates.postContent({post: post}));
2015-07-19 18:30:20 +02:00
$wrapper = $target.find('.object-wrapper');
2015-06-28 10:07:11 +02:00
if (post.contentType === 'image') {
loadPostNotes();
updatePostNotesSize();
}
2015-07-19 18:30:20 +02:00
changeFitMode({
style: browsingSettings.getSettings().fitMode,
upscale: browsingSettings.getSettings().upscale,
});
2015-07-19 11:27:36 +02:00
keyboard.keyup('f', cycleFitMode);
2015-06-28 10:07:11 +02:00
jQuery(window).resize(updatePostNotesSize);
}
2015-06-28 10:07:11 +02:00
function loadPostNotes() {
presenterManager.initPresenters([
[postNotesPresenter, {post: post, notes: post.notes, $target: $target.find('.post-notes-target')}]],
function() {});
}
2014-10-22 10:42:15 +02:00
2015-06-28 10:07:11 +02:00
function updatePostNotesSize() {
var $postNotes = $target.find('.post-notes-target');
2015-07-19 11:27:36 +02:00
var $wrapper = $target.find('.object-wrapper');
2015-06-28 10:07:11 +02:00
$postNotes.css({
2015-07-19 11:27:36 +02:00
width: $wrapper.outerWidth() + 'px',
height: $wrapper.outerHeight() + 'px',
left: ($wrapper.offset().left - $wrapper.parent().offset().left) + 'px',
top: ($wrapper.offset().top - $wrapper.parent().offset().top) + 'px',
2015-06-28 10:07:11 +02:00
});
}
2015-06-28 10:07:11 +02:00
function addNewPostNote() {
postNotesPresenter.addNewPostNote();
}
2014-10-26 01:01:16 +02:00
2015-06-28 10:07:11 +02:00
return {
init: init,
render: render,
addNewPostNote: addNewPostNote,
updatePostNotesSize: updatePostNotesSize,
getFitMode: getFitMode,
changeFitMode: changeFitMode,
cycleFitMode: cycleFitMode,
2015-06-28 10:07:11 +02:00
};
2014-10-22 10:42:15 +02:00
};
App.DI.register('postContentPresenter', [
2015-06-28 10:07:11 +02:00
'jQuery',
'util',
'promise',
'keyboard',
2015-06-28 10:07:11 +02:00
'presenterManager',
2015-07-19 11:53:34 +02:00
'postNotesPresenter',
'browsingSettings'],
2015-06-28 10:07:11 +02:00
App.Presenters.PostContentPresenter);