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