diff --git a/public/src/client/chats.js b/public/src/client/chats.js index 58e5afe904..2df0601dd3 100644 --- a/public/src/client/chats.js +++ b/public/src/client/chats.js @@ -29,7 +29,7 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll', Chats.scrollToBottom($('.expanded-chat ul')); Chats.initialised = true; - + Chats.handleSearch(); if (ajaxify.data.hasOwnProperty('roomId')) { @@ -535,71 +535,70 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll', callback(); }); } - + Chats.handleSearch = function() { var timeoutId = 0; - + components.get('chat/search').on('keyup', function() { if (timeoutId) { clearTimeout(timeoutId); timeoutId = 0; } - timeoutId = setTimeout(doSearch, 250); + timeoutId = setTimeout(doSearch, 250); }); - + function doSearch() { - var username = components.get('chat/search').val(); - var chatsListEl = $('[component="chat/search/list"]'); + var username = components.get('chat/search').val(); + var chatsListEl = $('[component="chat/search/list"]'); if (!username) { - return chatsListEl.empty(); + return chatsListEl.empty(); } - socket.emit('user.search', { - query: username, - searchBy: 'username' - }, function(err, data) { - if (err) { - return app.alertError(err.message); - } - - chatsListEl.empty(); - - if (data.users.length === 0) { - chatsListEl.translateHtml('
  • [[users:no-users-found]]
  • '); - } else { - data.users.forEach(function(userObj) { - function createUserImage() { - return (userObj.picture ? - '' : - '
    ' + userObj['icon:text'] + '
    ') + - ' ' + userObj.username; - } - - var chatEl = $('
  • ') + socket.emit('user.search', { + query: username, + searchBy: 'username' + }, function(err, data) { + if (err) { + return app.alertError(err.message); + } + + chatsListEl.empty(); + + if (data.users.length === 0) { + return chatsListEl.translateHtml('
  • [[users:no-users-found]]
  • '); + } + + data.users.forEach(function(userObj) { + function createUserImage() { + return (userObj.picture ? + '' : + '
    ' + userObj['icon:text'] + '
    ') + + ' ' + userObj.username; + } + + var chatEl = $('
  • ') .attr('data-uid', userObj.uid) .appendTo(chatsListEl); - - chatEl.append(createUserImage()); - - chatEl.click(function() { - socket.emit('modules.chats.hasPrivateChat', userObj.uid, function(err, roomId) { - if (err) { - return app.alertError(err.message); - } - if (roomId) { - ajaxify.go('chats/' + roomId); - } else { - app.newChat(userObj.uid); - } - }); - }); - }); - } - - }); - } + + chatEl.append(createUserImage()); + + chatEl.on('click', function() { + socket.emit('modules.chats.hasPrivateChat', userObj.uid, function(err, roomId) { + if (err) { + return app.alertError(err.message); + } + if (roomId) { + ajaxify.go('chats/' + roomId); + } else { + app.newChat(userObj.uid); + } + }); + }); + }); + }); + } }; return Chats; });