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