Mercurial Hosting > freedit
annotate src/thread.html.luan @ 49:f225e82b2bf8
add code bbcode
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 17 Nov 2022 16:46:21 -0700 |
parents | 289718f121e4 |
children | 78b2d6995244 |
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> | |
46 | 39 @import "/bbcode/bbcode.css"; |
40 | |
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 } | |
42 | 56 <% if user_name == nil then %> |
57 [logged_in] { | |
58 display: none; | |
59 } | |
60 <% end %> | |
61 [only_user] { | |
62 display: none; | |
25
66fd3784e60e
back to textarea for bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
21
diff
changeset
|
63 } |
42 | 64 <% if user_name ~= nil then %> |
65 [only_user="<%=html_encode(user_name)%>"] { | |
66 display: initial; | |
67 } | |
68 <% end %> | |
69 div[post]:first-of-type [delete_opton] { | |
70 display: none; | |
71 } | |
72 body[thread_size="1"] div[post]:first-of-type [delete_opton] { | |
73 display: initial; | |
74 } | |
75 </style> | |
46 | 76 <script src="/bbcode/bbcode.js"></script> |
15 | 77 <script> |
19 | 78 function getPostDiv(node) { |
79 do { | |
80 if( node.getAttribute('post') ) | |
81 return node; | |
82 } while( node = node.parentNode ); | |
83 } | |
84 | |
46 | 85 function cancelEdit(event) { |
86 let postDiv = getPostDiv(event.target); | |
15 | 87 postDiv.querySelector('[output]').style.display = 'block'; |
88 postDiv.querySelector('[edit]').innerHTML = ''; | |
89 } | |
46 | 90 function saveEdit(event) { |
91 let postDiv = getPostDiv(event.target); | |
19 | 92 let post = postDiv.getAttribute('post'); |
25
66fd3784e60e
back to textarea for bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
21
diff
changeset
|
93 let text = postDiv.querySelector('textarea').value; |
46 | 94 cancelEdit(event); |
15 | 95 let postData = 'post=' + post + '&text=' + encodeURIComponent(text); |
34 | 96 ajax('save_edit.js',postData); |
15 | 97 } |
42 | 98 function updated(postId,html) { |
99 let postDiv = document.querySelector('[post="'+postId+'"]'); | |
100 if(postDiv) | |
49 | 101 postDiv.querySelector('[from_bbcode]').outerHTML = html; |
42 | 102 } |
103 | |
104 function newPost() { | |
105 let postDiv = document.querySelector('[post="new"]'); | |
106 let textarea = postDiv.querySelector('textarea') | |
107 let text = textarea.value; | |
108 textarea.value = ''; | |
109 let postData = 'root=<%=root_id%>&text=' + encodeURIComponent(text); | |
110 ajax('new_post.js',postData); | |
111 } | |
112 function added(postId,html,thread_size) { | |
113 if( document.querySelector('[post="'+postId+'"]') ) | |
114 return; | |
115 let newDiv = document.querySelector('[post="new"]'); | |
116 newDiv.insertAdjacentHTML('beforebegin',html); | |
117 document.body.setAttribute('thread_size',thread_size); | |
118 } | |
16 | 119 |
35 | 120 function uploaded(input,url,filename) { |
121 let postDiv = getPostDiv(input); | |
122 let textarea = postDiv.querySelector('textarea'); | |
123 textarea.focus(); | |
124 textarea.setRangeText(url,textarea.selectionStart,textarea.selectionEnd,'select'); | |
32 | 125 } |
126 | |
16 | 127 function deletePost(span) { |
128 span.innerHTML = document.querySelector('[hidden][delete]').innerHTML; | |
129 } | |
130 function deleteNo(span) { | |
131 span.innerHTML = document.querySelector('[hidden][undelete]').innerHTML; | |
132 } | |
133 function deleteYes(span) { | |
19 | 134 let post = getPostDiv(span).getAttribute('post'); |
16 | 135 ajax( '/delete.js?post=' + post ); |
136 } | |
42 | 137 function deleted(postId,thread_size) { |
46 | 138 if( thread_size === 0 ) { |
139 location = '/'; | |
140 return; | |
141 } | |
42 | 142 let postDiv = document.querySelector('[post="'+postId+'"]'); |
143 if(!postDiv) | |
144 return | |
145 postDiv.outerHTML = ''; | |
146 document.body.setAttribute('thread_size',thread_size); | |
147 } | |
18 | 148 |
149 function init() { | |
150 let spans = document.querySelectorAll('span[ago]'); | |
151 for( let i=0; i<spans.length; i++ ) { | |
152 let span = spans[i]; | |
153 let date = span.getAttribute('date'); | |
154 date = parseInt(date); | |
155 span.title = new Date(date).toLocaleString(); | |
156 } | |
157 } | |
42 | 158 |
159 let eventSource = new EventSource(location.href); | |
160 eventSource.onmessage = function(event) { | |
161 eval( event.data ); | |
162 }; | |
15 | 163 </script> |
9 | 164 </head> |
42 | 165 <body onload="init()" thread_size="<%=Post.thread_size(root_id)%>"> |
9 | 166 <% header() %> |
167 <div content> | |
168 <h1><%=subject_html%></h1> | |
16 | 169 <% for _, post in ipairs(posts) do |
42 | 170 show_post(post,now) |
171 end %> | |
172 <div post=new logged_in> | |
173 <hr> | |
46 | 174 <div editor></div> |
15 | 175 </div> |
9 | 176 </div> |
177 <% footer() %> | |
42 | 178 |
16 | 179 <span hidden delete>Delete? <a href="javascript:" onclick="deleteYes(parentNode)">yes</a> / <a href="javascript:" onclick="deleteNo(parentNode)">no</a></span> |
42 | 180 <span hidden undelete><%delete_post()%></span> |
9 | 181 </body> |
46 | 182 <script> |
183 bbcodeCreate('div[post="new"] div[editor]',{ | |
184 save: newPost | |
185 }); | |
186 </script> | |
9 | 187 </html> |
188 <% | |
189 end |