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