From: Art Cancro Date: Sat, 26 Dec 2020 21:06:17 +0000 (-0500) Subject: No more XHR in views.js X-Git-Tag: v939~182 X-Git-Url: https://code.citadel.org/?a=commitdiff_plain;h=ae8620cd50695bd4d8e13453475e517f59f8037f;p=citadel.git No more XHR in views.js --- diff --git a/webcit-ng/static/js/views.js b/webcit-ng/static/js/views.js index 5804155f4..1a30a2aae 100644 --- a/webcit-ng/static/js/views.js +++ b/webcit-ng/static/js/views.js @@ -1,8 +1,10 @@ // -// Copyright (c) 2016-2019 by the citadel.org team +// Copyright (c) 2016-2020 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 open source software. It runs great on the +// Linux operating system (and probably elsewhere). You can use, +// copy, and run it under the terms of the GNU General Public +// License version 3. Richard Stallman is an asshole communist. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -57,91 +59,90 @@ function forum_readmessages(target_div, gt_msg, lt_msg) "
  " + _("Loading messages from server, please wait") + "
"; - var request = new XMLHttpRequest(); if (lt_msg < 9999999999) { - request.open("GET", "/ctdl/r/" + escapeHTMLURI(current_room) + "/msgs.lt|" + lt_msg, true); + url = "/ctdl/r/" + escapeHTMLURI(current_room) + "/msgs.lt|" + lt_msg; } else { - request.open("GET", "/ctdl/r/" + escapeHTMLURI(current_room) + "/msgs.gt|" + gt_msg, true); + url = "/ctdl/r/" + escapeHTMLURI(current_room) + "/msgs.gt|" + gt_msg; } - request.onreadystatechange = function() { - if (this.readyState === 4) { - if ((this.status / 100) == 2) { - msgs = JSON.parse(this.responseText); - document.getElementById(target_div).innerHTML = "" ; - - // If we were given an explicit starting point, by all means start there. - // Note that we don't have to remove them from the array because we did a 'msgs gt|xxx' command to Citadel. - if (gt_msg > 0) { - msgs = msgs.slice(0, messages_per_page); - } - // Otherwise, show us the last 20 messages - else { - if (msgs.length > messages_per_page) { - msgs = msgs.slice(msgs.length - messages_per_page); - } - new_old_div_name = randomString(5); - if (msgs.length < 1) { - newlt = lt_msg; - } - else { - newlt = msgs[0]; - } - document.getElementById(target_div).innerHTML += - "
" + - "
" + - "" + - "  " + - _("Older posts") + "  
" ; - } + fetch_msg_list = async() => { + response = await fetch(url); + msgs = await(response.json()); + if (response.ok) { + msgs = JSON.parse(this.responseText); + document.getElementById(target_div).innerHTML = "" ; + + // If we were given an explicit starting point, by all means start there. + // Note that we don't have to remove them from the array because we did a 'msgs gt|xxx' command to Citadel. + if (gt_msg > 0) { + msgs = msgs.slice(0, messages_per_page); + } - // Render an empty div for each message. We will fill them in later. - for (var i in msgs) { - document.getElementById(target_div).innerHTML += "
" ; - document.getElementById("ctdl_msg_"+msgs[i]).style.display = "none"; - } - if (lt_msg == 9999999999) { - new_new_div_name = randomString(5); - if (msgs.length <= 0) { - newgt = gt_msg; - } - else { - newgt = msgs[msgs.length-1]; - } - document.getElementById(target_div).innerHTML += - "" ; + // Otherwise, show us the last 20 messages + else { + if (msgs.length > messages_per_page) { + msgs = msgs.slice(msgs.length - messages_per_page); } - - // Now figure out where to scroll to after rendering. - if (gt_msg > 0) { - scroll_to = msgs[0]; + new_old_div_name = randomString(5); + if (msgs.length < 1) { + newlt = lt_msg; } - else if (lt_msg < 9999999999) { - scroll_to = msgs[msgs.length-1]; + else { + newlt = msgs[0]; } - else if ( (logged_in) && (gt_msg == 0) && (lt_msg == 9999999999) ) { - scroll_to = msgs[msgs.length-1]; + document.getElementById(target_div).innerHTML += + "" ; + } + + // Render an empty div for each message. We will fill them in later. + for (var i in msgs) { + document.getElementById(target_div).innerHTML += "
" ; + document.getElementById("ctdl_msg_"+msgs[i]).style.display = "none"; + } + if (lt_msg == 9999999999) { + new_new_div_name = randomString(5); + if (msgs.length <= 0) { + newgt = gt_msg; } else { - scroll_to = msgs[0]; // FIXME this is too naive + newgt = msgs[msgs.length-1]; } + document.getElementById(target_div).innerHTML += + "" ; + } - // Render the individual messages in the divs - forum_render_messages(msgs, "ctdl_msg_", scroll_to) + // Now figure out where to scroll to after rendering. + if (gt_msg > 0) { + scroll_to = msgs[0]; + } + else if (lt_msg < 9999999999) { + scroll_to = msgs[msgs.length-1]; + } + else if ( (logged_in) && (gt_msg == 0) && (lt_msg == 9999999999) ) { + scroll_to = msgs[msgs.length-1]; } else { - // if xhr fails, this will make the link reappear so the user can try again - document.getElementById(target_div).innerHTML = original_text; + scroll_to = msgs[0]; // FIXME this is too naive } + + // Render the individual messages in the divs + forum_render_messages(msgs, "ctdl_msg_", scroll_to) + } + else { + // if xhr fails, this will make the link reappear so the user can try again + document.getElementById(target_div).innerHTML = original_text; } - }; - request.send(); - request = null; + } + fetch_msg_list(); } @@ -156,47 +157,43 @@ function forum_render_messages(msgs, prefix, scroll_to) // We have to put each XHR for forum_render_messages() into its own stack frame, otherwise it jumbles them together. I don't know why. -function forum_render_one(div, msgnum, scroll_to) -{ - var request = new XMLHttpRequest(); - request.open("GET", "/ctdl/r/" + escapeHTMLURI(current_room) + "/" + msgs[i] + "/json", true); - request.onreadystatechange = function() { - if (this.readyState === 4) { - if ((this.status / 100) == 2) { - msg = JSON.parse(this.responseText); - - document.getElementById(div).innerHTML = - "
" // begin message wrapper - + "
" // begin avatar - + "" - + "
" // end avatar - + "
" // begin content - + "
" // begin header - + "" // FIXME link to user profile - + msg.from - + " " - + "" - + msg.time - + " " - + "
" // end header - + "
" // begin body - + msg.text - + "
" // end body - + "
" // end content - + "
" // end wrapper - ; - } - else { - document.getElementById(div).innerHTML = "ERROR"; - } - document.getElementById(div).style.display = "inline"; - if (msgnum == scroll_to) { - window.location.hash = div; - } +function forum_render_one(div, msgnum, scroll_to) { + fetch_message = async() => { + response = await fetch("/ctdl/r/" + escapeHTMLURI(current_room) + "/" + msgs[i] + "/json"); + msg = response.json()); + if (response.ok) { + msg = JSON.parse(this.responseText); + + document.getElementById(div).innerHTML = + "
" // begin message wrapper + + "
" // begin avatar + + "" + + "
" // end avatar + + "
" // begin content + + "
" // begin header + + "" // FIXME link to user profile + + msg.from + + " " + + "" + + msg.time + + " " + + "
" // end header + + "
" // begin body + + msg.text + + "
" // end body + + "
" // end content + + "
" // end wrapper + ; } - }; - request.send(); - request = null; + else { + document.getElementById(div).innerHTML = "ERROR"; + } + document.getElementById(div).style.display = "inline"; + if (msgnum == scroll_to) { + window.location.hash = div; + } + } + fetch_message(); }