comparison src/thread.html.luan @ 16:bff178656073

delete function
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 06 Jul 2022 22:42:42 -0600
parents 0edde02b908c
children 94e26bffd4fb
comparison
equal deleted inserted replaced
15:0edde02b908c 16:bff178656073
11 local forum_title = Forum.title or error() 11 local forum_title = Forum.title or error()
12 local Db = require "site:/lib/Db.luan" 12 local Db = require "site:/lib/Db.luan"
13 local Post = require "site:/lib/Post.luan" 13 local Post = require "site:/lib/Post.luan"
14 local Bbcode = require "site:/lib/Bbcode.luan" 14 local Bbcode = require "site:/lib/Bbcode.luan"
15 local bbcode_to_html = Bbcode.to_html or error() 15 local bbcode_to_html = Bbcode.to_html or error()
16 local User = require "site:/lib/User.luan"
16 17
18
19 local function deletePost()
20 %><a href="javascript:" onclick="deletePost(parentNode)">delete</a><%
21 end
17 22
18 return function() 23 return function()
19 local root_id = Http.request.parameters.root or error() 24 local root_id = Http.request.parameters.root or error()
20 local docs, total_hits = Db.search("post_root_id:"..root_id,1,1000,{sort="id"}) 25 local docs, total_hits = Db.search("post_root_id:"..root_id,1,1000,{sort="id"})
21 local posts = Post.from_docs(docs) 26 local posts = Post.from_docs(docs)
22 local subject_html = posts[1].subject_html 27 local subject_html = posts[1].subject_html
28 local user = User.current()
29 local user_name = user and user.name
23 Io.stdout = Http.response.text_writer() 30 Io.stdout = Http.response.text_writer()
24 %> 31 %>
25 <!doctype html> 32 <!doctype html>
26 <html> 33 <html>
27 <head> 34 <head>
36 function cancelEdit(post) { 43 function cancelEdit(post) {
37 let postDiv = document.querySelector('[post="'+post+'"]'); 44 let postDiv = document.querySelector('[post="'+post+'"]');
38 postDiv.querySelector('[output]').style.display = 'block'; 45 postDiv.querySelector('[output]').style.display = 'block';
39 postDiv.querySelector('[edit]').innerHTML = ''; 46 postDiv.querySelector('[edit]').innerHTML = '';
40 } 47 }
41
42 function saveEdit(post) { 48 function saveEdit(post) {
43 let postDiv = document.querySelector('[post="'+post+'"]'); 49 let postDiv = document.querySelector('[post="'+post+'"]');
44 let text = postDiv.querySelector('textarea').value; 50 let text = postDiv.querySelector('textarea').value;
45 let postData = 'post=' + post + '&text=' + encodeURIComponent(text); 51 let postData = 'post=' + post + '&text=' + encodeURIComponent(text);
46 ajax("save_edit.js",postData); 52 ajax("save_edit.js",postData);
53 }
54
55 function deletePost(span) {
56 span.innerHTML = document.querySelector('[hidden][delete]').innerHTML;
57 }
58 function deleteNo(span) {
59 span.innerHTML = document.querySelector('[hidden][undelete]').innerHTML;
60 }
61 function deleteYes(span) {
62 let post = span.getAttribute('delete');
63 ajax( '/delete.js?post=' + post );
47 } 64 }
48 </script> 65 </script>
49 </head> 66 </head>
50 <body> 67 <body>
51 <% header() %> 68 <% header() %>
52 <div content> 69 <div content>
53 <h1><%=subject_html%></h1> 70 <h1><%=subject_html%></h1>
54 <% for _, post in ipairs(posts) do %> 71 <% for _, post in ipairs(posts) do
72 if post.is_deleted then
73 continue
74 end
75 %>
55 <hr> 76 <hr>
56 <div post="<%=post.id%>"> 77 <div post="<%=post.id%>">
57 <div output> 78 <div output>
58 <% bbcode_to_html(post.content) %> 79 <% bbcode_to_html(post.content) %>
59 <p> 80 <p>
60 <a href="/reply.html?parent=<%=post.id%>">reply</a> 81 <a href="/reply.html?parent=<%=post.id%>">reply</a>
82 <% if post.author_name == user_name then %>
61 - <a href="javascript:ajax('/edit.js?post=<%=post.id%>')">edit</a> 83 - <a href="javascript:ajax('/edit.js?post=<%=post.id%>')">edit</a>
84 - <span delete="<%=post.id%>"><%deletePost()%></span>
85 <% end %>
62 </p> 86 </p>
63 </div> 87 </div>
64 <div edit></div> 88 <div edit></div>
65 </div> 89 </div>
66 <% end %> 90 <% end %>
67 </div> 91 </div>
68 <% footer() %> 92 <% footer() %>
93 <span hidden delete>Delete? <a href="javascript:" onclick="deleteYes(parentNode)">yes</a> / <a href="javascript:" onclick="deleteNo(parentNode)">no</a></span>
94 <span hidden undelete><%deletePost()%></span>
69 </body> 95 </body>
70 </html> 96 </html>
71 <% 97 <%
72 end 98 end