JavaScript and cookies

Hi,

My app has a series of sidebar menus that are getting a bit long and
I'd like to compact them into an optional dropdown. I know how to do a
simple show/hide action, but I'd like to use cookies to store the user
preferences so that same sidebars stay open/closed when they navigate
around the site.

Does anybody know any good rails based tutorials that would help a
JavaScript novice get started on this one.

thank you,

Dan

DanC wrote:

My app has a series of sidebar menus that are getting a bit long and
I'd like to compact them into an optional dropdown. I know how to do a
simple show/hide action, but I'd like to use cookies to store the user
preferences so that same sidebars stay open/closed when they navigate
around the site.

Does anybody know any good rails based tutorials that would help a
JavaScript novice get started on this one.

I'm thinking you could simply store a bit-mask in the session where each
bit represents the state of each menu item.

For example if you had 5 collapsible menus and the second and fourth
menu were open then store 01010 = 10 in session[:menu_state] = 10. Pass
session[:menu_state] to your JavaScript on each request and use that to
control your menu.

Hi Robert,

Thank you for your response.

I understand the basic idea of what your suggesting.

I hope this isn't a stupid question but I don't understand the 01010 =
10 part?

Could you elaborate a little more on that please?

Thank you,

Dan

DanC wrote:

I hope this isn't a stupid question but I don't understand the 01010 =
10 part?

I was simply showing the 01010 in binary (0b01010) is equal to 10 in
decimal.

So in order to create a bit mask all you need to store is a simple
integer (Fixnum).

Example 1:

Say your bit mask value is:
x = 0b01010
=> 10

If you want to know if the second bit from the left is set create a mask
with the desired bit set:

mask = 0b01000
=> 8

then bit-wise AND the value with the mask:

x & mask > 0
=> true

mask = 0b00100
x & mask > 0 # is the third bit from left set?
=> false

Example 2:
http://pastie.textmate.org/private/acweqtdqm3jdhtufe4ag

Hi Robert,

The suggestion you made was bit beyond me, so I asked a friend of mine
to take a look and he implemented your binary suggested excellently
using jquery

He added a menu.js file that looks like this for my 6 menus

menu.js