{SOLVED} Dynamic menu creation at runtime

Having problems with DHTML Menu? There is usually somebody here who knows the answer.
Post Reply
User avatar
mtd_conseil
Advanced
Advanced
Posts: 27
Joined: Fri Jul 26, 2002 6:41 pm
Location: Quebec, Canada
Contact:

{SOLVED} Dynamic menu creation at runtime

Post by mtd_conseil »

Hi,

I want to create dynamic menus when some event happens.

If I do this:

Code: Select all

<script>
with(milonic=new menuname("test")){
            margin=3;
            style=contextStyle;
            alwaysvisible=true;
            aI("type=draggable;text=test;url=javascript:alert('hello');");
}
drawMenus();
</script>
it works.

But if I do this:

Code: Select all

<script>
function test() {
      with(milonic=new menuname("test")){
            margin=3;
            style=contextStyle;
            alwaysvisible=true;
            aI("type=draggable;text=test;url=javascript:alert('hello');");
      }
}
drawMenus();
</script>
 <a href="#" onclick="javascript:test();return false">test</a>
I get errors. But that is exactly what I need.

Any idea? :)
Last edited by mtd_conseil on Wed Jan 04, 2006 3:41 pm, edited 1 time in total.
Mike
User avatar
Ruth
 Team
 Team
Posts: 8763
Joined: Thu May 15, 2003 5:02 am
Location: Yucaipa, CA
Contact:

Post by Ruth »

Hi,

Can you maybe give a page with this on it? There doesn't seem to be anyone who can understand from what you posted, maybe if there is a page someone could get the files and figure it out?

The only thing that came to mind was a function Kevin wrote called setTimeOut something about checking menus were loaded or something before running a function. It's in the demos.

http://support.milonic.com/demos/

Ruth
User avatar
mtd_conseil
Advanced
Advanced
Posts: 27
Joined: Fri Jul 26, 2002 6:41 pm
Location: Quebec, Canada
Contact:

Post by mtd_conseil »

What I say is that you can't use "with(milonic=new menuname(..." method when they are embeded inside a function. It does not seems to be understood or it's something like out of scope.

Let's say you want to create a new menu (not a menu item) when you click on an hyperlink. on an onlick event of the hyperlink, I would call a function where I have a "with(milonic=new menuname(..." method.

I don't have the time at the moment I write this message, but I could write a sample. My example above is the exact sample though.

;)
Mike
User avatar
mtd_conseil
Advanced
Advanced
Posts: 27
Joined: Fri Jul 26, 2002 6:41 pm
Location: Quebec, Canada
Contact:

Post by mtd_conseil »

here is a sample:

each time you click on the link create menu, it should create a new dragable menu with one item. Not a menu item only.

http://www.virtuelcom.net/sample/index.html
Mike
User avatar
Andy
Milonic
Milonic
Posts: 3308
Joined: Sun May 19, 2002 8:23 pm
Location: Menu Developer
Contact:

Post by Andy »

I have now created a function that will create a new menu from scratch.

Here are the functions required:


Code: Select all

function _excMD($dta){ 
_it=$dta.split(":"); 
_DoP=_it[1] 
return _DoP.replace(/;/g,"") 
} 

function mm_createNewMenus() 
{ 
for(_y=_mcnt;_y<_m.length;_y++) 
{ 
_nM=_d.createElement("div") 
_nM.id="menu"+_y 
_nM.onmouseout=new Function("startClose()"); 
_nM.onmouseover=new Function("stopClose("+_y+")"); 
_nM.onselectstart=new Function("return _f"); 
if(_dB.appendChild)_dB.appendChild(_nM) 
_drawMenu(_y,0) 
_nM.className=_cls 
_nS=_nM.style 
if(_padd)_nS.padding=_excMD(_padd) 
if(_ofb)_nS.background=_excMD(_ofb) 
if(_brd)_nS.border=_excMD(_brd) 
if(_wid)_nS.width=_excMD(_wid) 
_nM.style.zindex=499 
_nM.style.visibility=_visi 
if(_posi)_nS.position=_excMD(_posi) 
if(_top)_nS.top=_excMD(_top) 
if(_left)_nS.left=_excMD(_left) 
if(_bgimg)_nS.backgroundImage="url("+_excMD(_bgimg)+")"; 
if(_mbgc)_nS.background=_excMD(_mbgc) 
_m[_y][23]=0 
} 
} 


And here is how you can call it:


Code: Select all

with(milonic=new menuname("test")){ 
margin=3; 
style=menuStyle; 
alwaysvisible=true; 
aI("type=draggable;text=test;url=javascript:alert('hello');"); 
} 
mm_createNewMenus() 
The docs and a sample are enroute.
User avatar
mtd_conseil
Advanced
Advanced
Posts: 27
Joined: Fri Jul 26, 2002 6:41 pm
Location: Quebec, Canada
Contact:

Post by mtd_conseil »

Where should I put the function? because it does not seems to work for me.


Best regards,
Mike
User avatar
Andy
Milonic
Milonic
Posts: 3308
Joined: Sun May 19, 2002 8:23 pm
Location: Menu Developer
Contact:

Post by Andy »

Here is a new updated version of the above code:

Code: Select all

function _excMD($dta) { 
	_it=$dta.split(":"); 
	_DoP=_it[1] 
	return _DoP.replace(/;/g,Z$) 
} 

