view src/thread.html.luan @ 29:a1db5223ced1

luan changes
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 25 Jul 2022 21:28:10 -0600
parents 0837820b97fb
children 72a1b77b4548
line wrap: on
line source

local Luan = require "luan:Luan.luan"
local error = Luan.error
local ipairs = Luan.ipairs or error()
local Time = require "luan:Time.luan"
local time_now = Time.now 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 Forum = require "site:/lib/Forum.luan"
local forum_title = Forum.title or error()
local Db = require "site:/lib/Db.luan"
local Post = require "site:/lib/Post.luan"
local Bbcode = require "site:/lib/Bbcode.luan"
local bbcode_to_html = Bbcode.to_html or error()
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"})
	local posts = Post.from_docs(docs)
	local subject_html = posts[1].subject_html
	local user = User.current()
	local user_name = user and user.name
	local now = time_now()
	Io.stdout = Http.response.text_writer()
%>
<!doctype html>
<html>
	<head>
<%		head() %>
		<title><%=forum_title%>: <%=subject_html%></title>
		<style>
			div[author] {
				margin-bottom: 6px;
				font-size: 10px;
			}
			div[author] img {
				width: 28px;
				vertical-align: middle;
				border-radius: 50%;
			}
			div[author] a {
				font-weight: bold;
			}
			span[ago] {
				color: #888;
			}
			[message] {
				white-space: pre-wrap;
				line-height: 1.4;
			}
			textarea {
				width: 100%;
				xmax-width: 450px;
				height: 100px;
			}
 		</style>
		<script>
			function getPostDiv(node) {
				do {
					if( node.getAttribute('post') )
						return node;
				} while( node = node.parentNode );
			}

			function cancelEdit(a) {
				let postDiv = getPostDiv(a);
				postDiv.querySelector('[output]').style.display = 'block';
				postDiv.querySelector('[edit]').innerHTML = '';
			}
			function saveEdit(a) {
				let postDiv = getPostDiv(a);
				let post = postDiv.getAttribute('post');
				let text = postDiv.querySelector('textarea').value;
				let postData = 'post=' + post + '&text=' + encodeURIComponent(text);
				ajax("save_edit.js",postData);
			}

			function deletePost(span) {
				span.innerHTML = document.querySelector('[hidden][delete]').innerHTML;
			}
			function deleteNo(span) {
				span.innerHTML = document.querySelector('[hidden][undelete]').innerHTML;
			}
			function deleteYes(span) {
				let post = getPostDiv(span).getAttribute('post');
				ajax( '/delete.js?post=' + post );
			}

			function init() {
				let spans = document.querySelectorAll('span[ago]');
				for( let i=0; i<spans.length; i++ ) {
					let span = spans[i];
					let date = span.getAttribute('date');
					date = parseInt(date);
					span.title = new Date(date).toLocaleString();
				}
			}
		</script>
	</head>
	<body onload="init()">
<%		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>
			</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>
		<div hidden edit>
			<textarea></textarea>
			<p>
				<button onclick="saveEdit(this)">save</button>
				<button onclick="cancelEdit(this)">cancel</button>
			</p>
		</div>
	</body>
</html>
<%
end