1/*
2 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3 * Copyright (C) 2003-2007 Frederico Caldeira Knabben
4 *
5 * == BEGIN LICENSE ==
6 *
7 * Licensed under the terms of any of the following licenses at your
8 * choice:
9 *
10 *  - GNU General Public License Version 2 or later (the "GPL")
11 *    http://www.gnu.org/licenses/gpl.html
12 *
13 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14 *    http://www.gnu.org/licenses/lgpl.html
15 *
16 *  - Mozilla Public License Version 1.1 or later (the "MPL")
17 *    http://www.mozilla.org/MPL/MPL-1.1.html
18 *
19 * == END LICENSE ==
20 *
21 * Defines and renders a menu items in a menu block.
22 */
23
24var FCKMenuItem = function( parentMenuBlock, name, label, iconPathOrStripInfoArray, isDisabled )
25{
26	this.Name		= name ;
27	this.Label		= label || name ;
28	this.IsDisabled	= isDisabled ;
29
30	this.Icon = new FCKIcon( iconPathOrStripInfoArray ) ;
31
32	this.SubMenu			= new FCKMenuBlockPanel() ;
33	this.SubMenu.Parent		= parentMenuBlock ;
34	this.SubMenu.OnClick	= FCKTools.CreateEventListener( FCKMenuItem_SubMenu_OnClick, this ) ;
35
36	if ( FCK.IECleanup )
37		FCK.IECleanup.AddItem( this, FCKMenuItem_Cleanup ) ;
38}
39
40
41FCKMenuItem.prototype.AddItem = function( name, label, iconPathOrStripInfoArrayOrIndex, isDisabled )
42{
43	this.HasSubMenu = true ;
44	return this.SubMenu.AddItem( name, label, iconPathOrStripInfoArrayOrIndex, isDisabled ) ;
45}
46
47FCKMenuItem.prototype.AddSeparator = function()
48{
49	this.SubMenu.AddSeparator() ;
50}
51
52FCKMenuItem.prototype.Create = function( parentTable )
53{
54	var bHasSubMenu = this.HasSubMenu ;
55
56	var oDoc = FCKTools.GetElementDocument( parentTable ) ;
57
58	// Add a row in the table to hold the menu item.
59	var r = this.MainElement = parentTable.insertRow(-1) ;
60	r.className = this.IsDisabled ? 'MN_Item_Disabled' : 'MN_Item' ;
61
62	// Set the row behavior.
63	if ( !this.IsDisabled )
64	{
65		FCKTools.AddEventListenerEx( r, 'mouseover', FCKMenuItem_OnMouseOver, [ this ] ) ;
66		FCKTools.AddEventListenerEx( r, 'click', FCKMenuItem_OnClick, [ this ] ) ;
67
68		if ( !bHasSubMenu )
69			FCKTools.AddEventListenerEx( r, 'mouseout', FCKMenuItem_OnMouseOut, [ this ] ) ;
70	}
71
72	// Create the icon cell.
73	var eCell = r.insertCell(-1) ;
74	eCell.className = 'MN_Icon' ;
75	eCell.appendChild( this.Icon.CreateIconElement( oDoc ) ) ;
76
77	// Create the label cell.
78	eCell = r.insertCell(-1) ;
79	eCell.className = 'MN_Label' ;
80	eCell.noWrap = true ;
81	eCell.appendChild( oDoc.createTextNode( this.Label ) ) ;
82
83	// Create the arrow cell and setup the sub menu panel (if needed).
84	eCell = r.insertCell(-1) ;
85	if ( bHasSubMenu )
86	{
87		eCell.className = 'MN_Arrow' ;
88
89		// The arrow is a fixed size image.
90		var eArrowImg = eCell.appendChild( oDoc.createElement( 'IMG' ) ) ;
91		eArrowImg.src = FCK_IMAGES_PATH + 'arrow_' + FCKLang.Dir + '.gif' ;
92		eArrowImg.width	 = 4 ;
93		eArrowImg.height = 7 ;
94
95		this.SubMenu.Create() ;
96		this.SubMenu.Panel.OnHide = FCKTools.CreateEventListener( FCKMenuItem_SubMenu_OnHide, this ) ;
97	}
98}
99
100FCKMenuItem.prototype.Activate = function()
101{
102	this.MainElement.className = 'MN_Item_Over' ;
103
104	if ( this.HasSubMenu )
105	{
106		// Show the child menu block. The ( +2, -2 ) correction is done because
107		// of the padding in the skin. It is not a good solution because one
108		// could change the skin and so the final result would not be accurate.
109		// For now it is ok because we are controlling the skin.
110		this.SubMenu.Show( this.MainElement.offsetWidth + 2, -2, this.MainElement ) ;
111	}
112
113	FCKTools.RunFunction( this.OnActivate, this ) ;
114}
115
116FCKMenuItem.prototype.Deactivate = function()
117{
118	this.MainElement.className = 'MN_Item' ;
119
120	if ( this.HasSubMenu )
121		this.SubMenu.Hide() ;
122}
123
124/* Events */
125
126function FCKMenuItem_SubMenu_OnClick( clickedItem, listeningItem )
127{
128	FCKTools.RunFunction( listeningItem.OnClick, listeningItem, [ clickedItem ] ) ;
129}
130
131function FCKMenuItem_SubMenu_OnHide( menuItem )
132{
133	menuItem.Deactivate() ;
134}
135
136function FCKMenuItem_OnClick( ev, menuItem )
137{
138	if ( menuItem.HasSubMenu )
139		menuItem.Activate() ;
140	else
141	{
142		menuItem.Deactivate() ;
143		FCKTools.RunFunction( menuItem.OnClick, menuItem, [ menuItem ] ) ;
144	}
145}
146
147function FCKMenuItem_OnMouseOver( ev, menuItem )
148{
149	menuItem.Activate() ;
150}
151
152function FCKMenuItem_OnMouseOut( ev, menuItem )
153{
154	menuItem.Deactivate() ;
155}
156
157function FCKMenuItem_Cleanup()
158{
159	this.MainElement = null ;
160}