fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2011-04-04 02:01 pm

[dw-free] Send errors on failed comment expansion

[commit: http://hg.dwscoalition.org/dw-free/rev/99c8c1c5df98]

http://bugs.dwscoalition.org/show_bug.cgi?id=3401

jQuerify the thread expander, throw an alert when there are errors.

Patch by [personal profile] allen.

Files modified:
  • cgi-bin/LJ/S2/EntryPage.pm
  • htdocs/js/jquery.threadexpander.js
--------------------------------------------------------------------------------
diff -r 461e4985d9af -r 99c8c1c5df98 cgi-bin/LJ/S2/EntryPage.pm
--- a/cgi-bin/LJ/S2/EntryPage.pm	Mon Apr 04 19:13:17 2011 +0800
+++ b/cgi-bin/LJ/S2/EntryPage.pm	Mon Apr 04 22:01:30 2011 +0800
@@ -89,6 +89,8 @@ sub EntryPage
     LJ::need_res( { group => "jquery" }, qw(
             js/jquery/jquery.ui.widget.min.js
             js/jquery.quickreply.js
+            js/jquery.threadexpander.js
+            js/jquery/jquery.busy.min.js
         ) );
 
     $p->{'entry'} = $s2entry;
diff -r 461e4985d9af -r 99c8c1c5df98 htdocs/js/jquery.threadexpander.js
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/htdocs/js/jquery.threadexpander.js	Mon Apr 04 22:01:30 2011 +0800
@@ -0,0 +1,110 @@
+(function($) {
+  // makes the given comment element displayed fully.
+  function setFull(commentElement, full) {
+    commentElement.parent()
+      .toggleClass("full", full)
+      .toggleClass("partial", !full);
+  }
+
+  // Returns the spacer object for S1 indents.
+  function getS1SpacerObject(element){
+    return $("td.spacer img", element);
+  }
+
+  // Returns the given talkid, plus the talkids of all comments that are
+  // replies to this talkid.
+  function getReplies(LJ, talkid) {
+    var returnValue = [ talkid ];
+    for (var i = 0; i < LJ[talkid].rc.length; i++) {
+      returnValue = returnValue.concat(getReplies(LJ, LJ[talkid].rc[i]));
+    }
+    return returnValue;
+  }
+
+  // shows an error.  just uses alert() for now.
+  function showExpanderError(error) {
+    alert(error);
+  }
+
+  // ajax expands the comments for the given talkid
+  $.fn.expandComments = function(LJ, expand_url, talkid, isS1) {
+    element = this;
+    // if we've already been clicked, just return.
+    if (element.hasClass("disabled")) {
+      return;
+    }
+
+    if (!LJ) {
+      return false;
+    }
+
+    element.addClass("disabled");
+    element.fadeTo("fast", 0.5);
+    var img = $("<img>", { src: Site.imgprefix+"/ajax-loader.gif"});
+    element.append(img);
+
+    $.ajax( { url: expand_url,
+          datatype: "html",
+          timeout: 30000,
+          success: function(data) {
+            doJqExpand(LJ, data, talkid, isS1);
+          },
+          error: function(jqXHR, textStatus, errorThrown) {
+            img.remove();
+            element.removeClass("disabled");
+            element.fadeTo("fast", 1.0);
+            showExpanderError($.threadexpander.config.text.error);
+          }
+      } );
+  };
+
+  // callback to handle comment expansion
+  function doJqExpand(LJ, data, talkid, isS1) {
+    var updateCount = 0;
+    // check for matching expansions on the page
+    var replies = getReplies(LJ, talkid);
+    for (var cmtIdCnt = 0; cmtIdCnt < replies.length; cmtIdCnt++) {
+      var cmtId = replies[cmtIdCnt];
+      // if we're a valid comment, and either the comment is not expanded
+      // or it's the original comment, then it's valid to expand it.
+      if (/^\d*$/.test(cmtId) && (talkid == cmtId || (! LJ[cmtId].full))) {
+        var cmtElement = $("#cmt" + cmtId);
+        if (cmtElement) {
+          var newComment = $("#cmt" + cmtId, data);
+          if (newComment && newComment.attr('id') == 'cmt' + cmtId) {
+            if (isS1) {
+            var oldWidth = getS1SpacerObject(cmtElement).width();
+            getS1SpacerObject(newComment).width(oldWidth);
+          }
+            cmtElement.html($(newComment).html());
+            LJ[cmtId].full = true;
+            if (! isS1) {
+              setFull(cmtElement, true);
+            }
+            updateCount++;
+          }
+        }
+      }
+    }
+
+    // if we didn't update any comments, something must have gone wrong
+    if (updateCount == 0) {
+      showExpanderError($.threadexpander.config.text.error_nomatches);
+    }
+  }
+
+  $.threadexpander = {
+    config: {
+      text: {
+        error: "Error:  no response while expanding comments.",
+        error_nomatches: "Error:  no comments found to expand."
+      }
+    }
+  };
+})(jQuery);
+
+Expander = {
+  make: function(element, url, dtid, isS1) {
+    $(element).expandComments(LJ_cmtinfo, url, dtid, isS1);
+  }
+}
--------------------------------------------------------------------------------