PageRenderTime 30ms CodeModel.GetById 13ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/source/Plug-in/fck/editor/_source/classes/fckpanel.js

http://prosporous.googlecode.com/
JavaScript | 359 lines | 234 code | 69 blank | 56 comment | 52 complexity | 20ef51a6f716002607c9781e618e752a MD5 | raw file
  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 * Component that creates floating panels. It is used by many
 22 * other components, like the toolbar items, context menu, etc...
 23 */
 24
 25var FCKPanel = function( parentWindow )
 26{
 27	this.IsRTL			= ( FCKLang.Dir == 'rtl' ) ;
 28	this.IsContextMenu	= false ;
 29	this._LockCounter	= 0 ;
 30
 31	this._Window = parentWindow || window ;
 32
 33	var oDocument ;
 34
 35	if ( FCKBrowserInfo.IsIE )
 36	{
 37		// Create the Popup that will hold the panel.
 38		this._Popup	= this._Window.createPopup() ;
 39		oDocument = this.Document = this._Popup.document ;
 40
 41		FCK.IECleanup.AddItem( this, FCKPanel_Cleanup ) ;
 42	}
 43	else
 44	{
 45		var oIFrame = this._IFrame = this._Window.document.createElement('iframe') ;
 46		oIFrame.src					= 'javascript:void(0)' ;
 47		oIFrame.allowTransparency	= true ;
 48		oIFrame.frameBorder			= '0' ;
 49		oIFrame.scrolling			= 'no' ;
 50		oIFrame.width = oIFrame.height = 0 ;
 51		FCKDomTools.SetElementStyles( oIFrame,
 52			{
 53				position	: 'absolute',
 54				zIndex		: FCKConfig.FloatingPanelsZIndex
 55			} ) ;
 56
 57		if ( this._Window == window.parent && window.frameElement )
 58		{
 59			var scrollPos = null ;
 60			if ( FCKBrowserInfo.IsGecko && FCK && FCK.EditorDocument )
 61				scrollPos = [ FCK.EditorDocument.body.scrollLeft, FCK.EditorDocument.body.scrollTop ] ;
 62			window.frameElement.parentNode.insertBefore( oIFrame, window.frameElement ) ;
 63			if ( scrollPos )
 64			{
 65				var restoreFunc = function()
 66				{
 67					FCK.EditorDocument.body.scrollLeft = scrollPos[0] ;
 68					FCK.EditorDocument.body.scrollTop = scrollPos[1] ;
 69				}
 70				setTimeout( restoreFunc, 500 ) ;
 71			}
 72		}
 73		else
 74			this._Window.document.body.appendChild( oIFrame ) ;
 75
 76		var oIFrameWindow = oIFrame.contentWindow ;
 77
 78		oDocument = this.Document = oIFrameWindow.document ;
 79
 80		// Workaround for Safari 12256. Ticket #63
 81		var sBase = '' ;
 82		if ( FCKBrowserInfo.IsSafari )
 83			sBase = '<base href="' + window.document.location + '">' ;
 84
 85		// Initialize the IFRAME document body.
 86		oDocument.open() ;
 87		oDocument.write( '<html><head>' + sBase + '<\/head><body style="margin:0px;padding:0px;"><\/body><\/html>' ) ;
 88		oDocument.close() ;
 89
 90		FCKTools.AddEventListenerEx( oIFrameWindow, 'focus', FCKPanel_Window_OnFocus, this ) ;
 91		FCKTools.AddEventListenerEx( oIFrameWindow, 'blur', FCKPanel_Window_OnBlur, this ) ;
 92	}
 93
 94	oDocument.dir = FCKLang.Dir ;
 95
 96	FCKTools.AddEventListener( oDocument, 'contextmenu', FCKTools.CancelEvent ) ;
 97
 98
 99	// Create the main DIV that is used as the panel base.
100	this.MainNode = oDocument.body.appendChild( oDocument.createElement('DIV') ) ;
101
102	// The "float" property must be set so Firefox calculates the size correctly.
103	this.MainNode.style.cssFloat = this.IsRTL ? 'right' : 'left' ;
104}
105
106
107FCKPanel.prototype.AppendStyleSheet = function( styleSheet )
108{
109	FCKTools.AppendStyleSheet( this.Document, styleSheet ) ;
110}
111
112FCKPanel.prototype.Preload = function( x, y, relElement )
113{
114	// The offsetWidth and offsetHeight properties are not available if the
115	// element is not visible. So we must "show" the popup with no size to
116	// be able to use that values in the second call (IE only).
117	if ( this._Popup )
118		this._Popup.show( x, y, 0, 0, relElement ) ;
119}
120
121FCKPanel.prototype.Show = function( x, y, relElement, width, height )
122{
123	var iMainWidth ;
124	var eMainNode = this.MainNode ;
125
126	if ( this._Popup )
127	{
128		// The offsetWidth and offsetHeight properties are not available if the
129		// element is not visible. So we must "show" the popup with no size to
130		// be able to use that values in the second call.
131		this._Popup.show( x, y, 0, 0, relElement ) ;
132
133		// The following lines must be place after the above "show", otherwise it
134		// doesn't has the desired effect.
135		FCKDomTools.SetElementStyles( eMainNode,
136			{
137				width	: width ? width + 'px' : '',
138				height	: height ? height + 'px' : ''
139			} ) ;
140
141		iMainWidth = eMainNode.offsetWidth ;
142
143		if ( this.IsRTL )
144		{
145			if ( this.IsContextMenu )
146				x  = x - iMainWidth + 1 ;
147			else if ( relElement )
148				x  = ( x * -1 ) + relElement.offsetWidth - iMainWidth ;
149		}
150
151		// Second call: Show the Popup at the specified location, with the correct size.
152		this._Popup.show( x, y, iMainWidth, eMainNode.offsetHeight, relElement ) ;
153
154		if ( this.OnHide )
155		{
156			if ( this._Timer )
157				CheckPopupOnHide.call( this, true ) ;
158
159			this._Timer = FCKTools.SetInterval( CheckPopupOnHide, 100, this ) ;
160		}
161	}
162	else
163	{
164		// Do not fire OnBlur while the panel is opened.
165		if ( typeof( FCK.ToolbarSet.CurrentInstance.FocusManager ) != 'undefined' )
166			FCK.ToolbarSet.CurrentInstance.FocusManager.Lock() ;
167
168		if ( this.ParentPanel )
169		{
170			this.ParentPanel.Lock() ;
171
172			// Due to a bug on FF3, we must ensure that the parent panel will
173			// blur (#1584).
174			FCKPanel_Window_OnBlur( null, this.ParentPanel ) ;
175		}
176
177		// Be sure we'll not have more than one Panel opened at the same time.
178		if ( FCKPanel._OpenedPanel )
179			FCKPanel._OpenedPanel.Hide() ;
180
181		FCKDomTools.SetElementStyles( eMainNode,
182			{
183				width	: width ? width + 'px' : '',
184				height	: height ? height + 'px' : ''
185			} ) ;
186
187		iMainWidth = eMainNode.offsetWidth ;
188
189		if ( !width )	this._IFrame.width	= 1 ;
190		if ( !height )	this._IFrame.height	= 1 ;
191
192		// This is weird... but with Firefox, we must get the offsetWidth before
193		// setting the _IFrame size (which returns "0"), and then after that,
194		// to return the correct width. Remove the first step and it will not
195		// work when the editor is in RTL.
196		//
197		// The "|| eMainNode.firstChild.offsetWidth" part has been added
198		// for Opera compatibility (see #570).
199		iMainWidth = eMainNode.offsetWidth || eMainNode.firstChild.offsetWidth ;
200
201		// Base the popup coordinates upon the coordinates of relElement.
202		var oPos = FCKTools.GetDocumentPosition( this._Window,
203			relElement.nodeType == 9 ?
204				( FCKTools.IsStrictMode( relElement ) ? relElement.documentElement : relElement.body ) :
205				relElement ) ;
206
207		// Minus the offsets provided by any positioned parent element of the panel iframe.
208		var positionedAncestor = FCKDomTools.GetPositionedAncestor( FCKTools.GetElementWindow( this._IFrame ), this._IFrame.parentNode ) ;
209		if ( positionedAncestor )
210		{
211			var nPos = FCKTools.GetDocumentPosition( FCKTools.GetElementWindow( positionedAncestor ), positionedAncestor ) ;
212			oPos.x -= nPos.x ;
213			oPos.y -= nPos.y ;
214		}
215
216		if ( this.IsRTL && !this.IsContextMenu )
217			x = ( x * -1 ) ;
218
219		x += oPos.x ;
220		y += oPos.y ;
221
222		if ( this.IsRTL )
223		{
224			if ( this.IsContextMenu )
225				x  = x - iMainWidth + 1 ;
226			else if ( relElement )
227				x  = x + relElement.offsetWidth - iMainWidth ;
228		}
229		else
230		{
231			var oViewPaneSize = FCKTools.GetViewPaneSize( this._Window ) ;
232			var oScrollPosition = FCKTools.GetScrollPosition( this._Window ) ;
233
234			var iViewPaneHeight	= oViewPaneSize.Height + oScrollPosition.Y ;
235			var iViewPaneWidth	= oViewPaneSize.Width + oScrollPosition.X ;
236
237			if ( ( x + iMainWidth ) > iViewPaneWidth )
238				x -= x + iMainWidth - iViewPaneWidth ;
239
240			if ( ( y + eMainNode.offsetHeight ) > iViewPaneHeight )
241				y -= y + eMainNode.offsetHeight - iViewPaneHeight ;
242		}
243
244		if ( x < 0 )
245			 x = 0 ;
246
247		// Set the context menu DIV in the specified location.
248		FCKDomTools.SetElementStyles( this._IFrame,
249			{
250				left	: x + 'px',
251				top		: y + 'px'
252			} ) ;
253
254		var iWidth	= iMainWidth ;
255		var iHeight	= eMainNode.offsetHeight ;
256
257		this._IFrame.width	= iWidth ;
258		this._IFrame.height = iHeight ;
259
260		// Move the focus to the IFRAME so we catch the "onblur".
261		this._IFrame.contentWindow.focus() ;
262
263		FCKPanel._OpenedPanel = this ;
264	}
265
266	this._IsOpened = true ;
267
268	FCKTools.RunFunction( this.OnShow, this ) ;
269}
270
271FCKPanel.prototype.Hide = function( ignoreOnHide )
272{
273	if ( this._Popup )
274		this._Popup.hide() ;
275	else
276	{
277		if ( !this._IsOpened || this._LockCounter > 0 )
278			return ;
279
280		// Enable the editor to fire the "OnBlur".
281		if ( typeof( FCKFocusManager ) != 'undefined' )
282			FCKFocusManager.Unlock() ;
283
284		// It is better to set the sizes to 0, otherwise Firefox would have
285		// rendering problems.
286		this._IFrame.width = this._IFrame.height = 0 ;
287
288		this._IsOpened = false ;
289
290		if ( this.ParentPanel )
291			this.ParentPanel.Unlock() ;
292
293		if ( !ignoreOnHide )
294			FCKTools.RunFunction( this.OnHide, this ) ;
295	}
296}
297
298FCKPanel.prototype.CheckIsOpened = function()
299{
300	if ( this._Popup )
301		return this._Popup.isOpen ;
302	else
303		return this._IsOpened ;
304}
305
306FCKPanel.prototype.CreateChildPanel = function()
307{
308	var oWindow = this._Popup ? FCKTools.GetDocumentWindow( this.Document ) : this._Window ;
309
310	var oChildPanel = new FCKPanel( oWindow ) ;
311	oChildPanel.ParentPanel = this ;
312
313	return oChildPanel ;
314}
315
316FCKPanel.prototype.Lock = function()
317{
318	this._LockCounter++ ;
319}
320
321FCKPanel.prototype.Unlock = function()
322{
323	if ( --this._LockCounter == 0 && !this.HasFocus )
324		this.Hide() ;
325}
326
327/* Events */
328
329function FCKPanel_Window_OnFocus( e, panel )
330{
331	panel.HasFocus = true ;
332}
333
334function FCKPanel_Window_OnBlur( e, panel )
335{
336	panel.HasFocus = false ;
337
338	if ( panel._LockCounter == 0 )
339		FCKTools.RunFunction( panel.Hide, panel ) ;
340}
341
342function CheckPopupOnHide( forceHide )
343{
344	if ( forceHide || !this._Popup.isOpen )
345	{
346		window.clearInterval( this._Timer ) ;
347		this._Timer = null ;
348
349		FCKTools.RunFunction( this.OnHide, this ) ;
350	}
351}
352
353function FCKPanel_Cleanup()
354{
355	this._Popup = null ;
356	this._Window = null ;
357	this.Document = null ;
358	this.MainNode = null ;
359}