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 the FCKToolbarSet object that is used to load and draw the
22 * toolbar.
23 */
24
25function FCKToolbarSet_Create( overhideLocation )
26{
27	var oToolbarSet ;
28
29	var sLocation = overhideLocation || FCKConfig.ToolbarLocation ;
30	switch ( sLocation )
31	{
32		case 'In' :
33				document.getElementById( 'xToolbarRow' ).style.display = '' ;
34				oToolbarSet = new FCKToolbarSet( document ) ;
35			break ;
36
37//		case 'OutTop' :
38			// Not supported.
39
40		default :
41			FCK.Events.AttachEvent( 'OnBlur', FCK_OnBlur ) ;
42			FCK.Events.AttachEvent( 'OnFocus', FCK_OnFocus ) ;
43
44			var eToolbarTarget ;
45
46			// Out:[TargetWindow]([TargetId])
47			var oOutMatch = sLocation.match( /^Out:(.+)\((\w+)\)$/ ) ;
48			if ( oOutMatch )
49			{
50				eToolbarTarget = eval( 'parent.' + oOutMatch[1] ).document.getElementById( oOutMatch[2] ) ;
51			}
52			else
53			{
54				// Out:[TargetId]
55				oOutMatch = sLocation.match( /^Out:(\w+)$/ ) ;
56				if ( oOutMatch )
57					eToolbarTarget = parent.document.getElementById( oOutMatch[1] ) ;
58			}
59
60			if ( !eToolbarTarget )
61			{
62				alert( 'Invalid value for "ToolbarLocation"' ) ;
63				return this._Init( 'In' ) ;
64			}
65
66			// If it is a shared toolbar, it may be already available in the target element.
67			oToolbarSet = eToolbarTarget.__FCKToolbarSet ;
68			if ( oToolbarSet )
69				break ;
70
71			// Create the IFRAME that will hold the toolbar inside the target element.
72			var eToolbarIFrame = FCKTools.GetElementDocument( eToolbarTarget ).createElement( 'iframe' ) ;
73			eToolbarIFrame.frameBorder = 0 ;
74			eToolbarIFrame.width = '100%' ;
75			eToolbarIFrame.height = '10' ;
76			eToolbarTarget.appendChild( eToolbarIFrame ) ;
77			eToolbarIFrame.unselectable = 'on' ;
78
79			// Write the basic HTML for the toolbar (copy from the editor main page).
80			var eTargetDocument = eToolbarIFrame.contentWindow.document ;
81			eTargetDocument.open() ;
82			eTargetDocument.write( '<html><head><script type="text/javascript"> window.onload = window.onresize = function() { window.frameElement.height = document.body.scrollHeight ; } </script></head><body style="overflow: hidden">' + document.getElementById( 'xToolbarSpace' ).innerHTML + '</body></html>' ) ;
83			eTargetDocument.close() ;
84
85			eTargetDocument.oncontextmenu = FCKTools.CancelEvent ;
86
87			// Load external resources (must be done here, otherwise Firefox will not
88			// have the document DOM ready to be used right away.
89			FCKTools.AppendStyleSheet( eTargetDocument, FCKConfig.SkinPath + 'fck_editor.css' ) ;
90
91			oToolbarSet = eToolbarTarget.__FCKToolbarSet = new FCKToolbarSet( eTargetDocument ) ;
92			oToolbarSet._IFrame = eToolbarIFrame ;
93
94			if ( FCK.IECleanup )
95				FCK.IECleanup.AddItem( eToolbarTarget, FCKToolbarSet_Target_Cleanup ) ;
96	}
97
98	oToolbarSet.CurrentInstance = FCK ;
99
100	FCK.AttachToOnSelectionChange( oToolbarSet.RefreshItemsState ) ;
101
102	return oToolbarSet ;
103}
104
105function FCK_OnBlur( editorInstance )
106{
107	var eToolbarSet = editorInstance.ToolbarSet ;
108
109	if ( eToolbarSet.CurrentInstance == editorInstance )
110		eToolbarSet.Disable() ;
111}
112
113function FCK_OnFocus( editorInstance )
114{
115	var oToolbarset = editorInstance.ToolbarSet ;
116	var oInstance = editorInstance || FCK ;
117
118	// Unregister the toolbar window from the current instance.
119	oToolbarset.CurrentInstance.FocusManager.RemoveWindow( oToolbarset._IFrame.contentWindow ) ;
120
121	// Set the new current instance.
122	oToolbarset.CurrentInstance = oInstance ;
123
124	// Register the toolbar window in the current instance.
125	oInstance.FocusManager.AddWindow( oToolbarset._IFrame.contentWindow, true ) ;
126
127	oToolbarset.Enable() ;
128}
129
130function FCKToolbarSet_Cleanup()
131{
132	this._TargetElement = null ;
133	this._IFrame = null ;
134}
135
136function FCKToolbarSet_Target_Cleanup()
137{
138	this.__FCKToolbarSet = null ;
139}
140
141var FCKToolbarSet = function( targetDocument )
142{
143	this._Document = targetDocument ;
144
145	// Get the element that will hold the elements structure.
146	this._TargetElement	= targetDocument.getElementById( 'xToolbar' ) ;
147
148	// Setup the expand and collapse handlers.
149	var eExpandHandle	= targetDocument.getElementById( 'xExpandHandle' ) ;
150	var eCollapseHandle	= targetDocument.getElementById( 'xCollapseHandle' ) ;
151
152	eExpandHandle.title		= FCKLang.ToolbarExpand ;
153	eExpandHandle.onclick	= FCKToolbarSet_Expand_OnClick ;
154
155	eCollapseHandle.title	= FCKLang.ToolbarCollapse ;
156	eCollapseHandle.onclick	= FCKToolbarSet_Collapse_OnClick ;
157
158	// Set the toolbar state at startup.
159	if ( !FCKConfig.ToolbarCanCollapse || FCKConfig.ToolbarStartExpanded )
160		this.Expand() ;
161	else
162		this.Collapse() ;
163
164	// Enable/disable the collapse handler
165	eCollapseHandle.style.display = FCKConfig.ToolbarCanCollapse ? '' : 'none' ;
166
167	if ( FCKConfig.ToolbarCanCollapse )
168		eCollapseHandle.style.display = '' ;
169	else
170		targetDocument.getElementById( 'xTBLeftBorder' ).style.display = '' ;
171
172	// Set the default properties.
173	this.Toolbars = new Array() ;
174	this.IsLoaded = false ;
175
176	if ( FCK.IECleanup )
177		FCK.IECleanup.AddItem( this, FCKToolbarSet_Cleanup ) ;
178}
179
180function FCKToolbarSet_Expand_OnClick()
181{
182	FCK.ToolbarSet.Expand() ;
183}
184
185function FCKToolbarSet_Collapse_OnClick()
186{
187	FCK.ToolbarSet.Collapse() ;
188}
189
190FCKToolbarSet.prototype.Expand = function()
191{
192	this._ChangeVisibility( false ) ;
193}
194
195FCKToolbarSet.prototype.Collapse = function()
196{
197	this._ChangeVisibility( true ) ;
198}
199
200FCKToolbarSet.prototype._ChangeVisibility = function( collapse )
201{
202	this._Document.getElementById( 'xCollapsed' ).style.display = collapse ? '' : 'none' ;
203	this._Document.getElementById( 'xExpanded' ).style.display = collapse ? 'none' : '' ;
204
205	if ( FCKBrowserInfo.IsGecko )
206	{
207		// I had to use "setTimeout" because Gecko was not responding in a right
208		// way when calling window.onresize() directly.
209		FCKTools.RunFunction( window.onresize ) ;
210	}
211}
212
213FCKToolbarSet.prototype.Load = function( toolbarSetName )
214{
215	this.Name = toolbarSetName ;
216
217	this.Items = new Array() ;
218
219	// Reset the array of toolbat items that are active only on WYSIWYG mode.
220	this.ItemsWysiwygOnly = new Array() ;
221
222	// Reset the array of toolbar items that are sensitive to the cursor position.
223	this.ItemsContextSensitive = new Array() ;
224
225	// Cleanup the target element.
226	this._TargetElement.innerHTML = '' ;
227
228	var ToolbarSet = FCKConfig.ToolbarSets[toolbarSetName] ;
229
230	if ( !ToolbarSet )
231	{
232		alert( FCKLang.UnknownToolbarSet.replace( /%1/g, toolbarSetName ) ) ;
233		return ;
234	}
235
236	this.Toolbars = new Array() ;
237
238	for ( var x = 0 ; x < ToolbarSet.length ; x++ )
239	{
240		var oToolbarItems = ToolbarSet[x] ;
241
242		var oToolbar ;
243
244		if ( typeof( oToolbarItems ) == 'string' )
245		{
246			if ( oToolbarItems == '/' )
247				oToolbar = new FCKToolbarBreak() ;
248		}
249		else
250		{
251			oToolbar = new FCKToolbar() ;
252
253			for ( var j = 0 ; j < oToolbarItems.length ; j++ )
254			{
255				var sItem = oToolbarItems[j] ;
256
257				if ( sItem == '-')
258					oToolbar.AddSeparator() ;
259				else
260				{
261					var oItem = FCKToolbarItems.GetItem( sItem ) ;
262					if ( oItem )
263					{
264						oToolbar.AddItem( oItem ) ;
265
266						this.Items.push( oItem ) ;
267
268						if ( !oItem.SourceView )
269							this.ItemsWysiwygOnly.push( oItem ) ;
270
271						if ( oItem.ContextSensitive )
272							this.ItemsContextSensitive.push( oItem ) ;
273					}
274				}
275			}
276
277			// oToolbar.AddTerminator() ;
278		}
279
280		oToolbar.Create( this._TargetElement ) ;
281
282		this.Toolbars[ this.Toolbars.length ] = oToolbar ;
283	}
284
285	FCKTools.DisableSelection( this._Document.getElementById( 'xCollapseHandle' ).parentNode ) ;
286
287	if ( FCK.Status != FCK_STATUS_COMPLETE )
288		FCK.Events.AttachEvent( 'OnStatusChange', this.RefreshModeState ) ;
289	else
290		this.RefreshModeState() ;
291
292	this.IsLoaded = true ;
293	this.IsEnabled = true ;
294
295	FCKTools.RunFunction( this.OnLoad ) ;
296}
297
298FCKToolbarSet.prototype.Enable = function()
299{
300	if ( this.IsEnabled )
301		return ;
302
303	this.IsEnabled = true ;
304
305	var aItems = this.Items ;
306	for ( var i = 0 ; i < aItems.length ; i++ )
307		aItems[i].RefreshState() ;
308}
309
310FCKToolbarSet.prototype.Disable = function()
311{
312	if ( !this.IsEnabled )
313		return ;
314
315	this.IsEnabled = false ;
316
317	var aItems = this.Items ;
318	for ( var i = 0 ; i < aItems.length ; i++ )
319		aItems[i].Disable() ;
320}
321
322FCKToolbarSet.prototype.RefreshModeState = function( editorInstance )
323{
324	if ( FCK.Status != FCK_STATUS_COMPLETE )
325		return ;
326
327	var oToolbarSet = editorInstance ? editorInstance.ToolbarSet : this ;
328	var aItems = oToolbarSet.ItemsWysiwygOnly ;
329
330	if ( FCK.EditMode == FCK_EDITMODE_WYSIWYG )
331	{
332		// Enable all buttons that are available on WYSIWYG mode only.
333		for ( var i = 0 ; i < aItems.length ; i++ )
334			aItems[i].Enable() ;
335
336		// Refresh the buttons state.
337		oToolbarSet.RefreshItemsState( editorInstance ) ;
338	}
339	else
340	{
341		// Refresh the buttons state.
342		oToolbarSet.RefreshItemsState( editorInstance ) ;
343
344		// Disable all buttons that are available on WYSIWYG mode only.
345		for ( var j = 0 ; j < aItems.length ; j++ )
346			aItems[j].Disable() ;
347	}
348}
349
350FCKToolbarSet.prototype.RefreshItemsState = function( editorInstance )
351{
352
353	var aItems = ( editorInstance ? editorInstance.ToolbarSet : this ).ItemsContextSensitive ;
354
355	for ( var i = 0 ; i < aItems.length ; i++ )
356		aItems[i].RefreshState() ;
357}
358