diff --git a/TODO b/TODO index ddfde125..202fae28 100644 --- a/TODO +++ b/TODO @@ -3,7 +3,6 @@ first major release. everything related to posts: - single post view - - fav - score - editing - ability to loop video posts @@ -104,8 +103,6 @@ refactors: with query decorated by PostDao::decorateQueryFromFilter, call PostDao::decorateEntitiesWithFilter that will optimally load users and inject them using posts' lazy loaders. - - make view proxies less greedy, e.g. favs should be fetched only in post - view, not for each of 40 posts in post list. (same goes to other things) - reduce dependencies miscellaneous: diff --git a/public_html/css/post.css b/public_html/css/post.css index f2a4d37c..863000db 100644 --- a/public_html/css/post.css +++ b/public_html/css/post.css @@ -56,6 +56,10 @@ } } +#post-view-wrapper .favorites li { + display: inline-block; + margin: 0 0.25em 0.25em 0; +} #sidebar ul { list-style-type: none; diff --git a/public_html/js/Presenters/PostPresenter.js b/public_html/js/Presenters/PostPresenter.js index 99971d8d..341722ef 100644 --- a/public_html/js/Presenters/PostPresenter.js +++ b/public_html/js/Presenters/PostPresenter.js @@ -22,6 +22,7 @@ App.Presenters.PostPresenter = function( var historyTemplate; var post; + var postFavorites; var postHistory; var postNameOrId; @@ -74,11 +75,16 @@ App.Presenters.PostPresenter = function( promise.waitAll( api.get('/posts/' + postNameOrId), + api.get('/posts/' + postNameOrId + '/favorites'), privileges.canViewHistory ? api.get('/posts/' + postNameOrId + '/history') : null) - .then(function(postResponse, postHistoryResponse) { + .then(function( + postResponse, + postFavoritesResponse, + postHistoryResponse) { post = postResponse.json; + postFavorites = postFavoritesResponse && postFavoritesResponse.json && postFavoritesResponse.json.data; postHistory = postHistoryResponse && postHistoryResponse.json && postHistoryResponse.json.data; topNavigationPresenter.changeTitle('@' + post.id); render(); @@ -112,30 +118,43 @@ App.Presenters.PostPresenter = function( } $el.find('.post-edit-wrapper form').submit(editFormSubmitted); - $el.find('#sidebar .delete').click(deleteButtonClicked); - $el.find('#sidebar .feature').click(featureButtonClicked); - $el.find('#sidebar .edit').click(editButtonClicked); - $el.find('#sidebar .history').click(historyButtonClicked); + attachSidebarEvents(); } function renderSidebar() { $el.find('#sidebar').html(jQuery(renderPostTemplate()).find('#sidebar').html()); + attachSidebarEvents(); } function renderPostTemplate() { return postTemplate({ post: post, + postFavorites: postFavorites, postHistory: postHistory, + formatRelativeTime: util.formatRelativeTime, formatFileSize: util.formatFileSize, + postContentTemplate: postContentTemplate, postEditTemplate: postEditTemplate, historyTemplate: historyTemplate, + + hasFav: _.any(postFavorites, function(favUser) { return favUser.id === auth.getCurrentUser().id; }), + isLoggedIn: auth.isLoggedIn(), privileges: privileges, editPrivileges: editPrivileges, }); } + 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); + } + function deleteButtonClicked(e) { e.preventDefault(); messagePresenter.hideMessages($messages); @@ -268,6 +287,34 @@ App.Presenters.PostPresenter = function( $el.find('.post-history-wrapper').slideDown('slow'); } + function addFavoriteButtonClicked(e) { + e.preventDefault(); + addFavorite(); + } + + function deleteFavoriteButtonClicked(e) { + e.preventDefault(); + deleteFavorite(); + } + + function addFavorite() { + api.post('/posts/' + post.id + '/favorites') + .then(function(response) { + postFavorites = response.json.data; + renderSidebar(); + }) + .fail(showGenericError); + } + + function deleteFavorite() { + api.delete('/posts/' + post.id + '/favorites') + .then(function(response) { + postFavorites = response.json.data; + renderSidebar(); + }) + .fail(showGenericError); + } + function showEditError(response) { window.alert(response.json && response.json.error || response); } diff --git a/public_html/templates/post.tpl b/public_html/templates/post.tpl index 1bb3156c..f1eb1372 100644 --- a/public_html/templates/post.tpl +++ b/public_html/templates/post.tpl @@ -10,6 +10,20 @@ <%= post.contentExtension + ', ' + formatFileSize(post.originalFileSize) %> + + <% if (isLoggedIn) { %> +
Favorites:
+ +