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