PageRenderTime 62ms CodeModel.GetById 12ms app.highlight 45ms RepoModel.GetById 1ms app.codeStats 0ms

/pigeoncms/Plugins/fckeditor/editor/_source/internals/fcktoolbarset.js

http://pigeoncms.googlecode.com/
JavaScript | 399 lines | 262 code | 83 blank | 54 comment | 48 complexity | 5ed6139e6a1eac8e74624e8274e3cf89 MD5 | raw file
  1/*
  2 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
  3 * Copyright (C) 2003-2009 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		case 'None' :
 37			oToolbarSet = new FCKToolbarSet( document ) ;
 38			break ;
 39
 40//		case 'OutTop' :
 41			// Not supported.
 42
 43		default :
 44			FCK.Events.AttachEvent( 'OnBlur', FCK_OnBlur ) ;
 45			FCK.Events.AttachEvent( 'OnFocus', FCK_OnFocus ) ;
 46
 47			var eToolbarTarget ;
 48
 49			// Out:[TargetWindow]([TargetId])
 50			var oOutMatch = sLocation.match( /^Out:(.+)\((\w+)\)$/ ) ;
 51			if ( oOutMatch )
 52			{
 53				if ( FCKBrowserInfo.IsAIR )
 54					FCKAdobeAIR.ToolbarSet_GetOutElement( window, oOutMatch ) ;
 55				else
 56					eToolbarTarget = eval( 'parent.' + oOutMatch[1] ).document.getElementById( oOutMatch[2] ) ;
 57			}
 58			else
 59			{
 60				// Out:[TargetId]
 61				oOutMatch = sLocation.match( /^Out:(\w+)$/ ) ;
 62				if ( oOutMatch )
 63					eToolbarTarget = parent.document.getElementById( oOutMatch[1] ) ;
 64			}
 65
 66			if ( !eToolbarTarget )
 67			{
 68				alert( 'Invalid value for "ToolbarLocation"' ) ;
 69				return arguments.callee( 'In' );
 70			}
 71
 72			// If it is a shared toolbar, it may be already available in the target element.
 73			oToolbarSet = eToolbarTarget.__FCKToolbarSet ;
 74			if ( oToolbarSet )
 75				break ;
 76
 77			// Create the IFRAME that will hold the toolbar inside the target element.
 78			var eToolbarIFrame = FCKTools.GetElementDocument( eToolbarTarget ).createElement( 'iframe' ) ;
 79			eToolbarIFrame.src = 'javascript:void(0)' ;
 80			eToolbarIFrame.frameBorder = 0 ;
 81			eToolbarIFrame.width = '100%' ;
 82			eToolbarIFrame.height = '10' ;
 83			eToolbarTarget.appendChild( eToolbarIFrame ) ;
 84			eToolbarIFrame.unselectable = 'on' ;
 85
 86			// Write the basic HTML for the toolbar (copy from the editor main page).
 87			var eTargetDocument = eToolbarIFrame.contentWindow.document ;
 88
 89			// Workaround for Safari 12256. Ticket #63
 90			var sBase = '' ;
 91			if ( FCKBrowserInfo.IsSafari )
 92				sBase = '<base href="' + window.document.location + '">' ;
 93
 94			// Initialize the IFRAME document body.
 95			eTargetDocument.open() ;
 96			eTargetDocument.write( '<html><head>' + sBase + '<script type="text/javascript"> var adjust = function() { window.frameElement.height = document.body.scrollHeight ; }; '
 97					+ 'window.onresize = window.onload = '
 98					+ 'function(){'		// poll scrollHeight until it no longer changes for 1 sec.
 99					+ 'var timer = null;'
100					+ 'var lastHeight = -1;'
101					+ 'var lastChange = 0;'
102					+ 'var poller = function(){'
103					+ 'var currentHeight = document.body.scrollHeight || 0;'
104					+ 'var currentTime = (new Date()).getTime();'
105					+ 'if (currentHeight != lastHeight){'
106					+ 'lastChange = currentTime;'
107					+ 'adjust();'
108					+ 'lastHeight = document.body.scrollHeight;'
109					+ '}'
110					+ 'if (lastChange < currentTime - 1000) clearInterval(timer);'
111					+ '};'
112					+ 'timer = setInterval(poller, 100);'
113					+ '}'
114					+ '</script></head><body style="overflow: hidden">' + document.getElementById( 'xToolbarSpace' ).innerHTML + '</body></html>' ) ;
115			eTargetDocument.close() ;
116
117			if( FCKBrowserInfo.IsAIR )
118				FCKAdobeAIR.ToolbarSet_InitOutFrame( eTargetDocument ) ;
119
120			FCKTools.AddEventListener( eTargetDocument, 'contextmenu', FCKTools.CancelEvent ) ;
121
122			// Load external resources (must be done here, otherwise Firefox will not
123			// have the document DOM ready to be used right away.
124			FCKTools.AppendStyleSheet( eTargetDocument, FCKConfig.SkinEditorCSS ) ;
125
126			oToolbarSet = eToolbarTarget.__FCKToolbarSet = new FCKToolbarSet( eTargetDocument ) ;
127			oToolbarSet._IFrame = eToolbarIFrame ;
128
129			if ( FCK.IECleanup )
130				FCK.IECleanup.AddItem( eToolbarTarget, FCKToolbarSet_Target_Cleanup ) ;
131	}
132
133	oToolbarSet.CurrentInstance = FCK ;
134	if ( !oToolbarSet.ToolbarItems )
135		oToolbarSet.ToolbarItems = FCKToolbarItems ;
136
137	FCK.AttachToOnSelectionChange( oToolbarSet.RefreshItemsState ) ;
138
139	return oToolbarSet ;
140}
141
142function FCK_OnBlur( editorInstance )
143{
144	var eToolbarSet = editorInstance.ToolbarSet ;
145
146	if ( eToolbarSet.CurrentInstance == editorInstance )
147		eToolbarSet.Disable() ;
148}
149
150function FCK_OnFocus( editorInstance )
151{
152	var oToolbarset = editorInstance.ToolbarSet ;
153	var oInstance = editorInstance || FCK ;
154
155	// Unregister the toolbar window from the current instance.
156	oToolbarset.CurrentInstance.FocusManager.RemoveWindow( oToolbarset._IFrame.contentWindow ) ;
157
158	// Set the new current instance.
159	oToolbarset.CurrentInstance = oInstance ;
160
161	// Register the toolbar window in the current instance.
162	oInstance.FocusManager.AddWindow( oToolbarset._IFrame.contentWindow, true ) ;
163
164	oToolbarset.Enable() ;
165}
166
167function FCKToolbarSet_Cleanup()
168{
169	this._TargetElement = null ;
170	this._IFrame = null ;
171}
172
173function FCKToolbarSet_Target_Cleanup()
174{
175	this.__FCKToolbarSet = null ;
176}
177
178var FCKToolbarSet = function( targetDocument )
179{
180	this._Document = targetDocument ;
181
182	// Get the element that will hold the elements structure.
183	this._TargetElement	= targetDocument.getElementById( 'xToolbar' ) ;
184
185	// Setup the expand and collapse handlers.
186	var eExpandHandle	= targetDocument.getElementById( 'xExpandHandle' ) ;
187	var eCollapseHandle	= targetDocument.getElementById( 'xCollapseHandle' ) ;
188
189	eExpandHandle.title		= FCKLang.ToolbarExpand ;
190	FCKTools.AddEventListener( eExpandHandle, 'click', FCKToolbarSet_Expand_OnClick ) ;
191
192	eCollapseHandle.title	= FCKLang.ToolbarCollapse ;
193	FCKTools.AddEventListener( eCollapseHandle, 'click', FCKToolbarSet_Collapse_OnClick ) ;
194
195	// Set the toolbar state at startup.
196	if ( !FCKConfig.ToolbarCanCollapse || FCKConfig.ToolbarStartExpanded )
197		this.Expand() ;
198	else
199		this.Collapse() ;
200
201	// Enable/disable the collapse handler
202	eCollapseHandle.style.display = FCKConfig.ToolbarCanCollapse ? '' : 'none' ;
203
204	if ( FCKConfig.ToolbarCanCollapse )
205		eCollapseHandle.style.display = '' ;
206	else
207		targetDocument.getElementById( 'xTBLeftBorder' ).style.display = '' ;
208
209	// Set the default properties.
210	this.Toolbars = new Array() ;
211	this.IsLoaded = false ;
212
213	if ( FCK.IECleanup )
214		FCK.IECleanup.AddItem( this, FCKToolbarSet_Cleanup ) ;
215}
216
217function FCKToolbarSet_Expand_OnClick()
218{
219	FCK.ToolbarSet.Expand() ;
220}
221
222function FCKToolbarSet_Collapse_OnClick()
223{
224	FCK.ToolbarSet.Collapse() ;
225}
226
227FCKToolbarSet.prototype.Expand = function()
228{
229	this._ChangeVisibility( false ) ;
230}
231
232FCKToolbarSet.prototype.Collapse = function()
233{
234	this._ChangeVisibility( true ) ;
235}
236
237FCKToolbarSet.prototype._ChangeVisibility = function( collapse )
238{
239	this._Document.getElementById( 'xCollapsed' ).style.display = collapse ? '' : 'none' ;
240	this._Document.getElementById( 'xExpanded' ).style.display = collapse ? 'none' : '' ;
241
242	if ( FCKBrowserInfo.IsGecko )
243	{
244		// I had to use "setTimeout" because Gecko was not responding in a right
245		// way when calling window.onresize() directly.
246		FCKTools.RunFunction( window.onresize ) ;
247	}
248}
249
250FCKToolbarSet.prototype.Load = function( toolbarSetName )
251{
252	this.Name = toolbarSetName ;
253
254	this.Items = new Array() ;
255
256	// Reset the array of toolbar items that are active only on WYSIWYG mode.
257	this.ItemsWysiwygOnly = new Array() ;
258
259	// Reset the array of toolbar items that are sensitive to the cursor position.
260	this.ItemsContextSensitive = new Array() ;
261
262	// Cleanup the target element.
263	this._TargetElement.innerHTML = '' ;
264
265	var ToolbarSet = FCKConfig.ToolbarSets[toolbarSetName] ;
266
267	if ( !ToolbarSet )
268	{
269		alert( FCKLang.UnknownToolbarSet.replace( /%1/g, toolbarSetName ) ) ;
270		return ;
271	}
272
273	this.Toolbars = new Array() ;
274
275	for ( var x = 0 ; x < ToolbarSet.length ; x++ )
276	{
277		var oToolbarItems = ToolbarSet[x] ;
278
279		// If the configuration for the toolbar is missing some element or has any extra comma
280		// this item won't be valid, so skip it and keep on processing.
281		if ( !oToolbarItems )
282			continue ;
283
284		var oToolbar ;
285
286		if ( typeof( oToolbarItems ) == 'string' )
287		{
288			if ( oToolbarItems == '/' )
289				oToolbar = new FCKToolbarBreak() ;
290		}
291		else
292		{
293			oToolbar = new FCKToolbar() ;
294
295			for ( var j = 0 ; j < oToolbarItems.length ; j++ )
296			{
297				var sItem = oToolbarItems[j] ;
298
299				if ( sItem == '-')
300					oToolbar.AddSeparator() ;
301				else
302				{
303					var oItem = FCKToolbarItems.GetItem( sItem ) ;
304					if ( oItem )
305					{
306						oToolbar.AddItem( oItem ) ;
307
308						this.Items.push( oItem ) ;
309
310						if ( !oItem.SourceView )
311							this.ItemsWysiwygOnly.push( oItem ) ;
312
313						if ( oItem.ContextSensitive )
314							this.ItemsContextSensitive.push( oItem ) ;
315					}
316				}
317			}
318
319			// oToolbar.AddTerminator() ;
320		}
321
322		oToolbar.Create( this._TargetElement ) ;
323
324		this.Toolbars[ this.Toolbars.length ] = oToolbar ;
325	}
326
327	FCKTools.DisableSelection( this._Document.getElementById( 'xCollapseHandle' ).parentNode ) ;
328
329	if ( FCK.Status != FCK_STATUS_COMPLETE )
330		FCK.Events.AttachEvent( 'OnStatusChange', this.RefreshModeState ) ;
331	else
332		this.RefreshModeState() ;
333
334	this.IsLoaded = true ;
335	this.IsEnabled = true ;
336
337	FCKTools.RunFunction( this.OnLoad ) ;
338}
339
340FCKToolbarSet.prototype.Enable = function()
341{
342	if ( this.IsEnabled )
343		return ;
344
345	this.IsEnabled = true ;
346
347	var aItems = this.Items ;
348	for ( var i = 0 ; i < aItems.length ; i++ )
349		aItems[i].RefreshState() ;
350}
351
352FCKToolbarSet.prototype.Disable = function()
353{
354	if ( !this.IsEnabled )
355		return ;
356
357	this.IsEnabled = false ;
358
359	var aItems = this.Items ;
360	for ( var i = 0 ; i < aItems.length ; i++ )
361		aItems[i].Disable() ;
362}
363
364FCKToolbarSet.prototype.RefreshModeState = function( editorInstance )
365{
366	if ( FCK.Status != FCK_STATUS_COMPLETE )
367		return ;
368
369	var oToolbarSet = editorInstance ? editorInstance.ToolbarSet : this ;
370	var aItems = oToolbarSet.ItemsWysiwygOnly ;
371
372	if ( FCK.EditMode == FCK_EDITMODE_WYSIWYG )
373	{
374		// Enable all buttons that are available on WYSIWYG mode only.
375		for ( var i = 0 ; i < aItems.length ; i++ )
376			aItems[i].Enable() ;
377
378		// Refresh the buttons state.
379		oToolbarSet.RefreshItemsState( editorInstance ) ;
380	}
381	else
382	{
383		// Refresh the buttons state.
384		oToolbarSet.RefreshItemsState( editorInstance ) ;
385
386		// Disable all buttons that are available on WYSIWYG mode only.
387		for ( var j = 0 ; j < aItems.length ; j++ )
388			aItems[j].Disable() ;
389	}
390}
391
392FCKToolbarSet.prototype.RefreshItemsState = function( editorInstance )
393{
394
395	var aItems = ( editorInstance ? editorInstance.ToolbarSet : this ).ItemsContextSensitive ;
396
397	for ( var i = 0 ; i < aItems.length ; i++ )
398		aItems[i].RefreshState() ;
399}