annotate src/thread.html.luan @ 33:4fdc4ec0050b

upload with uploadcare
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 01 Aug 2022 22:50:16 -0600
parents 72a1b77b4548
children c8d47981c74f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 local ipairs = Luan.ipairs or error()
18
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
4 local Time = require "luan:Time.luan"
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
5 local time_now = Time.now or error()
9
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 local Io = require "luan:Io.luan"
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local Http = require "luan:http/Http.luan"
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local Shared = require "site:/lib/Shared.luan"
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local head = Shared.head or error()
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local header = Shared.header or error()
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 local footer = Shared.footer or error()
33
4fdc4ec0050b upload with uploadcare
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
12 local get_url_from_file = Shared.get_url_from_file or error()
9
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 local Forum = require "site:/lib/Forum.luan"
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 local forum_title = Forum.title or error()
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 local Db = require "site:/lib/Db.luan"
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 local Post = require "site:/lib/Post.luan"
12
ad1604c72156 fix bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
17 local Bbcode = require "site:/lib/Bbcode.luan"
ad1604c72156 fix bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
18 local bbcode_to_html = Bbcode.to_html or error()
16
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
19 local User = require "site:/lib/User.luan"
9
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20
11
3ed1e3f3a53a use bbcode_to_html
Franklin Schmidt <fschmidt@gmail.com>
parents: 9
diff changeset
21
16
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
22 local function deletePost()
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
23 %><a href="javascript:" onclick="deletePost(parentNode)">delete</a><%
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
24 end
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
25
9
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 return function()
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 local root_id = Http.request.parameters.root or error()
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 local docs, total_hits = Db.search("post_root_id:"..root_id,1,1000,{sort="id"})
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 local posts = Post.from_docs(docs)
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 local subject_html = posts[1].subject_html
16
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
31 local user = User.current()
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
32 local user_name = user and user.name
18
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
33 local now = time_now()
9
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 Io.stdout = Http.response.text_writer()
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 %>
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 <!doctype html>
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 <html>
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 <head>
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 <% head() %>
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 <title><%=forum_title%>: <%=subject_html%></title>
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 <style>
18
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
42 div[author] {
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
43 margin-bottom: 6px;
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
44 font-size: 10px;
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
45 }
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
46 div[author] img {
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
47 width: 28px;
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
48 vertical-align: middle;
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
49 border-radius: 50%;
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
50 }
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
51 div[author] a {
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
52 font-weight: bold;
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
53 }
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
54 span[ago] {
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
55 color: #888;
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
56 }
15
0edde02b908c edit in place
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
57 [message] {
9
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 white-space: pre-wrap;
26
0837820b97fb tinymce work
Franklin Schmidt <fschmidt@gmail.com>
parents: 25
diff changeset
59 line-height: 1.4;
9
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 }
25
66fd3784e60e back to textarea for bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 21
diff changeset
61 textarea {
66fd3784e60e back to textarea for bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 21
diff changeset
62 width: 100%;
66fd3784e60e back to textarea for bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 21
diff changeset
63 xmax-width: 450px;
66fd3784e60e back to textarea for bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 21
diff changeset
64 height: 100px;
66fd3784e60e back to textarea for bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 21
diff changeset
65 }
66fd3784e60e back to textarea for bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 21
diff changeset
66 </style>
15
0edde02b908c edit in place
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
67 <script>
19
da006d1c1eba use contentEditable
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
68 function getPostDiv(node) {
da006d1c1eba use contentEditable
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
69 do {
da006d1c1eba use contentEditable
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
70 if( node.getAttribute('post') )
da006d1c1eba use contentEditable
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
71 return node;
da006d1c1eba use contentEditable
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
72 } while( node = node.parentNode );
da006d1c1eba use contentEditable
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
73 }
da006d1c1eba use contentEditable
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
74
32
Franklin Schmidt <fschmidt@gmail.com>
parents: 26
diff changeset
75 function cancelEdit(src) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 26
diff changeset
76 let postDiv = getPostDiv(src);
15
0edde02b908c edit in place
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
77 postDiv.querySelector('[output]').style.display = 'block';
0edde02b908c edit in place
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
78 postDiv.querySelector('[edit]').innerHTML = '';
0edde02b908c edit in place
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
79 }
32
Franklin Schmidt <fschmidt@gmail.com>
parents: 26
diff changeset
80 function saveEdit(src) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 26
diff changeset
81 let postDiv = getPostDiv(src);
19
da006d1c1eba use contentEditable
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
82 let post = postDiv.getAttribute('post');
25
66fd3784e60e back to textarea for bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 21
diff changeset
83 let text = postDiv.querySelector('textarea').value;
15
0edde02b908c edit in place
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
84 let postData = 'post=' + post + '&text=' + encodeURIComponent(text);
0edde02b908c edit in place
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
85 ajax("save_edit.js",postData);
0edde02b908c edit in place
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
86 }
16
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
87
33
4fdc4ec0050b upload with uploadcare
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
88 function upload(input) {
4fdc4ec0050b upload with uploadcare
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
89 let postDiv = getPostDiv(input);
4fdc4ec0050b upload with uploadcare
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
90 let post = postDiv.getAttribute('post');
4fdc4ec0050b upload with uploadcare
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
91 let file = input.files[0];
4fdc4ec0050b upload with uploadcare
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
92 ajax("/upload_bbcode.js?post="+post,file);
32
Franklin Schmidt <fschmidt@gmail.com>
parents: 26
diff changeset
93 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 26
diff changeset
94
16
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
95 function deletePost(span) {
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
96 span.innerHTML = document.querySelector('[hidden][delete]').innerHTML;
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
97 }
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
98 function deleteNo(span) {
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
99 span.innerHTML = document.querySelector('[hidden][undelete]').innerHTML;
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
100 }
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
101 function deleteYes(span) {
19
da006d1c1eba use contentEditable
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
102 let post = getPostDiv(span).getAttribute('post');
16
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
103 ajax( '/delete.js?post=' + post );
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
104 }
18
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
105
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
106 function init() {
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
107 let spans = document.querySelectorAll('span[ago]');
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
108 for( let i=0; i<spans.length; i++ ) {
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
109 let span = spans[i];
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
110 let date = span.getAttribute('date');
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
111 date = parseInt(date);
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
112 span.title = new Date(date).toLocaleString();
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
113 }
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
114 }
15
0edde02b908c edit in place
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
115 </script>
9
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
116 </head>
18
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
117 <body onload="init()">
9
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
118 <% header() %>
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
119 <div content>
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
120 <h1><%=subject_html%></h1>
16
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
121 <% for _, post in ipairs(posts) do
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
122 if post.is_deleted then
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
123 continue
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
124 end
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
125 %>
9
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
126 <hr>
15
0edde02b908c edit in place
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
127 <div post="<%=post.id%>">
18
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
128 <div author>
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
129 <img src="/images/profile.png">
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
130 <a href="/whatever"><%= post.author_name %></a>
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
131 <span ago date="<%=post.date%>"><% post.ago(now) %> ago</span>
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
132 </div>
15
0edde02b908c edit in place
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
133 <div output>
0edde02b908c edit in place
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
134 <% bbcode_to_html(post.content) %>
0edde02b908c edit in place
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
135 <p>
0edde02b908c edit in place
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
136 <a href="/reply.html?parent=<%=post.id%>">reply</a>
16
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
137 <% if post.author_name == user_name then %>
15
0edde02b908c edit in place
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
138 - <a href="javascript:ajax('/edit.js?post=<%=post.id%>')">edit</a>
19
da006d1c1eba use contentEditable
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
139 - <span delete><%deletePost()%></span>
16
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
140 <% end %>
15
0edde02b908c edit in place
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
141 </p>
0edde02b908c edit in place
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
142 </div>
0edde02b908c edit in place
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
143 <div edit></div>
0edde02b908c edit in place
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
144 </div>
9
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
145 <% end %>
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
146 </div>
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
147 <% footer() %>
16
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
148 <span hidden delete>Delete? <a href="javascript:" onclick="deleteYes(parentNode)">yes</a> / <a href="javascript:" onclick="deleteNo(parentNode)">no</a></span>
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
149 <span hidden undelete><%deletePost()%></span>
19
da006d1c1eba use contentEditable
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
150 <div hidden edit>
25
66fd3784e60e back to textarea for bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 21
diff changeset
151 <textarea></textarea>
19
da006d1c1eba use contentEditable
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
152 <p>
33
4fdc4ec0050b upload with uploadcare
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
153 <label clickable
4fdc4ec0050b upload with uploadcare
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
154 ><input type=file onchange="upload(this)"
4fdc4ec0050b upload with uploadcare
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
155 ><span inline_button>upload</span>
4fdc4ec0050b upload with uploadcare
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
156 </label>
19
da006d1c1eba use contentEditable
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
157 <button onclick="saveEdit(this)">save</button>
da006d1c1eba use contentEditable
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
158 <button onclick="cancelEdit(this)">cancel</button>
da006d1c1eba use contentEditable
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
159 </p>
da006d1c1eba use contentEditable
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
160 </div>
9
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
161 </body>
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
162 </html>
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
163 <%
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
164 end