| 1 |
/* |
| 2 |
* FCKeditor - The text editor for Internet - http://www.fckeditor.net |
| 3 |
* Copyright (C) 2003-2009 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 |
* Component that creates floating panels. It is used by many |
| 22 |
* other components, like the toolbar items, context menu, etc... |
| 23 |
*/ |
| 24 |
|
| 25 |
var FCKPanel = function( parentWindow ) |
| 26 |
{ |
| 27 |
this.IsRTL = ( FCKLang.Dir == 'rtl' ) ; |
| 28 |
this.IsContextMenu = false ; |
| 29 |
this._LockCounter = 0 ; |
| 30 |
|
| 31 |
this._Window = parentWindow || window ; |
| 32 |
|
| 33 |
var oDocument ; |
| 34 |
|
| 35 |
if ( FCKBrowserInfo.IsIE ) |
| 36 |
{ |
| 37 |
// Create the Popup that will hold the panel. |
| 38 |
// The popup has to be created before playing with domain hacks, see #1666. |
| 39 |
this._Popup = this._Window.createPopup() ; |
| 40 |
|
| 41 |
// this._Window cannot be accessed while playing with domain hacks, but local variable is ok. |
| 42 |
// See #1666. |
| 43 |
var pDoc = this._Window.document ; |
| 44 |
|
| 45 |
// This is a trick to IE6 (not IE7). The original domain must be set |
| 46 |
// before creating the popup, so we are able to take a refence to the |
| 47 |
// document inside of it, and the set the proper domain for it. (#123) |
| 48 |
if ( FCK_IS_CUSTOM_DOMAIN && !FCKBrowserInfo.IsIE7 ) |
| 49 |
{ |
| 50 |
pDoc.domain = FCK_ORIGINAL_DOMAIN ; |
| 51 |
document.domain = FCK_ORIGINAL_DOMAIN ; |
| 52 |
} |
| 53 |
|
| 54 |
oDocument = this.Document = this._Popup.document ; |
| 55 |
|
| 56 |
// Set the proper domain inside the popup. |
| 57 |
if ( FCK_IS_CUSTOM_DOMAIN ) |
| 58 |
{ |
| 59 |
oDocument.domain = FCK_RUNTIME_DOMAIN ; |
| 60 |
pDoc.domain = FCK_RUNTIME_DOMAIN ; |
| 61 |
document.domain = FCK_RUNTIME_DOMAIN ; |
| 62 |
} |
| 63 |
|
| 64 |
FCK.IECleanup.AddItem( this, FCKPanel_Cleanup ) ; |
| 65 |
} |
| 66 |
else |
| 67 |
{ |
| 68 |
var oIFrame = this._IFrame = this._Window.document.createElement('iframe') ; |
| 69 |
FCKTools.ResetStyles( oIFrame ); |
| 70 |
oIFrame.src = 'javascript:void(0)' ; |
| 71 |
oIFrame.allowTransparency = true ; |
| 72 |
oIFrame.frameBorder = '0' ; |
| 73 |
oIFrame.scrolling = 'no' ; |
| 74 |
oIFrame.style.width = oIFrame.style.height = '0px' ; |
| 75 |
FCKDomTools.SetElementStyles( oIFrame, |
| 76 |
{ |
| 77 |
position : 'absolute', |
| 78 |
zIndex : FCKConfig.FloatingPanelsZIndex |
| 79 |
} ) ; |
| 80 |
|
| 81 |
this._Window.document.body.appendChild( oIFrame ) ; |
| 82 |
|
| 83 |
var oIFrameWindow = oIFrame.contentWindow ; |
| 84 |
|
| 85 |
oDocument = this.Document = oIFrameWindow.document ; |
| 86 |
|
| 87 |
// Workaround for Safari 12256. Ticket #63 |
| 88 |
var sBase = '' ; |
| 89 |
if ( FCKBrowserInfo.IsSafari ) |
| 90 |
sBase = '<base href="' + window.document.location + '">' ; |
| 91 |
|
| 92 |
// Initialize the IFRAME document body. |
| 93 |
oDocument.open() ; |
| 94 |
oDocument.write( '<html><head>' + sBase + '<\/head><body style="margin:0px;padding:0px;"><\/body><\/html>' ) ; |
| 95 |
oDocument.close() ; |
| 96 |
|
| 97 |
if( FCKBrowserInfo.IsAIR ) |
| 98 |
FCKAdobeAIR.Panel_Contructor( oDocument, window.document.location ) ; |
| 99 |
|
| 100 |
FCKTools.AddEventListenerEx( oIFrameWindow, 'focus', FCKPanel_Window_OnFocus, this ) ; |
| 101 |
FCKTools.AddEventListenerEx( oIFrameWindow, 'blur', FCKPanel_Window_OnBlur, this ) ; |
| 102 |
} |
| 103 |
|
| 104 |
oDocument.dir = FCKLang.Dir ; |
| 105 |
|
| 106 |
FCKTools.AddEventListener( oDocument, 'contextmenu', FCKTools.CancelEvent ) ; |
| 107 |
|
| 108 |
|
| 109 |
// Create the main DIV that is used as the panel base. |
| 110 |
this.MainNode = oDocument.body.appendChild( oDocument.createElement('DIV') ) ; |
| 111 |
|
| 112 |
// The "float" property must be set so Firefox calculates the size correctly. |
| 113 |
this.MainNode.style.cssFloat = this.IsRTL ? 'right' : 'left' ; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
FCKPanel.prototype.AppendStyleSheet = function( styleSheet ) |
| 118 |
{ |
| 119 |
FCKTools.AppendStyleSheet( this.Document, styleSheet ) ; |
| 120 |
} |
| 121 |
|
| 122 |
FCKPanel.prototype.Preload = function( x, y, relElement ) |
| 123 |
{ |
| 124 |
// The offsetWidth and offsetHeight properties are not available if the |
| 125 |
// element is not visible. So we must "show" the popup with no size to |
| 126 |
// be able to use that values in the second call (IE only). |
| 127 |
if ( this._Popup ) |
| 128 |
this._Popup.show( x, y, 0, 0, relElement ) ; |
| 129 |
} |
| 130 |
|
| 131 |
// Workaround for IE7 problem. See #1982 |
| 132 |
// Submenus are restricted to the size of its parent, so we increase it as needed. |
| 133 |
// Returns true if the panel has been repositioned |
| 134 |
FCKPanel.prototype.ResizeForSubpanel = function( panel, width, height ) |
| 135 |
{ |
| 136 |
if ( !FCKBrowserInfo.IsIE7 ) |
| 137 |
return false ; |
| 138 |
|
| 139 |
if ( !this._Popup.isOpen ) |
| 140 |
{ |
| 141 |
this.Subpanel = null ; |
| 142 |
return false ; |
| 143 |
} |
| 144 |
|
| 145 |
// If we are resetting the extra space |
| 146 |
if ( width == 0 && height == 0 ) |
| 147 |
{ |
| 148 |
// Another subpanel is being shown, so we must not shrink back |
| 149 |
if (this.Subpanel !== panel) |
| 150 |
return false ; |
| 151 |
|
| 152 |
// Reset values. |
| 153 |
// We leave the IncreasedY untouched to avoid vertical movement of the |
| 154 |
// menu if the submenu is higher than the main menu. |
| 155 |
this.Subpanel = null ; |
| 156 |
this.IncreasedX = 0 ; |
| 157 |
} |
| 158 |
else |
| 159 |
{ |
| 160 |
this.Subpanel = panel ; |
| 161 |
// If the panel has already been increased enough, get out |
| 162 |
if ( ( this.IncreasedX >= width ) && ( this.IncreasedY >= height ) ) |
| 163 |
return false ; |
| 164 |
|
| 165 |
this.IncreasedX = Math.max( this.IncreasedX, width ) ; |
| 166 |
this.IncreasedY = Math.max( this.IncreasedY, height ) ; |
| 167 |
} |
| 168 |
|
| 169 |
var x = this.ShowRect.x ; |
| 170 |
var w = this.IncreasedX ; |
| 171 |
if ( this.IsRTL ) |
| 172 |
x = x - w ; |
| 173 |
|
| 174 |
// Horizontally increase as needed (sum of widths). |
| 175 |
// Vertically, use only the maximum of this menu or the submenu |
| 176 |
var finalWidth = this.ShowRect.w + w ; |
| 177 |
var finalHeight = Math.max( this.ShowRect.h, this.IncreasedY ) ; |
| 178 |
if ( this.ParentPanel ) |
| 179 |
this.ParentPanel.ResizeForSubpanel( this, finalWidth, finalHeight ) ; |
| 180 |
this._Popup.show( x, this.ShowRect.y, finalWidth, finalHeight, this.RelativeElement ) ; |
| 181 |
|
| 182 |
return this.IsRTL ; |
| 183 |
} |
| 184 |
|
| 185 |
FCKPanel.prototype.Show = function( x, y, relElement, width, height ) |
| 186 |
{ |
| 187 |
var iMainWidth ; |
| 188 |
var eMainNode = this.MainNode ; |
| 189 |
|
| 190 |
if ( this._Popup ) |
| 191 |
{ |
| 192 |
// The offsetWidth and offsetHeight properties are not available if the |
| 193 |
// element is not visible. So we must "show" the popup with no size to |
| 194 |
// be able to use that values in the second call. |
| 195 |
this._Popup.show( x, y, 0, 0, relElement ) ; |
| 196 |
|
| 197 |
// The following lines must be place after the above "show", otherwise it |
| 198 |
// doesn't has the desired effect. |
| 199 |
FCKDomTools.SetElementStyles( eMainNode, |
| 200 |
{ |
| 201 |
width : width ? width + 'px' : '', |
| 202 |
height : height ? height + 'px' : '' |
| 203 |
} ) ; |
| 204 |
|
| 205 |
iMainWidth = eMainNode.offsetWidth ; |
| 206 |
|
| 207 |
if ( FCKBrowserInfo.IsIE7 ) |
| 208 |
{ |
| 209 |
if (this.ParentPanel && this.ParentPanel.ResizeForSubpanel(this, iMainWidth, eMainNode.offsetHeight) ) |
| 210 |
{ |
| 211 |
// As the parent has moved, allow the browser to update its internal data, so the new position is correct. |
| 212 |
FCKTools.RunFunction( this.Show, this, [x, y, relElement] ) ; |
| 213 |
return ; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
if ( this.IsRTL ) |
| 218 |
{ |
| 219 |
if ( this.IsContextMenu ) |
| 220 |
x = x - iMainWidth + 1 ; |
| 221 |
else if ( relElement ) |
| 222 |
x = ( x * -1 ) + relElement.offsetWidth - iMainWidth ; |
| 223 |
} |
| 224 |
|
| 225 |
if ( FCKBrowserInfo.IsIE7 ) |
| 226 |
{ |
| 227 |
// Store the values that will be used by the ResizeForSubpanel function |
| 228 |
this.ShowRect = {x:x, y:y, w:iMainWidth, h:eMainNode.offsetHeight} ; |
| 229 |
this.IncreasedX = 0 ; |
| 230 |
this.IncreasedY = 0 ; |
| 231 |
this.RelativeElement = relElement ; |
| 232 |
} |
| 233 |
|
| 234 |
// Second call: Show the Popup at the specified location, with the correct size. |
| 235 |
this._Popup.show( x, y, iMainWidth, eMainNode.offsetHeight, relElement ) ; |
| 236 |
|
| 237 |
if ( this.OnHide ) |
| 238 |
{ |
| 239 |
if ( this._Timer ) |
| 240 |
CheckPopupOnHide.call( this, true ) ; |
| 241 |
|
| 242 |
this._Timer = FCKTools.SetInterval( CheckPopupOnHide, 100, this ) ; |
| 243 |
} |
| 244 |
} |
| 245 |
else |
| 246 |
{ |
| 247 |
// Do not fire OnBlur while the panel is opened. |
| 248 |
if ( typeof( FCK.ToolbarSet.CurrentInstance.FocusManager ) != 'undefined' ) |
| 249 |
FCK.ToolbarSet.CurrentInstance.FocusManager.Lock() ; |
| 250 |
|
| 251 |
if ( this.ParentPanel ) |
| 252 |
{ |
| 253 |
this.ParentPanel.Lock() ; |
| 254 |
|
| 255 |
// Due to a bug on FF3, we must ensure that the parent panel will |
| 256 |
// blur (#1584). |
| 257 |
FCKPanel_Window_OnBlur( null, this.ParentPanel ) ; |
| 258 |
} |
| 259 |
|
| 260 |
// Toggle the iframe scrolling attribute to prevent the panel |
| 261 |
// scrollbars from disappearing in FF Mac. (#191) |
| 262 |
if ( FCKBrowserInfo.IsGecko && FCKBrowserInfo.IsMac ) |
| 263 |
{ |
| 264 |
this._IFrame.scrolling = '' ; |
| 265 |
FCKTools.RunFunction( function(){ this._IFrame.scrolling = 'no'; }, this ) ; |
| 266 |
} |
| 267 |
|
| 268 |
// Be sure we'll not have more than one Panel opened at the same time. |
| 269 |
// Do not unlock focus manager here because we're displaying another floating panel |
| 270 |
// instead of returning the editor to a "no panel" state (Bug #1514). |
| 271 |
if ( FCK.ToolbarSet.CurrentInstance.GetInstanceObject( 'FCKPanel' )._OpenedPanel && |
| 272 |
FCK.ToolbarSet.CurrentInstance.GetInstanceObject( 'FCKPanel' )._OpenedPanel != this ) |
| 273 |
FCK.ToolbarSet.CurrentInstance.GetInstanceObject( 'FCKPanel' )._OpenedPanel.Hide( false, true ) ; |
| 274 |
|
| 275 |
FCKDomTools.SetElementStyles( eMainNode, |
| 276 |
{ |
| 277 |
width : width ? width + 'px' : '', |
| 278 |
height : height ? height + 'px' : '' |
| 279 |
} ) ; |
| 280 |
|
| 281 |
iMainWidth = eMainNode.offsetWidth ; |
| 282 |
|
| 283 |
if ( !width ) this._IFrame.width = 1 ; |
| 284 |
if ( !height ) this._IFrame.height = 1 ; |
| 285 |
|
| 286 |
// This is weird... but with Firefox, we must get the offsetWidth before |
| 287 |
// setting the _IFrame size (which returns "0"), and then after that, |
| 288 |
// to return the correct width. Remove the first step and it will not |
| 289 |
// work when the editor is in RTL. |
| 290 |
// |
| 291 |
// The "|| eMainNode.firstChild.offsetWidth" part has been added |
| 292 |
// for Opera compatibility (see #570). |
| 293 |
iMainWidth = eMainNode.offsetWidth || eMainNode.firstChild.offsetWidth ; |
| 294 |
|
| 295 |
// Base the popup coordinates upon the coordinates of relElement. |
| 296 |
var oPos = FCKTools.GetDocumentPosition( this._Window, |
| 297 |
relElement.nodeType == 9 ? |
| 298 |
( FCKTools.IsStrictMode( relElement ) ? relElement.documentElement : relElement.body ) : |
| 299 |
relElement ) ; |
| 300 |
|
| 301 |
// Minus the offsets provided by any positioned parent element of the panel iframe. |
| 302 |
var positionedAncestor = FCKDomTools.GetPositionedAncestor( this._IFrame.parentNode ) ; |
| 303 |
if ( positionedAncestor ) |
| 304 |
{ |
| 305 |
var nPos = FCKTools.GetDocumentPosition( FCKTools.GetElementWindow( positionedAncestor ), positionedAncestor ) ; |
| 306 |
oPos.x -= nPos.x ; |
| 307 |
oPos.y -= nPos.y ; |
| 308 |
} |
| 309 |
|
| 310 |
if ( this.IsRTL && !this.IsContextMenu ) |
| 311 |
x = ( x * -1 ) ; |
| 312 |
|
| 313 |
x += oPos.x ; |
| 314 |
y += oPos.y ; |
| 315 |
|
| 316 |
if ( this.IsRTL ) |
| 317 |
{ |
| 318 |
if ( this.IsContextMenu ) |
| 319 |
x = x - iMainWidth + 1 ; |
| 320 |
else if ( relElement ) |
| 321 |
x = x + relElement.offsetWidth - iMainWidth ; |
| 322 |
} |
| 323 |
else |
| 324 |
{ |
| 325 |
var oViewPaneSize = FCKTools.GetViewPaneSize( this._Window ) ; |
| 326 |
var oScrollPosition = FCKTools.GetScrollPosition( this._Window ) ; |
| 327 |
|
| 328 |
var iViewPaneHeight = oViewPaneSize.Height + oScrollPosition.Y ; |
| 329 |
var iViewPaneWidth = oViewPaneSize.Width + oScrollPosition.X ; |
| 330 |
|
| 331 |
if ( ( x + iMainWidth ) > iViewPaneWidth ) |
| 332 |
x -= x + iMainWidth - iViewPaneWidth ; |
| 333 |
|
| 334 |
if ( ( y + eMainNode.offsetHeight ) > iViewPaneHeight ) |
| 335 |
y -= y + eMainNode.offsetHeight - iViewPaneHeight ; |
| 336 |
} |
| 337 |
|
| 338 |
// Set the context menu DIV in the specified location. |
| 339 |
FCKDomTools.SetElementStyles( this._IFrame, |
| 340 |
{ |
| 341 |
left : x + 'px', |
| 342 |
top : y + 'px' |
| 343 |
} ) ; |
| 344 |
|
| 345 |
// Move the focus to the IFRAME so we catch the "onblur". |
| 346 |
this._IFrame.contentWindow.focus() ; |
| 347 |
this._IsOpened = true ; |
| 348 |
|
| 349 |
var me = this ; |
| 350 |
this._resizeTimer = setTimeout( function() |
| 351 |
{ |
| 352 |
var iWidth = eMainNode.offsetWidth || eMainNode.firstChild.offsetWidth ; |
| 353 |
var iHeight = eMainNode.offsetHeight ; |
| 354 |
me._IFrame.style.width = iWidth + 'px' ; |
| 355 |
me._IFrame.style.height = iHeight + 'px' ; |
| 356 |
|
| 357 |
}, 0 ) ; |
| 358 |
|
| 359 |
FCK.ToolbarSet.CurrentInstance.GetInstanceObject( 'FCKPanel' )._OpenedPanel = this ; |
| 360 |
} |
| 361 |
|
| 362 |
FCKTools.RunFunction( this.OnShow, this ) ; |
| 363 |
} |
| 364 |
|
| 365 |
FCKPanel.prototype.Hide = function( ignoreOnHide, ignoreFocusManagerUnlock ) |
| 366 |
{ |
| 367 |
if ( this._Popup ) |
| 368 |
this._Popup.hide() ; |
| 369 |
else |
| 370 |
{ |
| 371 |
if ( !this._IsOpened || this._LockCounter > 0 ) |
| 372 |
return ; |
| 373 |
|
| 374 |
// Enable the editor to fire the "OnBlur". |
| 375 |
if ( typeof( FCKFocusManager ) != 'undefined' && !ignoreFocusManagerUnlock ) |
| 376 |
FCKFocusManager.Unlock() ; |
| 377 |
|
| 378 |
// It is better to set the sizes to 0, otherwise Firefox would have |
| 379 |
// rendering problems. |
| 380 |
this._IFrame.style.width = this._IFrame.style.height = '0px' ; |
| 381 |
|
| 382 |
this._IsOpened = false ; |
| 383 |
|
| 384 |
if ( this._resizeTimer ) |
| 385 |
{ |
| 386 |
clearTimeout( this._resizeTimer ) ; |
| 387 |
this._resizeTimer = null ; |
| 388 |
} |
| 389 |
|
| 390 |
if ( this.ParentPanel ) |
| 391 |
this.ParentPanel.Unlock() ; |
| 392 |
|
| 393 |
if ( !ignoreOnHide ) |
| 394 |
FCKTools.RunFunction( this.OnHide, this ) ; |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
FCKPanel.prototype.CheckIsOpened = function() |
| 399 |
{ |
| 400 |
if ( this._Popup ) |
| 401 |
return this._Popup.isOpen ; |
| 402 |
else |
| 403 |
return this._IsOpened ; |
| 404 |
} |
| 405 |
|
| 406 |
FCKPanel.prototype.CreateChildPanel = function() |
| 407 |
{ |
| 408 |
var oWindow = this._Popup ? FCKTools.GetDocumentWindow( this.Document ) : this._Window ; |
| 409 |
|
| 410 |
var oChildPanel = new FCKPanel( oWindow ) ; |
| 411 |
oChildPanel.ParentPanel = this ; |
| 412 |
|
| 413 |
return oChildPanel ; |
| 414 |
} |
| 415 |
|
| 416 |
FCKPanel.prototype.Lock = function() |
| 417 |
{ |
| 418 |
this._LockCounter++ ; |
| 419 |
} |
| 420 |
|
| 421 |
FCKPanel.prototype.Unlock = function() |
| 422 |
{ |
| 423 |
if ( --this._LockCounter == 0 && !this.HasFocus ) |
| 424 |
this.Hide() ; |
| 425 |
} |
| 426 |
|
| 427 |
/* Events */ |
| 428 |
|
| 429 |
function FCKPanel_Window_OnFocus( e, panel ) |
| 430 |
{ |
| 431 |
panel.HasFocus = true ; |
| 432 |
} |
| 433 |
|
| 434 |
function FCKPanel_Window_OnBlur( e, panel ) |
| 435 |
{ |
| 436 |
panel.HasFocus = false ; |
| 437 |
|
| 438 |
if ( panel._LockCounter == 0 ) |
| 439 |
FCKTools.RunFunction( panel.Hide, panel ) ; |
| 440 |
} |
| 441 |
|
| 442 |
function CheckPopupOnHide( forceHide ) |
| 443 |
{ |
| 444 |
if ( forceHide || !this._Popup.isOpen ) |
| 445 |
{ |
| 446 |
window.clearInterval( this._Timer ) ; |
| 447 |
this._Timer = null ; |
| 448 |
|
| 449 |
if (this._Popup && this.ParentPanel && !forceHide) |
| 450 |
this.ParentPanel.ResizeForSubpanel(this, 0, 0) ; |
| 451 |
|
| 452 |
FCKTools.RunFunction( this.OnHide, this ) ; |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
function FCKPanel_Cleanup() |
| 457 |
{ |
| 458 |
this._Popup = null ; |
| 459 |
this._Window = null ; |
| 460 |
this.Document = null ; |
| 461 |
this.MainNode = null ; |
| 462 |
this.RelativeElement = null ; |
| 463 |
} |
| 464 |
|