From: Art Cancro Date: Sat, 6 Nov 2021 20:43:05 +0000 (-0400) Subject: Split the forum view into a separate .js file. We will do this for all views. X-Git-Tag: v940~5 X-Git-Url: https://code.citadel.org/?a=commitdiff_plain;h=f7f764f8afba409aae2a6c026d2ab76033bc378d;p=citadel.git Split the forum view into a separate .js file. We will do this for all views. --- diff --git a/webcit-ng/static/index.html b/webcit-ng/static/index.html index 81b45b27e..b47412477 100644 --- a/webcit-ng/static/index.html +++ b/webcit-ng/static/index.html @@ -1,9 +1,9 @@ @@ -77,30 +77,37 @@ Loading... + diff --git a/webcit-ng/static/js/view_forum.js b/webcit-ng/static/js/view_forum.js new file mode 100644 index 000000000..4e0395d73 --- /dev/null +++ b/webcit-ng/static/js/view_forum.js @@ -0,0 +1,181 @@ +// +// Copyright (c) 2016-2021 by the citadel.org team +// +// 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 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + + +// Forum view (flat) -- all rendering is done client-side +// +function forum_readmessages(target_div, gt_msg, lt_msg) { + original_text = document.getElementById(target_div).innerHTML; // in case we need to replace it after an error + document.getElementById(target_div).innerHTML = + "
  " + + _("Loading messages from server, please wait") + "
"; + + if (lt_msg < 9999999999) { + url = "/ctdl/r/" + escapeHTMLURI(current_room) + "/msgs.lt|" + lt_msg; + } + else { + url = "/ctdl/r/" + escapeHTMLURI(current_room) + "/msgs.gt|" + gt_msg; + } + + fetch_msg_list = async() => { + response = await fetch(url); + msgs = await(response.json()); + if (response.ok) { + 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 += + "
" + + "
" ; + } + + // 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 += + "" ; + } + + // 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 { + 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; + } + } + fetch_msg_list(); +} + + +// Render a range of messages, with the div prefix specified +// +function forum_render_messages(msgs, prefix, scroll_to) { + for (i=0; i { + response = await fetch("/ctdl/r/" + escapeHTMLURI(current_room) + "/" + msgs[i] + "/json"); + msg = await response.json(); + if (response.ok) { + outmsg = + "
" // begin message wrapper + + "
" // begin avatar + + "" + + "
" // end avatar + + "
" // begin content + + "
" // begin header + + "" // begin header info on left side + + "" // FIXME link to user profile + + msg.from + + "" // end username + + "" + + msg.time + + "" // end msgdate + + "" // end header info on left side + + "" // begin buttons on right side + + + "" // Reply button + + " " + + _("Reply") + + "" + + + "" // ReplyQuoted , only show in forums + + " " + + _("ReplyQuoted") + + "" + + + "" // Delete , show only with permission + + " " + + _("Delete") + + "" + + + ""; // end buttons on right side + if (msg.subj) { + outmsg += + "
" + msg.subj + ""; + } + outmsg += + "

" // end header + + "
" // begin body + + msg.text + + "
" // end body + + "
" // end content + + "
" // end wrapper + ; + document.getElementById(div).innerHTML = outmsg; + } + else { + document.getElementById(div).innerHTML = "ERROR"; + } + document.getElementById(div).style.display = "inline"; + if (msgnum == scroll_to) { + window.location.hash = div; + } + } + fetch_message(); +} diff --git a/webcit-ng/static/js/views.js b/webcit-ng/static/js/views.js index 488f850fd..ba7bf716f 100644 --- a/webcit-ng/static/js/views.js +++ b/webcit-ng/static/js/views.js @@ -1,5 +1,5 @@ // -// Copyright (c) 2016-2020 by the citadel.org team +// Copyright (c) 2016-2021 by the citadel.org team // // This program is open source software. It runs great on the // Linux operating system (and probably elsewhere). You can use, @@ -48,172 +48,3 @@ function render_room_view(gt_msg, lt_msg) { } } - - -// Forum view (flat) -- all rendering is done client-side -// -function forum_readmessages(target_div, gt_msg, lt_msg) { - original_text = document.getElementById(target_div).innerHTML; // in case we need to replace it after an error - document.getElementById(target_div).innerHTML = - "
  " - + _("Loading messages from server, please wait") + "
"; - - if (lt_msg < 9999999999) { - url = "/ctdl/r/" + escapeHTMLURI(current_room) + "/msgs.lt|" + lt_msg; - } - else { - url = "/ctdl/r/" + escapeHTMLURI(current_room) + "/msgs.gt|" + gt_msg; - } - - fetch_msg_list = async() => { - response = await fetch(url); - msgs = await(response.json()); - if (response.ok) { - 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 += - "" ; - } - - // 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 += - "" ; - } - - // 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 { - 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; - } - } - fetch_msg_list(); -} - - -// Render a range of messages, with the div prefix specified -// -function forum_render_messages(msgs, prefix, scroll_to) { - for (i=0; i { - response = await fetch("/ctdl/r/" + escapeHTMLURI(current_room) + "/" + msgs[i] + "/json"); - msg = await response.json(); - if (response.ok) { - outmsg = - "
" // begin message wrapper - + "
" // begin avatar - + "" - + "
" // end avatar - + "
" // begin content - + "
" // begin header - + "" // begin header info on left side - + "" // FIXME link to user profile - + msg.from - + "" // end username - + "" - + msg.time - + "" // end msgdate - + "" // end header info on left side - + "" // begin buttons on right side - - + "" // Reply button - + " " - + _("Reply") - + "" - - + "" // ReplyQuoted , only show in forums - + " " - + _("ReplyQuoted") - + "" - - + "" // Delete , show only with permission - + " " - + _("Delete") - + "" - - + ""; // end buttons on right side - if (msg.subj) { - outmsg += - "
" + msg.subj + ""; - } - outmsg += - "

" // end header - + "
" // begin body - + msg.text - + "
" // end body - + "
" // end content - + "
" // end wrapper - ; - document.getElementById(div).innerHTML = outmsg; - } - else { - document.getElementById(div).innerHTML = "ERROR"; - } - document.getElementById(div).style.display = "inline"; - if (msgnum == scroll_to) { - window.location.hash = div; - } - } - fetch_message(); -}