[44] | 1 | /*
|
---|
| 2 | * FCKeditor - The text editor for internet
|
---|
| 3 | * Copyright (C) 2003-2006 Frederico Caldeira Knabben
|
---|
| 4 | *
|
---|
| 5 | * Licensed under the terms of the GNU Lesser General Public License:
|
---|
| 6 | * http://www.opensource.org/licenses/lgpl-license.php
|
---|
| 7 | *
|
---|
| 8 | * For further information visit:
|
---|
| 9 | * http://www.fckeditor.net/
|
---|
| 10 | *
|
---|
| 11 | * "Support Open Source software. What about a donation today?"
|
---|
| 12 | *
|
---|
| 13 | * File Name: fckpanel.js
|
---|
| 14 | * Component that creates floating panels. It is used by many
|
---|
| 15 | * other components, like the toolbar items, context menu, etc...
|
---|
| 16 | *
|
---|
| 17 | * File Authors:
|
---|
| 18 | * Frederico Caldeira Knabben (fredck@fckeditor.net)
|
---|
| 19 | */
|
---|
| 20 |
|
---|
| 21 |
|
---|
| 22 | var FCKPanel = function( parentWindow )
|
---|
| 23 | {
|
---|
| 24 | this.IsRTL = ( FCKLang.Dir == 'rtl' ) ;
|
---|
| 25 | this.IsContextMenu = false ;
|
---|
| 26 | this._LockCounter = 0 ;
|
---|
| 27 |
|
---|
| 28 | this._Window = parentWindow || window ;
|
---|
| 29 |
|
---|
| 30 | var oDocument ;
|
---|
| 31 |
|
---|
| 32 | if ( FCKBrowserInfo.IsIE )
|
---|
| 33 | {
|
---|
| 34 | // Create the Popup that will hold the panel.
|
---|
| 35 | this._Popup = this._Window.createPopup() ;
|
---|
| 36 | oDocument = this.Document = this._Popup.document ;
|
---|
| 37 | }
|
---|
| 38 | else
|
---|
| 39 | {
|
---|
| 40 | var oIFrame = this._IFrame = this._Window.document.createElement('iframe') ;
|
---|
| 41 | oIFrame.src = 'javascript:void(0)' ;
|
---|
| 42 | oIFrame.allowTransparency = true ;
|
---|
| 43 | oIFrame.frameBorder = '0' ;
|
---|
| 44 | oIFrame.scrolling = 'no' ;
|
---|
| 45 | oIFrame.style.position = 'absolute';
|
---|
| 46 | oIFrame.style.zIndex = FCKConfig.FloatingPanelsZIndex ;
|
---|
| 47 | oIFrame.width = oIFrame.height = 0 ;
|
---|
| 48 |
|
---|
| 49 | if ( this._Window == window.parent && window.frameElement )
|
---|
| 50 | window.frameElement.parentNode.insertBefore( oIFrame, window.frameElement ) ;
|
---|
| 51 | else
|
---|
| 52 | this._Window.document.body.appendChild( oIFrame ) ;
|
---|
| 53 |
|
---|
| 54 | var oIFrameWindow = oIFrame.contentWindow ;
|
---|
| 55 |
|
---|
| 56 | oDocument = this.Document = oIFrameWindow.document ;
|
---|
| 57 |
|
---|
| 58 | // Initialize the IFRAME document body.
|
---|
| 59 | oDocument.open() ;
|
---|
| 60 | oDocument.write( '<html><head></head><body style="margin:0px;padding:0px;"><\/body><\/html>' ) ;
|
---|
| 61 | oDocument.close() ;
|
---|
| 62 |
|
---|
| 63 | FCKTools.AddEventListenerEx( oIFrameWindow, 'focus', FCKPanel_Window_OnFocus, this ) ;
|
---|
| 64 | FCKTools.AddEventListenerEx( oIFrameWindow, 'blur', FCKPanel_Window_OnBlur, this ) ;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | oDocument.dir = FCKLang.Dir ;
|
---|
| 68 |
|
---|
| 69 | oDocument.oncontextmenu = FCKTools.CancelEvent ;
|
---|
| 70 |
|
---|
| 71 |
|
---|
| 72 | // Create the main DIV that is used as the panel base.
|
---|
| 73 | this.MainNode = oDocument.body.appendChild( oDocument.createElement('DIV') ) ;
|
---|
| 74 |
|
---|
| 75 | // The "float" property must be set so Firefox calculates the size correcly.
|
---|
| 76 | this.MainNode.style.cssFloat = this.IsRTL ? 'right' : 'left' ;
|
---|
| 77 |
|
---|
| 78 | if ( FCK.IECleanup )
|
---|
| 79 | FCK.IECleanup.AddItem( this, FCKPanel_Cleanup ) ;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 |
|
---|
| 83 | FCKPanel.prototype.AppendStyleSheet = function( styleSheet )
|
---|
| 84 | {
|
---|
| 85 | FCKTools.AppendStyleSheet( this.Document, styleSheet ) ;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | FCKPanel.prototype.Preload = function( x, y, relElement )
|
---|
| 89 | {
|
---|
| 90 | // The offsetWidth and offsetHeight properties are not available if the
|
---|
| 91 | // element is not visible. So we must "show" the popup with no size to
|
---|
| 92 | // be able to use that values in the second call (IE only).
|
---|
| 93 | if ( this._Popup )
|
---|
| 94 | this._Popup.show( x, y, 0, 0, relElement ) ;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | FCKPanel.prototype.Show = function( x, y, relElement, width, height )
|
---|
| 98 | {
|
---|
| 99 | if ( this._Popup )
|
---|
| 100 | {
|
---|
| 101 | // The offsetWidth and offsetHeight properties are not available if the
|
---|
| 102 | // element is not visible. So we must "show" the popup with no size to
|
---|
| 103 | // be able to use that values in the second call.
|
---|
| 104 | this._Popup.show( x, y, 0, 0, relElement ) ;
|
---|
| 105 |
|
---|
| 106 | // The following lines must be place after the above "show", otherwise it
|
---|
| 107 | // doesn't has the desired effect.
|
---|
| 108 | this.MainNode.style.width = width ? width + 'px' : '' ;
|
---|
| 109 | this.MainNode.style.height = height ? height + 'px' : '' ;
|
---|
| 110 |
|
---|
| 111 | var iMainWidth = this.MainNode.offsetWidth ;
|
---|
| 112 |
|
---|
| 113 | if ( this.IsRTL )
|
---|
| 114 | {
|
---|
| 115 | if ( this.IsContextMenu )
|
---|
| 116 | x = x - iMainWidth + 1 ;
|
---|
| 117 | else if ( relElement )
|
---|
| 118 | x = ( x * -1 ) + relElement.offsetWidth - iMainWidth ;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | // Second call: Show the Popup at the specified location, with the correct size.
|
---|
| 122 | this._Popup.show( x, y, iMainWidth, this.MainNode.offsetHeight, relElement ) ;
|
---|
| 123 |
|
---|
| 124 | if ( this.OnHide )
|
---|
| 125 | {
|
---|
| 126 | if ( this._Timer )
|
---|
| 127 | CheckPopupOnHide.call( this, true ) ;
|
---|
| 128 |
|
---|
| 129 | this._Timer = FCKTools.SetInterval( CheckPopupOnHide, 100, this ) ;
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 | else
|
---|
| 133 | {
|
---|
| 134 | // Do not fire OnBlur while the panel is opened.
|
---|
| 135 | if ( typeof( FCKFocusManager ) != 'undefined' )
|
---|
| 136 | FCKFocusManager.Lock() ;
|
---|
| 137 |
|
---|
| 138 | if ( this.ParentPanel )
|
---|
| 139 | this.ParentPanel.Lock() ;
|
---|
| 140 |
|
---|
| 141 | this.MainNode.style.width = width ? width + 'px' : '' ;
|
---|
| 142 | this.MainNode.style.height = height ? height + 'px' : '' ;
|
---|
| 143 |
|
---|
| 144 | var iMainWidth = this.MainNode.offsetWidth ;
|
---|
| 145 |
|
---|
| 146 | if ( !width ) this._IFrame.width = 1 ;
|
---|
| 147 | if ( !height ) this._IFrame.height = 1 ;
|
---|
| 148 |
|
---|
| 149 | // This is weird... but with Firefox, we must get the offsetWidth before
|
---|
| 150 | // setting the _IFrame size (which returns "0"), and then after that,
|
---|
| 151 | // to return the correct width. Remove the first step and it will not
|
---|
| 152 | // work when the editor is in RTL.
|
---|
| 153 | iMainWidth = this.MainNode.offsetWidth ;
|
---|
| 154 |
|
---|
| 155 | var oPos = FCKTools.GetElementPosition( ( relElement.nodeType == 9 ? relElement.body : relElement), this._Window ) ;
|
---|
| 156 |
|
---|
| 157 | if ( this.IsRTL && !this.IsContextMenu )
|
---|
| 158 | x = ( x * -1 ) ;
|
---|
| 159 |
|
---|
| 160 | x += oPos.X ;
|
---|
| 161 | y += oPos.Y ;
|
---|
| 162 |
|
---|
| 163 | if ( this.IsRTL )
|
---|
| 164 | {
|
---|
| 165 | if ( this.IsContextMenu )
|
---|
| 166 | x = x - iMainWidth + 1 ;
|
---|
| 167 | else if ( relElement )
|
---|
| 168 | x = x + relElement.offsetWidth - iMainWidth ;
|
---|
| 169 | }
|
---|
| 170 | else
|
---|
| 171 | {
|
---|
| 172 | var oViewPaneSize = FCKTools.GetViewPaneSize( this._Window ) ;
|
---|
| 173 | var oScrollPosition = FCKTools.GetScrollPosition( this._Window ) ;
|
---|
| 174 |
|
---|
| 175 | var iViewPaneHeight = oViewPaneSize.Height + oScrollPosition.Y ;
|
---|
| 176 | var iViewPaneWidth = oViewPaneSize.Width + oScrollPosition.X ;
|
---|
| 177 |
|
---|
| 178 | if ( ( x + iMainWidth ) > iViewPaneWidth )
|
---|
| 179 | x -= x + iMainWidth - iViewPaneWidth ;
|
---|
| 180 |
|
---|
| 181 | if ( ( y + this.MainNode.offsetHeight ) > iViewPaneHeight )
|
---|
| 182 | y -= y + this.MainNode.offsetHeight - iViewPaneHeight ;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | if ( x < 0 )
|
---|
| 186 | x = 0 ;
|
---|
| 187 |
|
---|
| 188 | // Set the context menu DIV in the specified location.
|
---|
| 189 | this._IFrame.style.left = x + 'px' ;
|
---|
| 190 | this._IFrame.style.top = y + 'px' ;
|
---|
| 191 |
|
---|
| 192 | var iWidth = iMainWidth ;
|
---|
| 193 | var iHeight = this.MainNode.offsetHeight ;
|
---|
| 194 |
|
---|
| 195 | this._IFrame.width = iWidth ;
|
---|
| 196 | this._IFrame.height = iHeight ;
|
---|
| 197 |
|
---|
| 198 | // Move the focus to the IFRAME so we catch the "onblur".
|
---|
| 199 | this._IFrame.contentWindow.focus() ;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | this._IsOpened = true ;
|
---|
| 203 |
|
---|
| 204 | FCKTools.RunFunction( this.OnShow, this ) ;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | FCKPanel.prototype.Hide = function( ignoreOnHide )
|
---|
| 208 | {
|
---|
| 209 | if ( this._Popup )
|
---|
| 210 | this._Popup.hide() ;
|
---|
| 211 | else
|
---|
| 212 | {
|
---|
| 213 | if ( !this._IsOpened )
|
---|
| 214 | return ;
|
---|
| 215 |
|
---|
| 216 | // Enable the editor to fire the "OnBlur".
|
---|
| 217 | if ( typeof( FCKFocusManager ) != 'undefined' )
|
---|
| 218 | FCKFocusManager.Unlock() ;
|
---|
| 219 |
|
---|
| 220 | // It is better to set the sizes to 0, otherwise Firefox would have
|
---|
| 221 | // rendering problems.
|
---|
| 222 | this._IFrame.width = this._IFrame.height = 0 ;
|
---|
| 223 |
|
---|
| 224 | this._IsOpened = false ;
|
---|
| 225 |
|
---|
| 226 | if ( this.ParentPanel )
|
---|
| 227 | this.ParentPanel.Unlock() ;
|
---|
| 228 |
|
---|
| 229 | if ( !ignoreOnHide )
|
---|
| 230 | FCKTools.RunFunction( this.OnHide, this ) ;
|
---|
| 231 | }
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | FCKPanel.prototype.CheckIsOpened = function()
|
---|
| 235 | {
|
---|
| 236 | if ( this._Popup )
|
---|
| 237 | return this._Popup.isOpen ;
|
---|
| 238 | else
|
---|
| 239 | return this._IsOpened ;
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | FCKPanel.prototype.CreateChildPanel = function()
|
---|
| 243 | {
|
---|
| 244 | var oWindow = this._Popup ? FCKTools.GetParentWindow( this.Document ) : this._Window ;
|
---|
| 245 |
|
---|
| 246 | var oChildPanel = new FCKPanel( oWindow, true ) ;
|
---|
| 247 | oChildPanel.ParentPanel = this ;
|
---|
| 248 |
|
---|
| 249 | return oChildPanel ;
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | FCKPanel.prototype.Lock = function()
|
---|
| 253 | {
|
---|
| 254 | this._LockCounter++ ;
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | FCKPanel.prototype.Unlock = function()
|
---|
| 258 | {
|
---|
| 259 | if ( --this._LockCounter == 0 && !this.HasFocus )
|
---|
| 260 | this.Hide() ;
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | /* Events */
|
---|
| 264 |
|
---|
| 265 | function FCKPanel_Window_OnFocus( e, panel )
|
---|
| 266 | {
|
---|
| 267 | panel.HasFocus = true ;
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | function FCKPanel_Window_OnBlur( e, panel )
|
---|
| 271 | {
|
---|
| 272 | panel.HasFocus = false ;
|
---|
| 273 |
|
---|
| 274 | if ( panel._LockCounter == 0 )
|
---|
| 275 | FCKTools.RunFunction( panel.Hide, panel ) ;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | function CheckPopupOnHide( forceHide )
|
---|
| 279 | {
|
---|
| 280 | if ( forceHide || !this._Popup.isOpen )
|
---|
| 281 | {
|
---|
| 282 | window.clearInterval( this._Timer ) ;
|
---|
| 283 | this._Timer = null ;
|
---|
| 284 |
|
---|
| 285 | FCKTools.RunFunction( this.OnHide, this ) ;
|
---|
| 286 | }
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | function FCKPanel_Cleanup()
|
---|
| 290 | {
|
---|
| 291 | this._Popup = null ;
|
---|
| 292 | this._Window = null ;
|
---|
| 293 | this.Document = null ;
|
---|
| 294 | this.MainNode = null ;
|
---|
| 295 | } |
---|