Plugin Directory

Changeset 200947


Ignore:
Timestamp:
02/02/2010 04:09:07 AM (16 years ago)
Author:
robfelty
Message:

reducing number of DOM elements by storing stuff in a javascript array

Location:
collapsing-categories/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • collapsing-categories/trunk/collapsFunctions.js

    r197221 r200947  
    1 /*  Collapse Functions, version 1.6
     1/*  Collapse Functions, version 1.7
    22 *
    33 *--------------------------------------------------------------------------*/
     
    133133  if (theSpan.className!='sym') {
    134134    theSpan = theSpan.childNodes[0];
    135     //alert(childList.getAttribute('id'));
    136135    theId = childList.childNodes[0].getAttribute('id');
    137136  }
     
    141140    src.setAttribute('title','click to expand');
    142141    theSpan.innerHTML=expand;
     142    childList.innerHTML='<li></li>';
    143143    if (animate==1) {
    144       //Effect.BlindUp(childList, {duration: 0.5});
    145144      jQuery(childList).hide('blind', '', 500);
    146145    } else {
     
    152151    src.setAttribute('title','click to collapse');
    153152    theSpan.innerHTML=collapse;
     153    childList.innerHTML=subCatPosts[theId];
    154154    if (animate==1) {
    155       //Effect.BlindDown(childList, {duration: 0.5});
    156155      jQuery(childList).show('blind', '', 500);
    157156    } else {
  • collapsing-categories/trunk/collapscat.php

    r200498 r200947  
    55Description: Uses javascript to expand and collapse categories to show the posts that belong to the category
    66Author: Robert Felty
    7 Version: 1.1.1
     7Version: 1.2
    88Author URI: http://robfelty.com
    99Tags: sidebar, widget, categories, menu, navigation, posts
     
    3333
    3434if (!is_admin()) {
    35   add_action('wp_head', wp_enqueue_script('jquery'));
    36   add_action('wp_head', wp_enqueue_script('collapsFunctions',
    37   "$url/wp-content/plugins/collapsing-categories/collapsFunctions.js",'',
    38   '1.6'));
     35  $inFooter=true;
     36  if ($options = get_option('collapsCatOptions') && $options['inFooter'])
     37    $inFooter = $options['inFooter'];
     38  wp_enqueue_script('collapsFunctions',
     39      "$url/wp-content/plugins/collapsing-categories/collapsFunctions.js",
     40      array('jquery'), '1.7', true);
    3941  add_action( 'wp_head', array('collapscat','get_head'));
    4042//  add_action( 'wp_footer', array('collapsCat','get_foot'));
     
    134136include_once( 'collapscatlist.php' );
    135137function collapsCat($args='', $print=true) {
     138  global $subCatPosts;
    136139  if (!is_admin()) {
    137140    list($posts, $categories, $parents, $options) =
     
    164167        ";
    165168      }
     169      //print_r($subCatPosts);
     170      echo phpArrayToJS($subCatPosts);
     171      // now we create an array indexed by the id of the ul for posts
     172
    166173      echo "// ]]>\n</script></li>\n";
    167174    } else {
  • collapsing-categories/trunk/collapscatlist.php

    r200498 r200947  
    22/*
    33collapsing categories version: 1.1.1
    4 copyright 2007 robert felty
    5 
    6 this work is largely based on the collapsing categories plugin by andrew rader
    7 (http://voidsplat.org), which was also distributed under the gplv2. i have tried
    8 contacting him, but his website has been down for quite some time now. see the
    9 changelog file for more information.
     4copyright 2007-2010 robert felty
    105
    116this file is part of collapsing categories
     
    2520    foundation, inc., 51 franklin st, fifth floor, boston, ma  02110-1301  usa
    2621*/
     22global $subCatPosts;
     23$subCatPosts = array();
     24
     25
     26function phpArrayToJS($array) {
     27  /* generates javscript code to create an array from a php array */
     28  $script = "var subCatPosts = new Object();\n";
     29  foreach ($array as $key => $value){
     30    $script .= "subCatPosts['$key'] = '" .
     31        addslashes(str_replace("\n", '', $value)) . "';\n";
     32  }
     33  return($script);
     34}
     35
    2736
    2837function add_to_includes($cat, $inexclusionarray) {
     
    6978function miscPosts($cat,$catlink,$subcatpostcount2, $posttext) {
    7079  /* this function will group posts into a miscellaneous sub-category */
    71   global $options, $expandSym, $collapseSym, $expandSymJS, $collapseSymJS,
    72       $cur_categories;
     80  global $options, $cur_categories, $subCatPosts;
    7381  extract($options);
    7482  $showHide='expand';
     
    102110  $miscposts.=$posttext;
    103111  $miscposts.="    </ul></li>\n";
     112  if ($theID!='' && !$subCatPosts[$theID]) {
     113    $subCatPosts[$theID] = $posttext;
     114  }
    104115  return($miscposts);
    105116}
     
    126137* add option to display number of comments
    127138*/
    128 function getSubPosts($posts, $cat2, $subCatPosts, $showPosts) {
     139function getSubPosts($posts, $cat2,$showPosts, $theID) {
    129140  /* returns all the posts for a given category */
    130   global $postsToExclude, $options, $thisPost;
     141  global $postsToExclude, $options, $thisPost, $subCatPosts;
    131142  extract($options);
    132143  $posttext2='';
     
    142153        continue;
    143154      if (!in_array($post2->ID, $postsToExclude)) {
    144         array_push($subCatPosts, $post2->ID);
    145155        $subCatPostCount2++;
    146156        if (!$showPosts) {
     
    170180          }
    171181        }
    172         $posttext2.= "<li class='collapsing categories item'><a $self
    173             href='".get_permalink($post2).
     182        $posttext2.= "<li class='collapsing categories item'><a $self " .
     183            "href='".get_permalink($post2).
    174184            "' title='$title_text'>$linktext</a></li>\n";
    175185      }
    176186    }
     187  }
     188  if ($theID!='' && !$subCatPosts[$theID]) {
     189    $subCatPosts[$theID] = $posttext2;
    177190  }
    178191  return array($subCatPostCount2, $posttext2);
     
    203216  $postself='';
    204217  extract($options);
    205   $subCatPosts=array();
    206218  $link2='';
    207219  if ($depth==0) {
     
    218230        $self="";
    219231      }
     232      list($subCatPostCount2, $posttext2) =
     233          getSubPosts($postsInCat[$cat2->term_id],$cat2, $showPosts, $theID);
    220234      if ($cat->term_id==$cat2->parent) {
    221         $foo.='hallo';
    222         list($subCatPostCount2, $posttext2) =
    223             getSubPosts($postsInCat[$cat2->term_id],$cat2, $subCatPosts,
    224             $showPosts);
     235        $theID='collapsCat-' . $cat2->term_id . "-$number";
    225236        $subCatPostCount+=$subCatPostCount2;
    226237        $subCatPostCounts[$depth]=$subCatPostCount2;
    227238        $expanded='none';
    228         $theID='collapsCat-' . $cat2->term_id . "-$number";
    229239        if (in_array($cat2->name, $autoExpand) ||
    230240            in_array($cat2->slug, $autoExpand) ||
     
    280290          }
    281291        } else {
    282           list ($subCatLink2, $subCatCount,$subCatPostCount,$subCatPosts)=
     292          list ($subCatLink2, $subCatCount,$subCatPostCount)=
    283293              get_sub_cat($cat2, $categories, $parents, $posts, $subCatCount,
    284294              $subCatPostCount,$expanded, $depth);
    285295          $subCatCount=1;
    286           list($subCatPostCount2, $posttext2) =
    287               getSubPosts($postsInCat[$cat2->term_id],$cat2, $subCatPosts,
    288               $showPosts);
    289296          if ($subCatPostCount2<1) {
    290297            continue;
     
    336343        if (($subCatCount>0) || ($showPosts)) {
    337344          $subCatLinks.="\n<ul id='$theID' style=\"display:$expanded\">\n";
     345          if ($expanded!='block')
     346            $posttext2='';
    338347          if ($subCatCount>0 && $posttext2!='' && $addMisc) {
    339348            $subCatLinks.=miscPosts($cat2,$catlink,$subCatPostCount2,
     
    355364        //echo $subCatLinks;
    356365  //      $subCatLinks.='<br />';
    357   return(array($subCatLinks,$subCatCount,$subCatPostCount,$subCatPosts));
     366  return(array($subCatLinks,$subCatCount,$subCatPostCount));
    358367  //return(array($foo,$subCatLinks));
    359368}
     
    541550  /* returns a list of categories, and optionally subcategories and posts,
    542551  which can be collapsed or expanded with javascript */
    543   global $expandSym,$collapseSym,$expandSymJS, $collapseSymJS,
    544       $wpdb,$options,$wp_query, $autoExpand, $postsToExclude,
     552  global $wpdb,$options,$wp_query, $autoExpand, $postsToExclude, $subCatPosts,
    545553      $cur_categories, $thisPost, $wp_rewrite, $catlink, $postsInCat;
    546554  extract($options);
     
    562570
    563571  foreach( $categories as $cat ) {
     572    $theID='collapsCat-' . $cat->term_id . "-$number";
    564573    if ($inExclude=='include' && !empty($includeCatArray)) {
    565574      if (!in_array($cat->term_id, $includeCatArray) &&
     
    581590    $subCatCount=0;
    582591    $subCatPostCounts=array();
    583     list ($subCatLinks, $subCatCount,$subCatPostCount, $subCatPosts)=
     592    list ($subCatLinks, $subCatCount,$subCatPostCount)=
    584593        get_sub_cat($cat, $categories, $parents, $posts,
    585594        $subCatCount,$subCatPostCount,$expanded,0);
    586     list($subCatPostCount2, $posttext2) =
    587         getSubPosts($postsInCat[$cat->term_id],$cat, $subCatPosts, $showPosts);
     595    //list($subCatPostCount2, $posttext2) =
     596        //getSubPosts($postsInCat[$cat->term_id],$cat, $showPosts, $theID);
     597    // Get post info
     598    $posttext='';
     599    if( ! empty($postsInCat[$cat->term_id]) ) {
     600      list ($subCatPostCount, $posttext) = getSubPosts($posts, $cat,
     601          $showPosts, $theID);
     602    }
    588603     
    589     $theCount=$subCatPostCount2+$subCatPostCount;
     604    //$theCount=$subCatPostCount2+$subCatPostCount;
     605    $theCount= $subCatPostCount;
     606
    590607    if ($theCount>0) {
    591608      $expanded='none';
    592       $theID='collapsCat-' . $cat->term_id . "-$number";
    593609      if (in_array($cat->name, $autoExpand) ||
    594610          in_array($cat->slug, $autoExpand) ||
     
    608624            "<span class='collapsing categories $showHide' ".
    609625            "onclick='expandCollapse(event, \"$expandSymJS\"," .
    610             "\"$collapseSymJS\", $animate, \"collapsing categories\"); return false'>".
    611             "<span class='sym'>$symbol</span>";
     626            "\"$collapseSymJS\", $animate, \"collapsing categories\");" .
     627            " return false'><span class='sym'>$symbol</span>";
    612628      } else {
    613629        $span = "      <li class='collapsing categories item'>";
     
    639655        }
    640656      }
    641       // Now print out the post info
    642       $posttext='';
    643       if( ! empty($postsInCat[$cat->term_id]) ) {
    644         list ($subCatPostCount, $posttext) = getSubPosts($posts, $cat,
    645             $subCatPosts, $showPosts);
    646       }
    647657      if( $showPostCount=='yes') {
    648658        $link .= ' (' . $theCount.')';
     
    662672      if ($showPosts) {
    663673        if ($subCatPostCount>0 && $subCatLinks!='' && $addMisc) {
    664           $posttext = (miscPosts($cat,$catlink,$subCatPostCount2,$posttext));
     674          $posttext = (miscPosts($cat,$catlink,$subCatPostCount,$posttext));
    665675        }
    666676      }
     
    670680        $text = $subCatLinks . $posttext;
    671681      }
     682      if ($theID!='') {
     683        $subCatPosts[$theID] = $text;
     684      }
     685      /* we only actually add the posts if it is expanded. Otherwise we add
     686         the posts dynamically to the dom from a javascript array
     687         However, we can't have an empty ul, so we create one emtpy li here */
     688      if ($expanded!='block') {
     689        $posttext='<li></li>';
     690      }
     691      if ($postsBeforeCats) {
     692        $text =$posttext . $subCatLinks;
     693      } else {
     694        $text = $subCatLinks . $posttext;
     695      }
    672696      $collapsCatText .= $text;
    673697      if ($showTopLevel) {
Note: See TracChangeset for help on using the changeset viewer.