b3b8212316d40f5d69a690b4506c136ae1c0dead
[citadel.git] / webcit-ng / static / js / views.js
1 //
2 // Copyright (c) 2016-2018 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.
34 //
35 function render_room_view(min_msg, max_msg)
36 {
37         switch(current_view)
38         {
39                 case views.VIEW_MAILBOX:                                                // FIXME view mail rooms as forums for now
40                 case views.VIEW_BBS:
41                         forum_readmessages("ctdl-main", min_msg, max_msg);
42                         break;
43                 default:
44                         document.getElementById("ctdl-main").innerHTML = "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, min_msg, max_msg)
54 {
55         var innerdivname = randomString(5);
56         document.getElementById(target_div).innerHTML = "<div id=\"" + innerdivname +
57                 "\"><br><br><br><center><h5><i class=\"fas fa-spinner fa-spin\"></i>&nbsp;&nbsp;"
58                 + _("Loading messages from server, please wait") + "</h5></center></div>" ;
59
60         var request = new XMLHttpRequest();
61         if (max_msg < 9999999999)
62         {
63                 request.open("GET", "/ctdl/r/" + escapeHTMLURI(current_room) + "/msgs.lt|" + max_msg, true);
64         }
65         else
66         {
67                 request.open("GET", "/ctdl/r/" + escapeHTMLURI(current_room) + "/msgs.gt|" + min_msg, true);
68         }
69         request.onreadystatechange = function()
70         {
71                 if (this.readyState === 4)
72                 {
73                         if ((this.status / 100) == 2)
74                         {
75                                 msgs = JSON.parse(this.responseText);
76                                 document.getElementById(innerdivname).innerHTML = "" ;
77
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.
80                                 if (min_msg > 0)
81                                 {
82                                         msgs = msgs.slice(0, messages_per_page);
83                                 }
84
85                                 // show us the last 20 messages and scroll to the bottom (this will become the not-logged-in behavior)
86                                 else if ((logged_in) | (!logged_in) | (max_msg < 9999999999))
87                                 {
88                                         if (msgs.length > messages_per_page)
89                                         {
90                                                 msgs = msgs.slice(msgs.length - messages_per_page);
91                                         }
92                                         new_old_div_name = randomString(5);
93                                         document.getElementById(innerdivname).innerHTML +=
94                                                 "<div id=\"" + new_old_div_name + "\">" +
95                                                 "<a href=\"javascript:forum_readmessages('" + new_old_div_name + "', 0, " + msgs[0] + ");\">" +
96                                                 "link to msgs less than " + msgs[0] + "</a></div>" ;
97                                 }
98
99                                 // It's render time, bitchez!
100                                 for (var i in msgs)
101                                 {
102                                         if ((msgs[i] > min_msg) && (msgs[i] < max_msg))
103                                         {
104                                                 document.getElementById(innerdivname).innerHTML +=
105                                                         "<div id=\"ctdl_msg_" + msgs[i] + "\">message #" + msgs[i] + "</div>" ;
106                                         }
107                                 }
108                                 if (max_msg == 9999999999)
109                                 {
110                                         new_new_div_name = randomString(5);
111                                         if (msgs.length <= 0)
112                                         {
113                                                 newgt = min_msg;
114                                         }
115                                         else
116                                         {
117                                                 newgt = msgs[msgs.length-1];
118                                         }
119                                         document.getElementById(innerdivname).innerHTML +=
120                                                 "<div id=\"" + new_new_div_name + "\">" +
121                                                 "<a href=\"javascript:forum_readmessages('" + new_new_div_name + "', " + newgt + ", 9999999999);\">" +
122                                                 "link to msgs greater than " + newgt + "</a></div>" ;
123                                 }
124                         }
125                         else
126                         {
127                                 document.getElementById(innerdivname).innerHTML = this.status ;         // error message
128                         }
129                 }
130         };
131         request.send();
132         request = null;
133 }