]> code.citadel.org Git - citadel.git/blob - webcit-ng/static/js/view_forum.js
bold, italic, and list buttons now work.
[citadel.git] / webcit-ng / static / js / view_forum.js
1 //
2 // Copyright (c) 2016-2021 by the citadel.org team
3 //
4 // This program is open source software.  It runs great on the
5 // Linux operating system (and probably elsewhere).  You can use,
6 // copy, and run it under the terms of the GNU General Public
7 // License version 3.  Richard Stallman is an asshole communist.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13
14
15 // Forum view (flat)
16 //
17 function forum_readmessages(target_div, gt_msg, lt_msg) {
18         original_text = document.getElementById(target_div).innerHTML;          // in case we need to replace it after an error
19         document.getElementById(target_div).innerHTML = 
20                 "<div class=\"ctdl-forum-nav\"><i class=\"fas fa-spinner fa-spin\"></i>&nbsp;&nbsp;"
21                 + _("Loading messages from server, please wait") + "</div>";
22
23         if (lt_msg < 9999999999) {
24                 url = "/ctdl/r/" + escapeHTMLURI(current_room) + "/msgs.lt|" + lt_msg;
25         }
26         else {
27                 url = "/ctdl/r/" + escapeHTMLURI(current_room) + "/msgs.gt|" + gt_msg;
28         }
29
30         fetch_msg_list = async() => {
31                 response = await fetch(url);
32                 msgs = await(response.json());
33                 if (response.ok) {
34                         document.getElementById(target_div).innerHTML = "" ;
35
36                         // If we were given an explicit starting point, by all means start there.
37                         // Note that we don't have to remove them from the array because we did a 'msgs gt|xxx' command to Citadel.
38                         if (gt_msg > 0) {
39                                 msgs = msgs.slice(0, messages_per_page);
40                         }
41
42                         // Otherwise, show us the last 20 messages
43                         else {
44                                 if (msgs.length > messages_per_page) {
45                                         msgs = msgs.slice(msgs.length - messages_per_page);
46                                 }
47                                 new_old_div_name = randomString(5);
48                                 if (msgs.length < 1) {
49                                         newlt = lt_msg;
50                                 }
51                                 else {
52                                         newlt = msgs[0];
53                                 }
54                                 document.getElementById(target_div).innerHTML +=
55                                         "<div id=\"" + new_old_div_name + "\">" +
56                                         "<div class=\"ctdl-forum-nav\">" +
57                                         "<a href=\"javascript:forum_readmessages('" + new_old_div_name + "', 0, " + newlt + ");\">" +
58                                         "<i class=\"fa fa-arrow-circle-up\"></i>&nbsp;&nbsp;" +
59                                         _("Older posts") + "&nbsp;&nbsp;<i class=\"fa fa-arrow-circle-up\"></a></div></div></a></div></div>" ;
60                         }
61
62                         // Render an empty div for each message.  We will fill them in later.
63                         for (var i in msgs) {
64                                 document.getElementById(target_div).innerHTML += "<div id=\"ctdl_msg_" + msgs[i] + "\"> </div>" ;
65                                 document.getElementById("ctdl_msg_"+msgs[i]).style.display = "none";
66                         }
67                         if (lt_msg == 9999999999) {
68                                 new_new_div_name = randomString(5);
69                                 if (msgs.length <= 0) {
70                                         newgt = gt_msg;
71                                 }
72                                 else {
73                                         newgt = msgs[msgs.length-1];
74                                 }
75                                 document.getElementById(target_div).innerHTML +=
76                                         "<div id=\"" + new_new_div_name + "\">" +
77                                         "<div class=\"ctdl-forum-nav\">" +
78                                         "<a href=\"javascript:forum_readmessages('" + new_new_div_name + "', " + newgt + ", 9999999999);\">" +
79                                         "<i class=\"fa fa-arrow-circle-down\"></i>&nbsp;&nbsp;" +
80                                         _("Newer posts") + "&nbsp;&nbsp;<i class=\"fa fa-arrow-circle-down\"></a></div></div>" ;
81                         }
82
83                         // Now figure out where to scroll to after rendering.
84                         if (gt_msg > 0) {
85                                 scroll_to = msgs[0];
86                         }
87                         else if (lt_msg < 9999999999) {
88                                 scroll_to = msgs[msgs.length-1];
89                         }
90                         else if ( (logged_in) && (gt_msg == 0) && (lt_msg == 9999999999) ) {
91                                 scroll_to = msgs[msgs.length-1];
92                         }
93                         else {
94                                 scroll_to = msgs[0];            // FIXME this is too naive
95                         }
96
97                         // Render the individual messages in the divs
98                         forum_render_messages(msgs, "ctdl_msg_", scroll_to)
99                 }
100                 else {
101                         // if xhr fails, this will make the link reappear so the user can try again
102                         document.getElementById(target_div).innerHTML = original_text;
103                 }
104         }
105         fetch_msg_list();
106 }
107
108
109 // Render a range of messages, with the div prefix specified
110 //
111 function forum_render_messages(msgs, prefix, scroll_to) {
112         for (i=0; i<msgs.length; ++i) {
113                 forum_render_one(prefix, msgs[i], scroll_to);
114         }
115 }
116
117
118 // We have to put each XHR for forum_render_messages() into its own stack frame, otherwise it jumbles them together.  I don't know why.
119 function forum_render_one(prefix, msgnum, scroll_to) {
120         fetch_message = async() => {
121                 response = await fetch("/ctdl/r/" + escapeHTMLURI(current_room) + "/" + msgs[i] + "/json");
122                 msg = await response.json();
123                 if (response.ok) {
124                         outmsg =
125                           "<div class=\"ctdl-msg-wrapper\">"                            // begin message wrapper
126                         + "<div class=\"ctdl-avatar\">"                                 // begin avatar
127                         + "<img src=\"/ctdl/u/" + msg.from + "/userpic\" width=\"32\" "
128                         + "onerror=\"this.parentNode.innerHTML='&lt;i class=&quot;fa fa-user-circle fa-2x&quot;&gt;&lt;/i&gt; '\">"
129                         + "</div>"                                                      // end avatar
130                         + "<div class=\"ctdl-msg-content\">"                            // begin content
131                         + "<div class=\"ctdl-msg-header\">"                             // begin header
132                         + "<span class=\"ctdl-msg-header-info\">"                       // begin header info on left side
133                         + "<span class=\"ctdl-username\"><a href=\"#\">"                // FIXME link to user profile
134                         + msg.from
135                         + "</a></span>"                                                 // end username
136                         + "<span class=\"ctdl-msgdate\">"
137                         + msg.time
138                         + "</span>"                                                     // end msgdate
139                         + "</span>"                                                     // end header info on left side
140                         + "<span class=\"ctdl-msg-header-buttons\">"                    // begin buttons on right side
141
142                         + "<span class=\"ctdl-msg-button\">"                            // Reply button FIXME make this work
143                         + "<a href=\"javascript:open_reply_box('"+prefix+"',"+msgnum+",false);\">"
144                         + "<i class=\"fa fa-reply\"></i> " 
145                         + _("Reply")
146                         + "</a></span>"
147
148                         + "<span class=\"ctdl-msg-button\">"                            // ReplyQuoted , only show in forums FIXME
149                         + "<a href=\"javascript:open_reply_box('"+prefix+"',"+msgnum+",true);\">"
150                         + "<i class=\"fa fa-comment\"></i> " 
151                         + _("ReplyQuoted")
152                         + "</a></span>"
153
154                         + "<span class=\"ctdl-msg-button\"><a href=\"#\">"              // Delete , show only with permission FIXME
155                         + "<i class=\"fa fa-trash\"></i> " 
156                         + _("Delete")
157                         + "</a></span>"
158
159                         + "</span>";                                                    // end buttons on right side
160                         if (msg.subj) {
161                                 outmsg +=
162                                 "<br><span class=\"ctdl-msgsubject\">" + msg.subj + "</span>";
163                         }
164                         outmsg +=
165                           "</div><br>"                                                  // end header
166                         + "<div class=\"ctdl-msg-body\">"                               // begin body
167                         + msg.text
168                         + "</div>"                                                      // end body
169                         + "</div>"                                                      // end content
170                         + "</div>"                                                      // end wrapper
171                         ;
172                         document.getElementById(prefix+msgnum).innerHTML = outmsg;
173                 }
174                 else {
175                         document.getElementById(prefix+msgnum).innerHTML = "ERROR";
176                 }
177                 document.getElementById(prefix+msgnum).style.display  = "inline";
178                 if (msgnum == scroll_to) {
179                         document.getElementById(prefix+msgnum).scrollIntoView({behavior: "smooth", block: "start", inline: "nearest"});
180                 }
181         }
182         fetch_message();
183 }
184
185
186 // Open a reply box directly below a specific message
187 function open_reply_box(prefix, msgnum, is_quoted) {
188         target_div_name = prefix+msgnum;
189         new_div_name = prefix + "_reply_to_" + msgnum;
190         document.getElementById(target_div_name).outerHTML += "<div id=\"" + new_div_name + "\">reply box put here</div>";
191
192         replybox =
193           "<div class=\"ctdl-msg-wrapper\">"                            // begin message wrapper
194         + "<div class=\"ctdl-avatar\">"                                 // begin avatar
195         + "<img src=\"/ctdl/u/" + "FIXME my name" + "/userpic\" width=\"32\" "
196         + "onerror=\"this.parentNode.innerHTML='&lt;i class=&quot;fa fa-user-circle fa-2x&quot;&gt;&lt;/i&gt; '\">"
197         + "</div>"                                                      // end avatar
198         + "<div class=\"ctdl-msg-content\">"                            // begin content
199         + "<div class=\"ctdl-msg-header\">"                             // begin header
200         + "<span class=\"ctdl-msg-header-info\">"                       // begin header info on left side
201         + "<span class=\"ctdl-username\"><a href=\"#\">"                // FIXME link to user profile
202         + "FIXME my name"
203         + "</a></span>"                                                 // end username
204         + "<span class=\"ctdl-msgdate\">"
205         + "FIXME now time"
206         + "</span>"                                                     // end msgdate
207         + "</span>"                                                     // end header info on left side
208         + "<span class=\"ctdl-msg-header-buttons\">"                    // begin buttons on right side
209
210         + "<span class=\"ctdl-msg-button\">"                            // bold button
211         + "<a href=\"javascript:void(0)\" onclick=\"forum_format('bold')\">"
212         + "<i class=\"fa fa-bold fa-fw\"></i>" 
213         + "</a></span>"
214
215         + "<span class=\"ctdl-msg-button\">"                            // italic button
216         + "<a href=\"javascript:void(0)\" onclick=\"forum_format('italic')\">" 
217         + "<i class=\"fa fa-italic fa-fw\"></i>" 
218         + "</a></span>"
219
220         + "<span class=\"ctdl-msg-button\">"                            // list button
221         + "<a href=\"javascript:void(0)\" onclick=\"forum_format('insertunorderedlist')\">"
222         + "<i class=\"fa fa-list fa-fw\"></i>" 
223         + "</a></span>"
224
225         + "<span class=\"ctdl-msg-button\">"                            // link button
226         + "<a href=\"javascript:void(0)\" onclick=\"forum_seturl()\">"
227         + "<i class=\"fa fa-link fa-fw\"></i>" 
228         + "</a></span>"
229
230         + "</span>";                                                    // end buttons on right side
231         if (msg.subj) {
232                 replybox +=
233                 "<br><span class=\"ctdl-msgsubject\">" + "FIXME subject" + "</span>";
234         }
235         replybox +=
236           "</div><br>"                                                  // end header
237
238                                                                         // begin body
239         + "<div class=\"ctdl-msg-body\" id=\"ctdl-editor-body\" style=\"padding:5px;\" contenteditable=\"true\">"
240         + "\n"                                                          // empty initial content
241         + "</div>"                                                      // end body
242
243         + "<div class=\"ctdl-msg-header\">"                             // begin footer
244         + "<span class=\"ctdl-msg-header-info\">"                       // begin footer info on left side
245         + "x"
246         + "</span>"                                                     // end footer info on left side
247         + "<span class=\"ctdl-msg-header-buttons\">"                    // begin buttons on right side
248
249         + "<span class=\"ctdl-msg-button\"><a href=\"javascript:forum_save_message('" +  new_div_name + "', '" + msgnum + "');\">"
250         + "<i class=\"fa fa-check\" style=\"color:green\"></i> "        // save button
251         + _("Post message")
252         + "</a></span>"
253
254         + "<span class=\"ctdl-msg-button\"><a href=\"javascript:forum_cancel_post('" +  new_div_name + "');\">"
255         + "<i class=\"fa fa-trash\" style=\"color:red\"></i> "          // cancel button
256         + _("Cancel")
257         + "</a></span>"
258
259         + "</span>"                                                     // end buttons on right side
260         + "</div><br>"                                                  // end footer
261
262
263         + "</div>"                                                      // end content
264         + "</div>"                                                      // end wrapper
265         ;
266
267         document.getElementById(new_div_name).innerHTML = replybox;
268         document.getElementById(new_div_name).scrollIntoView({behavior: "smooth", block: "end", inline: "nearest"});
269
270         // These actions must happen *after* the initial render loop completes.
271         setTimeout(function() {
272                 var tag = document.getElementById("ctdl-editor-body");
273                 tag.focus();                                            // sets the focus
274                 window.getSelection().collapse(tag.firstChild, 0);      // positions the cursor
275         }, 0);
276 }
277
278
279 // Abort a message post (it simply destroys the div)
280 function forum_cancel_post(div_name) {
281         document.getElementById(div_name).outerHTML = "";               // make it cease to exist
282 }
283
284
285 // Save the posted message to the server
286 function forum_save_message(div_name, reply_to_msgnum) {
287
288         document.body.style.cursor = "wait";
289
290         url = "/ctdl/r/" + escapeHTMLURI(current_room) + "/dummy_name_for_new_message";
291         boundary = randomString(20);
292         body_text =
293                 "--" + boundary + "\r\n"
294                 + "Content-type: text/html\r\n"
295                 + "Content-transfer-encoding: quoted-printable\r\n"
296                 + "\r\n"
297                 + quoted_printable_encode(
298                         "<html><body>" + document.getElementById("ctdl-editor-body").innerHTML + "</body></html>"
299                 ) + "\r\n"
300                 + "--" + boundary + "--\r\n"
301         ;
302
303         var request = new XMLHttpRequest();
304         request.open("PUT", url, true);
305         request.setRequestHeader("Content-type", "multipart/mixed; boundary=\"" + boundary + "\"");
306         request.onreadystatechange = function() {
307                 if (request.readyState == 4) {
308                         document.body.style.cursor = "default";
309                         if (Math.trunc(request.status / 100) == 2) {
310                                 alert("headers: " + request.getAllResponseHeaders());
311                                 document.getElementById(div_name).outerHTML = "";               // close the editor
312                         }
313                         else {
314                                 error_message = request.responseText;
315                                 if (error_message.length == 0) {
316                                         error_message = _("An error has occurred.");
317                                 }
318                                 alert(error_message);                                           // FIXME make this pretty
319                         }
320                 }
321         };
322         request.send(body_text);
323 }
324
325
326 function forum_format(command, value) {
327         document.execCommand(command, false, value);
328 }
329
330
331 function forum_seturl() {
332         var url = document.getElementById('txtFormatUrl').value;
333         var sText = document.getSelection();
334         document.execCommand('insertHTML', false, '<a href="' + url + '" target="_blank">' + sText + '</a>');
335         document.getElementById('txtFormatUrl').value = '';
336 }