szurubooru/public_html/js/Presenters/PostPresenter.js

424 lines
11 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-25 19:11:41 +02:00
keyboard,
2014-10-04 14:06:44 +02:00
presenterManager,
2014-10-05 10:09:02 +02:00
postsAroundCalculator,
2014-10-04 14:06:44 +02:00
postCommentListPresenter,
2014-09-18 17:39:51 +02:00
topNavigationPresenter,
messagePresenter) {
var $el = jQuery('#content');
var $messages = $el;
2014-09-26 20:41:28 +02:00
2014-10-05 10:41:12 +02:00
var templates = {};
2014-09-26 20:41:28 +02:00
2014-10-05 10:09:02 +02:00
var postNameOrId;
var searchArgs;
2014-09-18 17:39:51 +02:00
var post;
2014-09-26 20:41:28 +02:00
2014-09-23 20:18:12 +02:00
var privileges = {};
2014-09-25 19:11:41 +02:00
var editPrivileges = {};
2014-09-26 20:41:28 +02:00
2014-09-25 19:11:41 +02:00
var tagInput;
var postContentFileDropper;
var postThumbnailFileDropper;
var postContent;
var postThumbnail;
2014-09-18 17:39:51 +02:00
function init(args, loaded) {
topNavigationPresenter.select('posts');
2014-10-05 10:09:02 +02:00
postsAroundCalculator.resetCache();
2014-09-18 17:39:51 +02:00
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-26 20:41:28 +02:00
privileges.canViewHistory = auth.hasPrivilege(auth.privileges.viewHistory);
2014-09-25 19:11:41 +02:00
editPrivileges.canChangeSafety = auth.hasPrivilege(auth.privileges.changePostSafety);
editPrivileges.canChangeSource = auth.hasPrivilege(auth.privileges.changePostSource);
editPrivileges.canChangeTags = auth.hasPrivilege(auth.privileges.changePostTags);
editPrivileges.canChangeContent = auth.hasPrivilege(auth.privileges.changePostContent);
editPrivileges.canChangeThumbnail = auth.hasPrivilege(auth.privileges.changePostThumbnail);
2014-09-25 23:53:47 +02:00
editPrivileges.canChangeRelations = auth.hasPrivilege(auth.privileges.changePostRelations);
2014-10-09 09:45:06 +02:00
editPrivileges.canChangeFlags = auth.hasPrivilege(auth.privileges.changePostFlags);
2014-09-23 20:18:12 +02:00
2014-10-02 00:30:25 +02:00
promise.wait(
2014-09-18 17:39:51 +02:00
util.promiseTemplate('post'),
2014-09-25 19:11:41 +02:00
util.promiseTemplate('post-edit'),
2014-09-26 20:41:28 +02:00
util.promiseTemplate('post-content'),
util.promiseTemplate('history'))
2014-09-18 17:39:51 +02:00
.then(function(
2014-10-05 10:41:12 +02:00
postTemplate,
postEditTemplate,
postContentTemplate,
historyTemplate) {
templates.post = postTemplate;
templates.postEdit = postEditTemplate;
templates.postContent = postContentTemplate;
templates.history = historyTemplate;
2014-09-18 17:39:51 +02:00
2014-09-25 23:58:44 +02:00
reinit(args, loaded);
}).fail(function(response) {
showGenericError(response);
loaded();
});
2014-09-25 23:58:44 +02:00
}
function reinit(args, loaded) {
postNameOrId = args.postNameOrId;
2014-10-05 10:09:02 +02:00
searchArgs = util.parseComplexRouteArgs(args.searchArgs);
searchArgs.page = parseInt(searchArgs.page) || 1;
2014-10-02 00:30:25 +02:00
promise.wait(refreshPost())
2014-09-28 15:21:25 +02:00
.then(function() {
2014-09-18 17:39:51 +02:00
topNavigationPresenter.changeTitle('@' + post.id);
render();
loaded();
2014-09-30 22:09:43 +02:00
}).fail(loaded);
2014-09-18 17:39:51 +02:00
}
2014-10-05 10:09:02 +02:00
function attachLinksToPostsAround() {
var searchParams = {query: searchArgs.query, order: searchArgs.order};
promise.wait(postsAroundCalculator.getLinksToPostsAround(searchParams, searchArgs.page, post.id))
.then(function(nextPostUrl, prevPostUrl) {
var $prevPost = $el.find('#post-current-search .right a');
var $nextPost = $el.find('#post-current-search .left a');
if (nextPostUrl) {
$nextPost.addClass('enabled');
$nextPost.attr('href', nextPostUrl);
keyboard.keyup('a', function() {
router.navigate(nextPostUrl);
});
} else {
$nextPost.removeClass('enabled');
$nextPost.removeAttr('href');
keyboard.unbind('a');
}
if (prevPostUrl) {
$prevPost.addClass('enabled');
$prevPost.attr('href', prevPostUrl);
keyboard.keyup('d', function() {
router.navigate(prevPostUrl);
});
} else {
$prevPost.removeClass('enabled');
$prevPost.removeAttr('href');
keyboard.unbind('d');
}
});
}
2014-09-28 15:21:25 +02:00
function refreshPost() {
return promise.make(function(resolve, reject) {
2014-10-02 00:30:25 +02:00
promise.wait(api.get('/posts/' + postNameOrId))
2014-09-28 16:56:15 +02:00
.then(function(postResponse) {
2014-09-28 15:21:25 +02:00
post = postResponse.json;
resolve();
}).fail(function(response) {
showGenericError(response);
reject();
});
});
}
2014-09-18 17:39:51 +02:00
function render() {
2014-09-25 19:11:41 +02:00
$el.html(renderPostTemplate());
$messages = $el.find('.messages');
2014-09-26 15:40:27 +02:00
if (editPrivileges.canChangeTags) {
2014-10-05 13:25:40 +02:00
tagInput = new App.Controls.TagInput($el.find('form [name=tags]'));
2014-09-26 15:40:27 +02:00
tagInput.inputConfirmed = editPost;
}
2014-09-25 19:11:41 +02:00
2014-10-05 13:25:40 +02:00
postContentFileDropper = new App.Controls.FileDropper($el.find('form [name=content]'));
2014-09-25 19:11:41 +02:00
postContentFileDropper.onChange = postContentChanged;
postContentFileDropper.setNames = true;
2014-10-05 13:25:40 +02:00
postThumbnailFileDropper = new App.Controls.FileDropper($el.find('form [name=thumbnail]'));
2014-09-25 19:11:41 +02:00
postThumbnailFileDropper.onChange = postThumbnailChanged;
postThumbnailFileDropper.setNames = true;
2014-09-26 15:40:27 +02:00
if (_.any(editPrivileges)) {
keyboard.keyup('e', function() {
editButtonClicked(null);
});
}
2014-09-25 19:11:41 +02:00
$el.find('.post-edit-wrapper form').submit(editFormSubmitted);
2014-09-27 21:33:31 +02:00
attachSidebarEvents();
2014-10-04 14:06:44 +02:00
presenterManager.initPresenters([
[postCommentListPresenter, _.extend({post: post}, {$target: $el.find('#post-comments-target')})]],
function() { });
2014-10-05 10:09:02 +02:00
attachLinksToPostsAround();
2014-09-25 19:11:41 +02:00
}
2014-10-09 09:45:06 +02:00
function softRender() {
renderSidebar();
$el.find('video').prop('loop', post.flags.loop);
}
2014-09-25 19:11:41 +02:00
function renderSidebar() {
$el.find('#sidebar').html(jQuery(renderPostTemplate()).find('#sidebar').html());
2014-09-27 21:33:31 +02:00
attachSidebarEvents();
2014-09-25 19:11:41 +02:00
}
function renderPostTemplate() {
2014-10-05 10:41:12 +02:00
return templates.post({
2014-10-05 10:09:02 +02:00
searchArgs: searchArgs,
2014-09-18 17:39:51 +02:00
post: post,
2014-10-05 10:41:12 +02:00
ownScore: post.ownScore,
postFavorites: post.favorites,
postHistory: post.history,
2014-09-27 21:33:31 +02:00
2014-09-18 17:39:51 +02:00
formatRelativeTime: util.formatRelativeTime,
2014-09-23 19:00:40 +02:00
formatFileSize: util.formatFileSize,
2014-09-27 21:33:31 +02:00
2014-10-05 10:41:12 +02:00
postContentTemplate: templates.postContent,
postEditTemplate: templates.postEdit,
historyTemplate: templates.history,
2014-09-27 21:33:31 +02:00
2014-10-05 10:41:12 +02:00
hasFav: _.any(post.favorites, function(favUser) { return favUser.id === auth.getCurrentUser().id; }),
2014-09-27 21:33:31 +02:00
isLoggedIn: auth.isLoggedIn(),
2014-09-23 20:18:12 +02:00
privileges: privileges,
2014-09-25 19:11:41 +02:00
editPrivileges: editPrivileges,
});
2014-09-23 20:18:12 +02:00
}
2014-09-27 21:33:31 +02:00
function attachSidebarEvents() {
$el.find('#sidebar .delete').click(deleteButtonClicked);
$el.find('#sidebar .feature').click(featureButtonClicked);
$el.find('#sidebar .edit').click(editButtonClicked);
$el.find('#sidebar .history').click(historyButtonClicked);
$el.find('#sidebar .add-favorite').click(addFavoriteButtonClicked);
$el.find('#sidebar .delete-favorite').click(deleteFavoriteButtonClicked);
2014-09-28 15:21:25 +02:00
$el.find('#sidebar .score-up').click(scoreUpButtonClicked);
$el.find('#sidebar .score-down').click(scoreDownButtonClicked);
2014-09-27 21:33:31 +02:00
}
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-10-02 00:30:25 +02:00
promise.wait(api.delete('/posts/' + post.id))
2014-09-24 23:24:51 +02:00
.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() {
2014-10-02 00:30:25 +02:00
promise.wait(api.post('/posts/' + post.id + '/feature'))
2014-09-24 23:24:51 +02:00
.then(function(response) {
router.navigate('#/home');
2014-10-02 00:30:25 +02:00
}).fail(showGenericError);
2014-09-24 23:24:51 +02:00
}
2014-09-25 19:11:41 +02:00
function editButtonClicked(e) {
if (e) {
e.preventDefault();
}
messagePresenter.hideMessages($messages);
if ($el.find('.post-edit-wrapper').is(':visible')) {
hideEditForm();
} else {
showEditForm();
}
}
function editFormSubmitted(e) {
e.preventDefault();
editPost();
}
function showEditForm() {
$el.find('.post-edit-wrapper').slideDown('fast');
util.enableExitConfirmation();
tagInput.focus();
}
function hideEditForm() {
$el.find('.post-edit-wrapper').slideUp('fast');
util.disableExitConfirmation();
}
function editPost() {
var $form = $el.find('form');
var formData = {};
2014-09-25 22:19:15 +02:00
formData.seenEditTime = post.lastEditTime;
2014-10-09 09:45:06 +02:00
formData.flags = {};
2014-09-25 19:11:41 +02:00
if (editPrivileges.canChangeContent && postContent) {
formData.content = postContent;
}
if (editPrivileges.canChangeThumbnail && postThumbnail) {
formData.thumbnail = postThumbnail;
}
if (editPrivileges.canChangeSource) {
formData.source = $form.find('[name=source]').val();
}
if (editPrivileges.canChangeSafety) {
formData.safety = $form.find('[name=safety]:checked').val();
}
if (editPrivileges.canChangeTags) {
formData.tags = tagInput.getTags().join(' ');
}
2014-09-25 23:53:47 +02:00
if (editPrivileges.canChangeRelations) {
formData.relations = $form.find('[name=relations]').val();
}
2014-10-09 09:45:06 +02:00
if (editPrivileges.canChangeFlags) {
if (post.contentType === 'video') {
formData.flags.loop = $form.find('[name=loop]').is(':checked') ? 1 : 0;
}
}
2014-09-25 19:11:41 +02:00
if (post.tags.length === 0) {
showEditError('No tags set.');
return;
}
promise.wait(api.put('/posts/' + post.id, formData))
.then(function(response) {
post = response.json;
hideEditForm();
2014-10-09 09:45:06 +02:00
softRender();
2014-09-25 19:11:41 +02:00
}).fail(function(response) {
showEditError(response);
});
}
function postContentChanged(files) {
postContentFileDropper.readAsDataURL(files[0], function(content) {
postContent = content;
});
}
function postThumbnailChanged(files) {
postThumbnailFileDropper.readAsDataURL(files[0], function(content) {
postThumbnail = content;
});
}
2014-09-26 20:41:28 +02:00
function historyButtonClicked(e) {
e.preventDefault();
if ($el.find('.post-history-wrapper').is(':visible')) {
hideHistory();
} else {
showHistory();
}
}
function hideHistory() {
$el.find('.post-history-wrapper').slideUp('slow');
}
function showHistory() {
$el.find('.post-history-wrapper').slideDown('slow');
}
2014-09-27 21:33:31 +02:00
function addFavoriteButtonClicked(e) {
e.preventDefault();
addFavorite();
}
function deleteFavoriteButtonClicked(e) {
e.preventDefault();
deleteFavorite();
}
function addFavorite() {
2014-10-02 00:30:25 +02:00
promise.wait(api.post('/posts/' + post.id + '/favorites'))
2014-09-27 21:33:31 +02:00
.then(function(response) {
2014-10-09 09:45:06 +02:00
promise.wait(refreshPost()).then(softRender);
2014-10-02 00:30:25 +02:00
}).fail(showGenericError);
2014-09-27 21:33:31 +02:00
}
function deleteFavorite() {
2014-10-02 00:30:25 +02:00
promise.wait(api.delete('/posts/' + post.id + '/favorites'))
2014-09-27 21:33:31 +02:00
.then(function(response) {
2014-10-09 09:45:06 +02:00
promise.wait(refreshPost()).then(softRender);
2014-10-02 00:30:25 +02:00
}).fail(showGenericError);
2014-09-28 15:21:25 +02:00
}
function scoreUpButtonClicked(e) {
e.preventDefault();
var $target = jQuery(this);
score($target.hasClass('active') ? 0 : 1);
}
function scoreDownButtonClicked(e) {
e.preventDefault();
var $target = jQuery(this);
score($target.hasClass('active') ? 0 : -1);
}
function score(scoreValue) {
2014-10-02 00:30:25 +02:00
promise.wait(api.post('/posts/' + post.id + '/score', {score: scoreValue}))
2014-09-28 15:21:25 +02:00
.then(function() {
2014-10-09 09:45:06 +02:00
promise.wait(refreshPost()).then(softRender);
2014-10-02 00:30:25 +02:00
}).fail(showGenericError);
2014-09-27 21:33:31 +02:00
}
2014-09-25 19:11:41 +02:00
function showEditError(response) {
window.alert(response.json && response.json.error || response);
}
2014-09-24 23:24:51 +02:00
function showGenericError(response) {
2014-09-30 22:09:43 +02:00
if ($messages === $el) {
$el.empty();
}
2014-09-24 23:24:51 +02:00
messagePresenter.showError($messages, response.json && response.json.error || response);
2014-09-18 17:39:51 +02:00
}
return {
init: init,
2014-09-25 23:58:44 +02:00
reinit: reinit,
2014-09-18 17:39:51 +02:00
render: render
};
};
2014-10-04 14:06:44 +02:00
App.DI.register('postPresenter', [
'_',
'jQuery',
'util',
'promise',
'api',
'auth',
'router',
'keyboard',
'presenterManager',
2014-10-05 10:09:02 +02:00
'postsAroundCalculator',
2014-10-04 14:06:44 +02:00
'postCommentListPresenter',
'topNavigationPresenter',
'messagePresenter'],
App.Presenters.PostPresenter);