NDOJ/templates/chat/chat.html

132 lines
4.5 KiB
HTML
Raw Normal View History

2020-01-27 20:37:52 +00:00
{% extends "base.html" %}
{% block js_media %}
2020-03-19 22:51:56 +00:00
<script src="{{ static('libs/jquery.waypoints.min.js')}}"></script>
<script src="{{ static('libs/infinite.min.js') }}"></script>
2020-01-27 20:37:52 +00:00
<script type="text/javascript">
var chatSocket = new WebSocket(
'ws://' + window.location.host +
'/ws/chat/');
</script>
<script type="text/javascript">
$(function() {
chatSocket.onmessage = function(e) {
2020-03-19 05:13:55 +00:00
let data = JSON.parse(e.data)
data = data['message']
2020-03-19 22:51:56 +00:00
loadMessage(data['body'],
data['author'],
data['time'],
2020-03-19 05:13:55 +00:00
data['image'])
2020-01-27 20:37:52 +00:00
};
2020-03-19 05:13:55 +00:00
function loadMessage(content, user, time, image) {
2020-03-19 22:51:56 +00:00
li = `<li class="infinite-item">
2020-03-19 05:13:55 +00:00
<img src="${image}" class="profile-pic">
<div class="body-message">
<div class="user-time">
<a href="#" class="user">
${user}
</a>
<span class="time">${time}</span>
</div>
<span class="message">
<span>${content}</span>
</span>
</div>
<div class="clear"></div>
</li>`
ul = $('#chat-log')
ul.append(li)
$('#chat-log').scrollTop($('#chat-log')[0].scrollHeight);
}
2020-03-19 22:51:56 +00:00
(function init_chatlog() {
ul = $('#chat-log')
{% for msg in message|reverse %}
loadMessage('{{msg.body}}', '{{msg.author}}', '12:00:00', '{{gravatar(msg.author, 32)}}')
{% endfor %}
})()
var infinite = new Waypoint.Infinite({
element: $('.infinite-container')[0],
onBeforePageLoad: function () {
$('.loading').show();
},
onAfterPageLoad: function ($items) {
$('.loading').hide();
}
});
2020-03-19 05:13:55 +00:00
$("#chat-submit").click(function() {
if ($("#chat-input").val().trim()) {
2020-03-19 22:51:56 +00:00
let body = $('#chat-input').val().trim();
2020-03-19 05:13:55 +00:00
let img = '{{ gravatar(request.user, 32) }}'
message = {
2020-03-19 22:51:56 +00:00
'body': body,
2020-03-19 05:13:55 +00:00
'image': img,
2020-03-19 22:51:56 +00:00
'author': '{{request.profile}}',
'author_id': {{request.profile.id}},
2020-03-19 05:13:55 +00:00
}
2020-03-19 22:51:56 +00:00
// $.post("send/", message)
2020-03-19 05:13:55 +00:00
chatSocket.send(JSON.stringify({
2020-03-19 22:51:56 +00:00
'message': message
}));
2020-03-19 05:13:55 +00:00
$('#chat-input').val('').focus();
}
});
2020-01-27 20:37:52 +00:00
chatSocket.onclose = function(e) {
console.error('Chat socket closed unexpectedly');
};
2020-03-19 05:13:55 +00:00
$("#chat-log").change(function() {
$('#chat-log').scrollTop($('#chat-log')[0].scrollHeight);
2020-01-27 20:37:52 +00:00
});
2020-03-19 05:13:55 +00:00
$('#chat-input').focus();
$('#chat-input').keydown(function(e) {
if (e.keyCode === 13) {
if (e.ctrlKey || e.shiftKey) {
var val = this.value;
if (typeof this.selectionStart == "number" && typeof this.selectionEnd == "number") {
var start = this.selectionStart;
this.value = val.slice(0, start) + "\n" + val.slice(this.selectionEnd);
this.selectionStart = this.selectionEnd = start + 1;
} else if (document.selection && document.selection.createRange) {
this.focus();
var range = document.selection.createRange();
range.text = "\r\n";
range.collapse(false);
range.select();
}
}
else {
e.preventDefault();
$('#chat-submit').click();
}
return false
}
return true
2020-01-27 20:37:52 +00:00
});
2020-03-19 05:13:55 +00:00
2020-01-27 20:37:52 +00:00
});
</script>
{% endblock js_media %}
{% block body %}
2020-03-19 05:13:55 +00:00
<div id="chat-area">
2020-03-19 22:51:56 +00:00
<ul id="chat-log" class="infinite-container">
2020-03-19 05:13:55 +00:00
</ul>
{{_('Your message')}}
<textarea rows="6" id="chat-input"></textarea>
2020-03-16 07:56:55 +00:00
</div>
2020-03-19 05:13:55 +00:00
<button id="chat-submit"> Send </button>
2020-03-19 22:51:56 +00:00
{% if page_obj.has_next %}
<a href="{{ request.path }}?page={{ message.next_page_number }}">next</a>
{% endif %}
2020-01-27 20:37:52 +00:00
{% endblock body %}