changeset 18:94e26bffd4fb

UI work
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 11 Jul 2022 12:14:05 -0600
parents a602a5735a37
children da006d1c1eba
files src/edit.js.luan src/images/profile.png src/lib/Config.luan src/lib/Post.luan src/site.css src/site.js src/thread.html.luan
diffstat 7 files changed, 94 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/edit.js.luan	Thu Jul 07 15:30:06 2022 -0600
+++ b/src/edit.js.luan	Mon Jul 11 12:14:05 2022 -0600
@@ -29,5 +29,6 @@
 let postDiv = document.querySelector('[post="<%=post.id%>"]');
 postDiv.querySelector('[output]').style.display = 'none';
 postDiv.querySelector('[edit]').innerHTML = <%= json_string(html) %>;
+postDiv.querySelector('textarea').focus();
 <%
 end
Binary file src/images/profile.png has changed
--- a/src/lib/Config.luan	Thu Jul 07 15:30:06 2022 -0600
+++ b/src/lib/Config.luan	Mon Jul 11 12:14:05 2022 -0600
@@ -15,9 +15,11 @@
 Config.text = doc and doc.config or Config.default_text
 
 function Config.set(text)
-	local doc = Db.get_document("type:config") or {type="config"}
-	doc.config = text
-	Db.save(doc)
+	Db.run_in_transaction( function()
+		local doc = Db.get_document("type:config") or {type="config"}
+		doc.config = text
+		Db.save(doc)
+	end )
 end
 
 return Config
--- a/src/lib/Post.luan	Thu Jul 07 15:30:06 2022 -0600
+++ b/src/lib/Post.luan	Mon Jul 11 12:14:05 2022 -0600
@@ -11,6 +11,29 @@
 
 local Post = {}
 
+local times = {
+	{
+		time = 1000*60*60*24*365
+		unit = "year"
+	}
+	{
+		time = 1000*60*60*24*7
+		unit = "week"
+	}
+	{
+		time = 1000*60*60*24
+		unit = "day"
+	}
+	{
+		time = 1000*60*60
+		unit = "hour"
+	}
+	{
+		time = 1000*60
+		unit = "minute"
+	}
+}
+
 local function from_doc(doc)
 	doc.type == "post" or error "wrong type"
 	return Post.new {
@@ -91,6 +114,21 @@
 		return user ~= nil and user.name == post.author_name
 	end
 
+	function post.ago(now)
+		local diff = now - post.date
+		for _, t in ipairs(times) do
+			local n = diff // t.time
+			if n > 0 then
+				%><%=n%> <%=t.unit%><%
+				if n > 1 then
+					%>s<%
+				end
+				return
+			end
+		end
+		%>0 minutes<%
+	end
+
 	set_metatable(post,metatable)
 	return post
 end
--- a/src/site.css	Thu Jul 07 15:30:06 2022 -0600
+++ b/src/site.css	Mon Jul 11 12:14:05 2022 -0600
@@ -5,6 +5,7 @@
 body {
 	font-family: Sans-Serif;
 	margin: 0;
+	background-color: #F8F8F8;
 }
 
 a {
@@ -14,6 +15,12 @@
 	text-decoration: underline;
 }
 
+textarea {
+	font: inherit;
+	xpadding: 7px;
+	xborder-color: #DDDDDD;
+}
+
 div[header], div[footer] {
 	font-size: 14px;
 	background-color: #ddd;
--- a/src/site.js	Thu Jul 07 15:30:06 2022 -0600
+++ b/src/site.js	Mon Jul 11 12:14:05 2022 -0600
@@ -31,3 +31,7 @@
 		err += '\nstack = ' + error.stack;
 	ajax( '/error_log.js', 'err='+encodeURIComponent(err) );
 };
+
+function time(time) {
+	document.write(new Date(time).toLocaleString());
+}
--- a/src/thread.html.luan	Thu Jul 07 15:30:06 2022 -0600
+++ b/src/thread.html.luan	Mon Jul 11 12:14:05 2022 -0600
@@ -1,6 +1,8 @@
 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"
@@ -27,6 +29,7 @@
 	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>
@@ -35,9 +38,29 @@
 <%		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;
 			}
+			textarea {
+				width: 100%;
+				max-width: 450px;
+				height: 100px;
+			}
 		</style>
 		<script>
 			function cancelEdit(post) {
@@ -62,9 +85,19 @@
 				let post = span.getAttribute('delete');
 				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>
+	<body onload="init()">
 <%		header() %>
 		<div content>
 			<h1><%=subject_html%></h1>
@@ -75,6 +108,11 @@
 %>
 			<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>