changeset 35:27c41f22d2a9

improve fixPosts
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 12 Nov 2024 17:17:59 -0700
parents 62d04ca486dd
children 4e82d8f4dfc2
files src/chat.css src/chat.js src/index.html.luan src/lib/Shared.luan
diffstat 4 files changed, 29 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/src/chat.css	Tue Nov 12 16:08:24 2024 -0700
+++ b/src/chat.css	Tue Nov 12 17:17:59 2024 -0700
@@ -66,7 +66,8 @@
 
 span[pulldown] {
 	position: relative;
-	visibility: hidden;
+	width: 16px;
+	display: inline-block;
 }
 
 span[pulldown] > div {
--- a/src/chat.js	Tue Nov 12 16:08:24 2024 -0700
+++ b/src/chat.js	Tue Nov 12 17:17:59 2024 -0700
@@ -5,6 +5,7 @@
 let eventSource;
 let lastUpdate;
 let hasUnseen = false;
+let userId;
 
 function evalEvent(event) {
 	// console.log(event);
@@ -71,14 +72,14 @@
 }
 
 function fixPosts() {
-	let spans = document.querySelectorAll('span[when][fix]');
-	for( let span of spans ) {
-		span.textContent = new Date(Number(span.textContent)).toLocaleString();
-		span.removeAttribute('fix');
-	}
-	let divs = document.querySelectorAll('div[text][fix]');
+	let divs = document.querySelectorAll('div[post][fix]');
 	for( let div of divs ) {
-		div.innerHTML = urlsToLinks(div.innerHTML);
+		let whenSpan = div.querySelector('span[when]');
+		whenSpan.textContent = new Date(Number(whenSpan.textContent)).toLocaleString();
+		let textDiv = div.querySelector('div[text]');
+		textDiv.innerHTML = urlsToLinks(textDiv.innerHTML);
+		if( div.getAttribute('author') === userId )
+			div.querySelector('span[pulldown]').innerHTML = document.querySelector('div[hidden] span[pulldown]').innerHTML;
 		div.removeAttribute('fix');
 	}
 }
--- a/src/index.html.luan	Tue Nov 12 16:08:24 2024 -0700
+++ b/src/index.html.luan	Tue Nov 12 17:17:59 2024 -0700
@@ -85,6 +85,7 @@
 			selected = get_chat(with)
 		end
 	end
+	local user_id = user.id
 	Io.stdout = Http.response.text_writer()
 %>
 <!doctype html>
@@ -93,10 +94,6 @@
 <%		head() %>
 		<style>
 			@import "chat.css?s=<%=started%>";
-
-			div[who="<%=user.id%>"] span[pulldown] {
-				visibility: visible;
-			}
 		</style>
 		<style online></style>
 		<script src="chat.js?s=<%=started%>"></script>
@@ -111,6 +108,15 @@
 				<h3 intro>Choose a chat on the left</h3>
 			</div>
 		</div>
+		<div hidden>
+			<span pulldown>
+				<img onclick="clickMenu(this)" src="/images/more_vert.svg">
+				<div>
+					<span onclick="editPost('<%=user_id%>')">Edit</span>
+					<span onclick="deletePost('<%=user_id%>')">Delete</span>
+				</div>
+			</span>
+		</div>
 		<dialog delete_chat>
 			<h2>Delete Chat</h2>
 			<p>Are you sure that you want to delete this chat?</p>
@@ -136,6 +142,8 @@
 			</div>
 		</dialog>
 		<script>
+			'use strict';
+
 <%
 	if selected ~= nil then
 %>
@@ -144,8 +152,9 @@
 <%
 	end
 %>
-			setUserEventSource(<%=user.id%>);
+			setUserEventSource(<%=user_id%>);
 			lastUpdate = <%=user.last_update()%>;
+			userId = '<%=user_id%>';
 		</script>
 	</body>
 </html>
--- a/src/lib/Shared.luan	Tue Nov 12 16:08:24 2024 -0700
+++ b/src/lib/Shared.luan	Tue Nov 12 17:17:59 2024 -0700
@@ -97,21 +97,15 @@
 	local author = get_user_by_id(author_id)
 	local id = post.id
 %>
-		<div post="<%=id%>">
-			<div who="<%=author.id%>">
+		<div post="<%=id%>" author="<%=author.id%>" fix>
+			<div who>
 				<span author><%=author.email%></span>
 				<span right>
-					<span when fix><%=post.date%></span>
-					<span pulldown>
-						<img onclick="clickMenu(this)" src="/images/more_vert.svg">
-						<div>
-							<span onclick="editPost('<%=id%>')">Edit</span>
-							<span onclick="deletePost('<%=id%>')">Delete</span>
-						</div>
-					</span>
+					<span when><%=post.date%></span>
+					<span pulldown></span>
 				</span>
 			</div>
-			<div text fix><%= html_encode(post.content) %></div>
+			<div text><%= html_encode(post.content) %></div>
 		</div>
 <%
 end