Mercurial Hosting > freedit
annotate 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 |
rev | line source |
---|---|
9 | 1 local Luan = require "luan:Luan.luan" |
2 local error = Luan.error | |
3 local ipairs = Luan.ipairs or error() | |
18 | 4 local Time = require "luan:Time.luan" |
5 local time_now = Time.now or error() | |
42 | 6 local Html = require "luan:Html.luan" |
7 local html_encode = Html.encode or error() | |
9 | 8 local Io = require "luan:Io.luan" |
9 local Http = require "luan:http/Http.luan" | |
10 local Shared = require "site:/lib/Shared.luan" | |
11 local head = Shared.head or error() | |
12 local header = Shared.header or error() | |
13 local footer = Shared.footer or error() | |
42 | 14 local delete_post = Shared.delete_post or error() |
15 local show_post = Shared.show_post or error() | |
9 | 16 local Forum = require "site:/lib/Forum.luan" |
17 local forum_title = Forum.title or error() | |
18 local Db = require "site:/lib/Db.luan" | |
19 local Post = require "site:/lib/Post.luan" | |
12 | 20 local Bbcode = require "site:/lib/Bbcode.luan" |
21 local bbcode_to_html = Bbcode.to_html or error() | |
16 | 22 local User = require "site:/lib/User.luan" |
9 | 23 |
11 | 24 |
9 | 25 return function() |
26 local root_id = Http.request.parameters.root or error() | |
27 local docs, total_hits = Db.search("post_root_id:"..root_id,1,1000,{sort="id"}) | |
28 local posts = Post.from_docs(docs) | |
29 local subject_html = posts[1].subject_html | |
16 | 30 local user = User.current() |
31 local user_name = user and user.name | |
18 | 32 local now = time_now() |
9 | 33 Io.stdout = Http.response.text_writer() |
34 %> | |
35 <!doctype html> | |
36 <html> | |
37 <head> | |
38 <% head() %> | |
39 <title><%=forum_title%>: <%=subject_html%></title> | |
40 <style> | |
18 | 41 div[author] { |
42 margin-bottom: 6px; | |
43 font-size: 10px; | |
44 } | |
45 div[author] img { | |
46 width: 28px; | |
47 vertical-align: middle; | |
48 border-radius: 50%; | |
49 } | |
50 div[author] a { | |
51 font-weight: bold; | |
52 } | |
53 span[ago] { | |
54 color: #888; | |
55 } | |
15 | 56 [message] { |
9 | 57 white-space: pre-wrap; |
26 | 58 line-height: 1.4; |
9 | 59 } |
25
66fd3784e60e
back to textarea for bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
21
diff
changeset
|
60 textarea { |
66fd3784e60e
back to textarea for bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
21
diff
changeset
|
61 width: 100%; |
66fd3784e60e
back to textarea for bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
21
diff
changeset
|
62 xmax-width: 450px; |
42 | 63 xheight: 100px; |
64 } | |
65 <% if user_name == nil then %> | |
66 [logged_in] { | |
67 display: none; | |
68 } | |
69 <% end %> | |
70 [only_user] { | |
71 display: none; | |
25
66fd3784e60e
back to textarea for bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
21
diff
changeset
|
72 } |
42 | 73 <% if user_name ~= nil then %> |
74 [only_user="<%=html_encode(user_name)%>"] { | |
75 display: initial; | |
76 } | |
77 <% end %> | |
78 div[post]:first-of-type [delete_opton] { | |
79 display: none; | |
80 } | |
81 body[thread_size="1"] div[post]:first-of-type [delete_opton] { | |
82 display: initial; | |
83 } | |
84 </style> | |
15 | 85 <script> |
19 | 86 function getPostDiv(node) { |
87 do { | |
88 if( node.getAttribute('post') ) | |
89 return node; | |
90 } while( node = node.parentNode ); | |
91 } | |
92 | |
32 | 93 function cancelEdit(src) { |
94 let postDiv = getPostDiv(src); | |
15 | 95 postDiv.querySelector('[output]').style.display = 'block'; |
96 postDiv.querySelector('[edit]').innerHTML = ''; | |
97 } | |
32 | 98 function saveEdit(src) { |
99 let postDiv = getPostDiv(src); | |
19 | 100 let post = postDiv.getAttribute('post'); |
25
66fd3784e60e
back to textarea for bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
21
diff
changeset
|
101 let text = postDiv.querySelector('textarea').value; |
42 | 102 cancelEdit(src); |
15 | 103 let postData = 'post=' + post + '&text=' + encodeURIComponent(text); |
34 | 104 ajax('save_edit.js',postData); |
15 | 105 } |
42 | 106 function updated(postId,html) { |
107 let postDiv = document.querySelector('[post="'+postId+'"]'); | |
108 if(postDiv) | |
109 postDiv.querySelector('[message]').innerHTML = html; | |
110 } | |
111 | |
112 function newPost() { | |
113 let postDiv = document.querySelector('[post="new"]'); | |
114 let textarea = postDiv.querySelector('textarea') | |
115 let text = textarea.value; | |
116 textarea.value = ''; | |
117 let postData = 'root=<%=root_id%>&text=' + encodeURIComponent(text); | |
118 ajax('new_post.js',postData); | |
119 } | |
120 function added(postId,html,thread_size) { | |
121 if( document.querySelector('[post="'+postId+'"]') ) | |
122 return; | |
123 let newDiv = document.querySelector('[post="new"]'); | |
124 newDiv.insertAdjacentHTML('beforebegin',html); | |
125 document.body.setAttribute('thread_size',thread_size); | |
126 } | |
16 | 127 |
35 | 128 function uploaded(input,url,filename) { |
129 let postDiv = getPostDiv(input); | |
130 let textarea = postDiv.querySelector('textarea'); | |
131 textarea.focus(); | |
132 textarea.setRangeText(url,textarea.selectionStart,textarea.selectionEnd,'select'); | |
32 | 133 } |
134 | |
16 | 135 function deletePost(span) { |
136 span.innerHTML = document.querySelector('[hidden][delete]').innerHTML; | |
137 } | |
138 function deleteNo(span) { | |
139 span.innerHTML = document.querySelector('[hidden][undelete]').innerHTML; | |
140 } | |
141 function deleteYes(span) { | |
19 | 142 let post = getPostDiv(span).getAttribute('post'); |
16 | 143 ajax( '/delete.js?post=' + post ); |
144 } | |
42 | 145 function deleted(postId,thread_size) { |
146 let postDiv = document.querySelector('[post="'+postId+'"]'); | |
147 if(!postDiv) | |
148 return | |
149 postDiv.outerHTML = ''; | |
150 document.body.setAttribute('thread_size',thread_size); | |
151 } | |
18 | 152 |
153 function init() { | |
154 let spans = document.querySelectorAll('span[ago]'); | |
155 for( let i=0; i<spans.length; i++ ) { | |
156 let span = spans[i]; | |
157 let date = span.getAttribute('date'); | |
158 date = parseInt(date); | |
159 span.title = new Date(date).toLocaleString(); | |
160 } | |
161 } | |
42 | 162 |
163 let eventSource = new EventSource(location.href); | |
164 eventSource.onmessage = function(event) { | |
165 eval( event.data ); | |
166 }; | |
15 | 167 </script> |
9 | 168 </head> |
42 | 169 <body onload="init()" thread_size="<%=Post.thread_size(root_id)%>"> |
9 | 170 <% header() %> |
171 <div content> | |
172 <h1><%=subject_html%></h1> | |
16 | 173 <% for _, post in ipairs(posts) do |
42 | 174 show_post(post,now) |
175 end %> | |
176 <div post=new logged_in> | |
177 <hr> | |
178 <textarea oninput="fixTextarea(this)"></textarea> | |
179 <p> | |
180 <input type=file onchange="upload(this,uploaded)"> | |
181 <button onclick="fileButtonClick(this)">Upload File</button> | |
182 <button onclick="newPost()">save</button> | |
183 </p> | |
15 | 184 </div> |
9 | 185 </div> |
186 <% footer() %> | |
42 | 187 |
16 | 188 <span hidden delete>Delete? <a href="javascript:" onclick="deleteYes(parentNode)">yes</a> / <a href="javascript:" onclick="deleteNo(parentNode)">no</a></span> |
42 | 189 <span hidden undelete><%delete_post()%></span> |
19 | 190 <div hidden edit> |
42 | 191 <textarea oninput="fixTextarea(this)"></textarea> |
19 | 192 <p> |
35 | 193 <input type=file onchange="upload(this,uploaded)"> |
34 | 194 <button onclick="fileButtonClick(this)">Upload File</button> |
19 | 195 <button onclick="saveEdit(this)">save</button> |
196 <button onclick="cancelEdit(this)">cancel</button> | |
197 </p> | |
198 </div> | |
9 | 199 </body> |
200 </html> | |
201 <% | |
202 end |