[jQuery] Re: Not a plugin but code for anyone trying to have collapse-able menu with cookies

2008-01-31 Thread Karl Swedberg


Very cool, Charles! Wow, I wish I had the time to study all the stuff  
that comes through this discussion list. Hard to keep up.



--Karl
_
Karl Swedberg
www.englishrules.com
www.learningjquery.com



On Jan 31, 2008, at 8:22 AM, Charles K. Clarkson wrote:



Karl Swedberg wrote:

: On my test page (http://test.learningjquery.com/cookie-menu.html), I
: put a link to another page with the same script and menu so you can
: see the persistence of the expand/collapse state.

   Never being able to leave well enough alone and having a little  
time

on my hands, I squeezed the cookie information into a separate object.
By calling an instance of it (using new) we can easily have multiple
menus on a page.

Here is the beginning of the cookieMenu code:

   $.fn.cookieMenu = function(cookieName){

   var $topLevel = this.find('li ul');
   $topLevel.hide();

   var menuCookie = new $.cookie.menu(cookieName);
   menuCookie.showCookie();


   Using a cookie object reduced the size of each click function. The
bigIndex() function is a private method of the $.cookie.menu object.

   var $this = $(this), $checkElement = $this.next('ul');

   if ( menuCookie.isOpen(index) ) {
   $checkElement.show();
   }
   $this.click(function() {
   if ($checkElement.is(':hidden')) {
   $checkElement.slideDown();
   menuCookie.open(index);

   } else {
   $checkElement.slideUp();
   menuCookie.close(index);
   }
   menuCookie.showCookie();
   return false;
   });


   I also made calling the menus little simpler. It assumes the
structure of the menu is a simple unordered list of lists like we have
been using.

script>