Rewrote ctdl_startup() using fetch/await
[citadel.git] / webcit-ng / static / js / main.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 var current_room = "_BASEROOM_";
14 var new_messages = 0;
15 var total_messages = 0;
16 var default_view = 0;
17 var current_view = 0;
18 var logged_in = 0;
19 var current_user = _("Not logged in.");
20 var serv_info;
21 var last_seen = 0;
22 var messages_per_page = 20;
23 var march_list = [] ;
24
25
26 // Placeholder for when we add i18n later
27 function _(x) {
28         return x;
29 }
30
31
32 // Generate a random string of the specified length
33 // Useful for generating one-time-use div names
34 //
35 function randomString(length) {
36         var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghiklmnopqrstuvwxyz'.split('');
37         var str = '';
38
39         if (!length) {
40                 length = Math.floor(Math.random() * chars.length);
41         }
42         for (var i = 0; i < length; i++) {
43                 str += chars[Math.floor(Math.random() * chars.length)];
44         }
45         return str;
46 }
47
48
49 // string escape for html display
50 //
51 function escapeHTML(text) {
52         'use strict';
53         return text.replace(/[\"&<>]/g, function (a) {
54                 return {
55                         '"': '&quot;',
56                         '&': '&amp;',
57                         '<': '&lt;',
58                         '>': '&gt;'
59                 }[a];
60         });
61 }
62
63
64 // string escape for html display
65 //
66 function escapeHTMLURI(text) {
67         'use strict';
68         return text.replace(/./g, function (a) {
69                 return '%' + a.charCodeAt(0).toString(16);
70         });
71 }
72
73
74 // string escape for JavaScript string
75 //
76 function escapeJS(text) {
77         'use strict';
78         return text.replace(/[\"\']/g, function (a) {
79                 return '\\' + a ;
80         });
81 }
82
83
84 // This is called at the very beginning of the main page load.
85 //
86 function ctdl_startup() {
87
88         const csa = async () => {
89                 console.log("starting");
90                 const response = await fetch("/ctdl/c/info");
91                 const the_text = await(response.text());
92
93                 if (response.ok) {
94                         serv_info = JSON.parse(the_text);
95                         if (serv_info.serv_rev_level < 905) {
96                                 alert("Citadel server is too old, some functions may not work");
97                         }
98         
99                         update_banner();
100         
101                         // for now, show a room list in the main div
102                         gotoroom("_BASEROOM_");
103                         display_room_list();
104                 }
105         }
106         csa();
107 }
108
109
110 // Display a room list in the main div.
111 //
112 function display_room_list() {
113         document.getElementById("roomlist").innerHTML = "<img src=\"/ctdl/s/throbber.gif\" />" ;                // show throbber while loading
114
115         var request = new XMLHttpRequest();
116         request.open("GET", "/ctdl/r/", true);
117         request.onreadystatechange = function() {
118                 if ((this.readyState === 4) && ((this.status / 100) == 2)) {
119                         display_room_list_renderer(JSON.parse(this.responseText));
120                 }
121         };
122         request.send();
123         request = null;
124 }
125
126
127 // Renderer for display_room_list()
128 //
129 function display_room_list_renderer(data) {
130         data = data.sort(function(a,b) {
131                 if (a.floor != b.floor) {
132                         return(a.floor - b.floor);
133                 }
134                 if (a.rorder != b.rorder) {
135                         return(a.rorder - b.rorder);
136                 }
137                 return(a.name < b.name);
138         });
139
140         new_roomlist_text = "<ul>" ;
141
142         for (var i in data) {
143                 if (i > 0) {
144                         if (data[i].floor != data[i-1].floor) {
145                                 new_roomlist_text = new_roomlist_text + "<li class=\"divider\"></li>" ;
146                         }
147                 }
148                 new_roomlist_text = new_roomlist_text +
149                         "<li>"
150                         + (data[i].hasnewmsgs ? "<b>" : "")
151                         + "<a href=\"javascript:gotoroom('" + escapeJS(escapeHTML(data[i].name)) + "');\">"
152                         + escapeHTML(data[i].name)
153                         + (data[i].hasnewmsgs ? "</b>" : "")
154                         + "</a></li>"
155                 ;
156         }
157         new_roomlist_text = new_roomlist_text + "</ul>";
158         document.getElementById("roomlist").innerHTML = new_roomlist_text ;
159 }
160
161
162 // Update the "banner" div with all relevant info.
163 //
164 function update_banner() {
165         detect_logged_in();
166         if (current_room) {
167                 document.getElementById("ctdl_banner_title").innerHTML = current_room;
168                 document.title = current_room;
169         }
170         else {
171                 document.getElementById("ctdl_banner_title").innerHTML = serv_info.serv_humannode;
172         }
173         document.getElementById("current_user").innerHTML = current_user ;
174         if (logged_in) {
175                 document.getElementById("lilo").innerHTML = "<a href=\"/ctdl/a/logout\">" + _("Log off") + "</a>" ;
176         }
177         else {
178                 document.getElementById("lilo").innerHTML = "<a href=\"javascript:display_login_screen('')\">" + _("Log in") + "</a>" ;
179         }
180 }
181
182
183 // goto room
184 //
185 function gotoroom(roomname) {
186         var request = new XMLHttpRequest();
187         request.open("GET", "/ctdl/r/" + escapeHTMLURI(roomname) + "/", true);
188         request.onreadystatechange = function() {
189                 if ((this.readyState === 4) && ((this.status / 100) == 2)) {
190                         gotoroom_2(JSON.parse(this.responseText));
191                 }
192         };
193         request.send();
194         request = null;
195 }
196 function gotoroom_2(data) {
197         current_room = data.name;
198         new_messages = data.new_messages;
199         total_messages = data.total_messages;
200         current_view = data.current_view;
201         default_view = data.default_view;
202         last_seen = data.last_seen;
203         update_banner();
204         render_room_view(0, 9999999999);
205 }
206
207
208 // Goto next room with unread messages
209 // which_oper is 0=ungoto, 1=skip, 2=goto
210 //
211 function gotonext(which_oper) {
212         if (which_oper != 2) return;            // FIXME implement the other two
213         if (march_list.length == 0) {
214                 load_new_march_list();          // we will recurse back here
215         }
216         else {
217                 next_room = march_list[0].name;
218                 march_list.splice(0, 1);
219                 console.log("going to " + next_room + " , " + march_list.length + " rooms remaining in march list");
220                 gotoroom(next_room);
221         }
222 }
223
224
225 // Called by gotonext() when the march list is empty.
226 //
227 function load_new_march_list() {
228         var request = new XMLHttpRequest();
229         request.open("GET", "/ctdl/r/", true);
230         request.onreadystatechange = function() {
231                 if ((this.readyState === 4) && ((this.status / 100) == 2)) {
232                         march_list = (JSON.parse(this.responseText));
233                         march_list = march_list.filter(function(room) {
234                                 return room.hasnewmsgs;
235                         });
236                         march_list = march_list.sort(function(a,b) {
237                                 if (a.floor != b.floor) {
238                                         return(a.floor - b.floor);
239                                 }
240                                 if (a.rorder != b.rorder) {
241                                         return(a.rorder - b.rorder);
242                                 }
243                                 return(a.name < b.name);
244                         });
245                         march_list.push({name:"_BASEROOM_",known:true,hasnewmsgs:true,floor:0});
246                         gotonext();
247                 }
248         };
249         request.send();
250         request = null;
251 }