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