X-Git-Url: https://code.citadel.org/?a=blobdiff_plain;f=webcit-ng%2Fstatic%2Fjs%2Fviews.js;h=35fdbef5bc7f38abce720bd46d93d0824ef9379b;hb=439dbc67ce2cb6923a35efbd4c172ea3f5f83bf4;hp=b569cd6640c1ce73e60bbadea721d36671ceca48;hpb=32fe7887a4fc68e8148f79c4b9ec35ffbb2442d7;p=citadel.git diff --git a/webcit-ng/static/js/views.js b/webcit-ng/static/js/views.js index b569cd664..35fdbef5b 100644 --- a/webcit-ng/static/js/views.js +++ b/webcit-ng/static/js/views.js @@ -1,5 +1,5 @@ // -// Copyright (c) 2016-2018 by the citadel.org team +// Copyright (c) 2016-2019 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. @@ -30,48 +30,115 @@ var views = { // This function is the dispatcher that determines the correct view for a room, -// and calls the correct renderer. +// and calls the correct renderer. Greater/Less than bounds are accepted. // -function render_room_view() +function render_room_view(gt_msg, lt_msg) { - switch(current_view) - { + switch(current_view) { case views.VIEW_MAILBOX: // FIXME view mail rooms as forums for now case views.VIEW_BBS: - forum_readmessages(); + forum_readmessages("ctdl-main", gt_msg, lt_msg); break; default: - document.getElementById("main").innerHTML = "The view for " + current_room + " is " + current_view + " but there is no renderer." ; + document.getElementById("ctdl-main").innerHTML = + "The view for " + current_room + " is " + current_view + " but there is no renderer." ; break; } } -// Forum view -- flat or threaded -// The inner div exists so that if the user clicks away early, the main div doesn't get clobbered when the load completes. -// The parameter can be set to "flat" or "threads" which is passed directly to the API +// Forum view (flat) -- let's have another go at this with the rendering done client-side // -function XX_forum_readmessages(flat_or_threads) +function forum_readmessages(target_div, gt_msg, lt_msg) { - var innerdivname = randomString(5); - document.getElementById("main").innerHTML = "



  " - + _("Loading messages from server, please wait") + "
" ; + 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") ; var request = new XMLHttpRequest(); - request.open("GET", "/ctdl/r/" + escapeHTMLURI(current_room) + "/" + flat_or_threads, true); - request.onreadystatechange = function() - { - if (this.readyState === 4) - { - if ((this.status / 100) == 2) - { - document.getElementById(innerdivname).outerHTML = this.responseText; + if (lt_msg < 9999999999) { + request.open("GET", "/ctdl/r/" + escapeHTMLURI(current_room) + "/msgs.lt|" + lt_msg, true); + } + else { + request.open("GET", "/ctdl/r/" + escapeHTMLURI(current_room) + "/msgs.gt|" + gt_msg, true); + } + 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") + "  
" ; + } + + // Render the divs (we will fill them in later) + for (var i in msgs) { + document.getElementById(target_div).innerHTML += + "
#" + msgs[i] + + "
" ; + } + 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 += + "
" + + "
" + + "" + + "  " + + _("Newer posts") + "  
" ; + } + + // 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 - { - document.getElementById(innerdivname).outerHTML = "ERROR " + this.status ; + else { + // if xhr fails, this will make the link reappear so the user can try again + document.getElementById(target_div).innerHTML = original_text; } } }; @@ -80,55 +147,57 @@ function XX_forum_readmessages(flat_or_threads) } -// Forum view (flat) -- let's have another go at this with the rendering done client-side +// Render a range of messages, with the div prefix specified // -function forum_readmessages() +function forum_render_messages(msgs, prefix, scroll_to) { - var innerdivname = randomString(5); - document.getElementById("main").innerHTML = "



  " - + _("Loading messages from server, please wait") + "
" ; + for (i=0; i" - + "Last seen: " + last_seen + "
" - + "Number of messages: " + msgs.length + "
" ; - - if (msgs.length == 0) - { - document.getElementById(innerdivname).innerHTML += "FIXME no msgs" ; - } - // show us the last 20 messages and scroll to the bottom (this will become the not-logged-in behavior) - else if ((logged_in) | (!logged_in)) - { - if (msgs.length > messages_per_page) - { - msgs = msgs.slice(msgs.length - messages_per_page); - document.getElementById(innerdivname).innerHTML += "link to msgs less than " + msgs[0] + "
" ; - } - } +// 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); - for (var i in msgs) - { - document.getElementById(innerdivname).innerHTML += "message # " + msgs[i] + "
" ; - } + 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(innerdivname).innerHTML = this.status ; // error message + else { + document.getElementById(div).innerHTML = "ERROR"; + } + if (msgnum == scroll_to) { + window.location.hash = div; } } }; request.send(); request = null; } +