NDOJ/templates/chat/chat.html

337 lines
11 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-08-02 22:34:06 +00:00
{% block title %} {{_('Chat Box')}} {% endblock %}
2020-01-27 20:37:52 +00:00
{% block js_media %}
2020-06-08 20:11:07 +00:00
2020-05-17 18:35:32 +00:00
<script type="text/javascript" src="{{ static('mathjax_config.js') }}"></script>
2021-06-19 03:26:43 +00:00
<script type="text/javascript" src="{{ static('event.js') }}"></script>
2021-07-24 06:36:34 +00:00
<script src="https://unpkg.com/@popperjs/core@2"></script>
<script type="module" src="https://unpkg.com/emoji-picker-element@1"></script>
2020-01-27 20:37:52 +00:00
<script type="text/javascript">
2021-06-19 03:26:43 +00:00
window.currentPage = 1;
2021-07-24 06:36:34 +00:00
window.limit_time = 0;
window.messages_per_page = 50;
2020-03-21 04:48:04 +00:00
2021-06-19 03:26:43 +00:00
function load_page(page) {
$.get('?page=' + page)
.fail(function() {
console.log("Fail to load page " + page);
})
.done(function(data) {
setTimeout(function() {
let container = $('#chat-box');
let lastMsgPos = scrollTopOfBottom(container)
2021-07-24 06:36:34 +00:00
$('#loader').hide();
2021-06-19 03:26:43 +00:00
$('#chat-log').prepend(data);
2021-07-24 06:36:34 +00:00
$('.body-block').slice(0, window.messages_per_page).each(function() {
resize_emoji($(this));
});
register_time($('.time-with-rel'));
merge_authors();
2020-03-20 21:34:33 +00:00
2021-06-19 03:26:43 +00:00
container.scrollTop(scrollTopOfBottom(container) - lastMsgPos);
}, 500);
})
}
2020-03-19 05:13:55 +00:00
2021-06-19 03:26:43 +00:00
function scrollTopOfBottom(container) {
return container[0].scrollHeight - container.innerHeight()
}
2020-03-19 22:51:56 +00:00
2021-06-19 03:26:43 +00:00
function scrollContainer(container, loader) {
container.scroll(function() {
if (container.scrollTop() == 0) {
if (currentPage < {{paginator.num_pages}}) {
currentPage++;
loader.show();
load_page(currentPage);
}
2020-03-21 04:48:04 +00:00
}
2021-06-19 03:26:43 +00:00
})}
2020-03-21 04:48:04 +00:00
2021-06-19 03:26:43 +00:00
window.load_dynamic_update = function (last_msg) {
return new EventReceiver(
"{{ EVENT_DAEMON_LOCATION }}", "{{ EVENT_DAEMON_POLL_LOCATION }}",
['chat'], last_msg, function (message) {
switch (message.type) {
case 'new_message':
add_new_message(message.message);
break;
}
}
);
}
2020-03-21 04:48:04 +00:00
2021-06-19 03:26:43 +00:00
function add_new_message(message) {
$.get({
url: "{{ url('chat_message_ajax') }}",
data: {
message: message,
},
success: function (data) {
2021-07-24 06:36:34 +00:00
var $data = $(data);
resize_emoji($data.find('.body-block'));
$('#chat-log').append($data);
$('#chat-box').scrollTop($('#chat-box')[0].scrollHeight);
2021-06-19 03:26:43 +00:00
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
2021-07-24 06:36:34 +00:00
register_time($('.time-with-rel'));
merge_authors();
2021-06-19 03:26:43 +00:00
},
error: function (data) {
if (data.status === 403)
console.log('No right to see: ' + message);
else {
console.log('Could not load chat message:');
console.log(data.responseText);
2020-03-21 04:48:04 +00:00
}
2020-03-19 22:51:56 +00:00
}
2021-06-19 03:26:43 +00:00
});
}
2020-03-21 04:48:04 +00:00
2021-07-24 06:36:34 +00:00
function merge_authors() {
var time_limit = 5; // minutes
var last = {
username: null,
time: null,
$content: null
};
$('.body-message').each(function() {
var username = $(this).find(".username a").text().trim();
var $body = $(this).find(".content-message .body-block");
var time = moment($(this).find(".time-with-rel").attr('data-iso'));
var $content = $(this).children('.content-message');
if (username == last.username && time.diff(last.time, 'minutes') <= time_limit) {
last.$content.append($body);
$(this).parent().remove();
}
else {
last.username = username;
last.time = time;
last.$content = $content;
}
});
}
function submit_chat() {
if ($("#chat-input").val().trim()) {
var body = $('#chat-input').val().trim();
body = body.split('\n').join('\n\n');
var message = {
body: body,
};
$('#chat-input').val('');
$.post("{{ url('post_chat_message') }}", message)
.fail(function(res) {
console.log('Fail to send message');
})
.done(function(res, status) {
$('#chat-input').focus();
})
}
}
function resize_emoji(element) {
var html = element.html();
html = html.replace(/(\p{Extended_Pictographic})/ug, `<span class="big-emoji">$1</span>`);
element.html(html);
}
2021-07-26 20:46:27 +00:00
function insert_char_after_cursor(elem, char) {
var val = elem.value;
if (typeof elem.selectionStart == "number" && typeof elem.selectionEnd == "number") {
var start = elem.selectionStart;
var prefix = elem.value.slice(0, start);
var prefix_added = prefix + char;
var chars = [...val];
chars.splice([...prefix].length, 0, char);
elem.value = chars.join('');
elem.selectionStart = elem.selectionEnd = prefix_added.length;
} else if (document.selection && document.selection.createRange) {
var range = document.selection.createRange();
elem.focus();
range.text = char;
range.collapse(false);
range.select();
}
}
2021-06-19 03:26:43 +00:00
$(function() {
$('#loader').hide();
2021-07-24 06:36:34 +00:00
merge_authors();
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({
2021-06-19 03:26:43 +00:00
url: "{{ url('delete_chat_message') }}",
2020-05-05 18:17:42 +00:00
type: 'post',
2021-06-19 03:26:43 +00:00
data: {
message: elt.attr('value'),
},
2020-05-05 18:17:42 +00:00
dataType: 'json',
success: function(data){
2021-07-24 06:36:34 +00:00
var $block = elt.parent();
if ($block.parent().find('.body-block').length > 1) {
$block.remove();
}
else {
elt.closest('li').remove();
}
2021-06-17 00:39:09 +00:00
},
fail: function(data) {
alert('Fail to delete');
},
2020-05-05 18:17:42 +00:00
});
});
2020-05-17 18:01:59 +00:00
{% endif %}
2020-03-21 04:48:04 +00:00
2021-07-24 06:36:34 +00:00
$('.body-block').each(function() {
resize_emoji($(this));
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) {
2021-07-26 20:46:27 +00:00
insert_char_after_cursor(this, "\n");
2020-03-19 05:13:55 +00:00
}
else {
e.preventDefault();
2021-07-24 06:36:34 +00:00
submit_chat();
2020-03-19 05:13:55 +00:00
}
return false
}
return true
2020-01-27 20:37:52 +00:00
});
2020-03-19 05:13:55 +00:00
2020-06-08 20:11:07 +00:00
$('.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();
});
2021-06-19 03:26:43 +00:00
$('#refresh-button').on('click', function() {
$.get("{{url('online_status_ajax')}}")
.fail(function() {
console.log("Fail to get online status");
})
.done(function(data) {
if (data.status == 403) {
console.log("Fail to retrieve data");
}
else {
2021-07-02 06:38:48 +00:00
$('#chat-online-content').html(data).find('.toggle').each(function () {
register_toggle($(this));
});;
2021-06-19 03:26:43 +00:00
}
})
})
2021-06-19 06:09:16 +00:00
setInterval(function() {
$('#refresh-button').click();
}, 5 * 60 * 1000);
2021-06-19 03:26:43 +00:00
$('#chat-box').scrollTop($('#chat-box')[0].scrollHeight);
load_dynamic_update({{last_msg}});
2020-05-18 06:39:19 +00:00
2021-07-24 06:36:34 +00:00
const button = document.querySelector('#emoji-button')
const tooltip = document.querySelector('.tooltip')
Popper.createPopper(button, tooltip)
2020-06-08 23:19:09 +00:00
2021-07-24 06:36:34 +00:00
function toggleEmoji() {
tooltip.classList.toggle('shown')
2021-07-02 06:38:48 +00:00
}
2021-07-24 06:36:34 +00:00
$('#emoji-button').on('click', function(e) {
e.preventDefault();
toggleEmoji();
});
2021-07-02 06:38:48 +00:00
2021-07-24 06:36:34 +00:00
$('emoji-picker').on('emoji-click', function(e) {
2021-07-26 20:46:27 +00:00
var $chat = $('#chat-input').get(0);
insert_char_after_cursor($chat, e.detail.unicode);
$chat.focus();
2021-07-24 06:36:34 +00:00
})
2021-07-02 17:03:06 +00:00
2021-07-24 06:36:34 +00:00
document.addEventListener('keydown', function(e) {
if (e.keyCode === 27 && $('.tooltip').hasClass('shown')) {
toggleEmoji();
2021-07-02 17:03:06 +00:00
}
2021-07-24 06:36:34 +00:00
})
});
</script>
2021-07-08 22:09:30 +00:00
2021-07-24 06:36:34 +00:00
{% endblock js_media %}
2020-06-10 18:31:53 +00:00
2021-07-24 06:36:34 +00:00
{% block media %}
{% include "chat/chat_css.html" %}
{% endblock media %}
{% block footer %}{% endblock %}
2020-01-27 20:37:52 +00:00
{% block body %}
2020-06-08 20:11:07 +00:00
<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-online" class="chat-right-panel sidebox">
2021-07-02 17:03:06 +00:00
<h3 style="display:flex">
2021-06-19 03:26:43 +00:00
{{_('Online Users')}}
2021-07-02 17:03:06 +00:00
<button id="refresh-button" title="{{_('Refresh')}}">
<img src="/reload.png"
width="100%"
>
</button>
2020-06-08 20:11:07 +00:00
</h3>
2021-07-02 06:55:45 +00:00
<div id="chat-online-content">
2021-06-19 03:26:43 +00:00
{% include "chat/online_status.html" %}
2021-07-02 06:55:45 +00:00
</div>
2020-03-20 21:34:33 +00:00
</div>
2021-07-24 06:36:34 +00:00
<div id="chat-area" class="chat-left-panel" style="width:100%">
<div id="chat-box">
<img src="http://opengraphicdesign.com/wp-content/uploads/2009/01/loader64.gif" id="loader">
<ul id="chat-log">
{% include 'chat/message_list.html' %}
</ul>
</div>
<div style="height: 15%">
<a id="emoji-button" href="#" title="{{_('Emoji')}}"><i class="icofont-slightly-smile"></i></a>
<textarea id="chat-input" placeholder="{{_('Enter your message')}}"></textarea>
</div>
<div class="tooltip" role="tooltip">
<emoji-picker></emoji-picker>
</div>
</div>
2020-03-16 07:56:55 +00:00
</div>
2021-06-19 06:09:16 +00:00
{% endblock body %}