diff src/chat.js @ 10:f9e6a4cc4f7d

add Post
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 30 Oct 2024 23:18:45 -0600
parents
children 563a5358f2ee
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/chat.js	Wed Oct 30 23:18:45 2024 -0600
@@ -0,0 +1,48 @@
+'use strict';
+
+let currentChatId = null;
+
+function selectChat(div) {
+	let chatId = div.getAttribute('chat');
+	if( chatId === currentChatId )
+		return;
+	let selected = div.parentNode.querySelector('[selected]');
+	if( selected )  selected.removeAttribute('selected');
+	div.setAttribute('selected','');
+	ajax(`get_chat.js?chat=${chatId}`);
+	currentChatId = chatId;
+}
+
+function fixTextarea(event) {
+	let textarea = event.target;
+	textarea.style.height = 'initial';
+	textarea.style.height = (textarea.scrollHeight+2) + 'px';
+	textarea.scrollIntoViewIfNeeded(false);
+}
+
+const isMobile = 'ontouchstart' in window || navigator.maxTouchPoints > 0 || navigator.msMaxTouchPoints > 0;
+
+function addPost() {
+	let textarea = document.querySelector('div[input] textarea');
+	let text = textarea.value;
+	if( text.trim() === '' )
+		return;
+	ajax(`add_post.js?chat=${currentChatId}&content=${encodeURIComponent(text)}`);
+	textarea.value = '';
+	console.log('addPost');
+}
+
+function textareaKey(event) {
+	if( event.keyCode===13 && !event.shiftKey && !event.ctrlKey && !isMobile ) {
+		event.preventDefault();
+		addPost();
+	}
+}
+
+function fixDates() {
+	let spans = document.querySelectorAll('span[when][fix]');
+	for( let span of spans ) {
+		span.textContent = new Date(Number(span.textContent)).toLocaleString();
+		span.removeAttribute('fix');
+	}
+}