]> code.citadel.org Git - citadel.git/blobdiff - webcit-ng/static/js/views.js
async
[citadel.git] / webcit-ng / static / js / views.js
index 12642210f40a46b3b65b7ecd6e3bc32c4347e038..fbc8299027a77193d31238d76355677f3fd769c9 100644 (file)
@@ -82,8 +82,8 @@ function forum_readmessages(target_div, gt_msg, lt_msg)
                                        msgs = msgs.slice(0, messages_per_page);
                                }
 
-                               // 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) | (lt_msg < 9999999999))
+                               // Otherwise, show us the last 20 messages
+                               else
                                {
                                        if (msgs.length > messages_per_page)
                                        {
@@ -126,8 +126,26 @@ function forum_readmessages(target_div, gt_msg, lt_msg)
                                                "link to msgs greater than " + newgt + "</a></div>" ;
                                }
 
+                               // 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
-                               render_messages(msgs, "ctdl_msg_", views.VIEW_BBS);
+                               forum_render_messages(msgs, "ctdl_msg_", scroll_to)
                        }
                        else
                        {
@@ -140,13 +158,62 @@ function forum_readmessages(target_div, gt_msg, lt_msg)
 }
 
 
-// Render a range of messages, in the view specified, with the div prefix specified
+// Render a range of messages, with the div prefix specified
 //
-function render_messages(msgs, prefix, view)
+function forum_render_messages(msgs, prefix, scroll_to)
 {
-       for (var i in msgs)
+       for (i=0; i<msgs.length; ++i)
        {
-               document.getElementById(prefix + msgs[i]).innerHTML = "<b>Message " + msgs[i] + " got rendered!!!</b>";
+               forum_render_one(prefix+msgs[i], msgs[i], 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 =
+                                 "<div class=\"ctdl-msg-wrapper\">"                            // begin message wrapper
+                               + "<div class=\"ctdl-avatar\">"                                 // begin avatar
+                               + "<i class=\"fa fa-user-circle fa-2x\"></i> "                  // FIXME temporary avatar
+                               + "</div>"                                                      // end avatar
+                               + "<div>"                                                       // begin content
+                               + "<div>"                                                       // begin header
+                               + "<span class=\"ctdl-username\"><a href=\"#\">"                // FIXME link to user profile
+                               + msg.from
+                               + "</a></span> "
+                               + "<span class=\"ctdl-msgdate\">"
+                               + msg.time
+                               + "</span> "
+                               + "</div>"                                                      // end header
+                               + "<div>"                                                       // begin body
+                               + msg.text
+                               + "</div>"                                                      // end body
+                               + "</div>"                                                      // end content
+                               + "</div>"                                                      // end wrapper
+                               ;
+                       }
+                       else
+                       {
+                               document.getElementById(div).innerHTML = "ERROR";
+                       }
+                       if (msgnum == scroll_to)
+                       {
+                               window.location.hash = div;
+                       }
+               }
+       };
+       request.send();
+       request = null;
 }
+