NDOJ/templates/chat/chat.html

137 lines
4.8 KiB
HTML
Raw Normal View History

2020-03-19 05:13:55 +00:00
{% if request.user.is_authenticated %}
2020-01-27 20:37:52 +00:00
{% extends "base.html" %}
{% block js_media %}
<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']
console.log(data)
loadMessage(data['content'],
data['sender'],
data['time'],
data['image'])
2020-01-27 20:37:52 +00:00
};
2020-03-19 05:13:55 +00:00
function loadMessage(content, user, time, image) {
li = `<li>
<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);
}
$("#chat-submit").click(function() {
if ($("#chat-input").val().trim()) {
let content = $('#chat-input').val().trim();
let img = '{{ gravatar(request.user, 32) }}'
message = {
'content': content,
'image': img,
'time': calcTime(6), // HCM City
'sender': '{{ request.user }}'
}
chatSocket.send(JSON.stringify({
'message': message,
}))
// $.post('/chat/send', message)
$('#chat-input').val('').focus();
}
});
function calcTime(offset) {
utc = new Date().getTime()
nd = new Date(utc + (3600000*offset));
return nd.toLocaleString();
}
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
{% csrf_token %}
<div id="chat-area">
<ul id="chat-log">
<li>
<img src="https://via.placeholder.com/150" class="profile-pic">
<div class="body-message">
<div class="user-time">
<a href="#" class="user">
cuom1999
</a>
<span class="time">12:00:00</span>
</div>
<span class="message">
<span>Its possible that a request can come in via POST with an empty POST dictionary if, say, a form is requested via the POST HTTP method but does not include form data. Therefore, you shouldnt use if request.POST to check for use of the POST method; instead, use if request.method == "POST" (see HttpRequest.method).</span>
</span>
</div>
<div class="clear"></div>
</li>
</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-01-27 20:37:52 +00:00
{% endblock body %}
2020-03-19 05:13:55 +00:00
{% endif %}