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-22 10:27:20 +02:00
|
|
|
postEditPresenter,
|
2014-10-22 10:42:15 +02:00
|
|
|
postContentPresenter,
|
2014-11-22 12:56:44 +01:00
|
|
|
commentListPresenter,
|
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-10-09 21:41:46 +02:00
|
|
|
var params;
|
2014-09-26 20:41:28 +02:00
|
|
|
|
2014-10-05 10:09:02 +02:00
|
|
|
var postNameOrId;
|
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-18 17:39:51 +02:00
|
|
|
|
2014-10-09 21:41:46 +02:00
|
|
|
function init(params, loaded) {
|
2014-09-18 17:39:51 +02:00
|
|
|
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-10-26 01:01:16 +02:00
|
|
|
privileges.canAddPostNotes = auth.hasPrivilege(auth.privileges.addPostNotes);
|
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-26 20:41:28 +02:00
|
|
|
util.promiseTemplate('history'))
|
2014-09-18 17:39:51 +02:00
|
|
|
.then(function(
|
2014-10-05 10:41:12 +02:00
|
|
|
postTemplate,
|
|
|
|
historyTemplate) {
|
|
|
|
templates.post = postTemplate;
|
|
|
|
templates.history = historyTemplate;
|
2014-09-18 17:39:51 +02:00
|
|
|
|
2014-10-09 21:41:46 +02:00
|
|
|
reinit(params, loaded);
|
2014-09-27 10:31:12 +02:00
|
|
|
}).fail(function(response) {
|
|
|
|
showGenericError(response);
|
|
|
|
loaded();
|
|
|
|
});
|
2014-09-25 23:58:44 +02:00
|
|
|
}
|
|
|
|
|
2014-10-09 21:41:46 +02:00
|
|
|
function reinit(_params, loaded) {
|
|
|
|
params = _params;
|
|
|
|
params.query = params.query || {};
|
|
|
|
params.query.page = parseInt(params.query.page) || 1;
|
2014-09-25 23:58:44 +02:00
|
|
|
|
2014-10-09 21:41:46 +02:00
|
|
|
postNameOrId = params.postNameOrId;
|
2014-10-05 10:09:02 +02:00
|
|
|
|
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-10-22 10:27:20 +02:00
|
|
|
|
|
|
|
presenterManager.initPresenters([
|
2014-10-22 10:42:15 +02:00
|
|
|
[postContentPresenter, {post: post, $target: $el.find('#post-content-target')}],
|
2014-10-22 10:27:20 +02:00
|
|
|
[postEditPresenter, {post: post, $target: $el.find('#post-edit-target'), updateCallback: postEdited}],
|
2014-11-22 12:56:44 +01:00
|
|
|
[commentListPresenter, {post: post, $target: $el.find('#post-comments-target')}]],
|
2014-10-26 01:01:16 +02:00
|
|
|
function() { });
|
2014-10-22 10:27:20 +02:00
|
|
|
|
2014-10-10 18:13:46 +02:00
|
|
|
}).fail(function() {
|
|
|
|
console.log(arguments);
|
|
|
|
loaded();
|
|
|
|
});
|
2014-10-22 10:27:20 +02:00
|
|
|
|
2014-09-18 17:39:51 +02:00
|
|
|
}
|
|
|
|
|
2014-10-05 10:09:02 +02:00
|
|
|
function attachLinksToPostsAround() {
|
2014-10-09 21:41:46 +02:00
|
|
|
promise.wait(postsAroundCalculator.getLinksToPostsAround(params.query, post.id))
|
2014-10-05 10:09:02 +02:00
|
|
|
.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-11-03 20:42:10 +01:00
|
|
|
}).fail(function() {
|
2014-10-05 10:09:02 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-10-22 10:27:20 +02:00
|
|
|
keyboard.keyup('e', function() {
|
|
|
|
editButtonClicked(null);
|
|
|
|
});
|
2014-09-25 19:11:41 +02:00
|
|
|
|
2015-03-13 09:29:26 +01:00
|
|
|
keyboard.keyup('f', function() {
|
|
|
|
var $wrapper = $el.find('.object-wrapper');
|
|
|
|
if ($wrapper.data('full')) {
|
|
|
|
$wrapper.css({maxWidth: $wrapper.attr('data-width') + 'px', width: 'auto'});
|
|
|
|
$wrapper.data('full', false);
|
|
|
|
} else {
|
|
|
|
$wrapper.css({maxWidth: null, width: $wrapper.attr('data-width')});
|
|
|
|
$wrapper.data('full', true);
|
|
|
|
}
|
|
|
|
postContentPresenter.updatePostNotesSize();
|
|
|
|
});
|
|
|
|
|
2014-09-27 21:33:31 +02:00
|
|
|
attachSidebarEvents();
|
2014-10-04 14:06:44 +02:00
|
|
|
|
2014-10-05 10:09:02 +02:00
|
|
|
attachLinksToPostsAround();
|
2014-09-25 19:11:41 +02:00
|
|
|
}
|
|
|
|
|
2014-10-22 10:27:20 +02:00
|
|
|
function postEdited(newPost) {
|
|
|
|
post = newPost;
|
|
|
|
hideEditForm();
|
|
|
|
softRender();
|
|
|
|
}
|
|
|
|
|
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-09 21:41:46 +02:00
|
|
|
query: params.query,
|
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-12-20 10:30:10 +01:00
|
|
|
util: util,
|
2014-09-27 21:33:31 +02:00
|
|
|
|
2014-10-05 10:41:12 +02:00
|
|
|
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-10-22 10:27:20 +02:00
|
|
|
editPrivileges: postEditPresenter.getPrivileges(),
|
2014-09-25 19:11:41 +02:00
|
|
|
});
|
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-10-26 01:01:16 +02:00
|
|
|
$el.find('#sidebar .add-note').click(addNoteButtonClicked);
|
|
|
|
}
|
|
|
|
|
|
|
|
function addNoteButtonClicked(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
postContentPresenter.addNewPostNote();
|
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);
|
2014-10-25 23:39:37 +02:00
|
|
|
if (window.confirm('Do you want to feature this post on the front page?')) {
|
2014-09-24 23:24:51 +02:00
|
|
|
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);
|
2014-10-22 10:27:20 +02:00
|
|
|
if ($el.find('#post-edit-target').is(':visible')) {
|
2014-09-25 19:11:41 +02:00
|
|
|
hideEditForm();
|
|
|
|
} else {
|
|
|
|
showEditForm();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function showEditForm() {
|
2014-10-22 10:27:20 +02:00
|
|
|
$el.find('#post-edit-target').slideDown('fast');
|
2014-09-25 19:11:41 +02:00
|
|
|
util.enableExitConfirmation();
|
2014-10-22 10:27:20 +02:00
|
|
|
postEditPresenter.focus();
|
2014-09-25 19:11:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function hideEditForm() {
|
2014-10-22 10:27:20 +02:00
|
|
|
$el.find('#post-edit-target').slideUp('fast');
|
2014-09-25 19:11:41 +02:00
|
|
|
util.disableExitConfirmation();
|
|
|
|
}
|
|
|
|
|
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-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-22 10:27:20 +02:00
|
|
|
'postEditPresenter',
|
2014-10-22 10:42:15 +02:00
|
|
|
'postContentPresenter',
|
2014-11-22 12:56:44 +01:00
|
|
|
'commentListPresenter',
|
2014-10-04 14:06:44 +02:00
|
|
|
'topNavigationPresenter',
|
|
|
|
'messagePresenter'],
|
|
|
|
App.Presenters.PostPresenter);
|