Cleaned up some messy rendering in the bbs view.
[citadel.git] / webcit-ng / static / js / views.js
1 //
2 // Copyright (c) 2016-2019 by the citadel.org team
3 //
4 // This program is open source software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License version 3.
6 //
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 // GNU General Public License for more details.
11
12
13 // List of defined views shamelessly swiped from libcitadel headers
14 //
15 var views = {
16         VIEW_BBS                : 0,    /* Bulletin board view */
17         VIEW_MAILBOX            : 1,    /* Mailbox summary */
18         VIEW_ADDRESSBOOK        : 2,    /* Address book view */
19         VIEW_CALENDAR           : 3,    /* Calendar view */
20         VIEW_TASKS              : 4,    /* Tasks view */
21         VIEW_NOTES              : 5,    /* Notes view */
22         VIEW_WIKI               : 6,    /* Wiki view */
23         VIEW_CALBRIEF           : 7,    /* Brief Calendar view */
24         VIEW_JOURNAL            : 8,    /* Journal view */
25         VIEW_DRAFTS             : 9,    /* Drafts view */
26         VIEW_BLOG               : 10,   /* Blog view */
27         VIEW_QUEUE              : 11,   /* SMTP QUEUE rooms */
28         VIEW_WIKIMD             : 12,   /* Markdown Wiki view */
29 };
30
31
32 // This function is the dispatcher that determines the correct view for a room,
33 // and calls the correct renderer.  Greater/Less than bounds are accepted.
34 //
35 function render_room_view(gt_msg, lt_msg)
36 {
37         switch(current_view) {
38                 case views.VIEW_MAILBOX:                                                // FIXME view mail rooms as forums for now
39                 case views.VIEW_BBS:
40                         forum_readmessages("ctdl-main", gt_msg, lt_msg);
41                         break;
42                 default:
43                         document.getElementById("ctdl-main").innerHTML =
44                                 "The view for " + current_room + " is " + current_view + " but there is no renderer." ;
45                         break;
46         }
47
48 }
49
50
51 // Forum view (flat) -- let's have another go at this with the rendering done client-side
52 //
53 function forum_readmessages(target_div, gt_msg, lt_msg)
54 {
55         original_text = document.getElementById(target_div).innerHTML;          // in case we need to replace it after an error
56         document.getElementById(target_div).innerHTML = 
57                 "<div align=\"center\"><i class=\"fas fa-spinner fa-spin\"></i>&nbsp;&nbsp;"
58                 + _("Loading messages from server, please wait") + "</div>";
59
60         var request = new XMLHttpRequest();
61         if (lt_msg < 9999999999) {
62                 request.open("GET", "/ctdl/r/" + escapeHTMLURI(current_room) + "/msgs.lt|" + lt_msg, true);
63         }
64         else {
65                 request.open("GET", "/ctdl/r/" + escapeHTMLURI(current_room) + "/msgs.gt|" + gt_msg, true);
66         }
67         request.onreadystatechange = function() {
68                 if (this.readyState === 4) {
69                         if ((this.status / 100) == 2) {
70                                 msgs = JSON.parse(this.responseText);
71                                 document.getElementById(target_div).innerHTML = "" ;
72
73                                 // If we were given an explicit starting point, by all means start there.
74                                 // Note that we don't have to remove them from the array because we did a 'msgs gt|xxx' command to Citadel.
75                                 if (gt_msg > 0) {
76                                         msgs = msgs.slice(0, messages_per_page);
77                                 }
78
79                                 // Otherwise, show us the last 20 messages
80                                 else {
81                                         if (msgs.length > messages_per_page) {
82                                                 msgs = msgs.slice(msgs.length - messages_per_page);
83                                         }
84                                         new_old_div_name = randomString(5);
85                                         if (msgs.length < 1) {
86                                                 newlt = lt_msg;
87                                         }
88                                         else {
89                                                 newlt = msgs[0];
90                                         }
91                                         document.getElementById(target_div).innerHTML +=
92                                                 "<div id=\"" + new_old_div_name + "\">" +
93                                                 "<div align=\"center\">" +
94                                                 "<a href=\"javascript:forum_readmessages('" + new_old_div_name + "', 0, " + newlt + ");\">" +
95                                                 "<i class=\"fa fa-arrow-circle-up\"></i>&nbsp;&nbsp;" +
96                                                 _("Older posts") + "&nbsp;&nbsp;<i class=\"fa fa-arrow-circle-up\"></a></div></div></a></div></div>" ;
97                                 }
98
99                                 // Render an empty div for each message.  We will fill them in later.
100                                 for (var i in msgs) {
101                                         document.getElementById(target_div).innerHTML += "<div id=\"ctdl_msg_" + msgs[i] + "\"> </div>" ;
102                                         document.getElementById("ctdl_msg_"+msgs[i]).style.display = "none";
103                                 }
104                                 if (lt_msg == 9999999999) {
105                                         new_new_div_name = randomString(5);
106                                         if (msgs.length <= 0) {
107                                                 newgt = gt_msg;
108                                         }
109                                         else {
110                                                 newgt = msgs[msgs.length-1];
111                                         }
112                                         document.getElementById(target_div).innerHTML +=
113                                                 "<div id=\"" + new_new_div_name + "\">" +
114                                                 "<div align=\"center\">" +
115                                                 "<a href=\"javascript:forum_readmessages('" + new_new_div_name + "', " + newgt + ", 9999999999);\">" +
116                                                 "<i class=\"fa fa-arrow-circle-down\"></i>&nbsp;&nbsp;" +
117                                                 _("Newer posts") + "&nbsp;&nbsp;<i class=\"fa fa-arrow-circle-down\"></a></div></div>" ;
118                                 }
119
120                                 // Now figure out where to scroll to after rendering.
121                                 if (gt_msg > 0) {
122                                         scroll_to = msgs[0];
123                                 }
124                                 else if (lt_msg < 9999999999) {
125                                         scroll_to = msgs[msgs.length-1];
126                                 }
127                                 else if ( (logged_in) && (gt_msg == 0) && (lt_msg == 9999999999) ) {
128                                         scroll_to = msgs[msgs.length-1];
129                                 }
130                                 else {
131                                         scroll_to = msgs[0];            // FIXME this is too naive
132                                 }
133
134                                 // Render the individual messages in the divs
135                                 forum_render_messages(msgs, "ctdl_msg_", scroll_to)
136                         }
137                         else {
138                                 // if xhr fails, this will make the link reappear so the user can try again
139                                 document.getElementById(target_div).innerHTML = original_text;
140                         }
141                 }
142         };
143         request.send();
144         request = null;
145 }
146
147
148 // Render a range of messages, with the div prefix specified
149 //
150 function forum_render_messages(msgs, prefix, scroll_to)
151 {
152         for (i=0; i<msgs.length; ++i) {
153                 forum_render_one(prefix+msgs[i], msgs[i], scroll_to);
154         }
155 }
156
157
158 // 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.
159 function forum_render_one(div, msgnum, scroll_to)
160 {
161         var request = new XMLHttpRequest();
162         request.open("GET", "/ctdl/r/" + escapeHTMLURI(current_room) + "/" + msgs[i] + "/json", true);
163         request.onreadystatechange = function() {
164                 if (this.readyState === 4) {
165                         if ((this.status / 100) == 2) {
166                                 msg = JSON.parse(this.responseText);
167
168                                 document.getElementById(div).innerHTML =
169                                   "<div class=\"ctdl-msg-wrapper\">"                            // begin message wrapper
170                                 + "<div class=\"ctdl-avatar\">"                                 // begin avatar
171                                 + "<img src=\"/ctdl/u/" + msg.from + "/userpic\" width=\"32\" "
172                                 + "onerror=\"this.parentNode.innerHTML='&lt;i class=&quot;fa fa-user-circle fa-2x&quot;&gt;&lt;/i&gt; '\">"
173                                 + "</div>"                                                      // end avatar
174                                 + "<div class=\"ctdl-msg-content\">"                            // begin content
175                                 + "<div class=\"ctdl-msg-header\">"                             // begin header
176                                 + "<span class=\"ctdl-username\"><a href=\"#\">"                // FIXME link to user profile
177                                 + msg.from
178                                 + "</a></span> "
179                                 + "<span class=\"ctdl-msgdate\">"
180                                 + msg.time
181                                 + "</span> "
182                                 + "</div>"                                                      // end header
183                                 + "<div>"                                                       // begin body
184                                 + msg.text
185                                 + "</div>"                                                      // end body
186                                 + "</div>"                                                      // end content
187                                 + "</div>"                                                      // end wrapper
188                                 ;
189                         }
190                         else {
191                                 document.getElementById(div).innerHTML = "ERROR";
192                         }
193                         document.getElementById(div).style.display  = "inline";
194                         if (msgnum == scroll_to) {
195                                 window.location.hash = div;
196                         }
197                 }
198         };
199         request.send();
200         request = null;
201 }
202