6d1676c140de40dad8e671c57678481ed2fd844a
[citadel.git] / webcit-ng / static / js / roomlist.js
1 // Copyright (c) 2016-2023 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 function render_room_list() {
8
9         document.getElementById("ctdl-newmsg-button").style.display = "none";           // There is no "enter" button on this screen
10         document.getElementById("ctdl-main").innerHTML = "<div class=\"ctdl-middle\"><img src=\"/ctdl/s/images/throbber.gif\" /></div>";        // show throbber while loading
11         clear_sidebar_selection();
12         document.getElementById("ctdl-sidebar-button-forums").classList.add("ctdl-sidebar-button-selected");
13
14         fetch_room_list = async() => {
15                 floor_response = await fetch("/ctdl/f/");
16                 floor_list = await(floor_response.json());
17                 room_response = await fetch("/ctdl/r/");
18                 room_list = await(room_response.json());
19                 if (response.ok) {
20                         display_room_list_renderer(floor_list, room_list);
21                 }
22                 else {
23                         document.getElementById("ctdl-main").innerHTML = "<i>error</i>";
24                 }
25         }
26         fetch_room_list();
27 }
28
29
30 // Renderer for display_room_list()
31 function display_room_list_renderer(floor_list, room_list) {
32
33         // First sort by the room order indicated by the server
34         room_list = room_list.sort(function(a,b) {
35                 if (a.floor != b.floor) {
36                         return(a.floor - b.floor);
37                 }
38                 if (a.rorder != b.rorder) {
39                         return(a.rorder - b.rorder);
40                 }
41                 return(a.name < b.name);
42         });
43
44         // Then split the sorted list out into floors
45         output = [];
46         for (var f in floor_list) {
47                 output[floor_list[f].num] = "";
48         }
49
50         for (var i in room_list) {
51                 if (room_list[i].current_view == views.VIEW_BBS) {
52                         output[room_list[i].floor] +=
53
54                                 "<div class=\"ctdl-roomlist-room\" onClick=\"javascript:gotoroom('"     // container
55                                 + escapeJS(escapeHTML(room_list[i].name)) + "');\">"
56
57                                 + "<div><i class=\"ctdl-roomlist-roomicon "                             // room icon
58                                 + (room_list[i].hasnewmsgs ? "w3-blue" : "w3-gray")
59                                 + " fas fa-comments fa-fw\"></i></div>"
60
61                                 + "<div class=\"ctdl-roomlist-roomname"                                 // room name
62                                 + (room_list[i].hasnewmsgs ? " ctdl-roomlist-roomname-hasnewmsgs" : "")
63                                 + "\">"
64                                 + escapeHTML(room_list[i].name)
65                                 + "</div>"
66
67                                 + "<div class=\"ctdl-roomlist-mtime\">"                                 // date/time of last post
68                                 + string_timestamp(room_list[i].mtime)
69                                 + "</div>"
70
71                                 + "</div>"                                                              // end container
72                 }
73         }
74
75         final_output = "<div class=\"ctdl-roomlist-top\">";
76         for (var f in floor_list) {
77                 final_output +=
78                         "<div class=\"ctdl-roomlist-floor\">"
79                         + "<div class=\"ctdl-roomlist-floor-label\">" + floor_list[f].name + "</div>"
80                         + "<div class=\"ctdl-roomlist-floor-rooms\">"
81                         + output[floor_list[f].num]
82                         + "</div></div>";
83         }
84         final_output += "</div>";
85         document.getElementById("ctdl-main").innerHTML = final_output;
86 }
87