Click here to Skip to main content
15,886,258 members
Articles / Web Development / HTML

ABC to Handling Visual Objects using Javascript

Rate me:
Please Sign up or sign in to vote.
4.36/5 (4 votes)
13 Jul 2007CPOL3 min read 28.5K   165   21  
This article shows how to create, move, delete and select current visual objects inside a display
/******************************************************
*	definitii ContextMenu 
******************************************************/
	var indexCM = 0; 

/******************************************************
*	ContextMenu 
******************************************************/
	/* menu item object */
	function MenuItem(text, action, image)
	{
		this.text = text; 
		this.action = action; 
		this.image = image; 
	}
	/* context menu object */
	function ContextMenu(itemList)
	{
		this.id = "cm" + indexCM++; 
		this.Items = itemList; 
		this.mouseout = false; 
		this.Create = function() {CreateContextMenu(this);}
		this.Show = function() {return ShowContextMenu(this);}
		this.Hide = function() {HideContextMenu(this);}
	}
	/* creates context menu instance */
	function CreateContextMenu(obj)
	{
		var me = ""; 
		me += "<div id='contextMenu" + obj.id + "' class='contextMenu'"; 
	//	me += " style='z-index:0;'";
		me += ">"; 
		for (i=0; i<obj.Items.length; i++)
		{
			if (obj.Items[i].text == "hr")
			{
				me += "<hr>"; 
				me += "</hr>"; 
			}
			else 
			{
				me += "<div class='menuItem' "; 
				me += "onclick='" + obj.Items[i].action + "' "; 
				me += "onmouseover='this.className=\"menuItemOver\"'"; 
				me += "onmouseout='this.className=\"menuItem\"'"; 
				me += ">"; 
				me += "<div class='itemImage' >"; 
				if (obj.Items[i].image != "" && obj.Items[i].image != null)
				{
					me += "<img src='" + obj.Items[i].image + "' >"; 
				} 
				me += "</div>"; 
				me += obj.Items[i].text; 
				me += "</div>"; 
			}
		}
		me += "</div>"; 
		
		document.writeln(me);
	}
	/* shows CM */
	function ShowContextMenu(obj)
	{
		obj = document.getElementById("contextMenu" + obj.id); 
		if (obj == null) 
			return (true); 
		if (obj.mouseout) 
		{ 
			HideContextMenu(obj); 
			return (true); 
		} 
		obj.style.top = event.clientY; 
		obj.style.left = event.clientX; 
		obj.style.visibility = "visible"; 
		return (false); 
	} 
	/* hides CM */
	function HideContextMenu(obj) 
	{ 
		obj = document.getElementById("contextMenu" + obj.id); 
		if (obj == null) 
			return; 
		obj.style.visibility = "hidden"; 
	} 

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions