diff --git a/public/src/forum/users.js b/public/src/forum/users.js index 71767b15c9..35c68f18cb 100644 --- a/public/src/forum/users.js +++ b/public/src/forum/users.js @@ -1,21 +1,16 @@ +'use strict'; + +/* globals define, socket, app, ajaxify, templates, translator*/ + define('forum/users', function() { var Users = {}; - Users.init = function() { - var timeoutId = 0; - var loadingMoreUsers = false; + var loadingMoreUsers = false; - function getActiveSection() { - var url = window.location.href, - parts = url.split('/'), - active = parts[parts.length - 1]; - return active; - } + Users.init = function() { var active = getActiveSection(); - var lastSearch = null; - $('.nav-pills li').removeClass('active'); $('.nav-pills li a').each(function() { var $this = $(this); @@ -25,6 +20,78 @@ define('forum/users', function() { } }); + handleSearch(); + + socket.removeListener('user.anonDisconnect', updateAnonCount); + socket.removeListener('user.anonConnect', updateAnonCount); + socket.removeListener('user.isOnline', onUserIsOnline); + + socket.on('user.anonDisconnect', updateAnonCount); + socket.on('user.anonConnect', updateAnonCount); + socket.on('user.isOnline', onUserIsOnline); + + + $('#load-more-users-btn').on('click', loadMoreUsers); + + $(window).off('scroll').on('scroll', function() { + var bottom = ($(document).height() - $(window).height()) * 0.9; + + if ($(window).scrollTop() > bottom && !loadingMoreUsers) { + loadMoreUsers(); + } + }); + }; + + function loadMoreUsers() { + var set = ''; + var activeSection = getActiveSection(); + if (activeSection === 'latest') { + set = 'users:joindate'; + } else if (activeSection === 'sort-posts') { + set = 'users:postcount'; + } else if (activeSection === 'sort-reputation') { + set = 'users:reputation'; + } else if (activeSection === 'online' || activeSection === 'users') { + set = 'users:online'; + } + + if (set) { + startLoading(set, $('#users-container').children('.registered-user').length); + } + } + + function startLoading(set, after) { + loadingMoreUsers = true; + + socket.emit('user.loadMore', { + set: set, + after: after + }, function(err, data) { + if (data && data.users.length) { + onUsersLoaded(data.users); + $('#load-more-users-btn').removeClass('disabled'); + } else { + $('#load-more-users-btn').addClass('disabled'); + } + loadingMoreUsers = false; + }); + } + + function onUsersLoaded(users) { + ajaxify.loadTemplate('users', function(usersTemplate) { + var html = templates.parse(templates.getBlock(usersTemplate, 'users'), {users: users}); + + translator.translate(html, function(translated) { + $('#users-container').append(translated); + $('#users-container .anon-user').appendTo($('#users-container')); + }); + }); + } + + function handleSearch() { + var timeoutId = 0; + var lastSearch = null; + $('#search-user').on('keyup', function() { if (timeoutId !== 0) { clearTimeout(timeoutId); @@ -85,93 +152,54 @@ define('forum/users', function() { }, 250); }); + } - socket.on('user.isOnline', function(err, data) { - var section = getActiveSection(); - if((section.indexOf('online') === 0 || section.indexOf('users') === 0) && !loadingMoreUsers) { - startLoading('users:online', 0, true); - updateAnonCount(); - } - }); - - socket.on('user.anonDisconnect', updateAnonCount); - socket.on('user.anonConnect', updateAnonCount) - - function updateAnonCount() { - var section = getActiveSection(); - if((section.indexOf('online') === 0 || section.indexOf('users') === 0) && !loadingMoreUsers) { - socket.emit('user.getOnlineAnonCount', {} , function(err, anonCount) { - - if(parseInt(anonCount, 10) > 0) { - $('#users-container .anon-user').removeClass('hide'); - $('#online_anon_count').html(anonCount); - } else { - $('#users-container .anon-user').addClass('hide'); - } - }); - } + function onUserIsOnline(err, data) { + var section = getActiveSection(); + if((section.indexOf('online') === 0 || section.indexOf('users') === 0) && !loadingMoreUsers) { + updateUser(data); + updateAnonCount(); } + } - function onUsersLoaded(users, emptyContainer) { + function updateUser(data) { + var userEl = $('#users-container li[data-uid="' + data.uid +'"]'); + if (!data.online) { + userEl.remove(); + } else { ajaxify.loadTemplate('users', function(usersTemplate) { - var html = templates.parse(templates.getBlock(usersTemplate, 'users'), {users: users}); - + var html = templates.parse(templates.getBlock(usersTemplate, 'users'), {users: [data]}); translator.translate(html, function(translated) { - if(emptyContainer) { - $('#users-container .registered-user').remove(); + if (!userEl.length) { + $('#users-container').append(translated); + } else { + userEl.replaceWith(translated); } - - $('#users-container').append(translated); - $('#users-container .anon-user').appendTo($('#users-container')); }); }); } + } - function loadMoreUsers() { - var set = ''; - if (active === 'latest') { - set = 'users:joindate'; - } else if (active === 'sort-posts') { - set = 'users:postcount'; - } else if (active === 'sort-reputation') { - set = 'users:reputation'; - } else if (active === 'online' || active === 'users') { - set = 'users:online'; - } - - if (set) { - startLoading(set, $('#users-container').children('.registered-user').length); - } - } + function updateAnonCount() { + var section = getActiveSection(); + if((section.indexOf('online') === 0 || section.indexOf('users') === 0) && !loadingMoreUsers) { + socket.emit('user.getOnlineAnonCount', {} , function(err, anonCount) { - function startLoading(set, after, emptyContainer) { - loadingMoreUsers = true; - - socket.emit('user.loadMore', { - set: set, - after: after - }, function(err, data) { - if (data && data.users.length) { - onUsersLoaded(data.users, emptyContainer); - $('#load-more-users-btn').removeClass('disabled'); + if(parseInt(anonCount, 10) > 0) { + $('#users-container .anon-user').removeClass('hide'); + $('#online_anon_count').html(anonCount); } else { - $('#load-more-users-btn').addClass('disabled'); + $('#users-container .anon-user').addClass('hide'); } - loadingMoreUsers = false; }); } + } - - $('#load-more-users-btn').on('click', loadMoreUsers); - - $(window).off('scroll').on('scroll', function() { - var bottom = ($(document).height() - $(window).height()) * 0.9; - - if ($(window).scrollTop() > bottom && !loadingMoreUsers) { - loadMoreUsers(); - } - }); - }; + function getActiveSection() { + var url = window.location.href, + parts = url.split('/'); + return parts[parts.length - 1]; + } return Users; }); diff --git a/src/user.js b/src/user.js index 3c62a52d21..f20686fc7c 100644 --- a/src/user.js +++ b/src/user.js @@ -414,7 +414,7 @@ var bcrypt = require('bcryptjs'), }; User.isOnline = function(uid, callback) { - User.getUserFields(uid, ['username', 'userslug', 'picture', 'status'] , function(err, data) { + User.getUserFields(uid, ['username', 'userslug', 'picture', 'status', 'reputation', 'postcount'] , function(err, data) { if(err) { return callback(err); }