Mercurial Hosting > freedit
annotate src/thread.html.luan @ 34:c8d47981c74f
upload using ajax
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 02 Aug 2022 20:46:02 -0600 |
parents | 4fdc4ec0050b |
children | 1ce75c5ab0f7 |
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() | |
9 | 6 local Io = require "luan:Io.luan" |
7 local Http = require "luan:http/Http.luan" | |
8 local Shared = require "site:/lib/Shared.luan" | |
9 local head = Shared.head or error() | |
10 local header = Shared.header or error() | |
11 local footer = Shared.footer or error() | |
12 local Forum = require "site:/lib/Forum.luan" | |
13 local forum_title = Forum.title or error() | |
14 local Db = require "site:/lib/Db.luan" | |
15 local Post = require "site:/lib/Post.luan" | |
12 | 16 local Bbcode = require "site:/lib/Bbcode.luan" |
17 local bbcode_to_html = Bbcode.to_html or error() | |
16 | 18 local User = require "site:/lib/User.luan" |
9 | 19 |
11 | 20 |
16 | 21 local function deletePost() |
22 %><a href="javascript:" onclick="deletePost(parentNode)">delete</a><% | |
23 end | |
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; |
66fd3784e60e
back to textarea for bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
21
diff
changeset
|
63 height: 100px; |
66fd3784e60e
back to textarea for bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
21
diff
changeset
|
64 } |
66fd3784e60e
back to textarea for bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
21
diff
changeset
|
65 </style> |
15 | 66 <script> |
19 | 67 function getPostDiv(node) { |
68 do { | |
69 if( node.getAttribute('post') ) | |
70 return node; | |
71 } while( node = node.parentNode ); | |
72 } | |
73 | |
32 | 74 function cancelEdit(src) { |
75 let postDiv = getPostDiv(src); | |
15 | 76 postDiv.querySelector('[output]').style.display = 'block'; |
77 postDiv.querySelector('[edit]').innerHTML = ''; | |
78 } | |
32 | 79 function saveEdit(src) { |
80 let postDiv = getPostDiv(src); | |
19 | 81 let post = postDiv.getAttribute('post'); |
25
66fd3784e60e
back to textarea for bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
21
diff
changeset
|
82 let text = postDiv.querySelector('textarea').value; |
15 | 83 let postData = 'post=' + post + '&text=' + encodeURIComponent(text); |
34 | 84 ajax('save_edit.js',postData); |
15 | 85 } |
16 | 86 |
34 | 87 function gotFile(input) { |
88 function uploaded(url,filename) { | |
89 let postDiv = getPostDiv(input); | |
90 let textarea = postDiv.querySelector('textarea'); | |
91 textarea.focus(); | |
92 textarea.setRangeText(url,textarea.selectionStart,textarea.selectionEnd,'select'); | |
93 } | |
94 upload(input,uploaded); | |
32 | 95 } |
96 | |
16 | 97 function deletePost(span) { |
98 span.innerHTML = document.querySelector('[hidden][delete]').innerHTML; | |
99 } | |
100 function deleteNo(span) { | |
101 span.innerHTML = document.querySelector('[hidden][undelete]').innerHTML; | |
102 } | |
103 function deleteYes(span) { | |
19 | 104 let post = getPostDiv(span).getAttribute('post'); |
16 | 105 ajax( '/delete.js?post=' + post ); |
106 } | |
18 | 107 |
108 function init() { | |
109 let spans = document.querySelectorAll('span[ago]'); | |
110 for( let i=0; i<spans.length; i++ ) { | |
111 let span = spans[i]; | |
112 let date = span.getAttribute('date'); | |
113 date = parseInt(date); | |
114 span.title = new Date(date).toLocaleString(); | |
115 } | |
116 } | |
15 | 117 </script> |
9 | 118 </head> |
18 | 119 <body onload="init()"> |
9 | 120 <% header() %> |
121 <div content> | |
122 <h1><%=subject_html%></h1> | |
16 | 123 <% for _, post in ipairs(posts) do |
124 if post.is_deleted then | |
125 continue | |
126 end | |
127 %> | |
9 | 128 <hr> |
15 | 129 <div post="<%=post.id%>"> |
18 | 130 <div author> |
131 <img src="/images/profile.png"> | |
132 <a href="/whatever"><%= post.author_name %></a> | |
133 <span ago date="<%=post.date%>"><% post.ago(now) %> ago</span> | |
134 </div> | |
15 | 135 <div output> |
136 <% bbcode_to_html(post.content) %> | |
137 <p> | |
138 <a href="/reply.html?parent=<%=post.id%>">reply</a> | |
16 | 139 <% if post.author_name == user_name then %> |
15 | 140 - <a href="javascript:ajax('/edit.js?post=<%=post.id%>')">edit</a> |
19 | 141 - <span delete><%deletePost()%></span> |
16 | 142 <% end %> |
15 | 143 </p> |
144 </div> | |
145 <div edit></div> | |
146 </div> | |
9 | 147 <% end %> |
148 </div> | |
149 <% footer() %> | |
16 | 150 <span hidden delete>Delete? <a href="javascript:" onclick="deleteYes(parentNode)">yes</a> / <a href="javascript:" onclick="deleteNo(parentNode)">no</a></span> |
151 <span hidden undelete><%deletePost()%></span> | |
19 | 152 <div hidden edit> |
25
66fd3784e60e
back to textarea for bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
21
diff
changeset
|
153 <textarea></textarea> |
19 | 154 <p> |
34 | 155 <input type=file onchange="gotFile(this)"> |
156 <button onclick="fileButtonClick(this)">Upload File</button> | |
19 | 157 <button onclick="saveEdit(this)">save</button> |
158 <button onclick="cancelEdit(this)">cancel</button> | |
159 </p> | |
160 </div> | |
9 | 161 </body> |
162 </html> | |
163 <% | |
164 end |