v1.18.x
barisusakli 11 years ago
parent 6011399063
commit a0e784a612

@ -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;
});

@ -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);
});
});
};

@ -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;
Loading…
Cancel
Save