szurubooru/public_html/js/Presenters/PostContentPresenter.js

131 lines
4 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;
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 11:27:36 +02:00
var fitters = {
'fit-height': function($wrapper) {
var originalWidth = $wrapper.attr('data-width');
var originalHeight = $wrapper.attr('data-height');
var ratio = originalWidth / originalHeight;
var height = jQuery(window).height() - $wrapper.offset().top;
var width = (height - 10) * ratio;
$wrapper.css({maxWidth: width + 'px', width: ''});
2015-07-19 11:27:36 +02:00
},
'fit-width': function($wrapper) {
var originalWidth = $wrapper.attr('data-width');
$wrapper.css({maxWidth: originalWidth + 'px', width: ''});
2015-07-19 11:27:36 +02:00
},
'original': function($wrapper) {
var originalWidth = $wrapper.attr('data-width');
$wrapper.css({maxWidth: '', width: originalWidth + 'px'});
2015-07-19 11:27:36 +02:00
}
};
var fitterNames = Object.keys(fitters);
function getFitMode() {
var $wrapper = $target.find('.object-wrapper');
return $wrapper.data('current-fit');
}
2015-07-19 11:27:36 +02:00
function changeFitMode(mode) {
var $wrapper = $target.find('.object-wrapper');
$wrapper.data('current-fit', mode);
fitters[$wrapper.data('current-fit')]($wrapper);
}
function cycleFitMode() {
var $wrapper = $target.find('.object-wrapper');
var oldMode = getFitMode();
var newMode = fitterNames[(fitterNames.indexOf(oldMode) + 1) % fitterNames.length];
2015-07-19 11:27:36 +02:00
$wrapper.data('current-fit', newMode);
fitters[$wrapper.data('current-fit')]($wrapper);
updatePostNotesSize();
}
2015-06-28 10:07:11 +02:00
function render() {
$target.html(templates.postContent({post: post}));
2015-06-28 10:07:11 +02:00
if (post.contentType === 'image') {
loadPostNotes();
updatePostNotesSize();
}
2015-07-19 11:53:34 +02:00
changeFitMode(browsingSettings.getSettings().fitMode);
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,
fitWidth: function() { changeFitMode('fit-width'); },
fitHeight: function() { changeFitMode('fit-height'); },
fitOriginal: function() { changeFitMode('original'); },
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);