diff --git a/public/src/forum/topic/postTools.js b/public/src/forum/topic/postTools.js index 5fe564c3db..3b63650942 100644 --- a/public/src/forum/topic/postTools.js +++ b/public/src/forum/topic/postTools.js @@ -15,6 +15,8 @@ define('forum/topic/postTools', ['composer', 'share', 'navigator'], function(com share.addShareHandlers(topicName); addFavouriteHandler(); + + addVoteHandler(); }; PostTools.toggle = function(pid, isDeleted) { @@ -37,30 +39,42 @@ define('forum/topic/postTools', ['composer', 'share', 'navigator'], function(com function addFavouriteHandler() { $('#post-container').on('mouseenter', '.favourite-tooltip', function() { - if (!$(this).data('users-loaded')) { - $(this).data('users-loaded', 'true'); - var pid = $(this).parents('.post-row').attr('data-pid'); - var el = $(this).attr('title', "Loading..."); - socket.emit('posts.getFavouritedUsers', pid, function(err, usernames) { - if (err) { - return; - } - if (usernames.length > 6) { - var otherCount = usernames.length - 5; - usernames = usernames.slice(0, 5).join(', ').replace(/,/g, '|'); - translator.translate('[[topic:users_and_others, ' + usernames + ', ' + otherCount + ']]', function(translated) { - translated = translated.replace(/\|/g, ','); - el.attr('title', translated).tooltip('show'); - }); - } else { - usernames = usernames.join(', '); - el.attr('title', usernames).tooltip('show'); - } - }); + loadDataAndCreateTooltip($(this), 'posts.getFavouritedUsers'); + }); + } + + function addVoteHandler() { + $('#post-container').on('mouseenter', '.post-row .votes', function() { + loadDataAndCreateTooltip($(this), 'posts.getUpvoters'); + }); + } + + function loadDataAndCreateTooltip(el, method) { + var pid = el.parents('.post-row').attr('data-pid'); + socket.emit(method, pid, function(err, usernames) { + if (!err) { + createTooltip(el, usernames); } }); } + function createTooltip(el, usernames) { + if (!usernames.length) { + return; + } + if (usernames.length > 6) { + var otherCount = usernames.length - 5; + usernames = usernames.slice(0, 5).join(', ').replace(/,/g, '|'); + translator.translate('[[topic:users_and_others, ' + usernames + ', ' + otherCount + ']]', function(translated) { + translated = translated.replace(/\|/g, ','); + el.attr('title', translated).tooltip('destroy').tooltip('show'); + }); + } else { + usernames = usernames.join(', '); + el.attr('title', usernames).tooltip('destroy').tooltip('show'); + } + } + function addPostHandlers(tid, threadState) { $('.topic').on('click', '.post_reply', function() { if (!threadState.locked) { diff --git a/src/favourites.js b/src/favourites.js index c887c5c7e7..64de4fa32f 100644 --- a/src/favourites.js +++ b/src/favourites.js @@ -253,4 +253,12 @@ var async = require('async'), db.getSetsMembers(sets, callback); }; + Favourites.getUpvotedUidsByPids = function(pids, callback) { + var sets = pids.map(function(pid) { + return 'pid:' + pid + ':upvote'; + }); + db.getSetsMembers(sets, callback); + }; + + }(exports)); diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js index 6b3b51547c..d08903a779 100644 --- a/src/socket.io/posts.js +++ b/src/socket.io/posts.js @@ -250,20 +250,25 @@ SocketPosts.getPrivileges = function(socket, pid, callback) { SocketPosts.getFavouritedUsers = function(socket, pid, callback) { favourites.getFavouritedUidsByPids([pid], function(err, data) { - if(err) { + if (err || !Array.isArray(data) || !data.length) { return callback(err); } - if(!Array.isArray(data) || !data.length) { - callback(null, []); - } + user.getUsernamesByUids(data[0], callback); + }); +}; - var pid_uids = data[0]; +SocketPosts.getUpvoters = function(socket, pid, callback) { + favourites.getUpvotedUidsByPids([pid], function(err, data) { + if (err || !Array.isArray(data) || !data.length) { + return callback(err, []); + } - user.getUsernamesByUids(pid_uids, callback); + user.getUsernamesByUids(data[0], callback); }); }; + SocketPosts.getPidPage = function(socket, pid, callback) { posts.getPidPage(pid, socket.uid, callback); };