szurubooru/public_html/js/Presenters/PostPresenter.js

288 lines
7.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-25 19:11:41 +02:00
keyboard,
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-09-21 10:56:37 +02:00
var postTemplate;
2014-09-25 19:11:41 +02:00
var postEditTemplate;
2014-09-21 10:56:37 +02:00
var postContentTemplate;
2014-09-26 20:41:28 +02:00
var historyTemplate;
2014-09-18 17:39:51 +02:00
var post;
2014-09-26 20:41:28 +02:00
var postHistory;
2014-09-18 17:39:51 +02:00
var postNameOrId;
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-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-09-23 20:18:12 +02:00
2014-09-18 17:39:51 +02:00
promise.waitAll(
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-09-21 10:56:37 +02:00
postTemplateHtml,
2014-09-25 19:11:41 +02:00
postEditTemplateHtml,
2014-09-21 10:56:37 +02:00
postContentTemplateHtml,
2014-09-26 20:41:28 +02:00
historyTemplateHtml) {
2014-09-21 10:56:37 +02:00
postTemplate = _.template(postTemplateHtml);
2014-09-25 19:11:41 +02:00
postEditTemplate = _.template(postEditTemplateHtml);
2014-09-21 10:56:37 +02:00
postContentTemplate = _.template(postContentTemplateHtml);
2014-09-26 20:41:28 +02:00
historyTemplate = _.template(historyTemplateHtml);
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-09-26 20:41:28 +02:00
promise.waitAll(
api.get('/posts/' + postNameOrId),
privileges.canViewHistory ?
api.get('/posts/' + postNameOrId + '/history') :
null)
.then(function(postResponse, postHistoryResponse) {
post = postResponse.json;
postHistory = postHistoryResponse && postHistoryResponse.json && postHistoryResponse.json.data;
2014-09-18 17:39:51 +02:00
topNavigationPresenter.changeTitle('@' + post.id);
render();
loaded();
}).fail(function(response) {
showGenericError(response);
loaded();
});
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) {
tagInput = App.Controls.TagInput($el.find('form [name=tags]'), _, jQuery);
tagInput.inputConfirmed = editPost;
}
2014-09-25 19:11:41 +02:00
postContentFileDropper = new App.Controls.FileDropper($el.find('form [name=content]'), _, jQuery);
postContentFileDropper.onChange = postContentChanged;
postContentFileDropper.setNames = true;
postThumbnailFileDropper = new App.Controls.FileDropper($el.find('form [name=thumbnail]'), _, jQuery);
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-26 20:41:28 +02:00
$el.find('#sidebar .delete').click(deleteButtonClicked);
$el.find('#sidebar .feature').click(featureButtonClicked);
$el.find('#sidebar .edit').click(editButtonClicked);
$el.find('#sidebar .history').click(historyButtonClicked);
2014-09-25 19:11:41 +02:00
}
function renderSidebar() {
$el.find('#sidebar').html(jQuery(renderPostTemplate()).find('#sidebar').html());
}
function renderPostTemplate() {
return postTemplate({
2014-09-18 17:39:51 +02:00
post: post,
2014-09-26 20:41:28 +02:00
postHistory: postHistory,
2014-09-18 17:39:51 +02:00
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-25 19:11:41 +02:00
postEditTemplate: postEditTemplate,
2014-09-26 20:41:28 +02:00
historyTemplate: historyTemplate,
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
}
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);
}
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-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-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();
renderSidebar();
}).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-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) {
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-09-25 19:11:41 +02:00
App.DI.register('postPresenter', ['_', 'jQuery', 'util', 'promise', 'api', 'auth', 'router', 'keyboard', 'topNavigationPresenter', 'messagePresenter'], App.Presenters.PostPresenter);