]> code.citadel.org Git - citadel.git/blobdiff - webcit-ng/static/js/main.js
Make the font sizes less cartoonish at extreme viewport sizes
[citadel.git] / webcit-ng / static / js / main.js
index 0170a987aba037465982c312e67a0320ecb8ad7e..541a593c62db5d986cd317abe94fe5bb260fe7f7 100644 (file)
+// Copyright (c) 2016-2023 by the citadel.org team
 //
-// Copyright (c) 2016-2017 by the citadel.org team
-//
-// This program is open source software; you can redistribute it and/or modify
-// it under the terms of the GNU General Public License version 3.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-// GNU General Public License for more details.
-
-
-var current_room = "_BASEROOM_";
-var new_messages = 0;
-var total_messages = 0;
-var default_view = 0;
-var current_view = 0;
-var logged_in = 0;
-var current_user = _("Not logged in.");
-var serv_info;
-var last_seen = 0;
-var messages_per_page = 20;
-
-
-// Placeholder for when we add i18n later
-function _(x) {
-       return x;
-}
+// This program is open source software.  Use, duplication, or
+// disclosure are subject to the GNU General Public License v3.
 
 
-// Generate a random string of the specified length
-// Useful for generating one-time-use div names
-//
-function randomString(length) {
-       var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghiklmnopqrstuvwxyz'.split('');
-
-       if (! length) {
-               length = Math.floor(Math.random() * chars.length);
+do_biff = async() => {
+       response = await fetch("/ctdl/a/biff");
+       if (response.ok) {
+               biff_result = await(response.text());
+               try {
+                       new_mail += parseInt(biff_result);
+               }
+               catch {
+               }
        }
 
-       var str = '';
-       for (var i = 0; i < length; i++) {
-               str += chars[Math.floor(Math.random() * chars.length)];
+       if (new_mail > 0) {
+               console.log("YOU'VE GOT MAIL!  new_mail = " + new_mail);
+               new_mail_sound.play();                  // FIXME do a visual notification as well
+               new_mail = 0;
        }
-       return str;
-}
-
-
-// string escape for html display
-//
-function escapeHTML(text) {
-    'use strict';
-    return text.replace(/[\"&<>]/g, function (a) {
-        return {
-               '"': '&quot;',
-               '&': '&amp;',
-               '<': '&lt;',
-               '>': '&gt;'
-       }[a];
-    });
-}
-
-
-// string escape for html display
-//
-function escapeHTMLURI(text) {
-    'use strict';
-    return text.replace(/./g, function (a) {
-        return '%' + a.charCodeAt(0).toString(16);
-    });
-}
-
-
-// string escape for JavaScript string
-//
-function escapeJS(text) {
-    'use strict';
-    return text.replace(/[\"\']/g, function (a) {
-        return '\\' + a ;
-    });
 }
 
 
 // This is called at the very beginning of the main page load.
-//
-function ctdl_startup() {
-       var request = new XMLHttpRequest();
-       request.open("GET", "/ctdl/c/info", true);
-       request.onreadystatechange = function() {
-               if ((this.readyState === 4) && ((this.status / 100) == 2)) {
-                       ctdl_startup_2(JSON.parse(this.responseText));
-               }
-       };
-       request.send();
-       request = null;
-}
-
-// Continuation of ctdl_startup() after serv_info is retrieved
-//
-function ctdl_startup_2(data) {
-       serv_info = data;
+ctdl_startup = async() => {
+       response = await fetch("/ctdl/c/info");
 
-       if (data.serv_rev_level < 905) {
-               alert("Citadel server is too old, some functions may not work");
-       }
-
-       update_banner();
+       if (response.ok) {
+               serv_info = await(response.json());
+               if (serv_info.serv_rev_level < 905) {
+                       alert(_("Citadel server is too old, some functions may not work"));
+               }
 
-       // for now, show a room list in the main div
-       gotoroom("_BASEROOM_");
-       display_room_list();
-}
+               update_banner();
 
-// Display a room list in the main div.
-//
-function display_room_list() {
-       document.getElementById("roomlist").innerHTML = "<img src=\"/ctdl/s/throbber.gif\" />" ;                // show throbber while loading
-
-       var request = new XMLHttpRequest();
-       request.open("GET", "/ctdl/r/", true);
-       request.onreadystatechange = function() {
-               if ((this.readyState === 4) && ((this.status / 100) == 2)) {
-                       display_room_list_renderer(JSON.parse(this.responseText));
-               }
-       };
-       request.send();
-       request = null;
-}
+               // What do we do upon landing?
 
-// Renderer for display_room_list()
-//
-function display_room_list_renderer(data) {
-       data = data.sort(function(a,b) {
-               if (a.floor != b.floor) {
-                       return(a.floor - b.floor);
+               if ( (serv_info.serv_supports_guest) || (logged_in) ) {                 // If the Lobby is visible,
+                       gotonext(1);                                                    // go there.
                }
-               if (a.rorder != b.rorder) {
-                       return(a.rorder - b.rorder);
+               else {                                                                  // Otherwise,
+                       display_login_screen("");                                       // display the login modal.
                }
-               return(a.name < b.name);
-       });
 
-       new_roomlist_text = "<ul>" ;
-
-       for (var i in data) {
-               if (i > 0) {
-                       if (data[i].floor != data[i-1].floor) {
-                               new_roomlist_text = new_roomlist_text + "<li class=\"divider\"></li>" ;
-                       }
+               var biff_interval;
+               try {                                                                   // if this was already set up,
+                       clearInterval(biff_interval);                                   // clear the old one so there's only one.
                }
-               new_roomlist_text = new_roomlist_text +
-                       "<li>"
-                       + "<a href=\"javascript:gotoroom('" + escapeJS(escapeHTML(data[i].name)) + "');\">"
-                       + escapeHTML(data[i].name)
-                       + "</a></li>"
-               ;
+               catch {
+               }
+               do_biff();
+               biff_interval = setInterval(do_biff, 10000);
+       }
+       else {
+               document.getElementById("ctdl-main").innerHTML =
+                       "<div class=\"ctdl-fatal-error\"><p>"
+                       + _("This program was unable to connect or stay connected to the Citadel server.  Please report this problem to your system administrator.")
+                       + "</div>";
        }
-       new_roomlist_text = new_roomlist_text + "</ul>";
-       document.getElementById("roomlist").innerHTML = new_roomlist_text ;
 }
 
+
+// Display a room list in the main div.
+
 // Update the "banner" div with all relevant info.
-//
 function update_banner() {
        detect_logged_in();
        if (current_room) {
                document.getElementById("ctdl_banner_title").innerHTML = current_room;
+               if (is_trash_folder) {
+                       document.getElementById("ctdl_banner_title").innerHTML += "&nbsp;<i class=\"fa fa-trash\"></i>";
+               }
+               if (is_room_aide) {
+                       document.getElementById("ctdl_banner_title").innerHTML += "&nbsp;<i class=\"fa fa-user-cog\"></i>";
+               }
                document.title = current_room;
        }
        else {
                document.getElementById("ctdl_banner_title").innerHTML = serv_info.serv_humannode;
        }
+
+       document.getElementById("current_user_avatar").innerHTML = render_userpic(current_user);
        document.getElementById("current_user").innerHTML = current_user ;
+
        if (logged_in) {
-               document.getElementById("lilo").innerHTML = "<a href=\"/ctdl/a/logout\">" + _("Log off") + "</a>" ;
+               document.getElementById("lilo").innerHTML = "<a href=\"/ctdl/a/logout\"><i class=\"fa fa-right-from-bracket\"></i>&nbsp;" + _("Log off") + "</a>" ;
        }
        else {
-               document.getElementById("lilo").innerHTML = "<a href=\"javascript:display_login_screen('')\">" + _("Log in") + "</a>" ;
+               document.getElementById("lilo").innerHTML = "<a href=\"javascript:display_login_screen('')\"><i class=\"fa fa-right-to-bracket\"></i>&nbsp;" + _("Log in") + "</a>" ;
        }
 }
 
 
 // goto room
-//
 function gotoroom(roomname) {
-       var request = new XMLHttpRequest();
-       request.open("GET", "/ctdl/r/" + escapeHTMLURI(roomname) + "/", true);
-       request.onreadystatechange = function() {
-               if ((this.readyState === 4) && ((this.status / 100) == 2)) {
-                       gotoroom_2(JSON.parse(this.responseText));
+
+       fetch_room = async() => {
+               response = await fetch("/ctdl/r/" + escapeHTMLURI(roomname) + "/");
+               if (response.ok) {
+                       data = await(response.json());
+                       current_room = data.name;
+                       new_messages = data.new_messages;
+                       total_messages = data.total_messages;
+                       current_view = data.current_view;
+                       default_view = data.default_view;
+                       last_seen = data.last_seen;
+                       is_room_aide = data.is_room_aide;
+                       is_trash_folder = data.is_trash_folder;
+                       room_mtime = data.room_mtime;
+                       can_delete_messages = data.can_delete_messages;
+                       update_banner();
+                       render_room_view();
+               }
+       }
+       fetch_room();
+}
+
+
+// Goto next room with unread messages
+// which_oper is 0=ungoto, 1=skip, 2=goto
+function gotonext(which_oper) {
+       if (which_oper == 2) {                                  // Goto needs to mark messages as seen
+
+               set_last_read_pointer = async() => {
+                       response = await fetch("/ctdl/r/" + escapeHTMLURI(current_room) + "/slrp?last=" + last_seen);
+               }
+               set_last_read_pointer();
+       }
+
+       if ((which_oper == 1) || (which_oper == 2)) {           // Skip or Goto both take us to the "next" room
+               if (march_list.length == 0) {
+                       load_new_march_list(which_oper);        // we will recurse back here
+               }
+               else {
+                       next_room = march_list[0].name;
+                       march_list.splice(0, 1);
+                       gotoroom(next_room);
+               }
+       }
+
+       // FIXME implement mode 0 (ungoto)
+
+}
+
+
+// Called by gotonext() when the march list is empty.
+function load_new_march_list(which_oper) {
+       fetchm = async() => {
+               response = await fetch("/ctdl/r/");
+               march_list = await(response.json());
+               if (response.ok) {
+                       march_list = march_list.filter(function(room) {
+                               return(room.hasnewmsgs && room.default_view == views.VIEW_BBS);
+                       });
+                       march_list = march_list.sort(function(a,b) {
+                               if (a.floor != b.floor) {
+                                       return(a.floor - b.floor);
+                               }
+                               if (a.rorder != b.rorder) {
+                                       return(a.rorder - b.rorder);
+                               }
+                               return(a.name < b.name);
+                       });
+
+                       // If there are still no rooms with new messages, go to the Lobby.
+                       if (march_list.length == 0) {
+                               march_list.push({name:"_BASEROOM_",known:true,hasnewmsgs:true,floor:0});
+                       }
+
+                       gotonext(which_oper);
                }
-       };
-       request.send();
-       request = null;
+       }
+       fetchm();
 }
-function gotoroom_2(data) {
-       current_room = data.name;
-       new_messages = data.new_messages;
-       total_messages = data.total_messages;
-       current_view = data.current_view;
-       default_view = data.default_view;
-       last_seen = data.last_seen;
-       update_banner();
-       render_room_view();
+
+
+// Activate the "Loading..." modal
+function activate_loading_modal() {
+       document.getElementById("ctdl_big_modal").innerHTML =
+               "<i class=\"fas fa-spinner fa-spin\"></i>&nbsp;&nbsp;"
+               + _("Loading messages from server, please wait");
+       document.getElementById("ctdl_big_modal").style.display = "block";
+}
+
+
+// Disappear the "Loading..." modal
+function deactivate_loading_modal() {
+       document.getElementById("ctdl_big_modal").style.display = "none";
 }