0abc6ae7f8a577399f092b870c694f47b3bfb2b2
[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                 serv_info = await(response.json());
92
93                 if (response.ok) {
94                         if (serv_info.serv_rev_level < 905) {
95                                 alert("Citadel server is too old, some functions may not work");
96                         }
97         
98                         update_banner();
99         
100                         // for now, show a room list in the main div
101                         gotoroom("_BASEROOM_");
102                         display_room_list();
103                 }
104         }
105         csa();
106 }
107
108
109 // Display a room list in the main div.
110 //
111 function display_room_list() {
112         document.getElementById("roomlist").innerHTML = "<img src=\"/ctdl/s/throbber.gif\" />" ;                // show throbber while loading
113
114         var request = new XMLHttpRequest();
115         request.open("GET", "/ctdl/r/", true);
116         request.onreadystatechange = function() {
117                 if ((this.readyState === 4) && ((this.status / 100) == 2)) {
118                         display_room_list_renderer(JSON.parse(this.responseText));
119                 }
120         };
121         request.send();
122         request = null;
123 }
124
125
126 // Renderer for display_room_list()
127 //
128 function display_room_list_renderer(data) {
129         data = data.sort(function(a,b) {
130                 if (a.floor != b.floor) {
131                         return(a.floor - b.floor);
132                 }
133                 if (a.rorder != b.rorder) {
134                         return(a.rorder - b.rorder);
135                 }
136                 return(a.name < b.name);
137         });
138
139         new_roomlist_text = "<ul>" ;
140
141         for (var i in data) {
142                 if (i > 0) {
143                         if (data[i].floor != data[i-1].floor) {
144                                 new_roomlist_text = new_roomlist_text + "<li class=\"divider\"></li>" ;
145                         }
146                 }
147                 new_roomlist_text = new_roomlist_text +
148                         "<li>"
149                         + (data[i].hasnewmsgs ? "<b>" : "")
150                         + "<a href=\"javascript:gotoroom('" + escapeJS(escapeHTML(data[i].name)) + "');\">"
151                         + escapeHTML(data[i].name)
152                         + (data[i].hasnewmsgs ? "</b>" : "")
153                         + "</a></li>"
154                 ;
155         }
156         new_roomlist_text = new_roomlist_text + "</ul>";
157         document.getElementById("roomlist").innerHTML = new_roomlist_text ;
158 }
159
160
161 // Update the "banner" div with all relevant info.
162 //
163 function update_banner() {
164         detect_logged_in();
165         if (current_room) {
166                 document.getElementById("ctdl_banner_title").innerHTML = current_room;
167                 document.title = current_room;
168         }
169         else {
170                 document.getElementById("ctdl_banner_title").innerHTML = serv_info.serv_humannode;
171         }
172         document.getElementById("current_user").innerHTML = current_user ;
173         if (logged_in) {
174                 document.getElementById("lilo").innerHTML = "<a href=\"/ctdl/a/logout\">" + _("Log off") + "</a>" ;
175         }
176         else {
177                 document.getElementById("lilo").innerHTML = "<a href=\"javascript:display_login_screen('')\">" + _("Log in") + "</a>" ;
178         }
179 }
180
181
182 // goto room
183 //
184 function gotoroom(roomname) {
185         var request = new XMLHttpRequest();
186         request.open("GET", "/ctdl/r/" + escapeHTMLURI(roomname) + "/", true);
187         request.onreadystatechange = function() {
188                 if ((this.readyState === 4) && ((this.status / 100) == 2)) {
189                         gotoroom_2(JSON.parse(this.responseText));
190                 }
191         };
192         request.send();
193         request = null;
194 }
195 function gotoroom_2(data) {
196         current_room = data.name;
197         new_messages = data.new_messages;
198         total_messages = data.total_messages;
199         current_view = data.current_view;
200         default_view = data.default_view;
201         last_seen = data.last_seen;
202         update_banner();
203         render_room_view(0, 9999999999);
204 }
205
206
207 // Goto next room with unread messages
208 // which_oper is 0=ungoto, 1=skip, 2=goto
209 //
210 function gotonext(which_oper) {
211         if (which_oper != 2) return;            // FIXME implement the other two
212         if (march_list.length == 0) {
213                 load_new_march_list();          // we will recurse back here
214         }
215         else {
216                 next_room = march_list[0].name;
217                 march_list.splice(0, 1);
218                 console.log("going to " + next_room + " , " + march_list.length + " rooms remaining in march list");
219                 gotoroom(next_room);
220         }
221 }
222
223
224 // Called by gotonext() when the march list is empty.
225 //
226 function load_new_march_list() {
227         var request = new XMLHttpRequest();
228         request.open("GET", "/ctdl/r/", true);
229         request.onreadystatechange = function() {
230                 if ((this.readyState === 4) && ((this.status / 100) == 2)) {
231                         march_list = (JSON.parse(this.responseText));
232                         march_list = march_list.filter(function(room) {
233                                 return room.hasnewmsgs;
234                         });
235                         march_list = march_list.sort(function(a,b) {
236                                 if (a.floor != b.floor) {
237                                         return(a.floor - b.floor);
238                                 }
239                                 if (a.rorder != b.rorder) {
240                                         return(a.rorder - b.rorder);
241                                 }
242                                 return(a.name < b.name);
243                         });
244                         march_list.push({name:"_BASEROOM_",known:true,hasnewmsgs:true,floor:0});
245                         gotonext();
246                 }
247         };
248         request.send();
249         request = null;
250 }