szurubooru/public_html/js/Presenters/PostPresenter.js

109 lines
2.5 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,
2014-09-23 20:18:12 +02:00
auth,
router,
2014-09-18 17:39:51 +02:00
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;
2014-09-23 20:18:12 +02:00
var privileges = {};
2014-09-18 17:39:51 +02:00
function init(args, loaded) {
postNameOrId = args.postNameOrId;
topNavigationPresenter.select('posts');
2014-09-23 20:18:12 +02:00
privileges.canDeletePosts = auth.hasPrivilege(auth.privileges.deletePosts);
2014-09-24 23:24:51 +02:00
privileges.canFeaturePosts = auth.hasPrivilege(auth.privileges.featurePosts);
2014-09-23 20:18:12 +02:00
2014-09-18 17:39:51 +02:00
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) {
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();
2014-09-24 23:24:51 +02:00
showGenericError(response);
2014-09-18 17:39:51 +02:00
});
}
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-23 20:18:12 +02:00
privileges: privileges,
2014-09-18 17:39:51 +02:00
}));
2014-09-24 23:24:51 +02:00
$messages = $el.find('.messages');
2014-09-23 20:18:12 +02:00
$el.find('.delete').click(deleteButtonClicked);
2014-09-24 23:24:51 +02:00
$el.find('.feature').click(featureButtonClicked);
2014-09-23 20:18:12 +02:00
}
function deleteButtonClicked(e) {
e.preventDefault();
2014-09-24 23:24:51 +02:00
messagePresenter.hideMessages($messages);
2014-09-23 20:18:12 +02:00
if (window.confirm('Do you really want to delete this post?')) {
deletePost();
}
}
function deletePost() {
2014-09-24 23:24:51 +02:00
api.delete('/posts/' + post.id)
.then(function(response) {
router.navigate('#/posts');
}).fail(showGenericError);
}
function featureButtonClicked(e) {
e.preventDefault();
messagePresenter.hideMessages($messages);
if (window.confirm('Do you want to feature this post on fron page?')) {
featurePost();
}
}
function featurePost() {
api.post('/posts/' + post.id + '/feature')
.then(function(response) {
router.navigate('#/home');
})
.fail(showGenericError);
}
function showGenericError(response) {
messagePresenter.showError($messages, response.json && response.json.error || response);
2014-09-18 17:39:51 +02:00
}
return {
init: init,
render: render
};
};
2014-09-23 20:18:12 +02:00
App.DI.register('postPresenter', ['_', 'jQuery', 'util', 'promise', 'api', 'auth', 'router', 'topNavigationPresenter', 'messagePresenter'], App.Presenters.PostPresenter);