can3p (can3p) wrote in changelog,
can3p
can3p
changelog

[livejournal] r17650: LJSV-1279: Thread expander can't manage ...

Committer: dpetrov
LJSV-1279: Thread expander can't manage long threads
U   trunk/htdocs/js/commentmanage.js
U   trunk/htdocs/js/thread_expander.ex.js
Modified: trunk/htdocs/js/commentmanage.js
===================================================================
--- trunk/htdocs/js/commentmanage.js	2010-11-09 05:24:56 UTC (rev 17649)
+++ trunk/htdocs/js/commentmanage.js	2010-11-09 08:25:37 UTC (rev 17650)
@@ -448,19 +448,33 @@
 				}
 
 				if(isS1){
+					var newNode, showExpand, j, children;
 					var threadId = dItemid,
 						threadExpanded = !!(LJ_cmtinfo[ threadId ].oldvars && LJ_cmtinfo[ threadId ].full);
 						populateComments = function(result){
 							for( var i = 0; i < result.length; ++i ){
 								if( LJ_cmtinfo[ result[i].thread ].full ){
-									setupAjax(
-										jQuery("#ljcmtxt" + result[i].thread).replaceWith(
-											ExpanderEx.prepareCommentBlock(
-													result[i].html,
-													result[ i ].thread,
-													!( 'oldvars' in LJ_cmtinfo[ result[i].thread ] )
-											)
-										)[0], isS1 );
+									showExpand = !( 'oldvars' in LJ_cmtinfo[ result[i].thread ]);
+
+									//still show expand button if children comments are folded
+									if( !showExpand ) {
+										children  = LJ_cmtinfo[ result[i].thread ].rc;
+
+										for( j = 0; j < children.length;  ++j ) {
+											if( !( 'oldvars' in LJ_cmtinfo[ children[j] ] ) ) {
+												showExpand = true;
+											}
+										}
+									}
+
+									newNode = ExpanderEx.prepareCommentBlock(
+											result[i].html,
+											result[ i ].thread,
+											showExpand
+									);
+
+									setupAjax( newNode[0], isS1 );
+									jQuery("#ljcmtxt" + result[i].thread).replaceWith( newNode );
 								}
 							}
 							hourglass.hide();

Modified: trunk/htdocs/js/thread_expander.ex.js
===================================================================
--- trunk/htdocs/js/thread_expander.ex.js	2010-11-09 05:24:56 UTC (rev 17649)
+++ trunk/htdocs/js/thread_expander.ex.js	2010-11-09 08:25:37 UTC (rev 17650)
@@ -105,23 +105,44 @@
 ExpanderEx.prototype.expandThread = function(json){
     this.loadingStateOff();
 
+    //we show expand link if comment block has collapsed children
+    function isChildCollapsed( idx )
+    {
+        var state;
+        for( var i = idx + 1; i < json.length; ++i ) {
+            state = json[ i ].state;
+            if( state === "expanded" ) { return false; }
+            if( state === "collapsed" ) { return true; }
+        }
+
+        return  false;
+    }
+
     var threadId, cell;
     for( var i = 0; i < json.length; ++i ) {
+        //we skip comment blocks thate were not expanded
+        if( json[ i ].state && json[ i ].state !== "expanded") {
+            continue;
+        }
+
         threadId = json[ i ].thread;
-        if( threadId in ExpanderEx.Collection )
+        cell = jQuery( '#ljcmtxt' + threadId );
+        if( threadId in ExpanderEx.Collection ) {
+            ExpanderEx.showExpandLink( threadId, cell, isChildCollapsed( i ) );
             continue; //this comment is already expanded
+        }
 
-        cell = jQuery( '#ljcmtxt' + threadId );
-
         ExpanderEx.Collection[ threadId ] = cell.html();
-        cell.replaceWith( ExpanderEx.prepareCommentBlock( json[ i ].html, threadId, false ) );
+        cell.replaceWith( ExpanderEx.prepareCommentBlock( json[ i ].html, threadId, isChildCollapsed( i ) ) );
     }
 
     //duplicate cycle, because we do not know, that external scripts do with node
     for( var i = 0; i < json.length; ++i ) {
         threadId = json[ i ].thread;
-        this.initCommentBlock( jQuery( '#ljcmt' + threadId )[0] , threadId );
         LJ_cmtinfo[ threadId ].parent = this.id;
+        if( json[ i ].state && json[ i ].state === "expanded") {
+            this.initCommentBlock( jQuery( '#ljcmt' + threadId )[0] , threadId );
+        }
     }
 
     return true;
@@ -207,19 +228,33 @@
     var block = jQuery("<td>" + html + "</td>").attr( {
             id: 'ljcmtxt' + id,
             width: '100%'
-        } ),
-        selector = '';
+        } );
 
+    this.showExpandLink( id, block, showExpand );
+    return block;
+}
 
-    if( LJ_cmtinfo[ id ].has_link > 0 )
-        selector = '#' + ((showExpand ? 'collapse_' : 'expand_' ) + id);
-    else
-        selector = '#collapse_' + id + ', #expand_'  + id
+ExpanderEx.showExpandLink = function ( id, block, showExpand ) {
+    var expandSel = "#expand_" + id,
+        collapseSel = "#collapse_" + id,
+        selector, resetSelector;
 
-    block.find(selector)
-        .css('display', 'none');
+    if( LJ_cmtinfo[ id ].has_link > 0 ) {
+        if( showExpand ) {
+            selector = collapseSel;
+            resetSelector = expandSel;
+        } else {
+            selector = expandSel;
+            resetSelector = collapseSel;
+        }
+        block.find( resetSelector ).css( 'display', '' );
+    }
+    else {
+        selector = collapseSel + "," + expandSel;
+    }
 
-    return block;
+    block.find( selector )
+        .css( 'display', 'none' );
 }
 
 ExpanderEx.preloadImg();

Tags: can3p, js, livejournal
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments