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 var panelDoc = block.element.getDocument(); 211 var width = ( CKEDITOR.env.webkit? block.element : panelDoc.getBody() )[ '$' ].scrollWidth; 212 213 // Account for extra height needed due to IE quirks box model bug: 214 // http://en.wikipedia.org/wiki/Internet_Explorer_box_model_bug 215 // (#3426) 216 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks && width > 0 ) 217 width += ( target.$.offsetWidth || 0 ) - ( target.$.clientWidth || 0 ) + 3; 218 // A little extra at the end. 219 // If not present, IE6 might break into the next line, but also it looks better this way 220 width += 4 ; 221 222 target.setStyle( 'width', width + 'px' ); 223 224 // IE doesn't compute the scrollWidth if a filter is applied previously 225 block.element.addClass( 'cke_frameLoaded' ); 226 227 var height = block.element.$.scrollHeight; 228 229 // Account for extra height needed due to IE quirks box model bug: 230 // http://en.wikipedia.org/wiki/Internet_Explorer_box_model_bug 231 // (#3426) 232 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks && height > 0 ) 233 height += ( target.$.offsetHeight || 0 ) - ( target.$.clientHeight || 0 ) + 3; 234 235 target.setStyle( 'height', height + 'px' ); 236 237 // Fix IE < 8 visibility. 238 panel._.currentBlock.element.setStyle( 'display', 'none' ).removeStyle( 'display' ); 239 } 240 else 241 target.removeStyle( 'height' ); 242 243 // Flip panel layout horizontally in RTL with known width. 244 if ( rtl ) 245 left -= element.$.offsetWidth; 246 247 // Pop the style now for measurement. 248 element.setStyle( 'left', left + 'px' ); 249 250 /* panel layout smartly fit the viewport size. */ 251 var panelElement = panel.element, 252 panelWindow = panelElement.getWindow(), 253 rect = element.$.getBoundingClientRect(), 254 viewportSize = panelWindow.getViewPaneSize(); 255 256 // Compensation for browsers that dont support "width" and "height". 257 var rectWidth = rect.width || rect.right - rect.left, 258 rectHeight = rect.height || rect.bottom - rect.top; 259 260 // Check if default horizontal layout is impossible. 261 var spaceAfter = rtl ? rect.right : viewportSize.width - rect.left, 262 spaceBefore = rtl ? viewportSize.width - rect.right : rect.left; 263 264 if ( rtl ) 265 { 266 if ( spaceAfter < rectWidth ) 267 { 268 // Flip to show on right. 269 if ( spaceBefore > rectWidth ) 270 left += rectWidth; 271 // Align to window left. 272 else if ( viewportSize.width > rectWidth ) 273 left = left - rect.left; 274 // Align to window right, never cutting the panel at right. 275 else 276 left = left - rect.right + viewportSize.width; 277 } 278 } 279 else if ( spaceAfter < rectWidth ) 280 { 281 // Flip to show on left. 282 if ( spaceBefore > rectWidth ) 283 left -= rectWidth; 284 // Align to window right. 285 else if ( viewportSize.width > rectWidth ) 286 left = left - rect.right + viewportSize.width; 287 // Align to window left, never cutting the panel at left. 288 else 289 left = left - rect.left; 290 } 291 292 293 // Check if the default vertical layout is possible. 294 var spaceBelow = viewportSize.height - rect.top, 295 spaceAbove = rect.top; 296 297 if ( spaceBelow < rectHeight ) 298 { 299 // Flip to show above. 300 if ( spaceAbove > rectHeight ) 301 top -= rectHeight; 302 // Align to window bottom. 303 else if ( viewportSize.height > rectHeight ) 304 top = top - rect.bottom + viewportSize.height; 305 // Align to top, never cutting the panel at top. 306 else 307 top = top - rect.top; 308 } 309 310 // If IE is in RTL, we have troubles with absolute 311 // position and horizontal scrolls. Here we have a 312 // series of hacks to workaround it. (#6146) 313 if ( CKEDITOR.env.ie ) 314 { 315 var offsetParent = new CKEDITOR.dom.element( element.$.offsetParent ), 316 scrollParent = offsetParent; 317 318 // Quirks returns <body>, but standards returns <html>. 319 if ( scrollParent.getName() == 'html' ) 320 scrollParent = scrollParent.getDocument().getBody(); 321 322 if ( scrollParent.getComputedStyle( 'direction' ) == 'rtl' ) 323 { 324 // For IE8, there is not much logic on this, but it works. 325 if ( CKEDITOR.env.ie8Compat ) 326 left -= element.getDocument().getDocumentElement().$.scrollLeft * 2; 327 else 328 left -= ( offsetParent.$.scrollWidth - offsetParent.$.clientWidth ); 329 } 330 } 331 332 // Trigger the onHide event of the previously active panel to prevent 333 // incorrect styles from being applied (#6170) 334 var innerElement = element.getFirst(), 335 activePanel; 336 if ( ( activePanel = innerElement.getCustomData( 'activePanel' ) ) ) 337 activePanel.onHide && activePanel.onHide.call( this, 1 ); 338 innerElement.setCustomData( 'activePanel', this ); 339 340 element.setStyles( 341 { 342 top : top + 'px', 343 left : left + 'px' 344 } ); 345 element.setOpacity( 1 ); 346 } , this ); 347 348 panel.isLoaded ? panelLoad() : panel.onLoad = panelLoad; 349 350 // Set the panel frame focus, so the blur event gets fired. 351 CKEDITOR.tools.setTimeout( function() 352 { 353 iframe.$.contentWindow.focus(); 354 // We need this get fired manually because of unfired focus() function. 355 this.allowBlur( true ); 356 }, 0, this); 357 }, CKEDITOR.env.air ? 200 : 0, this); 358 this.visible = 1; 359 360 if ( this.onShow ) 361 this.onShow.call( this ); 362 363 isShowing = 0; 364 }, 365 366 hide : function( returnFocus ) 367 { 368 if ( this.visible && ( !this.onHide || this.onHide.call( this ) !== true ) ) 369 { 370 this.hideChild(); 371 // Blur previously focused element. (#6671) 372 CKEDITOR.env.gecko && this._.iframe.getFrameDocument().$.activeElement.blur(); 373 this.element.setStyle( 'display', 'none' ); 374 this.visible = 0; 375 this.element.getFirst().removeCustomData( 'activePanel' ); 376 377 // Return focus properly. (#6247) 378 var focusReturn = returnFocus !== false && this._.returnFocus; 379 if ( focusReturn ) 380 { 381 // Webkit requires focus moved out panel iframe first. 382 if ( CKEDITOR.env.webkit && focusReturn.type ) 383 focusReturn.getWindow().$.focus(); 384 385 focusReturn.focus(); 386 } 387 } 388 }, 389 390 allowBlur : function( allow ) // Prevent editor from hiding the panel. #3222. 391 { 392 var panel = this._.panel; 393 if ( allow != undefined ) 394 panel.allowBlur = allow; 395 396 return panel.allowBlur; 397 }, 398 399 showAsChild : function( panel, blockName, offsetParent, corner, offsetX, offsetY ) 400 { 401 // Skip reshowing of child which is already visible. 402 if ( this._.activeChild == panel && panel._.panel._.offsetParentId == offsetParent.getId() ) 403 return; 404 405 this.hideChild(); 406 407 panel.onHide = CKEDITOR.tools.bind( function() 408 { 409 // Use a timeout, so we give time for this menu to get 410 // potentially focused. 411 CKEDITOR.tools.setTimeout( function() 412 { 413 if ( !this._.focused ) 414 this.hide(); 415 }, 416 0, this ); 417 }, 418 this ); 419 420 this._.activeChild = panel; 421 this._.focused = false; 422 423 panel.showBlock( blockName, offsetParent, corner, offsetX, offsetY ); 424 425 /* #3767 IE: Second level menu may not have borders */ 426 if ( CKEDITOR.env.ie7Compat || ( CKEDITOR.env.ie8 && CKEDITOR.env.ie6Compat ) ) 427 { 428 setTimeout(function() 429 { 430 panel.element.getChild( 0 ).$.style.cssText += ''; 431 }, 100); 432 } 433 }, 434 435 hideChild : function() 436 { 437 var activeChild = this._.activeChild; 438 439 if ( activeChild ) 440 { 441 delete activeChild.onHide; 442 // Sub panels don't manage focus. (#7881) 443 delete activeChild._.returnFocus; 444 delete this._.activeChild; 445 activeChild.hide(); 446 } 447 } 448 } 449 }); 450 451 CKEDITOR.on( 'instanceDestroyed', function() 452 { 453 var isLastInstance = CKEDITOR.tools.isEmpty( CKEDITOR.instances ); 454 455 for ( var i in panels ) 456 { 457 var panel = panels[ i ]; 458 // Safe to destroy it since there're no more instances.(#4241) 459 if ( isLastInstance ) 460 panel.destroy(); 461 // Panel might be used by other instances, just hide them.(#4552) 462 else 463 panel.element.hide(); 464 } 465 // Remove the registration. 466 isLastInstance && ( panels = {} ); 467 468 } ); 469 })(); 470