2ede01340c3a1ec0d7c2ffcc0dcc6eca592fd19c
[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 // This is called at the very beginning of the main page load.
35 //
36 ctdl_startup = async() => {
37         response = await fetch("/ctdl/c/info");
38
39         if (response.ok) {
40                 serv_info = await(response.json());
41                 if (serv_info.serv_rev_level < 905) {
42                         alert(_("Citadel server is too old, some functions may not work"));
43                 }
44
45                 update_banner();
46
47                 // for now, show a room list in the main div
48                 gotoroom("_BASEROOM_");
49                 display_room_list();
50         }
51         else {
52                 document.getElementById("ctdl-main").innerHTML =
53                         "<div class=\"w3-panel w3-red\"><p>"
54                         + _("This program was unable to connect or stay connected to the Citadel server.  Please report this problem to your system administrator.")
55                         + "</div>";
56         }
57 }
58
59
60 // Display a room list in the main div.
61 //
62 function display_room_list() {
63         document.getElementById("roomlist").innerHTML = "<img src=\"/ctdl/s/static/throbber.gif\" />";  // show throbber while loading
64
65         fetch_room_list = async() => {
66                 response = await fetch("/ctdl/r/");
67                 room_list = await(response.json());
68                 if (response.ok) {
69                         display_room_list_renderer(room_list);
70                 }
71         }
72         fetch_room_list();
73 }
74
75
76 // Renderer for display_room_list()
77 //
78 function display_room_list_renderer(data) {
79         data = data.sort(function(a,b) {
80                 if (a.floor != b.floor) {
81                         return(a.floor - b.floor);
82                 }
83                 if (a.rorder != b.rorder) {
84                         return(a.rorder - b.rorder);
85                 }
86                 return(a.name < b.name);
87         });
88
89         new_roomlist_text = "<ul>" ;
90
91         for (var i in data) {
92                 if (i > 0) {
93                         if (data[i].floor != data[i-1].floor) {
94                                 new_roomlist_text = new_roomlist_text + "<li class=\"divider\"></li>" ;
95                         }
96                 }
97                 new_roomlist_text = new_roomlist_text +
98                         "<li>"
99                         + (data[i].hasnewmsgs ? "<b>" : "")
100                         + "<a href=\"javascript:gotoroom('" + escapeJS(escapeHTML(data[i].name)) + "');\">"
101                         + escapeHTML(data[i].name)
102                         + (data[i].hasnewmsgs ? "</b>" : "")
103                         + "</a></li>"
104                 ;
105         }
106         new_roomlist_text = new_roomlist_text + "</ul>";
107         document.getElementById("roomlist").innerHTML = new_roomlist_text ;
108 }
109
110
111 // Update the "banner" div with all relevant info.
112 //
113 function update_banner() {
114         detect_logged_in();
115         if (current_room) {
116                 document.getElementById("ctdl_banner_title").innerHTML = current_room;
117                 document.title = current_room;
118         }
119         else {
120                 document.getElementById("ctdl_banner_title").innerHTML = serv_info.serv_humannode;
121         }
122         document.getElementById("current_user").innerHTML = current_user ;
123         if (logged_in) {
124                 document.getElementById("lilo").innerHTML = "<a href=\"/ctdl/a/logout\">" + _("Log off") + "</a>" ;
125         }
126         else {
127                 document.getElementById("lilo").innerHTML = "<a href=\"javascript:display_login_screen('')\">" + _("Log in") + "</a>" ;
128         }
129 }
130
131
132 // goto room
133 //
134 function gotoroom(roomname) {
135
136         fetch_room = async() => {
137                 response = await fetch("/ctdl/r/" + escapeHTMLURI(roomname) + "/");
138                 data = await(response.json());
139                 if (response.ok) {
140                         current_room = data.name;
141                         new_messages = data.new_messages;
142                         total_messages = data.total_messages;
143                         current_view = data.current_view;
144                         default_view = data.default_view;
145                         last_seen = data.last_seen;
146                         update_banner();
147                         render_room_view(0, 9999999999);
148                 }
149         }
150         fetch_room();
151 }
152
153
154 // Goto next room with unread messages
155 // which_oper is 0=ungoto, 1=skip, 2=goto
156 //
157 function gotonext(which_oper) {
158         if (which_oper != 2) return;            // FIXME implement the other two
159         if (march_list.length == 0) {
160                 load_new_march_list();          // we will recurse back here
161         }
162         else {
163                 next_room = march_list[0].name;
164                 march_list.splice(0, 1);
165                 console.log("going to " + next_room + " , " + march_list.length + " rooms remaining in march list");
166                 gotoroom(next_room);
167         }
168 }
169
170
171 // Called by gotonext() when the march list is empty.
172 //
173 function load_new_march_list() {
174         fetchm = async() => {
175                 response = await fetch("/ctdl/r/");
176                 march_list = await(response.json());
177                 if (response.ok) {
178                         march_list = march_list.filter(function(room) {
179                                 return room.hasnewmsgs;
180                         });
181                         march_list = march_list.sort(function(a,b) {
182                                 if (a.floor != b.floor) {
183                                         return(a.floor - b.floor);
184                                 }
185                                 if (a.rorder != b.rorder) {
186                                         return(a.rorder - b.rorder);
187                                 }
188                                 return(a.name < b.name);
189                         });
190                         march_list.push({name:"_BASEROOM_",known:true,hasnewmsgs:true,floor:0});
191                         gotonext();
192                 }
193         }
194         fetchm();
195 }
196
197
198 // Activate the "Loading..." modal
199 function activate_loading_modal() {
200         document.getElementById("ctdl_big_modal_content").innerHTML =
201                 "<i class=\"fas fa-spinner fa-spin\"></i>&nbsp;&nbsp;"
202                 + _("Loading messages from server, please wait");
203         document.getElementById("ctdl_big_modal").style.display = "block";
204 }
205
206
207 // Disappear the "Loading..." modal
208 function deactivate_loading_modal() {
209         document.getElementById("ctdl_big_modal").style.display = "none";
210 }