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