2 // Copyright (c) 2016-2018 by the citadel.org team
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.
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.
13 // List of defined views shamelessly swiped from libcitadel headers
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 */
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.
35 function render_room_view(gt_msg, lt_msg)
39 case views.VIEW_MAILBOX: // FIXME view mail rooms as forums for now
41 forum_readmessages("ctdl-main", gt_msg, lt_msg);
44 document.getElementById("ctdl-main").innerHTML = "The view for " + current_room + " is " + current_view + " but there is no renderer." ;
51 // Forum view (flat) -- let's have another go at this with the rendering done client-side
53 function forum_readmessages(target_div, gt_msg, lt_msg)
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 "<i class=\"fas fa-spinner fa-spin\"></i> "
58 + _("Loading messages from server, please wait") ;
60 var request = new XMLHttpRequest();
61 if (lt_msg < 9999999999)
63 request.open("GET", "/ctdl/r/" + escapeHTMLURI(current_room) + "/msgs.lt|" + lt_msg, true);
67 request.open("GET", "/ctdl/r/" + escapeHTMLURI(current_room) + "/msgs.gt|" + gt_msg, true);
69 request.onreadystatechange = function()
71 if (this.readyState === 4)
73 if ((this.status / 100) == 2)
75 msgs = JSON.parse(this.responseText);
76 document.getElementById(target_div).innerHTML = "" ;
78 // If we were given an explicit starting point, by all means start there.
79 // Note that we don't have to remove them from the array because we did a 'msgs gt|xxx' command to Citadel.
82 msgs = msgs.slice(0, messages_per_page);
85 // Otherwise, show us the last 20 messages
88 if (msgs.length > messages_per_page)
90 msgs = msgs.slice(msgs.length - messages_per_page);
92 new_old_div_name = randomString(5);
101 document.getElementById(target_div).innerHTML +=
102 "<div id=\"" + new_old_div_name + "\">" +
103 "<a href=\"javascript:forum_readmessages('" + new_old_div_name + "', 0, " + newlt + ");\">" +
104 "link to msgs less than " + newlt + "</a></div>" ;
107 // Render the divs (we will fill them in later)
110 document.getElementById(target_div).innerHTML += "<div id=\"ctdl_msg_" + msgs[i] + "\">#" + msgs[i] + "</div>" ;
112 if (lt_msg == 9999999999)
114 new_new_div_name = randomString(5);
115 if (msgs.length <= 0)
121 newgt = msgs[msgs.length-1];
123 document.getElementById(target_div).innerHTML +=
124 "<div id=\"" + new_new_div_name + "\">" +
125 "<a href=\"javascript:forum_readmessages('" + new_new_div_name + "', " + newgt + ", 9999999999);\">" +
126 "link to msgs greater than " + newgt + "</a></div>" ;
129 // Now figure out where to scroll to after rendering.
134 else if (lt_msg < 9999999999)
136 scroll_to = msgs[msgs.length-1];
138 else if ( (logged_in) && (gt_msg == 0) && (lt_msg == 9999999999) )
140 scroll_to = msgs[msgs.length-1];
144 scroll_to = msgs[0]; // FIXME this is too naive
147 // Render the individual messages in the divs
148 forum_render_messages(msgs, "ctdl_msg_", scroll_to)
152 document.getElementById(target_div).innerHTML = original_text; // this will make the link reappear so the user can try again
161 // Render a range of messages, with the div prefix specified
163 function forum_render_messages(msgs, prefix, scroll_to)
165 for (i=0; i<msgs.length; ++i)
167 forum_render_one(prefix+msgs[i], msgs[i], scroll_to)
172 // 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.
173 function forum_render_one(div, msgnum, scroll_to)
175 var request = new XMLHttpRequest();
176 request.open("GET", "/ctdl/r/" + escapeHTMLURI(current_room) + "/" + msgs[i] + "/json", true);
177 request.onreadystatechange = function()
179 if (this.readyState === 4)
181 if ((this.status / 100) == 2)
183 msg = JSON.parse(this.responseText);
185 document.getElementById(div).innerHTML =
186 "<div class=\"ctdl-msg-wrapper\">" // begin message wrapper
187 + "<div class=\"ctdl-avatar\">" // begin avatar
188 + "<i class=\"fa fa-user-circle fa-2x\"></i> " // FIXME temporary avatar
189 + "</div>" // end avatar
190 + "<div>" // begin content
191 + "<div>" // begin header
192 + "<span class=\"ctdl-username\"><a href=\"#\">" // FIXME link to user profile
195 + "<span class=\"ctdl-msgdate\">"
198 + "</div>" // end header
199 + "<div>" // begin body
201 + "</div>" // end body
202 + "</div>" // end content
203 + "</div>" // end wrapper
208 document.getElementById(div).innerHTML = "ERROR";
210 if (msgnum == scroll_to)
212 window.location.hash = div;