szurubooru/public_html/js/Presenters/PostContentPresenter.js

119 lines
3.5 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,
postNotesPresenter) {
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'});
},
'fit-width': function($wrapper) {
var originalWidth = $wrapper.attr('data-width');
$wrapper.css({maxWidth: originalWidth + 'px', width: 'auto'});
},
'original': function($wrapper) {
var originalWidth = $wrapper.attr('data-width');
$wrapper.css({maxWidth: null, width: originalWidth + 'px'});
}
};
var fitterNames = Object.keys(fitters);
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 mode = $wrapper.data('current-fit');
var newMode = fitterNames[(fitterNames.indexOf(mode) + 1) % fitterNames.length];
$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:27:36 +02:00
changeFitMode('fit-width');
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,
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',
'postNotesPresenter'],
App.Presenters.PostContentPresenter);