szurubooru/public_html/js/Presenters/HomePresenter.js

69 lines
1.6 KiB
JavaScript
Raw Normal View History

2014-09-07 19:49:11 +02:00
var App = App || {};
App.Presenters = App.Presenters || {};
App.Presenters.HomePresenter = function(
2014-09-24 23:24:51 +02:00
_,
2014-09-07 19:49:11 +02:00
jQuery,
2014-09-24 23:24:51 +02:00
util,
promise,
api,
2014-09-30 22:09:43 +02:00
auth,
2014-09-24 23:24:51 +02:00
topNavigationPresenter,
messagePresenter) {
2014-09-07 19:49:11 +02:00
var $el = jQuery('#content');
2014-09-24 23:24:51 +02:00
var homeTemplate;
var postContentTemplate;
2014-09-25 11:45:46 +02:00
var globals;
2014-09-24 23:24:51 +02:00
var post;
2014-09-07 19:49:11 +02:00
2014-09-17 22:37:49 +02:00
function init(args, loaded) {
2014-09-07 19:49:11 +02:00
topNavigationPresenter.select('home');
topNavigationPresenter.changeTitle('Home');
2014-09-24 23:24:51 +02:00
promise.waitAll(
util.promiseTemplate('home'),
util.promiseTemplate('post-content'),
2014-09-25 11:45:46 +02:00
api.get('/globals'),
2014-09-24 23:24:51 +02:00
api.get('/posts/featured'))
.then(function(
homeTemplateHtml,
postContentTemplateHtml,
2014-09-25 11:45:46 +02:00
globalsResponse,
featuredPostResponse) {
2014-09-24 23:24:51 +02:00
homeTemplate = _.template(homeTemplateHtml);
postContentTemplate = _.template(postContentTemplateHtml);
2014-09-25 11:45:46 +02:00
globals = globalsResponse.json;
post = featuredPostResponse.json;
2014-09-24 23:24:51 +02:00
render();
loaded();
}).fail(function(response) {
messagePresenter.showError($el, response.json && response.json.error || response);
loaded();
2014-09-24 23:24:51 +02:00
});
2014-09-07 19:49:11 +02:00
}
function render() {
2014-09-24 23:24:51 +02:00
$el.html(homeTemplate({
post: post,
postContentTemplate: postContentTemplate,
2014-09-25 11:45:46 +02:00
globals: globals,
2014-09-24 23:24:51 +02:00
title: topNavigationPresenter.getBaseTitle(),
2014-09-30 22:09:43 +02:00
canViewUsers: auth.hasPrivilege(auth.privileges.viewUsers),
canViewPosts: auth.hasPrivilege(auth.privileges.viewPosts),
2014-09-24 23:24:51 +02:00
formatRelativeTime: util.formatRelativeTime,
2014-09-25 11:45:46 +02:00
formatFileSize: util.formatFileSize,
2014-09-24 23:24:51 +02:00
}));
2014-09-08 22:02:28 +02:00
}
2014-09-07 19:49:11 +02:00
return {
init: init,
render: render,
};
};
2014-09-30 22:09:43 +02:00
App.DI.register('homePresenter', ['_', 'jQuery', 'util', 'promise', 'api', 'auth', 'topNavigationPresenter', 'messagePresenter'], App.Presenters.HomePresenter);