szurubooru/public_html/js/Presenters/PostPresenter.js

64 lines
1.4 KiB
JavaScript
Raw Normal View History

2014-09-18 17:39:51 +02:00
var App = App || {};
App.Presenters = App.Presenters || {};
App.Presenters.PostPresenter = function(
_,
jQuery,
util,
promise,
api,
topNavigationPresenter,
messagePresenter) {
var $el = jQuery('#content');
var $messages = $el;
2014-09-21 10:56:37 +02:00
var postTemplate;
var postContentTemplate;
2014-09-18 17:39:51 +02:00
var post;
var postNameOrId;
function init(args, loaded) {
postNameOrId = args.postNameOrId;
topNavigationPresenter.select('posts');
promise.waitAll(
util.promiseTemplate('post'),
2014-09-21 10:56:37 +02:00
util.promiseTemplate('post-content'),
2014-09-18 17:39:51 +02:00
api.get('/posts/' + postNameOrId))
.then(function(
2014-09-21 10:56:37 +02:00
postTemplateHtml,
postContentTemplateHtml,
2014-09-18 17:39:51 +02:00
response) {
$messages = $el.find('.messages');
2014-09-21 10:56:37 +02:00
postTemplate = _.template(postTemplateHtml);
postContentTemplate = _.template(postContentTemplateHtml);
2014-09-18 17:39:51 +02:00
post = response.json;
topNavigationPresenter.changeTitle('@' + post.id);
render();
loaded();
}).fail(function(response) {
$el.empty();
messagePresenter.showError($messages, response.json && response.json.error || response);
});
}
function render() {
2014-09-21 10:56:37 +02:00
$el.html(postTemplate({
2014-09-18 17:39:51 +02:00
post: post,
formatRelativeTime: util.formatRelativeTime,
2014-09-23 19:00:40 +02:00
formatFileSize: util.formatFileSize,
2014-09-21 10:56:37 +02:00
postContentTemplate: postContentTemplate,
2014-09-18 17:39:51 +02:00
}));
}
return {
init: init,
render: render
};
};
App.DI.register('postPresenter', ['_', 'jQuery', 'util', 'promise', 'api', 'topNavigationPresenter', 'messagePresenter'], App.Presenters.PostPresenter);