function mm_createNewMenus() { 
	$r();
	_startM=0
	for(_y=_mcnt;_y<_m.length;_y++) { 
		_NM=_m[_y]
		_nM=_d.createElement("div") 
		_nM.id="menu"+_y 
		_nM.onmouseout=new Function("$I()"); 
		_nM.onmouseover=new Function("$J("+_y+")"); 
		_nM.onselectstart=new Function("return _f"); 
		
		if(_dB.appendChild)
		{
			_dB.appendChild(_nM) 
			o$(_y,0) 
			_nM.className=_cls 
			_nS=_nM.style 
			if($1B)_nS.padding=_excMD($1B) 
			if(_NM[17])_nS.width=$pX(_NM[17])
			if(_NM[24])_nS.height=$pX(_NM[24])
			if(_ofb)_nS.background=_excMD(_ofb) 
			if(p_)_nS.border=_excMD(p_) 
			if(_wid)_nS.width=_excMD(_wid) 
			_nM.style.zindex=499 
			_nM.style.visibility=_visi 
			if(n_)_nS.position=_excMD(n_) 
			if($k)_nS.top=_excMD($k) 
			if($l)_nS.left=_excMD($l) 
			if(_bgimg)_nS.backgroundImage="url("+_excMD(_bgimg)+")"; 
			//if(_mbgc)_nS.background=_excMD(_mbgc) 
			_NM[23]=0 
		}
	} 
} 
User avatar
mtd_conseil
Advanced
Advanced
Posts: 27
Joined: Fri Jul 26, 2002 6:41 pm
Location: Quebec, Canada
Contact:

Post by mtd_conseil »

Wonderful... It works like a charm!

Thank you very much!

Kind regards,

Michel
Mike
erickmiller
Beginner
Beginner
Posts: 5
Joined: Thu Jan 19, 2006 7:06 am

Post by erickmiller »

Just checking to see if there were complete examples of this yet? I haven't been able to make this code work.

Thanks!

Eric
User avatar
mtd_conseil
Advanced
Advanced
Posts: 27
Joined: Fri Jul 26, 2002 6:41 pm
Location: Quebec, Canada
Contact:

Post by mtd_conseil »

Well, actually, not from me.

Our actual project is on a private intranet for now.
Menus will be created from AJAX-like commands.
For example, to create user menus ater the user has login, without reloading the page.

If you are not using the dragable stuff, just remove the type=draggable and be certain to have the latest version 5.740 or later.

Regards,
Mike
erickmiller
Beginner
Beginner
Posts: 5
Joined: Thu Jan 19, 2006 7:06 am

Post by erickmiller »

That's exactly what we're doing! :) I have a system with draggable objects that I'd like to have contextual menus show up on the objects based on what is in a database. I'm using AJAX to pull the information to the browser on-the-fly when a right-click occurs on an object. I'd like to integrate the contextual menus into this so they have updated information based on the changes made in the database.

I was wondering if Andy had any examples using the code shown in this post. You must have gotten it to work. I created a relatively simple example and couldn't get a new menu created. I haven't had time to come back to it until now, but I'll see what I can make work. If I can come up with a simple example, I'll post it.
User avatar
Andy
Milonic
Milonic
Posts: 3308
Joined: Sun May 19, 2002 8:23 pm
Location: Menu Developer
Contact:

Post by Andy »

Hi,

There is a demo of this at http://milonic.com/createmenu.htm

Note that you will need version 5.743 (Pre Release) along with the updated mm_menueditapi.js file from http://milonic.com/menuversions/mm_menueditapi.js

Hope this helps,
Andy
JasperJames
Beginner
Beginner
Posts: 1
Joined: Tue Jan 31, 2006 8:53 pm

Post by JasperJames »

Could you show an example of inserting a dynamically created menu into an existing menu?
Is it possible to substitute a new menu for an existing menu item?

Also, I've been trying to use mm_insertItem method and haven't had much luck. I get a bunch of '_gm has no properties' errors. deleteItem and editItem do seem to work.

Thanks for your help,

Jasper
gelid
Advanced
Advanced
Posts: 11
Joined: Fri Dec 30, 2005 3:53 pm

Post by gelid »

JasperJames wrote:Could you show an example of inserting a dynamically created menu into an existing menu?
Is it possible to substitute a new menu for an existing menu item?

Also, I've been trying to use mm_insertItem method and haven't had much luck. I get a bunch of '_gm has no properties' errors. deleteItem and editItem do seem to work.

Thanks for your help,

Jasper
I am also trying to use the mm_insertItem without any luck. Are there any samples of this anywhere? Any help would be appreciated.
User avatar
Andy
Milonic
Milonic
Posts: 3308
Joined: Sun May 19, 2002 8:23 pm
Location: Menu Developer
Contact:

Post by Andy »

Hi,

There is a sample at http://milonic.com/menueditapi.php

Hope this helps,
Andy
User avatar
mtd_conseil
Advanced
Advanced
Posts: 27
Joined: Fri Jul 26, 2002 6:41 pm
Location: Quebec, Canada
Contact:

Post by mtd_conseil »

hi Andy,

You changed completely the concept, or the other method is also still working ? mm_createNewMenus();

Michel
Mike
User avatar
Andy
Milonic
Milonic
Posts: 3308
Joined: Sun May 19, 2002 8:23 pm
Location: Menu Developer
Contact:

Post by Andy »

It should be backward compatible.

mm_createNewMenus(); is a new function so wont have any effect on previous versions.
User avatar
mtd_conseil
Advanced
Advanced
Posts: 27
Joined: Fri Jul 26, 2002 6:41 pm
Location: Quebec, Canada
Contact:

Post by mtd_conseil »

ok,

I was not aware through out all the tests we made together of those new functions in the last demo you placed above.

They look easier to use then the previous way we made it.

At least we will have the time reprogramme our page.

thank you,

Best regards,

Michel
Mike
Post Reply