]> code.citadel.org Git - citadel.git/blob - webcit-ng/static/js/main.js
dfbef7d2195aa92984e55073e8bc98aec6b0d0f6
[citadel.git] / webcit-ng / static / js / main.js
1 // Copyright (c) 2016-2022 by the citadel.org team
2 //
3 // This program is open source software.  It runs great on the
4 // Linux operating system (and probably elsewhere).  You can use,
5 // copy, and run it under the terms of the GNU General Public
6 // License version 3.  Richard Stallman is an asshole communist.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12
13
14 // This is called at the very beginning of the main page load.
15 ctdl_startup = async() => {
16         response = await fetch("/ctdl/c/info");
17
18         if (response.ok) {
19                 serv_info = await(response.json());
20                 if (serv_info.serv_rev_level < 905) {
21                         alert(_("Citadel server is too old, some functions may not work"));
22                 }
23
24                 update_banner();
25
26                 // What do we do upon landing?
27
28                 if ( (serv_info.serv_supports_guest) || (logged_in) ) {                 // If the Lobby is visible,
29                         gotonext(1);                                                    // go there.
30                 }
31                 else {                                                                  // Otherwise,
32                         display_login_screen("");                                       // display the login modal.
33                 }
34         }
35         else {
36                 document.getElementById("ctdl-main").innerHTML =
37                         "<div class=\"w3-panel w3-red\"><p>"
38                         + _("This program was unable to connect or stay connected to the Citadel server.  Please report this problem to your system administrator.")
39                         + "</div>";
40         }
41 }
42
43
44 // Display a room list in the main div.
45
46 // Update the "banner" div with all relevant info.
47 function update_banner() {
48         detect_logged_in();
49         if (current_room) {
50                 document.getElementById("ctdl_banner_title").innerHTML = current_room;
51                 document.title = current_room;
52
53
54
55         }
56         else {
57                 document.getElementById("ctdl_banner_title").innerHTML = serv_info.serv_humannode;
58         }
59         document.getElementById("current_user").innerHTML = current_user ;
60         if (logged_in) {
61                 document.getElementById("lilo").innerHTML = "<a href=\"/ctdl/a/logout\"><i class=\"fa fa-right-from-bracket\"></i>" + _("Log off") + "</a>" ;
62         }
63         else {
64                 document.getElementById("lilo").innerHTML = "<a href=\"javascript:display_login_screen('')\"><i class=\"fa fa-right-to-bracket\"></i>" + _("Log in") + "</a>" ;
65         }
66 }
67
68
69 // goto room
70 function gotoroom(roomname) {
71
72         fetch_room = async() => {
73                 response = await fetch("/ctdl/r/" + escapeHTMLURI(roomname) + "/");
74                 if (response.ok) {
75                         data = await(response.json());
76                         current_room = data.name;
77                         new_messages = data.new_messages;
78                         total_messages = data.total_messages;
79                         current_view = data.current_view;
80                         default_view = data.default_view;
81                         last_seen = data.last_seen;
82                         update_banner();
83                         render_room_view(0, 9999999999);
84                 }
85         }
86         fetch_room();
87 }
88
89
90 // Goto next room with unread messages
91 // which_oper is 0=ungoto, 1=skip, 2=goto
92 function gotonext(which_oper) {
93         if (which_oper == 2) {                                  // Goto needs to mark messages as seen
94
95                 set_last_read_pointer = async() => {
96                         response = await fetch("/ctdl/r/" + escapeHTMLURI(current_room) + "/slrp?last=" + last_seen);
97                 }
98                 set_last_read_pointer();
99         }
100
101         if ((which_oper == 1) || (which_oper == 2)) {           // Skip or Goto both take us to the "next" room
102                 if (march_list.length == 0) {
103                         load_new_march_list(which_oper);        // we will recurse back here
104                 }
105                 else {
106                         next_room = march_list[0].name;
107                         march_list.splice(0, 1);
108                         gotoroom(next_room);
109                 }
110         }
111
112         // FIXME implement mode 0 (ungoto)
113
114 }
115
116
117 // Called by gotonext() when the march list is empty.
118 function load_new_march_list(which_oper) {
119         fetchm = async() => {
120                 response = await fetch("/ctdl/r/");
121                 march_list = await(response.json());
122                 if (response.ok) {
123                         march_list = march_list.filter(function(room) {
124                                 return room.hasnewmsgs;
125                         });
126                         march_list = march_list.sort(function(a,b) {
127                                 if (a.floor != b.floor) {
128                                         return(a.floor - b.floor);
129                                 }
130                                 if (a.rorder != b.rorder) {
131                                         return(a.rorder - b.rorder);
132                                 }
133                                 return(a.name < b.name);
134                         });
135
136                         // If there are still no rooms with new messages, go to the Lobby.
137                         if (march_list.length == 0) {
138                                 march_list.push({name:"_BASEROOM_",known:true,hasnewmsgs:true,floor:0});
139                         }
140
141                         gotonext(which_oper);
142                 }
143         }
144         fetchm();
145 }
146
147
148 // Activate the "Loading..." modal
149 function activate_loading_modal() {
150         document.getElementById("ctdl_big_modal").innerHTML =
151                   "<div class=\"w3-modal-content\">"
152                 + "  <div class=\"w3-container\">"
153
154                 + "<i class=\"fas fa-spinner fa-spin\"></i>&nbsp;&nbsp;"
155                 + _("Loading messages from server, please wait")
156
157                 + "  </div>"
158                 + "</div>";
159         document.getElementById("ctdl_big_modal").style.display = "block";
160 }
161
162
163 // Disappear the "Loading..." modal
164 function deactivate_loading_modal() {
165         document.getElementById("ctdl_big_modal").style.display = "none";
166 }