diff --git a/chat/static/chat.js b/chat/static/chat.js index 9295de2..dcef533 100644 --- a/chat/static/chat.js +++ b/chat/static/chat.js @@ -184,8 +184,12 @@ function redrawMessages() { let newTimestamp = new Date(message['timestamp']) if ((newTimestamp - lastTimestamp) / 1000 < 60 * 10) { let messageContentDiv = document.createElement('div') - messageContentDiv.innerText = message['content'] lastContentDiv.appendChild(messageContentDiv) + let messageContentSpan = document.createElement('span') + messageContentSpan.innerText = message['content'] + messageContentDiv.appendChild(messageContentSpan) + + registerMessageContextMenu(message, messageContentSpan) continue } } @@ -202,25 +206,7 @@ function redrawMessages() { authorSpan.innerText = message['author'] authorDiv.appendChild(authorSpan) - authorSpan.addEventListener('contextmenu', (menu_event) => { - menu_event.preventDefault() - const popover = bootstrap.Popover.getOrCreateInstance(authorSpan, { - 'title': message['author'], - 'content': `Envoyer un message privé`, - 'html': true, - 'placement': "bottom", - }) - popover.show() - - document.getElementById('send-private-message-link-' + message['id']).addEventListener('click', event => { - event.preventDefault() - popover.hide() - socket.send(JSON.stringify({ - 'type': 'start_private_chat', - 'user_id': message['author_id'], - })) - }) - }) + registerSendPrivateMessageContextMenu(message, authorSpan) let dateSpan = document.createElement('span') dateSpan.classList.add('text-muted', 'float-end') @@ -231,8 +217,12 @@ function redrawMessages() { messageElement.appendChild(contentDiv) let messageContentDiv = document.createElement('div') - messageContentDiv.innerText = message['content'] contentDiv.appendChild(messageContentDiv) + let messageContentSpan = document.createElement('span') + messageContentSpan.innerText = message['content'] + messageContentDiv.appendChild(messageContentSpan) + + registerMessageContextMenu(message, messageContentSpan) lastMessage = message lastContentDiv = contentDiv @@ -245,6 +235,52 @@ function redrawMessages() { fetchMoreButton.classList.remove('d-none') } +function registerSendPrivateMessageContextMenu(message, element) { + element.addEventListener('contextmenu', (menu_event) => { + menu_event.preventDefault() + const popover = bootstrap.Popover.getOrCreateInstance(element, { + 'title': message['author'], + 'content': `Envoyer un message privé`, + 'html': true, + }) + popover.show() + + document.getElementById('send-private-message-link-' + message['id']).addEventListener('click', event => { + event.preventDefault() + popover.hide() + socket.send(JSON.stringify({ + 'type': 'start_private_chat', + 'user_id': message['author_id'], + })) + }) + }) +} + +function registerMessageContextMenu(message, element) { + element.addEventListener('contextmenu', (menu_event) => { + menu_event.preventDefault() + let content = `Envoyer un message privé` + content += `