7e10e3668b5d3b79a2499d9e79a826c20adacfe5
[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.  Greater/Less than bounds are accepted.
34 //
35 function render_room_view(gt_msg, lt_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", gt_msg, lt_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, gt_msg, lt_msg)
54 {
55         document.getElementById(target_div).innerHTML = 
56                 "<i class=\"fas fa-spinner fa-spin\"></i>&nbsp;&nbsp;"
57                 + _("Loading messages from server, please wait") ;
58
59         var request = new XMLHttpRequest();
60         if (lt_msg < 9999999999)
61         {
62                 request.open("GET", "/ctdl/r/" + escapeHTMLURI(current_room) + "/msgs.lt|" + lt_msg, true);
63         }
64         else
65         {
66                 request.open("GET", "/ctdl/r/" + escapeHTMLURI(current_room) + "/msgs.gt|" + gt_msg, true);
67         }
68         request.onreadystatechange = function()
69         {
70                 if (this.readyState === 4)
71                 {
72                         if ((this.status / 100) == 2)
73                         {
74                                 msgs = JSON.parse(this.responseText);
75                                 document.getElementById(target_div).innerHTML = "" ;
76
77                                 // If we were given an explicit starting point, by all means start there.
78                                 // Note that we don't have to remove them from the array because we did a 'msgs gt|xxx' command to Citadel.
79                                 if (gt_msg > 0)
80                                 {
81                                         msgs = msgs.slice(0, messages_per_page);
82                                 }
83
84                                 // show us the last 20 messages and scroll to the bottom (this will become the not-logged-in behavior)
85                                 else if ((logged_in) | (!logged_in) | (lt_msg < 9999999999))
86                                 {
87                                         if (msgs.length > messages_per_page)
88                                         {
89                                                 msgs = msgs.slice(msgs.length - messages_per_page);
90                                         }
91                                         new_old_div_name = randomString(5);
92                                         if (msgs.length < 1)
93                                         {
94                                                 newlt = lt_msg;
95                                         }
96                                         else
97                                         {
98                                                 newlt = msgs[0];
99                                         }
100                                         document.getElementById(target_div).innerHTML +=
101                                                 "<div id=\"" + new_old_div_name + "\">" +
102                                                 "<a href=\"javascript:forum_readmessages('" + new_old_div_name + "', 0, " + newlt + ");\">" +
103                                                 "link to msgs less than " + newlt + "</a></div>" ;
104                                 }
105
106                                 // Render the divs (we will fill them in later)
107                                 for (var i in msgs)
108                                 {
109                                         document.getElementById(target_div).innerHTML += "<div id=\"ctdl_msg_" + msgs[i] + "\">#" + msgs[i] + "</div>" ;
110                                 }
111                                 if (lt_msg == 9999999999)
112                                 {
113                                         new_new_div_name = randomString(5);
114                                         if (msgs.length <= 0)
115                                         {
116                                                 newgt = gt_msg;
117                                         }
118                                         else
119                                         {
120                                                 newgt = msgs[msgs.length-1];
121                                         }
122                                         document.getElementById(target_div).innerHTML +=
123                                                 "<div id=\"" + new_new_div_name + "\">" +
124                                                 "<a href=\"javascript:forum_readmessages('" + new_new_div_name + "', " + newgt + ", 9999999999);\">" +
125                                                 "link to msgs greater than " + newgt + "</a></div>" ;
126                                 }
127
128                                 // Render the individual messages in the divs
129                                 render_messages(msgs, "ctdl_msg_", views.VIEW_BBS);
130                         }
131                         else
132                         {
133                                 document.getElementById(target_div).innerHTML = "error put it back";
134                                 alert("ERROR " + this.status + " retrieving messages from server");
135                         }
136                 }
137         };
138         request.send();
139         request = null;
140 }
141
142
143 // Render a range of messages, in the view specified, with the div prefix specified
144 //
145 function render_messages(msgs, prefix, view)
146 {
147         for (var i in msgs)
148         {
149                 document.getElementById(prefix + msgs[i]).innerHTML = "<b>Message " + msgs[i] + " got rendered!!!</b>";
150         }
151
152 }