Programatically / dynamically set properties

Having problems with DHTML Menu? There is usually somebody here who knows the answer.
Post Reply
JMM
Advanced
Advanced
Posts: 13
Joined: Tue Nov 11, 2003 10:47 pm

Programatically / dynamically set properties

Post by JMM »

Is there a way to programatically set individual menu and style properties and menu items? E.g. after setting up a menu using the normal syntax

Code: Select all

with(milonic=new menuname("facility")){
style=VHRC_sub_menu_style;
top="offset=+4";

aI('text=Directions;url=/inside/facility/directions.html;');

 . . .

}
is there a way to change individual properties, something like facility.top = "offset=+20", and add / remove menu items?
User avatar
Maz
Milonic God
Milonic God
Posts: 1717
Joined: Fri Jun 06, 2003 11:39 pm
Location: San Francisco
Contact:

Post by Maz »

Well, yes there is or was one, but I don't see it listed and can't remember what it was exactly. I'm sure someone will know, or a forum search.

Sorry,
maz
User avatar
kevin3442
Milonic God
Milonic God
Posts: 2460
Joined: Sat Sep 07, 2002 12:09 am
Location: Lincoln, NE
Contact:

Post by kevin3442 »

You can modify menu and menu item properties programatically. I've been playing around with some methods, but still have some kinks to work out (didn't know I'd be posting it). As for adding or removing menu items, I have an idea or two, but haven't been able to try them yet to see if they work. If you're willing to wait a bit, I'll probably be able to post something early next week. Just wanted to let you know that I've read your post and think I can help, but it'll take a little longer. Any specific properties you're interested in changing? A list would be helpful (some seem easier than others).

Kevin
JMM
Advanced
Advanced
Posts: 13
Joined: Tue Nov 11, 2003 10:47 pm

Post by JMM »

Thanks for the replies, sorry I let this lapse. The reason I asked originally is because I wanted to use the same menu as part of two different designs. Everything about the menu would be the same except for the positioning, as controlled by the top and left properties, so I was hoping I could load the same menu data file for both designs and then just change those properties before drawing the menus. I ended up setting those top and left values in the menu data file with a variable, which I assigned a value to just before the code calling for the menu data file.


The other specific thing I was interested in was adding / removing menu items after initially defining the menu. It was the same situation, I wanted to use the same menu as part of two different designs, but on the homepage of the site I didn't want a link to "Home" and on the other pages I did. I'd hate to have two separate copies of the same code for the menu definition with just the addition / omission of one menu item as the only difference. I guess I could always generate the menu data file dynamically with PHP (or whatever), but I was wondering if there was a way to do it just throught the JavaScript.
User avatar
kevin3442
Milonic God
Milonic God
Posts: 2460
Joined: Sat Sep 07, 2002 12:09 am
Location: Lincoln, NE
Contact:

Post by kevin3442 »

JMM wrote:Thanks for the replies, sorry I let this lapse.
NP... it's not like I actually replied early last week either! Holidays have been very busy indeed so far.
...Everything about the menu would be the same except for the positioning, as controlled by the top and left properties, so I was hoping I could load the same menu data file for both designs and then just change those properties before drawing the menus. I ended up setting those top and left values in the menu data file with a variable, which I assigned a value to just before the code calling for the menu data file.
You can change the position of the menu programatically after it has been rendered. But, if you can figure out the desired location before the menu is rendered, as you did, I think that's the better approach. If you do it after the menu is already visible, then the user may see the menu appear briefly and then change locations; visually better to avoid this "jump" effect if you can.
The other specific thing I was interested in was adding / removing menu items after initially defining the menu. It was the same situation, I wanted to use the same menu as part of two different designs, but on the homepage of the site I didn't want a link to "Home" and on the other pages I did...
Programatically adding items is something I have yet to figure out. But I can help you with removing items.

The following functions will help:

Code: Select all

function mm_getItemByName(itemName)
{
  for (i=0; i<_mi.length; i++) if (_mi[i][1].indexOf(itemName) != -1) return i;
  return -1;
}

function mm_removeItem(itemName)
{
  var itemNum = mm_getItemByName(itemName);
  if (itemNum == -1) return;

  var idx = 0;
  newItemArr = new Array();
  for (i=0; i<_m[_mi[itemNum][0]][0].length; i++) if (_m[_mi[itemNum][0]][0][i] != itemNum) newItemArr[idx++] = _m[_mi[itemNum][0]][0][i];
  _m[_mi[itemNum][0]][0] = newItemArr;
  BDMenu(_mi[itemNum][0]);
}
mm_removeItem() is the funciton you'd call to remove an item, as specified by the item's name. The passed parameter, itemName, is the name of the menu item, as defined by the text= property in the item's aI() definition. itemName is case sensitive.

mm_getItemByName() resolves the internal item ID based on the menu item's name; you won't call it directly in this case, but it is called from mm_removeItem(), so both functions are required to remove a menu item.

Suppose you have a menu item defined like so:

Code: Select all

aI("text=Home;url=index.html;status=To the home page;");
You could remove this item from the menu system by calling

Code: Select all

mm_removeItem('Home');
In your case, you could call the function from the body's onload event, on your home page. One catch: depending on how quickly your menu loads and renders, the user may see the 'Home' item briefly, and then see it disappear.

Caveat #1: right now, mm_removeItem() only takes one parameter, the itemName. Since there is no internal item name like there is for menu names, the itemName is based on the actual text the item displays. So, the search fo itemName is going to return on the first menu item it finds that displays that text. This may not be ideal if you have multiple menu items that display the same text. I plan to change the function so that you can specify the menu name as well, but I have not done so yet (feel free to change it if you like).

Caveat #2: I have only tested these two functions in: IE6, NS7, and Opera 7.2, in win2k. They worked in all three of these browser/OS combinations, but I have no idea about others.

BTW: have you considered leaving the menu the same on all pages, including the Home page, then using the built-in methods for highlighting the user's current location within the menu?

Hope that helps,

Kevin
User avatar
Hergio
Milonic God
Milonic God
Posts: 1123
Joined: Wed Jun 12, 2002 7:46 pm
Location: Rochester, NY

Post by Hergio »

Also look at some of the examples Milonic has up on the main website. For example, this http://milonic.com/menusample.php?sampleid=23 shows an example of how to get at some of the innards of the menu on the fly.
Dave Hergert
Software Engineer
"Helping to make the menu better, one :?: at a time."
Post Reply