Commit f7c85346 authored by Thibaut Frain's avatar Thibaut Frain

auto scrolling to chat box bottom at new message

parent 88baa541
...@@ -4,13 +4,14 @@ ...@@ -4,13 +4,14 @@
(function($, rJS, Handlebars) { (function($, rJS, Handlebars) {
"use strict"; "use strict";
var gadget_klass = rJS(window), message_template_source = gadget_klass.__template_element.querySelector(".message-template").innerHTML, message_template = Handlebars.compile(message_template_source); var gadget_klass = rJS(window), message_template_source = gadget_klass.__template_element.querySelector(".message-template").innerHTML, message_template = Handlebars.compile(message_template_source);
function displayMessage(message) { function displayMessage(gadget, message) {
var html_message = message_template({ var talk_box = gadget.__element.querySelector(".talk-box"), html_message = message_template({
time: message.time, time: message.time,
jid: message.from, jid: message.from,
content: message.content content: message.content
}); });
$(".talk-box").append(html_message); $(talk_box).append(html_message);
talk_box.scrollTop = talk_box.scrollHeight;
} }
function Message(from, to, time, content) { function Message(from, to, time, content) {
this.from = from; this.from = from;
...@@ -45,7 +46,7 @@ ...@@ -45,7 +46,7 @@
_id: "chatbox_history", _id: "chatbox_history",
datas: JSON.stringify(gadget.props.talks) datas: JSON.stringify(gadget.props.talks)
}); });
displayMessage(message); displayMessage(gadget, message);
gadget.send($msg({ gadget.send($msg({
to: to, to: to,
type: "chat" type: "chat"
...@@ -78,7 +79,7 @@ ...@@ -78,7 +79,7 @@
if (this.props.talks[this.props.current_contact_jid]) { if (this.props.talks[this.props.current_contact_jid]) {
messages = this.props.talks[this.props.current_contact_jid].messages; messages = this.props.talks[this.props.current_contact_jid].messages;
messages.forEach(function(message) { messages.forEach(function(message) {
displayMessage(message); displayMessage(gadget, message);
}); });
} }
$(this.__element).find(".send-button").click(function(e) { $(this.__element).find(".send-button").click(function(e) {
...@@ -108,7 +109,7 @@ ...@@ -108,7 +109,7 @@
datas: JSON.stringify(this.props.talks) datas: JSON.stringify(this.props.talks)
}); });
if (this.props.current_contact_jid === from) { if (this.props.current_contact_jid === from) {
displayMessage(message); displayMessage(this, message);
} }
}); });
})($, rJS, Handlebars); })($, rJS, Handlebars);
\ No newline at end of file
...@@ -9,14 +9,16 @@ ...@@ -9,14 +9,16 @@
.querySelector(".message-template").innerHTML, .querySelector(".message-template").innerHTML,
message_template = Handlebars.compile(message_template_source); message_template = Handlebars.compile(message_template_source);
function displayMessage(message) { function displayMessage(gadget, message) {
var html_message = message_template({ var talk_box = gadget.__element.querySelector('.talk-box'),
html_message = message_template({
time: message.time, time: message.time,
jid: message.from, jid: message.from,
content: message.content content: message.content
}); });
$('.talk-box').append(html_message); $(talk_box).append(html_message);
talk_box.scrollTop = talk_box.scrollHeight;
} }
function Message(from, to, time, content) { function Message(from, to, time, content) {
...@@ -64,7 +66,7 @@ ...@@ -64,7 +66,7 @@
"_id": "chatbox_history", "_id": "chatbox_history",
"datas": JSON.stringify(gadget.props.talks) "datas": JSON.stringify(gadget.props.talks)
}); });
displayMessage(message); displayMessage(gadget, message);
gadget.send( gadget.send(
$msg({to: to, type: "chat"}).c('body').t(content).toString() $msg({to: to, type: "chat"}).c('body').t(content).toString()
); );
...@@ -109,7 +111,7 @@ ...@@ -109,7 +111,7 @@
if (this.props.talks[this.props.current_contact_jid]) { if (this.props.talks[this.props.current_contact_jid]) {
messages = this.props.talks[this.props.current_contact_jid].messages; messages = this.props.talks[this.props.current_contact_jid].messages;
messages.forEach(function (message) { messages.forEach(function (message) {
displayMessage(message); displayMessage(gadget, message);
}); });
} }
...@@ -157,7 +159,7 @@ ...@@ -157,7 +159,7 @@
}); });
if (this.props.current_contact_jid === from) { if (this.props.current_contact_jid === from) {
displayMessage(message); displayMessage(this, message);
} }
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment