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