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