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 * Stretch the editor to full window size and back.
22 */
23
24var FCKFitWindow = function()
25{
26	this.Name = 'FitWindow' ;
27}
28
29FCKFitWindow.prototype.Execute = function()
30{
31	var eEditorFrame		= window.frameElement ;
32	var eEditorFrameStyle	= eEditorFrame.style ;
33
34	var eMainWindow			= parent ;
35	var eDocEl				= eMainWindow.document.documentElement ;
36	var eBody				= eMainWindow.document.body ;
37	var eBodyStyle			= eBody.style ;
38	var eParent ;
39
40	// No original style properties known? Go fullscreen.
41	if ( !this.IsMaximized )
42	{
43		// Registering an event handler when the window gets resized.
44		if( FCKBrowserInfo.IsIE )
45			eMainWindow.attachEvent( 'onresize', FCKFitWindow_Resize ) ;
46		else
47			eMainWindow.addEventListener( 'resize', FCKFitWindow_Resize, true ) ;
48
49		// Save the scrollbars position.
50		this._ScrollPos = FCKTools.GetScrollPosition( eMainWindow ) ;
51
52		// Save and reset the styles for the entire node tree. They could interfere in the result.
53		eParent = eEditorFrame ;
54		// The extra () is to avoid a warning with strict error checking. This is ok.
55		while( (eParent = eParent.parentNode) )
56		{
57			if ( eParent.nodeType == 1 )
58			{
59				eParent._fckSavedStyles = FCKTools.SaveStyles( eParent ) ;
60				eParent.style.zIndex = FCKConfig.FloatingPanelsZIndex - 1 ;
61			}
62		}
63
64		// Hide IE scrollbars (in strict mode).
65		if ( FCKBrowserInfo.IsIE )
66		{
67			this.documentElementOverflow = eDocEl.style.overflow ;
68			eDocEl.style.overflow	= 'hidden' ;
69			eBodyStyle.overflow		= 'hidden' ;
70		}
71		else
72		{
73			// Hide the scroolbars in Firefox.
74			eBodyStyle.overflow = 'hidden' ;
75			eBodyStyle.width = '0px' ;
76			eBodyStyle.height = '0px' ;
77		}
78
79		// Save the IFRAME styles.
80		this._EditorFrameStyles = FCKTools.SaveStyles( eEditorFrame ) ;
81
82		// Resize.
83		var oViewPaneSize = FCKTools.GetViewPaneSize( eMainWindow ) ;
84
85		eEditorFrameStyle.position	= "absolute";
86		eEditorFrameStyle.zIndex	= FCKConfig.FloatingPanelsZIndex - 1;
87		eEditorFrameStyle.left		= "0px";
88		eEditorFrameStyle.top		= "0px";
89		eEditorFrameStyle.width		= oViewPaneSize.Width + "px";
90		eEditorFrameStyle.height	= oViewPaneSize.Height + "px";
91
92		// Giving the frame some (huge) borders on his right and bottom
93		// side to hide the background that would otherwise show when the
94		// editor is in fullsize mode and the window is increased in size
95		// not for IE, because IE immediately adapts the editor on resize,
96		// without showing any of the background oddly in firefox, the
97		// editor seems not to fill the whole frame, so just setting the
98		// background of it to white to cover the page laying behind it anyway.
99		if ( !FCKBrowserInfo.IsIE )
100		{
101			eEditorFrameStyle.borderRight = eEditorFrameStyle.borderBottom = "9999px solid white" ;
102			eEditorFrameStyle.backgroundColor		= "white";
103		}
104
105		// Scroll to top left.
106		eMainWindow.scrollTo(0, 0);
107
108		// Is the editor still not on the top left? Let's find out and fix that as well. (Bug #174)
109		var editorPos = FCKTools.GetWindowPosition( eMainWindow, eEditorFrame ) ;
110		if ( editorPos.x != 0 )
111			eEditorFrameStyle.left = ( -1 * editorPos.x ) + "px" ;
112		if ( editorPos.y != 0 )
113			eEditorFrameStyle.top = ( -1 * editorPos.y ) + "px" ;
114
115		this.IsMaximized = true ;
116	}
117	else	// Resize to original size.
118	{
119		// Remove the event handler of window resizing.
120		if( FCKBrowserInfo.IsIE )
121			eMainWindow.detachEvent( "onresize", FCKFitWindow_Resize ) ;
122		else
123			eMainWindow.removeEventListener( "resize", FCKFitWindow_Resize, true ) ;
124
125		// Restore the CSS position for the entire node tree.
126		eParent = eEditorFrame ;
127		// The extra () is to avoid a warning with strict error checking. This is ok.
128		while( (eParent = eParent.parentNode) )
129		{
130			if ( eParent._fckSavedStyles )
131			{
132				FCKTools.RestoreStyles( eParent, eParent._fckSavedStyles ) ;
133				eParent._fckSavedStyles = null ;
134			}
135		}
136
137		// Restore IE scrollbars
138		if ( FCKBrowserInfo.IsIE )
139			eDocEl.style.overflow = this.documentElementOverflow ;
140
141		// Restore original size
142		FCKTools.RestoreStyles( eEditorFrame, this._EditorFrameStyles ) ;
143
144		// Restore the window scroll position.
145		eMainWindow.scrollTo( this._ScrollPos.X, this._ScrollPos.Y ) ;
146
147		this.IsMaximized = false ;
148	}
149
150	FCKToolbarItems.GetItem('FitWindow').RefreshState() ;
151
152	// It seams that Firefox restarts the editing area when making this changes.
153	// On FF 1.0.x, the area is not anymore editable. On FF 1.5+, the special
154	//configuration, like DisableFFTableHandles and DisableObjectResizing get
155	//lost, so we must reset it. Also, the cursor position and selection are
156	//also lost, even if you comment the following line (MakeEditable).
157	// if ( FCKBrowserInfo.IsGecko10 )	// Initially I thought it was a FF 1.0 only problem.
158	if ( FCK.EditMode == FCK_EDITMODE_WYSIWYG )
159		FCK.EditingArea.MakeEditable() ;
160
161	FCK.Focus() ;
162}
163
164FCKFitWindow.prototype.GetState = function()
165{
166	if ( FCKConfig.ToolbarLocation != 'In' )
167		return FCK_TRISTATE_DISABLED ;
168	else
169		return ( this.IsMaximized ? FCK_TRISTATE_ON : FCK_TRISTATE_OFF );
170}
171
172function FCKFitWindow_Resize()
173{
174	var oViewPaneSize = FCKTools.GetViewPaneSize( parent ) ;
175
176	var eEditorFrameStyle = window.frameElement.style ;
177
178	eEditorFrameStyle.width		= oViewPaneSize.Width + 'px' ;
179	eEditorFrameStyle.height	= oViewPaneSize.Height + 'px' ;
180}
181