mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-06 05:55:48 +01:00
getting chats working between both page and modal, yay #1788
This commit is contained in:
@@ -1,21 +1,27 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
/* globals define, app, ajaxify, utils, socket */
|
/* globals define, app, ajaxify, utils, socket, templates */
|
||||||
|
|
||||||
define('forum/chats', ['string', 'sounds'], function(S, sounds) {
|
define('forum/chats', ['string', 'sounds'], function(S, sounds) {
|
||||||
var Chats = {};
|
var Chats = {
|
||||||
|
initialised: false
|
||||||
|
};
|
||||||
|
|
||||||
Chats.init = function() {
|
Chats.init = function() {
|
||||||
var containerEl = $('.expanded-chat ul');
|
var containerEl = $('.expanded-chat ul');
|
||||||
|
|
||||||
Chats.addEventListeners();
|
if (!Chats.initialised) {
|
||||||
Chats.addSocketListeners();
|
Chats.addSocketListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
Chats.addEventListeners();
|
||||||
Chats.scrollToBottom(containerEl);
|
Chats.scrollToBottom(containerEl);
|
||||||
Chats.setActive();
|
Chats.setActive();
|
||||||
|
|
||||||
|
Chats.initialised = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
Chats.getRecipientUid = function() {
|
Chats.getRecipientUid = function() {
|
||||||
console.log($('.expanded-chat'));
|
|
||||||
return parseInt($('.expanded-chat').attr('data-uid'), 10);
|
return parseInt($('.expanded-chat').attr('data-uid'), 10);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -33,17 +39,16 @@ define('forum/chats', ['string', 'sounds'], function(S, sounds) {
|
|||||||
sendEl = $('.expanded-chat button[data-action="send"]');
|
sendEl = $('.expanded-chat button[data-action="send"]');
|
||||||
|
|
||||||
$('.chats-list').on('click', 'li', function(e) {
|
$('.chats-list').on('click', 'li', function(e) {
|
||||||
// app.openChat($(this).attr('data-username'), $(this).attr('data-uid'));
|
|
||||||
ajaxify.go('chats/' + utils.slugify($(this).attr('data-username')));
|
ajaxify.go('chats/' + utils.slugify($(this).attr('data-username')));
|
||||||
});
|
});
|
||||||
|
|
||||||
inputEl.off('keypress').on('keypress', function(e) {
|
inputEl.on('keypress', function(e) {
|
||||||
if(e.which === 13) {
|
if(e.which === 13) {
|
||||||
Chats.sendMessage(Chats.getRecipientUid(), inputEl);
|
Chats.sendMessage(Chats.getRecipientUid(), inputEl);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
inputEl.off('keyup').on('keyup', function() {
|
inputEl.on('keyup', function() {
|
||||||
if ($(this).val()) {
|
if ($(this).val()) {
|
||||||
Chats.notifyTyping(Chats.getRecipientUid(), true);
|
Chats.notifyTyping(Chats.getRecipientUid(), true);
|
||||||
} else {
|
} else {
|
||||||
@@ -51,29 +56,45 @@ define('forum/chats', ['string', 'sounds'], function(S, sounds) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
sendEl.off('click').on('click', function(e) {
|
sendEl.on('click', function(e) {
|
||||||
Chats.sendMessage(Chats.getRecipientUid(), inputEl);
|
Chats.sendMessage(Chats.getRecipientUid(), inputEl);
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Chats.addSocketListeners = function() {
|
Chats.addSocketListeners = function() {
|
||||||
var typingNotifEl = $('.user-typing');
|
var typingNotifEl = $('.user-typing'),
|
||||||
|
containerEl = $('.expanded-chat ul');
|
||||||
|
|
||||||
socket.on('event:chats.receive', function(data) {
|
socket.on('event:chats.receive', function(data) {
|
||||||
|
if (Chats.isCurrentChat(data.withUid)) {
|
||||||
|
Chats.parseMessage(data.message, function(html) {
|
||||||
|
var newMessage = $(html);
|
||||||
|
newMessage.insertBefore(typingNotifEl);
|
||||||
|
newMessage.find('span.timeago').timeago();
|
||||||
|
newMessage.find('img:not(".chat-user-image")').addClass('img-responsive');
|
||||||
|
Chats.scrollToBottom(containerEl);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
$('.chats-list li[data-uid="' + data.withUid + '"]').addClass('unread');
|
||||||
|
app.alternatingTitle('[[modules:chat.user_has_messaged_you, ' + data.message.username + ']]');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('event:chats.userStartTyping', function(withUid) {
|
socket.on('event:chats.userStartTyping', function(withUid) {
|
||||||
if (Chats.isCurrentChat(withUid)) {
|
if (Chats.isCurrentChat(withUid)) {
|
||||||
typingNotifEl.removeClass('hide');
|
typingNotifEl.removeClass('hide');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$('.chats-list li[data-uid="' + withUid + '"]').addClass('typing');
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('event:chats.userStopTyping', function(withUid) {
|
socket.on('event:chats.userStopTyping', function(withUid) {
|
||||||
if (Chats.isCurrentChat(withUid)) {
|
if (Chats.isCurrentChat(withUid)) {
|
||||||
typingNotifEl.addClass('hide');
|
typingNotifEl.addClass('hide');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$('.chats-list li[data-uid="' + withUid + '"]').removeClass('typing');
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -112,5 +133,11 @@ define('forum/chats', ['string', 'sounds'], function(S, sounds) {
|
|||||||
$('.chats-list li[data-uid="' + Chats.getRecipientUid() + '"]').addClass('bg-primary');
|
$('.chats-list li[data-uid="' + Chats.getRecipientUid() + '"]').addClass('bg-primary');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Chats.parseMessage = function(data, callback) {
|
||||||
|
templates.parse('partials/chat_message' + (Array.isArray(data) ? 's' : ''), {
|
||||||
|
messages: data
|
||||||
|
}, callback);
|
||||||
|
};
|
||||||
|
|
||||||
return Chats;
|
return Chats;
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -263,10 +263,8 @@ define('chat', ['taskbar', 'string', 'sounds', 'forum/chats'], function(taskbar,
|
|||||||
|
|
||||||
function getChatMessages(chatModal, callback) {
|
function getChatMessages(chatModal, callback) {
|
||||||
socket.emit('modules.chats.get', {touid:chatModal.touid}, function(err, messages) {
|
socket.emit('modules.chats.get', {touid:chatModal.touid}, function(err, messages) {
|
||||||
for(var i = 0; i<messages.length; ++i) {
|
console.log(messages);
|
||||||
module.appendChatMessage(chatModal, messages[i]);
|
module.appendChatMessage(chatModal, messages, callback);
|
||||||
}
|
|
||||||
callback();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -292,35 +290,18 @@ define('chat', ['taskbar', 'string', 'sounds', 'forum/chats'], function(taskbar,
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
module.appendChatMessage = function(chatModal, data) {
|
module.appendChatMessage = function(chatModal, data, done) {
|
||||||
var chatContent = chatModal.find('#chat-content');
|
var chatContent = chatModal.find('#chat-content');
|
||||||
|
|
||||||
var isYou = parseInt(app.uid, 10) === parseInt(data.fromuid, 10);
|
Chats.parseMessage(data, function(html) {
|
||||||
|
var message = $(html);
|
||||||
var message = $('<li class="chat-message clear" data-uid="' + data.fromuid + '"></li>');
|
|
||||||
var time = '<span class="chat-timestamp pull-right timeago" title="' + utils.toISOString(data.timestamp) + '"></span> ';
|
|
||||||
|
|
||||||
|
|
||||||
if (data.fromuid !== chatContent.children('.chat-message').last().attr('data-uid')) {
|
|
||||||
var userPicture = $('<a href="/user/' + data.fromUser.userslug + '"><img class="chat-user-image" src="' + data.fromUser.picture + '"></a>');
|
|
||||||
var userName = $('<strong><span class="chat-user"> '+ data.fromUser.username + '</span></strong>');
|
|
||||||
userName.toggleClass('chat-user-you', isYou);
|
|
||||||
|
|
||||||
message.append(userPicture)
|
|
||||||
.append(userName)
|
|
||||||
.append('<br/>')
|
|
||||||
.prepend(time);
|
|
||||||
}
|
|
||||||
|
|
||||||
message.append(S(data.content).stripTags('p').s);
|
|
||||||
|
|
||||||
message.toggleClass('chat-message-them', !isYou);
|
|
||||||
message.find('img:not(".chat-user-image")').addClass('img-responsive');
|
message.find('img:not(".chat-user-image")').addClass('img-responsive');
|
||||||
message.find('span.timeago').timeago();
|
message.find('span.timeago').timeago();
|
||||||
|
|
||||||
chatContent.append(message);
|
chatContent.append(message);
|
||||||
|
|
||||||
Chats.scrollToBottom(chatContent);
|
Chats.scrollToBottom(chatContent);
|
||||||
|
|
||||||
|
if (typeof done === 'function') done();
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
module.toggleNew = function(uuid, state) {
|
module.toggleNew = function(uuid, state) {
|
||||||
|
|||||||
Reference in New Issue
Block a user