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