Get 2GB of free storage at PersonalGrid

Control.ContextMenu

Pure JavaScript/CSS contextual menus for Prototype.

Introduction

Control.ContextMenu provides a simple API for programming contextual menus with Prototype. You can attach a context menu to the entire document or any Element. Menus can be triggered via a right click (default), or left click.

Right Click on Image

Left Click on Image


Show third menu item?

Consoleclear

Adding Context: Conditional Menu Items & Labels

You can modify the contents of the label by passing a function that returns a string into the label option instead of string. This function will be called each time the menu is opened. An optional condition function option can be used as well. If this condition returns false, the menu item will not appear in the menu. If no items meet the condition checks, the menu will not open.

JavaScript


	var context_menu_one = new Control.ContextMenu('container_one');
	context_menu_one.addItem({
		label: 'Left Image, Item 1',
		callback: function(){
			log('Left Image, item one picked.');
		}
	});
	context_menu_one.addItem({
		label: 'Left Image, Item 2',
		callback: function(){
			log('Left Image, item two picked.');
		}
	});
	
	var context_menu_two = new Control.ContextMenu('container_two',{
		leftClick: true
	});
	context_menu_two.addItem({
		label: 'Right Image, Item 1',
		callback: function(){
			log('Right Image, item one picked.');
		}
	});
	context_menu_two.addItem({
		label: 'Right Image, Item 2',
		callback: function(){
			log('Right Image, item two picked.');
		}
	});
	context_menu_two.addItem({
		label: function(){
			//return value can be modified to reflect application state
			return 'Right Image, Item 3';
		},
		condition: function(){
			return $('show_third_item').checked;
		},
		callback: function(){
			log('Right Image, item three picked.');
		}
	});
	

CSS


	#control_contextmenu {
		border:1px solid #666;
		background-color:#eee;
		min-width:150px;
	}

	#control_contextmenu ul {
		list-style:none;
		padding:0;
		margin:0;
		cursor:hand;
	}

	#control_contextmenu ul li {
		text-align:left;
		padding:3px 10px 3px 5px;
		margin:0;
		cursor:pointer;
		cursor:hand;
		font-family:"Lucida Grande",Verdana;
		text-decoration:none;
		color:#333;
		font-size:12px;
		font-weight:bold;
		border-top:1px solid #fff;
		border-left:1px solid #fff;
		border-bottom:1px solid #999;
		border-right:1px solid #999;
	}

	#control_contextmenu ul li.selected,
	#control_contextmenu ul li:hover {
		color:#fff;
		background-color:#3875d7;
		cursor:hand;
	}

	#control_contextmenu ul li.selected:hover {
		color:#333;
		background-color:#eee;
		cursor:hand;
	}

DOM Modifications

Control.ContextMenu.load() will insert a div#control_contextmenu as a child of document.body.

Class

ReturnNameDescription
nullload([bool capture_all])capture_all defaults to false, and will stop all context menu events if true. If you want to capture_all you must explicitly call load before creating any Control.ContextMenu instances.
nulldisable()
nullenable()Called by load()
mixedcurrentReturns false or open Control.ContextMenu instance.
boolenabled
arraymenus
numberoffsetDefaults to 4. How many pixels from the edge of the screen the menu can be positioned.

Instance

ReturnNameDescription
Control.ContextMenuinitialize(Element container [,Hash options])container is the element that the ContextMenu is attached to.
Control.ContextMenuaddItem(Hash item)See Item Hash below. Returns "this", for chaining.
boolclose([Event])
nulldestroy()
boolopen(Event)To manually open the menu you must pass a mouse event.

Item Hash Structure

TypeKeyDescription
stringlabelText that will appear in the menu.
functioncallbackWill be called when the menu item is selected.
functionconditionOptional condition that will be called each time the menu is opened to see if the item will be included in the menu.

Options

TypeNameDefaultDescription
stringactivatedClassName'activated'Added to the menu item once it has been selected, not toggled during animation.
boolanimationtrueToggles selectedClassName animationCycles * 2 times over animationLength.
numberanimationCycles2
numberanimationLength300Length of animationCycles combined in milliseconds.
booldelayCallbacktrueWether to call the callback immediately, or after the animation is complete.
booldisableOnAltKeytrueAllows browser context menu to open if alt key is pressed.
booldisableOnShiftKeytrueAllows browser context menu to open if shift key is pressed.
boolleftClickfalseWill open the context menu with a left click, instead of right click.
stringselectedClassName'selected'

Events

NameDescription
afterClose()
afterOpen()
beforeClose()Throwing $break inside an observer will prevent the menu from closing.
beforeOpen()Throwing $break inside an observer will prevent the menu from opening.