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 * Utility functions. (Gecko version).
22 */
23
24FCKTools.CancelEvent = function( e )
25{
26	if ( e )
27		e.preventDefault() ;
28}
29
30FCKTools.DisableSelection = function( element )
31{
32	if ( FCKBrowserInfo.IsGecko )
33		element.style.MozUserSelect	= 'none' ;	// Gecko only.
34	else
35		element.style.userSelect	= 'none' ;	// CSS3 (not supported yet).
36}
37
38// Appends a CSS file to a document.
39FCKTools._AppendStyleSheet = function( documentElement, cssFileUrl )
40{
41	var e = documentElement.createElement( 'LINK' ) ;
42	e.rel	= 'stylesheet' ;
43	e.type	= 'text/css' ;
44	e.href	= cssFileUrl ;
45	documentElement.getElementsByTagName("HEAD")[0].appendChild( e ) ;
46	return e ;
47}
48
49// Appends a CSS style string to a document.
50FCKTools._AppendStyleString = function( documentElement, cssStyles )
51{
52	var e = documentElement.createElement( "STYLE" ) ;
53	e.appendChild( documentElement.createTextNode( cssStyles ) ) ;
54	documentElement.getElementsByTagName( "HEAD" )[0].appendChild( e ) ;
55	return e ;
56}
57
58// Removes all attributes and values from the element.
59FCKTools.ClearElementAttributes = function( element )
60{
61	// Loop throw all attributes in the element
62	for ( var i = 0 ; i < element.attributes.length ; i++ )
63	{
64		// Remove the element by name.
65		element.removeAttribute( element.attributes[i].name, 0 ) ;	// 0 : Case Insensitive
66	}
67}
68
69// Returns an Array of strings with all defined in the elements inside another element.
70FCKTools.GetAllChildrenIds = function( parentElement )
71{
72	// Create the array that will hold all Ids.
73	var aIds = new Array() ;
74
75	// Define a recursive function that search for the Ids.
76	var fGetIds = function( parent )
77	{
78		for ( var i = 0 ; i < parent.childNodes.length ; i++ )
79		{
80			var sId = parent.childNodes[i].id ;
81
82			// Check if the Id is defined for the element.
83			if ( sId && sId.length > 0 ) aIds[ aIds.length ] = sId ;
84
85			// Recursive call.
86			fGetIds( parent.childNodes[i] ) ;
87		}
88	}
89
90	// Start the recursive calls.
91	fGetIds( parentElement ) ;
92
93	return aIds ;
94}
95
96// Replaces a tag with its contents. For example "<span>My <b>tag</b></span>"
97// will be replaced with "My <b>tag</b>".
98FCKTools.RemoveOuterTags = function( e )
99{
100	var oFragment = e.ownerDocument.createDocumentFragment() ;
101
102	for ( var i = 0 ; i < e.childNodes.length ; i++ )
103		oFragment.appendChild( e.childNodes[i].cloneNode(true) ) ;
104
105	e.parentNode.replaceChild( oFragment, e ) ;
106}
107
108FCKTools.CreateXmlObject = function( object )
109{
110	switch ( object )
111	{
112		case 'XmlHttp' :
113			return new XMLHttpRequest() ;
114		case 'DOMDocument' :
115			return document.implementation.createDocument( '', '', null ) ;
116	}
117	return null ;
118}
119
120FCKTools.GetScrollPosition = function( relativeWindow )
121{
122	return { X : relativeWindow.pageXOffset, Y : relativeWindow.pageYOffset } ;
123}
124
125FCKTools.AddEventListener = function( sourceObject, eventName, listener )
126{
127	sourceObject.addEventListener( eventName, listener, false ) ;
128}
129
130FCKTools.RemoveEventListener = function( sourceObject, eventName, listener )
131{
132	sourceObject.removeEventListener( eventName, listener, false ) ;
133}
134
135// Listeners attached with this function cannot be detached.
136FCKTools.AddEventListenerEx = function( sourceObject, eventName, listener, paramsArray )
137{
138	sourceObject.addEventListener(
139		eventName,
140		function( e )
141		{
142			listener.apply( sourceObject, [ e ].concat( paramsArray || [] ) ) ;
143		},
144		false
145	) ;
146}
147
148// Returns and object with the "Width" and "Height" properties.
149FCKTools.GetViewPaneSize = function( win )
150{
151	return { Width : win.innerWidth, Height : win.innerHeight } ;
152}
153
154FCKTools.SaveStyles = function( element )
155{
156	var data = FCKTools.ProtectFormStyles( element ) ;
157
158	var oSavedStyles = new Object() ;
159
160	if ( element.className.length > 0 )
161	{
162		oSavedStyles.Class = element.className ;
163		element.className = '' ;
164	}
165
166	var sInlineStyle = element.getAttribute( 'style' ) ;
167
168	if ( sInlineStyle && sInlineStyle.length > 0 )
169	{
170		oSavedStyles.Inline = sInlineStyle ;
171		element.setAttribute( 'style', '', 0 ) ;	// 0 : Case Insensitive
172	}
173
174	FCKTools.RestoreFormStyles( element, data ) ;
175	return oSavedStyles ;
176}
177
178FCKTools.RestoreStyles = function( element, savedStyles )
179{
180	var data = FCKTools.ProtectFormStyles( element ) ;
181	element.className = savedStyles.Class || '' ;
182
183	if ( savedStyles.Inline )
184		element.setAttribute( 'style', savedStyles.Inline, 0 ) ;	// 0 : Case Insensitive
185	else
186		element.removeAttribute( 'style', 0 ) ;
187	FCKTools.RestoreFormStyles( element, data ) ;
188}
189
190FCKTools.RegisterDollarFunction = function( targetWindow )
191{
192	targetWindow.$ = function( id )
193	{
194		return this.document.getElementById( id ) ;
195	} ;
196}
197
198FCKTools.AppendElement = function( target, elementName )
199{
200	return target.appendChild( target.ownerDocument.createElement( elementName ) ) ;
201}
202
203// Get the coordinates of an element.
204//		@el : The element to get the position.
205//		@relativeWindow: The window to which we want the coordinates relative to.
206FCKTools.GetElementPosition = function( el, relativeWindow )
207{
208	// Initializes the Coordinates object that will be returned by the function.
209	var c = { X:0, Y:0 } ;
210
211	var oWindow = relativeWindow || window ;
212
213	var oOwnerWindow = FCKTools.GetElementWindow( el ) ;
214
215	var previousElement = null ;
216	// Loop throw the offset chain.
217	while ( el )
218	{
219		var sPosition = oOwnerWindow.getComputedStyle(el, '').position ;
220
221		// Check for non "static" elements.
222		// 'FCKConfig.FloatingPanelsZIndex' -- Submenus are under a positioned IFRAME.
223		if ( sPosition && sPosition != 'static' && el.style.zIndex != FCKConfig.FloatingPanelsZIndex )
224			break ;
225
226		/*
227		FCKDebug.Output( el.tagName + ":" + "offset=" + el.offsetLeft + "," + el.offsetTop + "  "
228				+ "scroll=" + el.scrollLeft + "," + el.scrollTop ) ;
229		*/
230
231		c.X += el.offsetLeft - el.scrollLeft ;
232		c.Y += el.offsetTop - el.scrollTop  ;
233
234		// Backtrack due to offsetParent's calculation by the browser ignores scrollLeft and scrollTop.
235		// Backtracking is not needed for Opera
236		if ( ! FCKBrowserInfo.IsOpera )
237		{
238			var scrollElement = previousElement ;
239			while ( scrollElement && scrollElement != el )
240			{
241				c.X -= scrollElement.scrollLeft ;
242				c.Y -= scrollElement.scrollTop ;
243				scrollElement = scrollElement.parentNode ;
244			}
245		}
246
247		previousElement = el ;
248		if ( el.offsetParent )
249			el = el.offsetParent ;
250		else
251		{
252			if ( oOwnerWindow != oWindow )
253			{
254				el = oOwnerWindow.frameElement ;
255				previousElement = null ;
256				if ( el )
257					oOwnerWindow = FCKTools.GetElementWindow( el ) ;
258			}
259			else
260			{
261				c.X += el.scrollLeft ;
262				c.Y += el.scrollTop  ;
263				break ;
264			}
265		}
266	}
267
268	// Return the Coordinates object
269	return c ;
270}
271