2014-07-29 23:49:45 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
/* globals define, socket, app, ajaxify, templates, translator*/
|
|
|
|
|
|
2014-05-29 17:24:38 -04:00
|
|
|
define('forum/users', function() {
|
2013-10-03 15:04:25 -04:00
|
|
|
var Users = {};
|
2013-09-17 13:05:54 -04:00
|
|
|
|
2014-07-29 23:49:45 -04:00
|
|
|
var loadingMoreUsers = false;
|
2013-09-17 13:05:54 -04:00
|
|
|
|
2014-07-29 23:49:45 -04:00
|
|
|
Users.init = function() {
|
2013-10-09 17:16:07 -04:00
|
|
|
|
|
|
|
|
var active = getActiveSection();
|
2013-06-27 14:38:45 -04:00
|
|
|
|
2014-02-26 21:55:29 -05:00
|
|
|
$('.nav-pills li').removeClass('active');
|
|
|
|
|
$('.nav-pills li a').each(function() {
|
|
|
|
|
var $this = $(this);
|
|
|
|
|
if ($this.attr('href').match(active)) {
|
|
|
|
|
$this.parent().addClass('active');
|
2013-06-27 14:38:45 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-09-17 13:05:54 -04:00
|
|
|
|
2014-07-29 23:49:45 -04:00
|
|
|
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;
|
|
|
|
|
|
2014-02-26 21:55:29 -05:00
|
|
|
$('#search-user').on('keyup', function() {
|
2013-09-17 13:05:54 -04:00
|
|
|
if (timeoutId !== 0) {
|
2013-06-27 14:38:45 -04:00
|
|
|
clearTimeout(timeoutId);
|
|
|
|
|
timeoutId = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
timeoutId = setTimeout(function() {
|
2014-06-23 19:10:59 -04:00
|
|
|
function reset() {
|
|
|
|
|
notify.html('<i class="fa fa-search"></i>');
|
|
|
|
|
notify.parent().removeClass('btn-warning label-warning btn-success label-success');
|
|
|
|
|
}
|
2013-06-27 14:38:45 -04:00
|
|
|
var username = $('#search-user').val();
|
2014-06-23 19:10:59 -04:00
|
|
|
var notify = $('#user-notfound-notify');
|
2013-09-17 13:05:54 -04:00
|
|
|
|
2014-06-23 19:10:59 -04:00
|
|
|
if (username === '') {
|
|
|
|
|
notify.html('<i class="fa fa-circle-o"></i>');
|
|
|
|
|
notify.parent().removeClass('btn-warning label-warning btn-success label-success');
|
2013-08-24 01:11:50 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2013-09-17 13:05:54 -04:00
|
|
|
|
2014-06-23 19:10:59 -04:00
|
|
|
if (lastSearch === username) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-08-24 01:11:50 +08:00
|
|
|
lastSearch = username;
|
2013-09-17 13:05:54 -04:00
|
|
|
|
2014-06-23 19:10:59 -04:00
|
|
|
notify.html('<i class="fa fa-spinner fa-spin"></i>');
|
2013-08-24 01:11:50 +08:00
|
|
|
|
2014-04-18 12:46:46 -04:00
|
|
|
socket.emit('user.search', username, function(err, data) {
|
2014-06-23 19:10:59 -04:00
|
|
|
if (err) {
|
|
|
|
|
reset();
|
|
|
|
|
return app.alertError(err.message);
|
2014-04-18 12:46:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!data) {
|
2014-06-23 19:10:59 -04:00
|
|
|
reset();
|
2014-04-18 12:46:46 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ajaxify.loadTemplate('users', function(usersTemplate) {
|
|
|
|
|
var html = templates.parse(templates.getBlock(usersTemplate, 'users'), data);
|
|
|
|
|
|
|
|
|
|
translator.translate(html, function(translated) {
|
|
|
|
|
$('#users-container').html(translated);
|
2014-06-23 19:10:59 -04:00
|
|
|
if (!data.users.length) {
|
|
|
|
|
translator.translate('[[users:user-not-found]]', function(translated) {
|
|
|
|
|
notify.html(translated);
|
|
|
|
|
notify.parent().addClass('btn-warning label-warning');
|
|
|
|
|
});
|
2014-04-18 12:46:46 -04:00
|
|
|
} else {
|
2014-06-23 19:10:59 -04:00
|
|
|
translator.translate('[[users:users-found-search-took, ' + data.users.length + ', ' + data.timing + ']]', function(translated) {
|
|
|
|
|
notify.html(translated);
|
|
|
|
|
notify.parent().addClass('btn-success label-success');
|
|
|
|
|
});
|
2014-04-18 12:46:46 -04:00
|
|
|
}
|
2014-03-09 22:35:09 -04:00
|
|
|
});
|
2014-01-08 22:53:55 -05:00
|
|
|
});
|
2014-04-18 12:46:46 -04:00
|
|
|
});
|
2013-09-17 13:05:54 -04:00
|
|
|
|
2013-06-27 14:38:45 -04:00
|
|
|
}, 250);
|
|
|
|
|
});
|
2014-07-29 23:49:45 -04:00
|
|
|
}
|
2013-09-17 13:05:54 -04:00
|
|
|
|
2014-07-29 23:49:45 -04:00
|
|
|
function onUserIsOnline(err, data) {
|
|
|
|
|
var section = getActiveSection();
|
|
|
|
|
if((section.indexOf('online') === 0 || section.indexOf('users') === 0) && !loadingMoreUsers) {
|
|
|
|
|
updateUser(data);
|
|
|
|
|
updateAnonCount();
|
2014-02-06 16:32:11 -05:00
|
|
|
}
|
2014-07-29 23:49:45 -04:00
|
|
|
}
|
2014-02-06 16:32:11 -05:00
|
|
|
|
2014-07-29 23:49:45 -04:00
|
|
|
function updateUser(data) {
|
|
|
|
|
var userEl = $('#users-container li[data-uid="' + data.uid +'"]');
|
|
|
|
|
if (!data.online) {
|
|
|
|
|
userEl.remove();
|
|
|
|
|
} else {
|
2014-03-28 13:29:51 -04:00
|
|
|
ajaxify.loadTemplate('users', function(usersTemplate) {
|
2014-07-29 23:49:45 -04:00
|
|
|
var html = templates.parse(templates.getBlock(usersTemplate, 'users'), {users: [data]});
|
2014-03-09 22:35:09 -04:00
|
|
|
translator.translate(html, function(translated) {
|
2014-07-29 23:49:45 -04:00
|
|
|
if (!userEl.length) {
|
|
|
|
|
$('#users-container').append(translated);
|
|
|
|
|
} else {
|
|
|
|
|
userEl.replaceWith(translated);
|
2014-03-09 22:35:09 -04:00
|
|
|
}
|
|
|
|
|
});
|
2014-02-23 21:50:02 -05:00
|
|
|
});
|
2013-08-12 14:32:56 -04:00
|
|
|
}
|
2014-07-29 23:49:45 -04:00
|
|
|
}
|
2013-09-17 13:05:54 -04:00
|
|
|
|
2014-07-29 23:49:45 -04:00
|
|
|
function updateAnonCount() {
|
|
|
|
|
var section = getActiveSection();
|
|
|
|
|
if((section.indexOf('online') === 0 || section.indexOf('users') === 0) && !loadingMoreUsers) {
|
|
|
|
|
socket.emit('user.getOnlineAnonCount', {} , function(err, anonCount) {
|
2013-09-17 13:05:54 -04:00
|
|
|
|
2014-07-29 23:49:45 -04:00
|
|
|
if(parseInt(anonCount, 10) > 0) {
|
|
|
|
|
$('#users-container .anon-user').removeClass('hide');
|
|
|
|
|
$('#online_anon_count').html(anonCount);
|
2013-09-23 13:43:15 -04:00
|
|
|
} else {
|
2014-07-29 23:49:45 -04:00
|
|
|
$('#users-container .anon-user').addClass('hide');
|
2013-09-23 13:43:15 -04:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2014-07-29 23:49:45 -04:00
|
|
|
}
|
2013-09-23 13:43:15 -04:00
|
|
|
|
2014-07-29 23:49:45 -04:00
|
|
|
function getActiveSection() {
|
|
|
|
|
var url = window.location.href,
|
|
|
|
|
parts = url.split('/');
|
|
|
|
|
return parts[parts.length - 1];
|
|
|
|
|
}
|
2013-05-29 12:17:44 -04:00
|
|
|
|
2013-10-03 15:04:25 -04:00
|
|
|
return Users;
|
2014-04-10 20:31:57 +01:00
|
|
|
});
|