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