From a0e784a612edad2112d9716ef8f2158b2d705b7b Mon Sep 17 00:00:00 2001 From: barisusakli Date: Thu, 13 Mar 2014 20:24:04 -0400 Subject: [PATCH] closes #1138 --- public/src/forum/home.js | 74 +++++++++++++++++++++++++++++++++++----- src/posts.js | 11 ++---- src/socket.io/posts.js | 3 ++ 3 files changed, 72 insertions(+), 16 deletions(-) diff --git a/public/src/forum/home.js b/public/src/forum/home.js index efaaba4033..5a70082de1 100644 --- a/public/src/forum/home.js +++ b/public/src/forum/home.js @@ -1,31 +1,89 @@ 'use strict'; +/* globals define, socket, app, templates, translator*/ + define(function() { var home = {}; - $(window).on('action:ajaxify.end', function(ev, data) { - if (data.url === '') { - socket.removeListener('event:new_topic', home.onNewTopic); + $(window).on('action:ajaxify.start', function(ev, data) { + if (data.url !== '') { socket.removeListener('event:new_post', home.onNewPost); } }); home.init = function() { - app.enterRoom('home'); - socket.on('event:new_topic', home.onNewTopic); socket.on('event:new_post', home.onNewPost); }; - home.onNewTopic = function(data) { + home.onNewPost = function(data) { - }; + if (data && data.posts && data.posts.length) { - home.onNewPost = function(data) { + socket.emit('posts.getCategory', data.posts[0].pid, function(err, cid) { + if (err) { + return; + } + renderNewPost(cid, data.posts[0]); + }); + } }; + function renderNewPost(cid, post) { + var category = $('.home .category-item[data-cid="' + cid + '"]'); + var categoryBox = category.find('.category-box'); + var numRecentReplies = category.attr('data-numRecentReplies'); + if (!numRecentReplies) { + return; + } + + var recentPosts = categoryBox.find('.post-preview'); + var insertBefore = recentPosts.first(); + + parseAndTranslate([post], function(html) { + html.hide(); + if(recentPosts.length === 0) { + html.appendTo(categoryBox); + } else { + html.insertBefore(recentPosts.first()); + } + + html.fadeIn(); + + app.createUserTooltips(); + + if (categoryBox.find('.post-preview').length > parseInt(numRecentReplies, 10)) { + recentPosts.last().remove(); + } + }); + } + + function parseAndTranslate(posts, callback) { + templates.preload_template('home', function() { + + templates.home.parse({ + categories: { + posts: [] + } + }); + + var html = templates.prepare(templates.home.blocks['categories.posts']).parse({ + categories: { + posts: posts + } + }); + + translator.translate(html, function(translatedHTML) { + translatedHTML = $(translatedHTML); + translatedHTML.find('img').addClass('img-responsive'); + translatedHTML.find('span.timeago').timeago(); + callback(translatedHTML); + }); + }); + } + return home; }); diff --git a/src/posts.js b/src/posts.js index be10f66417..dff6af855d 100644 --- a/src/posts.js +++ b/src/posts.js @@ -407,15 +407,10 @@ var db = require('./database'), } topics.getTopicField(tid, 'cid', function(err, cid) { - if(err) { - return callback(err); - } - - if (cid) { - callback(null, cid); - } else { - callback(new Error('invalid-category-id')); + if(err || !cid) { + return callback(err || new Error('invalid-category-id')); } + callback(null, cid); }); }); }; diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js index fedb968522..6723d16684 100644 --- a/src/socket.io/posts.js +++ b/src/socket.io/posts.js @@ -304,5 +304,8 @@ SocketPosts.getRecentPosts = function(socket, data, callback) { posts.getRecentPosts(socket.uid, 0, data.count - 1, data.term, callback); }; +SocketPosts.getCategory = function(socket, pid, callback) { + posts.getCidByPid(pid, callback); +} module.exports = SocketPosts; \ No newline at end of file