diff src/thread.html.luan @ 42:0c1b820fff34

use push
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 08 Nov 2022 14:02:28 -0700
parents 1ce75c5ab0f7
children 96f0c3d65698
line wrap: on
line diff
--- a/src/thread.html.luan	Sun Nov 06 17:39:21 2022 -0700
+++ b/src/thread.html.luan	Tue Nov 08 14:02:28 2022 -0700
@@ -3,12 +3,16 @@
 local ipairs = Luan.ipairs or error()
 local Time = require "luan:Time.luan"
 local time_now = Time.now or error()
+local Html = require "luan:Html.luan"
+local html_encode = Html.encode or error()
 local Io = require "luan:Io.luan"
 local Http = require "luan:http/Http.luan"
 local Shared = require "site:/lib/Shared.luan"
 local head = Shared.head or error()
 local header = Shared.header or error()
 local footer = Shared.footer or error()
+local delete_post = Shared.delete_post or error()
+local show_post = Shared.show_post or error()
 local Forum = require "site:/lib/Forum.luan"
 local forum_title = Forum.title or error()
 local Db = require "site:/lib/Db.luan"
@@ -18,10 +22,6 @@
 local User = require "site:/lib/User.luan"
 
 
-local function deletePost()
-	%><a href="javascript:" onclick="deletePost(parentNode)">delete</a><%
-end
-
 return function()
 	local root_id = Http.request.parameters.root or error()
 	local docs, total_hits = Db.search("post_root_id:"..root_id,1,1000,{sort="id"})
@@ -60,9 +60,28 @@
 			textarea {
 				width: 100%;
 				xmax-width: 450px;
-				height: 100px;
+				xheight: 100px;
+			}
+<%	if user_name == nil then %>
+			[logged_in] {
+				display: none;
+			}
+<%	end %>
+			[only_user] {
+				display: none;
 			}
- 		</style>
+<%	if user_name ~= nil then %>
+			[only_user="<%=html_encode(user_name)%>"] {
+				display: initial;
+			}
+<%	end %>
+			div[post]:first-of-type [delete_opton] {
+				display: none;
+			}
+			body[thread_size="1"] div[post]:first-of-type [delete_opton] {
+				display: initial;
+			}
+		</style>
 		<script>
 			function getPostDiv(node) {
 				do {
@@ -80,9 +99,31 @@
 				let postDiv = getPostDiv(src);
 				let post = postDiv.getAttribute('post');
 				let text = postDiv.querySelector('textarea').value;
+				cancelEdit(src);
 				let postData = 'post=' + post + '&text=' + encodeURIComponent(text);
 				ajax('save_edit.js',postData);
 			}
+			function updated(postId,html) {
+				let postDiv = document.querySelector('[post="'+postId+'"]');
+				if(postDiv)
+					postDiv.querySelector('[message]').innerHTML = html;
+			}
+
+			function newPost() {
+				let postDiv = document.querySelector('[post="new"]');
+				let textarea = postDiv.querySelector('textarea')
+				let text = textarea.value;
+				textarea.value = '';
+				let postData = 'root=<%=root_id%>&text=' + encodeURIComponent(text);
+				ajax('new_post.js',postData);
+			}
+			function added(postId,html,thread_size) {
+				if( document.querySelector('[post="'+postId+'"]') )
+					return;
+				let newDiv = document.querySelector('[post="new"]');
+				newDiv.insertAdjacentHTML('beforebegin',html);
+				document.body.setAttribute('thread_size',thread_size);
+			}
 
 			function uploaded(input,url,filename) {
 				let postDiv = getPostDiv(input);
@@ -101,6 +142,13 @@
 				let post = getPostDiv(span).getAttribute('post');
 				ajax( '/delete.js?post=' + post );
 			}
+			function deleted(postId,thread_size) {
+				let postDiv = document.querySelector('[post="'+postId+'"]');
+				if(!postDiv)
+					return
+				postDiv.outerHTML = '';
+				document.body.setAttribute('thread_size',thread_size);
+			}
 
 			function init() {
 				let spans = document.querySelectorAll('span[ago]');
@@ -111,43 +159,36 @@
 					span.title = new Date(date).toLocaleString();
 				}
 			}
+
+			let eventSource = new EventSource(location.href);
+			eventSource.onmessage = function(event) {
+				eval( event.data );
+			};
 		</script>
 	</head>
-	<body onload="init()">
+	<body onload="init()" thread_size="<%=Post.thread_size(root_id)%>">
 <%		header() %>
 		<div content>
 			<h1><%=subject_html%></h1>
 <%	for _, post in ipairs(posts) do
-		if post.is_deleted then
-			continue
-		end
-%>
-			<hr>
-			<div post="<%=post.id%>">
-				<div author>
-					<img src="/images/profile.png">
-					<a href="/whatever"><%= post.author_name %></a>
-					<span ago date="<%=post.date%>"><% post.ago(now) %> ago</span>
-				</div>
-				<div output>
-					<% bbcode_to_html(post.content) %>
-					<p>
-						<a href="/reply.html?parent=<%=post.id%>">reply</a>
-<%	if post.author_name == user_name then %>
-						- <a href="javascript:ajax('/edit.js?post=<%=post.id%>')">edit</a>
-						- <span delete><%deletePost()%></span>
-<%	end %>
-					</p>
-				</div>
-				<div edit></div>
+		show_post(post,now)
+	end %>
+			<div post=new logged_in>
+				<hr>
+				<textarea oninput="fixTextarea(this)"></textarea>
+				<p>
+					<input type=file onchange="upload(this,uploaded)">
+					<button onclick="fileButtonClick(this)">Upload File</button>
+					<button onclick="newPost()">save</button>
+				</p>
 			</div>
-<%	end %>
 		</div>
 <%		footer() %>
+
 		<span hidden delete>Delete? <a href="javascript:" onclick="deleteYes(parentNode)">yes</a> / <a href="javascript:" onclick="deleteNo(parentNode)">no</a></span>
-		<span hidden undelete><%deletePost()%></span>
+		<span hidden undelete><%delete_post()%></span>
 		<div hidden edit>
-			<textarea></textarea>
+			<textarea oninput="fixTextarea(this)"></textarea>
 			<p>
 				<input type=file onchange="upload(this,uploaded)">
 				<button onclick="fileButtonClick(this)">Upload File</button>