NDOJ/templates/chat/chat.html

257 lines
9.3 KiB
HTML
Raw Normal View History

2020-01-27 20:37:52 +00:00
{% extends "base.html" %}
2020-06-08 20:11:07 +00:00
{% block title_row %}{% endblock %}
{% block title_ruler %}{% endblock %}
2020-01-27 20:37:52 +00:00
{% block js_media %}
2020-06-08 20:11:07 +00:00
2020-01-27 20:37:52 +00:00
<script type="text/javascript">
var chatSocket = new WebSocket(
'ws://' + window.location.host +
'/ws/chat/');
</script>
2020-05-17 18:35:32 +00:00
<script type="text/javascript" src="{{ static('mathjax_config.js') }}"></script>
2020-05-18 06:39:19 +00:00
<script type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS_HTML"></script>
2020-01-27 20:37:52 +00:00
<script type="text/javascript">
$(function() {
2020-03-21 04:48:04 +00:00
let currentPage = 1;
2020-03-20 21:34:33 +00:00
$('#loader').hide();
2020-01-27 20:37:52 +00:00
chatSocket.onmessage = function(e) {
2020-05-17 18:01:59 +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-05-06 04:51:16 +00:00
data['id'],
2020-03-21 04:48:04 +00:00
data['image'],
2020-05-23 23:52:19 +00:00
data['css_class'],
2020-05-17 18:01:59 +00:00
true);
// console.log(data);
2020-03-21 04:48:04 +00:00
$('#chat-box').scrollTop($('#chat-box')[0].scrollHeight);
2020-01-27 20:37:52 +00:00
};
2020-03-21 04:48:04 +00:00
function encodeHTML(content) {
return content.replace(/[\u00A0-\u9999<>\&]/gim, function(i) {
return '&#'+i.charCodeAt(0)+';';
});
}
2020-03-19 05:13:55 +00:00
2020-05-23 23:52:19 +00:00
function loadMessage(content, user, time, messid, image, css_class, isNew) {
2020-05-17 18:01:59 +00:00
// if (isNew) content = encodeHTML(content)
2020-05-23 23:52:19 +00:00
time = new Date(time);
time = moment(time).format("HH:mm DD-MM-YYYY");
2020-05-17 18:01:59 +00:00
content = encodeHTML(content);
2020-03-21 04:48:04 +00:00
li = `<li class="message">
2020-03-19 05:13:55 +00:00
<img src="${image}" class="profile-pic">
<div class="body-message">
<div class="user-time">
2020-05-23 23:52:19 +00:00
<span class="${css_class}">
<a href="{{ url('user_page') }}/${user}" class="user">
${user}
</a>
</span>
2020-03-19 05:13:55 +00:00
<span class="time">${time}</span>
2020-05-05 18:17:42 +00:00
{% if request.user.is_staff %}
<a class="chatbtn_remove_mess" style="color:red; cursor: pointer;" data-messtime="${time}" data-author="${user}" data-messid="${messid}">Delete</a>
2020-05-05 18:17:42 +00:00
{% endif %}
2020-03-19 05:13:55 +00:00
</div>
2020-03-21 04:48:04 +00:00
<span class="content-message">${content} </span>
2020-03-19 05:13:55 +00:00
</div>
<div class="clear"></div>
</li>`
ul = $('#chat-log')
2020-03-21 04:48:04 +00:00
if (isNew) {
ul.append(li)
}
else {
ul.prepend(li)
}
2020-05-18 06:39:19 +00:00
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
2020-03-19 05:13:55 +00:00
}
2020-03-19 22:51:56 +00:00
(function init_chatlog() {
ul = $('#chat-log')
2020-05-17 18:01:59 +00:00
{% autoescape on %}
2020-03-21 04:48:04 +00:00
{% for msg in message %}
2020-05-23 23:52:19 +00:00
loadMessage("{{msg.body|safe|escapejs}}", `{{msg.author}}`, `{{msg.time}}`, `{{msg.id}}`, `{{gravatar(msg.author, 32)}}`,`{{msg.author.css_class}}`);
2020-03-19 22:51:56 +00:00
{% endfor %}
2020-05-17 18:01:59 +00:00
{% endautoescape %}
2020-03-21 04:48:04 +00:00
$('#chat-box').scrollTop($('#chat-box')[0].scrollHeight);
2020-03-19 22:51:56 +00:00
})()
2020-03-21 04:48:04 +00:00
function scrollTopOfBottom(container) {
return container[0].scrollHeight - container.innerHeight()
}
function scrollContainer(container, loader) {
container.scroll(function() {
if (container.scrollTop() == 0) {
if (currentPage < {{paginator.num_pages}}) {
currentPage++;
loader.show();
$.ajax({
url: `{{request.path}}?page=${currentPage}`,
success: function(data) {
let lastMsg = $('.message:first')
let lastMsgPos = scrollTopOfBottom(container)
data = JSON.parse(data)
setTimeout( () => {
for (msg of data) {
2020-05-23 23:52:19 +00:00
loadMessage(msg.body, msg.author, msg.time, msg.id, msg.image, msg.css_class)
2020-03-21 04:48:04 +00:00
}
loader.hide()
// scroll to last msg
container.scrollTop(
scrollTopOfBottom(container) - lastMsgPos
)
}, 500)
}
})
}
2020-03-19 22:51:56 +00:00
}
2020-03-21 04:48:04 +00:00
})}
scrollContainer($('#chat-box'), $('#loader'))
2020-03-19 05:13:55 +00:00
2020-05-17 18:01:59 +00:00
{% if request.user.is_staff %}
2020-05-05 18:17:42 +00:00
$(document).on("click", ".chatbtn_remove_mess", function() {
var elt = $(this);
$.ajax({
url: 'delete/',
type: 'post',
data: elt.data(),
dataType: 'json',
success: function(data){
console.log('delete ajax call success!');
location.reload();
}
});
});
2020-05-17 18:01:59 +00:00
{% endif %}
2020-03-21 04:48:04 +00:00
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-05-23 23:52:19 +00:00
'css_class': '{{request.profile.css_class}}',
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
});
2020-06-08 20:11:07 +00:00
$(document).ready(function () {
$('.chat-right-panel').hide();
$('#chat-tab').find('a').click(function (e) {
e.preventDefault();
$('#chat-tab').addClass('active');
$('#online-tab').removeClass('active');
$('.chat-left-panel').show();
$('.chat-right-panel').hide();
});
$('#online-tab').find('a').click(function (e) {
e.preventDefault();
$('#online-tab').addClass('active');
$('#chat-tab').removeClass('active');
$('.chat-left-panel').hide();
$('.chat-right-panel').show();
});
});
2020-01-27 20:37:52 +00:00
</script>
2020-05-18 06:39:19 +00:00
2020-01-27 20:37:52 +00:00
{% endblock js_media %}
{% block body %}
2020-06-08 20:11:07 +00:00
{% csrf_token %}
{% block before_posts %}{% endblock %}
<div id="mobile" class="tabs">
<ul>
<li id="chat-tab" class="tab active"><a href="#">
<i class="tab-icon fa fa-comments"></i> {{ _('Chat') }}
</a></li>
<li id="online-tab" class="tab"><a href="#"><i class="tab-icon fa fa-wifi"></i> {{ _('Online Users') }}</a></li>
</ul>
</div>
<div id="chat-container">
<div id="chat-area" class="chat-left-panel">
<div id="chat-box">
<img src="http://opengraphicdesign.com/wp-content/uploads/2009/01/loader64.gif" id="loader">
<ul id="chat-log">
</ul>
</div>
<textarea id="chat-input" placeholder="{{_('Enter your message')}}"></textarea>
</div>
<button id="chat-submit" style="display:none;"> Send </button>
<div id="chat-online" class="chat-right-panel sidebox">
<h3>
{{_('Online Users')}}
<i class="fa fa-wifi"></i>
</h3>
<ul id="chat-online-content">
{% for user in online_users %}
<li>
<span class="dot"></span>
<span style="padding-left:0.25em">
{{ link_user(user) }}
</span>
</li>
{% endfor %}
2020-03-20 21:34:33 +00:00
</ul>
</div>
2020-03-16 07:56:55 +00:00
</div>
2020-06-08 20:11:07 +00:00
2020-01-27 20:37:52 +00:00
{% endblock body %}