Turning Drupal's "Popular" Block Into Tabs With jQuery

I've been working on a couple of sites the last week, and and both of them ended up using the "Popular Content" block from Drupal's Statistics module (not used on this site). One thing that I really don't like about that block is that is uses so much vertical space, so to make it all webified I decided to turn it into tabs. I know there's a couple of modules like Quicktabs that allow for easily configurable tabbed blocks, but this was going to be one of the few tabbed blocks (in one case the only one) on the site, and it seemed like overkill to install a new module to "tab" a single block. I decided to just use the jQuery Tools tabs plugin to fix the block. Here's the default output from Drupal:

Popular content

My Issues With this block

Notice how tall this thing is for about ten links. I suppose it would like nice with longer titles, but really, it feels like it can get much more compact, and I don't know if this that important for users to be able to scan all the titles. I hate lists that are too long, and I assume others are the same. On the other hand, I think this is a perfectly good view for those who aren't using javascript so I don't want to mess the formatting up for them.

Popular content

Here's the stub of a plugin I wrote to handle the situation:
$.fn.extend({
  tabbify: function() {
    return this.each( function(){
      # The 
tags in Drupal's output mess up formatting, so # these get removed outright $(this).find('br').each( function() { $(this).remove(); }) # In order for these to appear as tabs, we have to add a CSS class $(this).addClass("tabbed-panes"); # We get a list of h3 tags from the block as these are going to # act as our tab titles var headers = $(this).find('h3'); var hcount = headers.length; var init = 1; var pid = $(this).parent().attr("id"); var tabid = pid +'-tabs'; var tablist = '
    '; # We iterate through the h3s and create list items from each headers.each( function() { tablist += '
  • '+$(this).html()+'
  • '; if (init == hcount) { tablist +='
'; } else { init++; } # I opted to remove the h3s, but I suppose I could have done this instead: # $(this).css({'display': 'none'}); # It would have had the same effect, and not messed with what # was there too much $(this).remove(); }); # I should note that the target of the plugin would be the div .content # inside the block. The Tabs plugin requires an item list before the # element containing the panels and this is where we insert that list $(this).before(tablist); # We initialize our tabs $("ul#"+tabid).tabs("#"+pid+" div.content > div"); }); } });
...and here's how we call on this plugin:
$(document).ready( function() { 
  $("#block-statistics-0 .content").tabbify();
});

Demo

Here's a demo of the difference : Toggle the block like you mean it!

It's not a real plug in as this was written pretty specifically for this particular task, but I assume it can be generalised into a block tabbing plugin if it really needed to be.

The tabs are pretty basically styled (in this case with horrible colors, but I didn't want to spend too much time styling a demo) with this css file. I have no idea how this looks in IE6, but I really don't care. People who use it deserve the experience they get.

Popular content