2
* FCKeditor - The text editor for Internet - http://www.fckeditor.net
3
* Copyright (C) 2003-2009 Frederico Caldeira Knabben
7
* Licensed under the terms of any of the following licenses at your
10
* - GNU General Public License Version 2 or later (the "GPL")
11
* http://www.gnu.org/licenses/gpl.html
13
* - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14
* http://www.gnu.org/licenses/lgpl.html
16
* - Mozilla Public License Version 1.1 or later (the "MPL")
17
* http://www.mozilla.org/MPL/MPL-1.1.html
21
* Utility functions. (Gecko version).
24
FCKTools.CancelEvent = function( e )
30
FCKTools.DisableSelection = function( element )
32
if ( FCKBrowserInfo.IsGecko )
33
element.style.MozUserSelect = 'none' ; // Gecko only.
34
else if ( FCKBrowserInfo.IsSafari )
35
element.style.KhtmlUserSelect = 'none' ; // WebKit only.
37
element.style.userSelect = 'none' ; // CSS3 (not supported yet).
40
// Appends a CSS file to a document.
41
FCKTools._AppendStyleSheet = function( documentElement, cssFileUrl )
43
var e = documentElement.createElement( 'LINK' ) ;
44
e.rel = 'stylesheet' ;
47
documentElement.getElementsByTagName("HEAD")[0].appendChild( e ) ;
51
// Appends a CSS style string to a document.
52
FCKTools.AppendStyleString = function( documentElement, cssStyles )
57
var e = documentElement.createElement( "STYLE" ) ;
58
e.appendChild( documentElement.createTextNode( cssStyles ) ) ;
59
documentElement.getElementsByTagName( "HEAD" )[0].appendChild( e ) ;
63
// Removes all attributes and values from the element.
64
FCKTools.ClearElementAttributes = function( element )
66
// Loop throw all attributes in the element
67
for ( var i = 0 ; i < element.attributes.length ; i++ )
69
// Remove the element by name.
70
element.removeAttribute( element.attributes[i].name, 0 ) ; // 0 : Case Insensitive
74
// Returns an Array of strings with all defined in the elements inside another element.
75
FCKTools.GetAllChildrenIds = function( parentElement )
77
// Create the array that will hold all Ids.
78
var aIds = new Array() ;
80
// Define a recursive function that search for the Ids.
81
var fGetIds = function( parent )
83
for ( var i = 0 ; i < parent.childNodes.length ; i++ )
85
var sId = parent.childNodes[i].id ;
87
// Check if the Id is defined for the element.
88
if ( sId && sId.length > 0 ) aIds[ aIds.length ] = sId ;
91
fGetIds( parent.childNodes[i] ) ;
95
// Start the recursive calls.
96
fGetIds( parentElement ) ;
101
// Replaces a tag with its contents. For example "<span>My <b>tag</b></span>"
102
// will be replaced with "My <b>tag</b>".
103
FCKTools.RemoveOuterTags = function( e )
105
var oFragment = e.ownerDocument.createDocumentFragment() ;
107
for ( var i = 0 ; i < e.childNodes.length ; i++ )
108
oFragment.appendChild( e.childNodes[i].cloneNode(true) ) ;
110
e.parentNode.replaceChild( oFragment, e ) ;
113
FCKTools.CreateXmlObject = function( object )
118
return new XMLHttpRequest() ;
121
// Originaly, we were had the following here:
122
// return document.implementation.createDocument( '', '', null ) ;
123
// But that doesn't work if we're running under domain relaxation mode, so we need a workaround.
124
// See http://ajaxian.com/archives/xml-messages-with-cross-domain-json about the trick we're using.
125
var doc = ( new DOMParser() ).parseFromString( '<tmp></tmp>', 'text/xml' ) ;
126
FCKDomTools.RemoveNode( doc.firstChild ) ;
132
FCKTools.GetScrollPosition = function( relativeWindow )
134
return { X : relativeWindow.pageXOffset, Y : relativeWindow.pageYOffset } ;
137
FCKTools.AddEventListener = function( sourceObject, eventName, listener )
139
sourceObject.addEventListener( eventName, listener, false ) ;
142
FCKTools.RemoveEventListener = function( sourceObject, eventName, listener )
144
sourceObject.removeEventListener( eventName, listener, false ) ;
147
// Listeners attached with this function cannot be detached.
148
FCKTools.AddEventListenerEx = function( sourceObject, eventName, listener, paramsArray )
150
sourceObject.addEventListener(
154
listener.apply( sourceObject, [ e ].concat( paramsArray || [] ) ) ;
160
// Returns and object with the "Width" and "Height" properties.
161
FCKTools.GetViewPaneSize = function( win )
163
return { Width : win.innerWidth, Height : win.innerHeight } ;
166
FCKTools.SaveStyles = function( element )
168
var data = FCKTools.ProtectFormStyles( element ) ;
170
var oSavedStyles = new Object() ;
172
if ( element.className.length > 0 )
174
oSavedStyles.Class = element.className ;
175
element.className = '' ;
178
var sInlineStyle = element.getAttribute( 'style' ) ;
180
if ( sInlineStyle && sInlineStyle.length > 0 )
182
oSavedStyles.Inline = sInlineStyle ;
183
element.setAttribute( 'style', '', 0 ) ; // 0 : Case Insensitive
186
FCKTools.RestoreFormStyles( element, data ) ;
187
return oSavedStyles ;
190
FCKTools.RestoreStyles = function( element, savedStyles )
192
var data = FCKTools.ProtectFormStyles( element ) ;
193
element.className = savedStyles.Class || '' ;
195
if ( savedStyles.Inline )
196
element.setAttribute( 'style', savedStyles.Inline, 0 ) ; // 0 : Case Insensitive
198
element.removeAttribute( 'style', 0 ) ;
199
FCKTools.RestoreFormStyles( element, data ) ;
202
FCKTools.RegisterDollarFunction = function( targetWindow )
204
targetWindow.$ = function( id )
206
return targetWindow.document.getElementById( id ) ;
210
FCKTools.AppendElement = function( target, elementName )
212
return target.appendChild( target.ownerDocument.createElement( elementName ) ) ;
215
// Get the coordinates of an element.
216
// @el : The element to get the position.
217
// @relativeWindow: The window to which we want the coordinates relative to.
218
FCKTools.GetElementPosition = function( el, relativeWindow )
220
// Initializes the Coordinates object that will be returned by the function.
221
var c = { X:0, Y:0 } ;
223
var oWindow = relativeWindow || window ;
225
var oOwnerWindow = FCKTools.GetElementWindow( el ) ;
227
var previousElement = null ;
228
// Loop throw the offset chain.
231
var sPosition = oOwnerWindow.getComputedStyle(el, '').position ;
233
// Check for non "static" elements.
234
// 'FCKConfig.FloatingPanelsZIndex' -- Submenus are under a positioned IFRAME.
235
if ( sPosition && sPosition != 'static' && el.style.zIndex != FCKConfig.FloatingPanelsZIndex )
239
FCKDebug.Output( el.tagName + ":" + "offset=" + el.offsetLeft + "," + el.offsetTop + " "
240
+ "scroll=" + el.scrollLeft + "," + el.scrollTop ) ;
243
c.X += el.offsetLeft - el.scrollLeft ;
244
c.Y += el.offsetTop - el.scrollTop ;
246
// Backtrack due to offsetParent's calculation by the browser ignores scrollLeft and scrollTop.
247
// Backtracking is not needed for Opera
248
if ( ! FCKBrowserInfo.IsOpera )
250
var scrollElement = previousElement ;
251
while ( scrollElement && scrollElement != el )
253
c.X -= scrollElement.scrollLeft ;
254
c.Y -= scrollElement.scrollTop ;
255
scrollElement = scrollElement.parentNode ;
259
previousElement = el ;
260
if ( el.offsetParent )
261
el = el.offsetParent ;
264
if ( oOwnerWindow != oWindow )
266
el = oOwnerWindow.frameElement ;
267
previousElement = null ;
269
oOwnerWindow = FCKTools.GetElementWindow( el ) ;
273
c.X += el.scrollLeft ;
274
c.Y += el.scrollTop ;
280
// Return the Coordinates object