Initial commit of the "new" room list. It's going to be a lot more "traditional...
[citadel.git] / webcit-ng / static / js / roomlist.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 function render_room_list() {
15
16         document.getElementById("ctdl-newmsg-button").style.display = "none";           // There is no "enter" button on this screen
17         document.getElementById("ctdl-main").innerHTML = "<img src=\"/ctdl/s/images/throbber.gif\" />"; // show throbber while loading
18
19         fetch_room_list = async() => {
20
21                 floor_response = await fetch("/ctdl/f/");
22                 floor_list = await(floor_response.json());
23                 room_response = await fetch("/ctdl/r/");
24                 room_list = await(room_response.json());
25                 if (response.ok) {
26                         display_room_list_renderer(floor_list, room_list);
27                 }
28                 else {
29                         document.getElementById("ctdl-main").innerHTML = "<i>error</i>";
30                 }
31         }
32         fetch_room_list();
33 }
34
35
36 // Renderer for display_room_list()
37 function display_room_list_renderer(floor_list, room_list) {
38         room_list = room_list.sort(function(a,b) {
39                 if (a.floor != b.floor) {
40                         return(a.floor - b.floor);
41                 }
42                 if (a.rorder != b.rorder) {
43                         return(a.rorder - b.rorder);
44                 }
45                 return(a.name < b.name);
46         });
47
48         output = [];
49
50         for (var f in floor_list) {
51                 output[floor_list[f].num] = "";
52         }
53
54         for (var i in room_list) {
55                 if (room_list[i].current_view == views.VIEW_BBS) {
56                         output[room_list[i].floor] += "<div class=\"ctdl-roomlist-room\">";
57                         output[room_list[i].floor] += "<i class=\"ctdl-roomlist-roomicon fas fa-comments fa-fw\"></i>";
58                         output[room_list[i].floor] += "<a class=\"ctdl-roomlist-roomname";
59                         output[room_list[i].floor] += (room_list[i].hasnewmsgs ? " ctdl-roomlist-roomname-hasnewmsgs" : "");
60                         output[room_list[i].floor] += "\" href=\"javascript:gotoroom('" + escapeJS(escapeHTML(room_list[i].name)) + "');\">";
61                         output[room_list[i].floor] += escapeHTML(room_list[i].name);
62                         output[room_list[i].floor] += (room_list[i].hasnewmsgs ? "</b>" : "");
63                         output[room_list[i].floor] += "</a></div>";
64                 }
65         }
66
67         final_output = "<div class=\"ctdl-roomlist-top\">";
68         for (var f in floor_list) {
69                 final_output += "<div class=\"ctdl-roomlist-floor\">";
70                 final_output += "<div class=\"ctdl-roomlist-floor-label\">" + floor_list[f].name + "</div>";
71                 final_output += "<div class=\"ctdl-roomlist-floor-rooms\">";
72                 final_output += output[floor_list[f].num];
73                 final_output += "</div></div>";
74         }
75         final_output += "</div>";
76         document.getElementById("ctdl-main").innerHTML = final_output ;
77 }
78