1 /* 2 Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved. 3 For licensing, see LICENSE.html or http://ckeditor.com/license 4 */ 5 6 CKEDITOR.plugins.add( 'floatpanel', 7 { 8 requires : [ 'panel' ] 9 }); 10 11 (function() 12 { 13 var panels = {}; 14 var isShowing = false; 15 16 function getPanel( editor, doc, parentElement, definition, level ) 17 { 18 // Generates the panel key: docId-eleId-skinName-langDir[-uiColor][-CSSs][-level] 19 var key = CKEDITOR.tools.genKey( doc.getUniqueId(), parentElement.getUniqueId(), editor.skinName, editor.lang.dir, 20 editor.uiColor || '', definition.css || '', level || '' ); 21 22 var panel = panels[ key ]; 23 24 if ( !panel ) 25 { 26 panel = panels[ key ] = new CKEDITOR.ui.panel( doc, definition ); 27 panel.element = parentElement.append( CKEDITOR.dom.element.createFromHtml( panel.renderHtml( editor ), doc ) ); 28 29 panel.element.setStyles( 30 { 31 display : 'none', 32 position : 'absolute' 33 }); 34 } 35 36 return panel; 37 } 38 39 CKEDITOR.ui.floatPanel = CKEDITOR.tools.createClass( 40 { 41 $ : function( editor, parentElement, definition, level ) 42 { 43 definition.forceIFrame = 1; 44 45 var doc = parentElement.getDocument(), 46 panel = getPanel( editor, doc, parentElement, definition, level || 0 ), 47 element = panel.element, 48 iframe = element.getFirst().getFirst(); 49 50 // Disable native browser menu. (#4825) 51 element.disableContextMenu(); 52 53 this.element = element; 54 55 this._ = 56 { 57 editor : editor, 58 // The panel that will be floating. 59 panel : panel, 60 parentElement : parentElement, 61 definition : definition, 62 document : doc, 63 iframe : iframe, 64 children : [], 65 dir : editor.lang.dir 66 }; 67 68 editor.on( 'mode', function(){ this.hide(); }, this ); 69 }, 70 71 proto : 72 { 73 addBlock : function( name, block ) 74 { 75 return this._.panel.addBlock( name, block ); 76 }, 77 78 addListBlock : function( name, multiSelect ) 79 { 80 return this._.panel.addListBlock( name, multiSelect ); 81 }, 82 83 getBlock : function( name ) 84 { 85 return this._.panel.getBlock( name ); 86 }, 87 88 /* 89 corner (LTR): 90 1 = top-left 91 2 = top-right 92 3 = bottom-right 93 4 = bottom-left 94 95 corner (RTL): 96 1 = top-right 97 2 = top-left 98 3 = bottom-left 99 4 = bottom-right 100 */ 101 showBlock : function( name, offsetParent, corner, offsetX, offsetY ) 102 { 103 var panel = this._.panel, 104 block = panel.showBlock( name ); 105 106 this.allowBlur( false ); 107 isShowing = 1; 108 109 // Record from where the focus is when open panel. 110 this._.returnFocus = this._.editor.focusManager.hasFocus ? this._.editor : new CKEDITOR.dom.element( CKEDITOR.document.$.activeElement ); 111 112 113 var element = this.element, 114 iframe = this._.iframe, 115 definition = this._.definition, 116 position = offsetParent.getDocumentPosition( element.getDocument() ), 117 rtl = this._.dir == 'rtl'; 118 119 var left = position.x + ( offsetX || 0 ), 120 top = position.y + ( offsetY || 0 ); 121 122 // Floating panels are off by (-1px, 0px) in RTL mode. (#3438) 123 if ( rtl && ( corner == 1 || corner == 4 ) ) 124 left += offsetParent.$.offsetWidth; 125 else if ( !rtl && ( corner == 2 || corner == 3 ) ) 126 left += offsetParent.$.offsetWidth - 1; 127 128 if ( corner == 3 || corner == 4 ) 129 top += offsetParent.$.offsetHeight - 1; 130 131 // Memorize offsetParent by it's ID. 132 this._.panel._.offsetParentId = offsetParent.getId(); 133 134 element.setStyles( 135 { 136 top : top + 'px', 137 left: 0, 138 display : '' 139 }); 140 141 // Don't use display or visibility style because we need to 142 // calculate the rendering layout later and focus the element. 143 element.setOpacity( 0 ); 144 145 // To allow the context menu to decrease back their width 146 element.getFirst().removeStyle( 'width' ); 147 148 // Configure the IFrame blur event. Do that only once. 149 if ( !this._.blurSet ) 150 { 151 // Non IE prefer the event into a window object. 152 var focused = CKEDITOR.env.ie ? iframe : new CKEDITOR.dom.window( iframe.$.contentWindow ); 153 154 // With addEventListener compatible browsers, we must 155 // useCapture when registering the focus/blur events to 156 // guarantee they will be firing in all situations. (#3068, #3222 ) 157 CKEDITOR.event.useCapture = true; 158 159 focused.on( 'blur', function( ev ) 160 { 161 if ( !this.allowBlur() ) 162 return; 163 164 // As we are using capture to register the listener, 165 // the blur event may get fired even when focusing 166 // inside the window itself, so we must ensure the 167 // target is out of it. 168 var target = ev.data.getTarget() ; 169 if ( target.getName && target.getName() != 'iframe' ) 170 return; 171 172 if ( this.visible && !this._.activeChild && !isShowing ) 173 { 174 // Panel close is caused by user's navigating away the focus, e.g. click outside the panel. 175 // DO NOT restore focus in this case. 176 delete this._.returnFocus; 177 this.hide(); 178 } 179 }, 180 this ); 181 182 focused.on( 'focus', function() 183 { 184 this._.focused = true; 185 this.hideChild(); 186 this.allowBlur( true ); 187 }, 188 this ); 189 190 CKEDITOR.event.useCapture = false; 191 192 this._.blurSet = 1; 193 } 194 195 panel.onEscape = CKEDITOR.tools.bind( function( keystroke ) 196 { 197 if ( this.onEscape && this.onEscape( keystroke ) === false ) 198 return false; 199 }, 200 this ); 201 202 CKEDITOR.tools.setTimeout( function() 203 { 204 var panelLoad = CKEDITOR.tools.bind( function () 205 { 206 var target = element.getFirst(); 207 208 if ( block.autoSize ) 209 { 210 // We must adjust first the width or IE6 could include extra lines in the height computation 211 var widthNode = block.element.$; 212 213 if ( CKEDITOR.env.gecko || CKEDITOR.env.opera ) 214 widthNode = widthNode.parentNode; 215 216 if ( CKEDITOR.env.ie ) 217 widthNode = widthNode.document.body; 218 219 var width = widthNode.scrollWidth; 220 // Account for extra height needed due to IE quirks box model bug: 221 // http://en.wikipedia.org/wiki/Internet_Explorer_box_model_bug 222 // (#3426) 223 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks && width > 0 ) 224 width += ( target.$.offsetWidth || 0 ) - ( target.$.clientWidth || 0 ) + 3; 225 // A little extra at the end. 226 // If not present, IE6 might break into the next line, but also it looks better this way 227 width += 4 ; 228 229 target.setStyle( 'width', width + 'px' ); 230 231 // IE doesn't compute the scrollWidth if a filter is applied previously 232 block.element.addClass( 'cke_frameLoaded' ); 233 234 var height = block.element.$.scrollHeight; 235 236 // Account for extra height needed due to IE quirks box model bug: 237 // http://en.wikipedia.org/wiki/Internet_Explorer_box_model_bug 238 // (#3426) 239 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks && height > 0 ) 240 height += ( target.$.offsetHeight || 0 ) - ( target.$.clientHeight || 0 ) + 3; 241 242 target.setStyle( 'height', height + 'px' ); 243 244 // Fix IE < 8 visibility. 245 panel._.currentBlock.element.setStyle( 'display', 'none' ).removeStyle( 'display' ); 246 } 247 else 248 target.removeStyle( 'height' ); 249 250 // Flip panel layout horizontally in RTL with known width. 251 if ( rtl ) 252 left -= element.$.offsetWidth; 253 254 // Pop the style now for measurement. 255 element.setStyle( 'left', left + 'px' ); 256 257 /* panel layout smartly fit the viewport size. */ 258 var panelElement = panel.element, 259 panelWindow = panelElement.getWindow(), 260 rect = element.$.getBoundingClientRect(), 261 viewportSize = panelWindow.getViewPaneSize(); 262 263 // Compensation for browsers that dont support "width" and "height". 264 var rectWidth = rect.width || rect.right - rect.left, 265 rectHeight = rect.height || rect.bottom - rect.top; 266 267 // Check if default horizontal layout is impossible. 268 var spaceAfter = rtl ? rect.right : viewportSize.width - rect.left, 269 spaceBefore = rtl ? viewportSize.width - rect.right : rect.left; 270 271 if ( rtl ) 272 { 273 if ( spaceAfter < rectWidth ) 274 { 275 // Flip to show on right. 276 if ( spaceBefore > rectWidth ) 277 left += rectWidth; 278 // Align to window left. 279 else if ( viewportSize.width > rectWidth ) 280 left = left - rect.left; 281 // Align to window right, never cutting the panel at right. 282 else 283 left = left - rect.right + viewportSize.width; 284 } 285 } 286 else if ( spaceAfter < rectWidth ) 287 { 288 // Flip to show on left. 289 if ( spaceBefore > rectWidth ) 290 left -= rectWidth; 291 // Align to window right. 292 else if ( viewportSize.width > rectWidth ) 293 left = left - rect.right + viewportSize.width; 294 // Align to window left, never cutting the panel at left. 295 else 296 left = left - rect.left; 297 } 298 299 300 // Check if the default vertical layout is possible. 301 var spaceBelow = viewportSize.height - rect.top, 302 spaceAbove = rect.top; 303 304 if ( spaceBelow < rectHeight ) 305 { 306 // Flip to show above. 307 if ( spaceAbove > rectHeight ) 308 top -= rectHeight; 309 // Align to window bottom. 310 else if ( viewportSize.height > rectHeight ) 311 top = top - rect.bottom + viewportSize.height; 312 // Align to top, never cutting the panel at top. 313 else 314 top = top - rect.top; 315 } 316 317 // If IE is in RTL, we have troubles with absolute 318 // position and horizontal scrolls. Here we have a 319 // series of hacks to workaround it. (#6146) 320 if ( CKEDITOR.env.ie ) 321 { 322 var offsetParent = new CKEDITOR.dom.element( element.$.offsetParent ), 323 scrollParent = offsetParent; 324 325 // Quirks returns <body>, but standards returns <html>. 326 if ( scrollParent.getName() == 'html' ) 327 scrollParent = scrollParent.getDocument().getBody(); 328 329 if ( scrollParent.getComputedStyle( 'direction' ) == 'rtl' ) 330 { 331 // For IE8, there is not much logic on this, but it works. 332 if ( CKEDITOR.env.ie8Compat ) 333 left -= element.getDocument().getDocumentElement().$.scrollLeft * 2; 334 else 335 left -= ( offsetParent.$.scrollWidth - offsetParent.$.clientWidth ); 336 } 337 } 338 339 // Trigger the onHide event of the previously active panel to prevent 340 // incorrect styles from being applied (#6170) 341 var innerElement = element.getFirst(), 342 activePanel; 343 if ( ( activePanel = innerElement.getCustomData( 'activePanel' ) ) ) 344 activePanel.onHide && activePanel.onHide.call( this, 1 ); 345 innerElement.setCustomData( 'activePanel', this ); 346 347 element.setStyles( 348 { 349 top : top + 'px', 350 left : left + 'px' 351 } ); 352 element.setOpacity( 1 ); 353 } , this ); 354 355 panel.isLoaded ? panelLoad() : panel.onLoad = panelLoad; 356 357 // Set the panel frame focus, so the blur event gets fired. 358 CKEDITOR.tools.setTimeout( function() 359 { 360 iframe.$.contentWindow.focus(); 361 // We need this get fired manually because of unfired focus() function. 362 this.allowBlur( true ); 363 }, 0, this); 364 }, CKEDITOR.env.air ? 200 : 0, this); 365 this.visible = 1; 366 367 if ( this.onShow ) 368 this.onShow.call( this ); 369 370 isShowing = 0; 371 }, 372 373 hide : function( returnFocus ) 374 { 375 if ( this.visible && ( !this.onHide || this.onHide.call( this ) !== true ) ) 376 { 377 this.hideChild(); 378 // Blur previously focused element. (#6671) 379 CKEDITOR.env.gecko && this._.iframe.getFrameDocument().$.activeElement.blur(); 380 this.element.setStyle( 'display', 'none' ); 381 this.visible = 0; 382 this.element.getFirst().removeCustomData( 'activePanel' ); 383 384 // Return focus properly. (#6247) 385 var focusReturn = returnFocus !== false && this._.returnFocus; 386 if ( focusReturn ) 387 { 388 // Webkit requires focus moved out panel iframe first. 389 if ( CKEDITOR.env.webkit && focusReturn.type ) 390 focusReturn.getWindow().$.focus(); 391 392 focusReturn.focus(); 393 } 394 } 395 }, 396 397 allowBlur : function( allow ) // Prevent editor from hiding the panel. #3222. 398 { 399 var panel = this._.panel; 400 if ( allow != undefined ) 401 panel.allowBlur = allow; 402 403 return panel.allowBlur; 404 }, 405 406 showAsChild : function( panel, blockName, offsetParent, corner, offsetX, offsetY ) 407 { 408 // Skip reshowing of child which is already visible. 409 if ( this._.activeChild == panel && panel._.panel._.offsetParentId == offsetParent.getId() ) 410 return; 411 412 this.hideChild(); 413 414 panel.onHide = CKEDITOR.tools.bind( function() 415 { 416 // Use a timeout, so we give time for this menu to get 417 // potentially focused. 418 CKEDITOR.tools.setTimeout( function() 419 { 420 if ( !this._.focused ) 421 this.hide(); 422 }, 423 0, this ); 424 }, 425 this ); 426 427 this._.activeChild = panel; 428 this._.focused = false; 429 430 panel.showBlock( blockName, offsetParent, corner, offsetX, offsetY ); 431 432 /* #3767 IE: Second level menu may not have borders */ 433 if ( CKEDITOR.env.ie7Compat || ( CKEDITOR.env.ie8 && CKEDITOR.env.ie6Compat ) ) 434 { 435 setTimeout(function() 436 { 437 panel.element.getChild( 0 ).$.style.cssText += ''; 438 }, 100); 439 } 440 }, 441 442 hideChild : function() 443 { 444 var activeChild = this._.activeChild; 445 446 if ( activeChild ) 447 { 448 delete activeChild.onHide; 449 // Sub panels don't manage focus. (#7881) 450 delete activeChild._.returnFocus; 451 delete this._.activeChild; 452 activeChild.hide(); 453 } 454 } 455 } 456 }); 457 458 CKEDITOR.on( 'instanceDestroyed', function() 459 { 460 var isLastInstance = CKEDITOR.tools.isEmpty( CKEDITOR.instances ); 461 462 for ( var i in panels ) 463 { 464 var panel = panels[ i ]; 465 // Safe to destroy it since there're no more instances.(#4241) 466 if ( isLastInstance ) 467 panel.destroy(); 468 // Panel might be used by other instances, just hide them.(#4552) 469 else 470 panel.element.hide(); 471 } 472 // Remove the registration. 473 isLastInstance && ( panels = {} ); 474 475 } ); 476 })(); 477