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