[795] | 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 | |
---|
| 24 | FCKTools.CancelEvent = function( e ) |
---|
| 25 | { |
---|
| 26 | if ( e ) |
---|
| 27 | e.preventDefault() ; |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | FCKTools.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. |
---|
| 39 | FCKTools._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 | // Removes all attributes and values from the element. |
---|
| 50 | FCKTools.ClearElementAttributes = function( element ) |
---|
| 51 | { |
---|
| 52 | // Loop throw all attributes in the element |
---|
| 53 | for ( var i = 0 ; i < element.attributes.length ; i++ ) |
---|
| 54 | { |
---|
| 55 | // Remove the element by name. |
---|
| 56 | element.removeAttribute( element.attributes[i].name, 0 ) ; // 0 : Case Insensitive |
---|
| 57 | } |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | // Returns an Array of strings with all defined in the elements inside another element. |
---|
| 61 | FCKTools.GetAllChildrenIds = function( parentElement ) |
---|
| 62 | { |
---|
| 63 | // Create the array that will hold all Ids. |
---|
| 64 | var aIds = new Array() ; |
---|
| 65 | |
---|
| 66 | // Define a recursive function that search for the Ids. |
---|
| 67 | var fGetIds = function( parent ) |
---|
| 68 | { |
---|
| 69 | for ( var i = 0 ; i < parent.childNodes.length ; i++ ) |
---|
| 70 | { |
---|
| 71 | var sId = parent.childNodes[i].id ; |
---|
| 72 | |
---|
| 73 | // Check if the Id is defined for the element. |
---|
| 74 | if ( sId && sId.length > 0 ) aIds[ aIds.length ] = sId ; |
---|
| 75 | |
---|
| 76 | // Recursive call. |
---|
| 77 | fGetIds( parent.childNodes[i] ) ; |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | // Start the recursive calls. |
---|
| 82 | fGetIds( parentElement ) ; |
---|
| 83 | |
---|
| 84 | return aIds ; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | // Replaces a tag with its contents. For example "<span>My <b>tag</b></span>" |
---|
| 88 | // will be replaced with "My <b>tag</b>". |
---|
| 89 | FCKTools.RemoveOuterTags = function( e ) |
---|
| 90 | { |
---|
| 91 | var oFragment = e.ownerDocument.createDocumentFragment() ; |
---|
| 92 | |
---|
| 93 | for ( var i = 0 ; i < e.childNodes.length ; i++ ) |
---|
| 94 | oFragment.appendChild( e.childNodes[i].cloneNode(true) ) ; |
---|
| 95 | |
---|
| 96 | e.parentNode.replaceChild( oFragment, e ) ; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | FCKTools.CreateXmlObject = function( object ) |
---|
| 100 | { |
---|
| 101 | switch ( object ) |
---|
| 102 | { |
---|
| 103 | case 'XmlHttp' : |
---|
| 104 | return new XMLHttpRequest() ; |
---|
| 105 | case 'DOMDocument' : |
---|
| 106 | return document.implementation.createDocument( '', '', null ) ; |
---|
| 107 | } |
---|
| 108 | return null ; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | FCKTools.GetScrollPosition = function( relativeWindow ) |
---|
| 112 | { |
---|
| 113 | return { X : relativeWindow.pageXOffset, Y : relativeWindow.pageYOffset } ; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | FCKTools.AddEventListener = function( sourceObject, eventName, listener ) |
---|
| 117 | { |
---|
| 118 | sourceObject.addEventListener( eventName, listener, false ) ; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | FCKTools.RemoveEventListener = function( sourceObject, eventName, listener ) |
---|
| 122 | { |
---|
| 123 | sourceObject.removeEventListener( eventName, listener, false ) ; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | // Listeners attached with this function cannot be detached. |
---|
| 127 | FCKTools.AddEventListenerEx = function( sourceObject, eventName, listener, paramsArray ) |
---|
| 128 | { |
---|
| 129 | sourceObject.addEventListener( |
---|
| 130 | eventName, |
---|
| 131 | function( e ) |
---|
| 132 | { |
---|
| 133 | listener.apply( sourceObject, [ e ].concat( paramsArray || [] ) ) ; |
---|
| 134 | }, |
---|
| 135 | false |
---|
| 136 | ) ; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | // Returns and object with the "Width" and "Height" properties. |
---|
| 140 | FCKTools.GetViewPaneSize = function( win ) |
---|
| 141 | { |
---|
| 142 | return { Width : win.innerWidth, Height : win.innerHeight } ; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | FCKTools.SaveStyles = function( element ) |
---|
| 146 | { |
---|
| 147 | var oSavedStyles = new Object() ; |
---|
| 148 | |
---|
| 149 | if ( element.className.length > 0 ) |
---|
| 150 | { |
---|
| 151 | oSavedStyles.Class = element.className ; |
---|
| 152 | element.className = '' ; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | var sInlineStyle = element.getAttribute( 'style' ) ; |
---|
| 156 | |
---|
| 157 | if ( sInlineStyle && sInlineStyle.length > 0 ) |
---|
| 158 | { |
---|
| 159 | oSavedStyles.Inline = sInlineStyle ; |
---|
| 160 | element.setAttribute( 'style', '', 0 ) ; // 0 : Case Insensitive |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | return oSavedStyles ; |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | FCKTools.RestoreStyles = function( element, savedStyles ) |
---|
| 167 | { |
---|
| 168 | element.className = savedStyles.Class || '' ; |
---|
| 169 | |
---|
| 170 | if ( savedStyles.Inline ) |
---|
| 171 | element.setAttribute( 'style', savedStyles.Inline, 0 ) ; // 0 : Case Insensitive |
---|
| 172 | else |
---|
| 173 | element.removeAttribute( 'style', 0 ) ; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | FCKTools.RegisterDollarFunction = function( targetWindow ) |
---|
| 177 | { |
---|
| 178 | targetWindow.$ = function( id ) |
---|
| 179 | { |
---|
| 180 | return this.document.getElementById( id ) ; |
---|
| 181 | } ; |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | FCKTools.AppendElement = function( target, elementName ) |
---|
| 185 | { |
---|
| 186 | return target.appendChild( target.ownerDocument.createElement( elementName ) ) ; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | // Get the coordinates of an element. |
---|
| 190 | // @el : The element to get the position. |
---|
| 191 | // @relativeWindow: The window to which we want the coordinates relative to. |
---|
| 192 | FCKTools.GetElementPosition = function( el, relativeWindow ) |
---|
| 193 | { |
---|
| 194 | // Initializes the Coordinates object that will be returned by the function. |
---|
| 195 | var c = { X:0, Y:0 } ; |
---|
| 196 | |
---|
| 197 | var oWindow = relativeWindow || window ; |
---|
| 198 | |
---|
| 199 | var oOwnerWindow = FCKTools.GetElementWindow( el ) ; |
---|
| 200 | |
---|
| 201 | // Loop throw the offset chain. |
---|
| 202 | while ( el ) |
---|
| 203 | { |
---|
| 204 | var sPosition = oOwnerWindow.getComputedStyle(el, '').position ; |
---|
| 205 | |
---|
| 206 | // Check for non "static" elements. |
---|
| 207 | // 'FCKConfig.FloatingPanelsZIndex' -- Submenus are under a positioned IFRAME. |
---|
| 208 | if ( sPosition && sPosition != 'static' && el.style.zIndex != FCKConfig.FloatingPanelsZIndex ) |
---|
| 209 | break ; |
---|
| 210 | |
---|
| 211 | c.X += el.offsetLeft - el.scrollLeft ; |
---|
| 212 | c.Y += el.offsetTop - el.scrollTop ; |
---|
| 213 | |
---|
| 214 | if ( el.offsetParent ) |
---|
| 215 | el = el.offsetParent ; |
---|
| 216 | else |
---|
| 217 | { |
---|
| 218 | if ( oOwnerWindow != oWindow ) |
---|
| 219 | { |
---|
| 220 | el = oOwnerWindow.frameElement ; |
---|
| 221 | if ( el ) |
---|
| 222 | oOwnerWindow = FCKTools.GetElementWindow( el ) ; |
---|
| 223 | } |
---|
| 224 | else |
---|
| 225 | { |
---|
| 226 | c.X += el.scrollLeft ; |
---|
| 227 | c.Y += el.scrollTop ; |
---|
| 228 | break ; |
---|
| 229 | } |
---|
| 230 | } |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | // Return the Coordinates object |
---|
| 234 | return c ; |
---|
| 235 | } |
---|