[2862] | 1 | /*
|
---|
| 2 | Copyright (c) 2003-2010, 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 =
|
---|
| 20 | doc.getUniqueId() +
|
---|
| 21 | '-' + parentElement.getUniqueId() +
|
---|
| 22 | '-' + editor.skinName +
|
---|
| 23 | '-' + editor.lang.dir +
|
---|
| 24 | ( ( editor.uiColor && ( '-' + editor.uiColor ) ) || '' ) +
|
---|
| 25 | ( ( definition.css && ( '-' + definition.css ) ) || '' ) +
|
---|
| 26 | ( ( level && ( '-' + level ) ) || '' );
|
---|
| 27 |
|
---|
| 28 | var panel = panels[ key ];
|
---|
| 29 |
|
---|
| 30 | if ( !panel )
|
---|
| 31 | {
|
---|
| 32 | panel = panels[ key ] = new CKEDITOR.ui.panel( doc, definition );
|
---|
| 33 | panel.element = parentElement.append( CKEDITOR.dom.element.createFromHtml( panel.renderHtml( editor ), doc ) );
|
---|
| 34 |
|
---|
| 35 | panel.element.setStyles(
|
---|
| 36 | {
|
---|
| 37 | display : 'none',
|
---|
| 38 | position : 'absolute'
|
---|
| 39 | });
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | return panel;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | CKEDITOR.ui.floatPanel = CKEDITOR.tools.createClass(
|
---|
| 46 | {
|
---|
| 47 | $ : function( editor, parentElement, definition, level )
|
---|
| 48 | {
|
---|
| 49 | definition.forceIFrame = true;
|
---|
| 50 |
|
---|
| 51 | var doc = parentElement.getDocument(),
|
---|
| 52 | panel = getPanel( editor, doc, parentElement, definition, level || 0 ),
|
---|
| 53 | element = panel.element,
|
---|
| 54 | iframe = element.getFirst().getFirst();
|
---|
| 55 |
|
---|
| 56 | this.element = element;
|
---|
| 57 |
|
---|
| 58 | this._ =
|
---|
| 59 | {
|
---|
| 60 | // The panel that will be floating.
|
---|
| 61 | panel : panel,
|
---|
| 62 | parentElement : parentElement,
|
---|
| 63 | definition : definition,
|
---|
| 64 | document : doc,
|
---|
| 65 | iframe : iframe,
|
---|
| 66 | children : [],
|
---|
| 67 | dir : editor.lang.dir
|
---|
| 68 | };
|
---|
| 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 = true;
|
---|
| 108 |
|
---|
| 109 | var element = this.element,
|
---|
| 110 | iframe = this._.iframe,
|
---|
| 111 | definition = this._.definition,
|
---|
| 112 | position = offsetParent.getDocumentPosition( element.getDocument() ),
|
---|
| 113 | rtl = this._.dir == 'rtl';
|
---|
| 114 |
|
---|
| 115 | var left = position.x + ( offsetX || 0 ),
|
---|
| 116 | top = position.y + ( offsetY || 0 );
|
---|
| 117 |
|
---|
| 118 | // Floating panels are off by (-1px, 0px) in RTL mode. (#3438)
|
---|
| 119 | if ( rtl && ( corner == 1 || corner == 4 ) )
|
---|
| 120 | left += offsetParent.$.offsetWidth;
|
---|
| 121 | else if ( !rtl && ( corner == 2 || corner == 3 ) )
|
---|
| 122 | left += offsetParent.$.offsetWidth - 1;
|
---|
| 123 |
|
---|
| 124 | if ( corner == 3 || corner == 4 )
|
---|
| 125 | top += offsetParent.$.offsetHeight - 1;
|
---|
| 126 |
|
---|
| 127 | // Memorize offsetParent by it's ID.
|
---|
| 128 | this._.panel._.offsetParentId = offsetParent.getId();
|
---|
| 129 |
|
---|
| 130 | element.setStyles(
|
---|
| 131 | {
|
---|
| 132 | top : top + 'px',
|
---|
| 133 | left : '-3000px',
|
---|
| 134 | opacity : '0', // FF3 is ignoring "visibility"
|
---|
| 135 | display : ''
|
---|
| 136 | });
|
---|
| 137 |
|
---|
| 138 | // To allow the context menu to decrease back their width
|
---|
| 139 | element.getFirst().removeStyle('width');
|
---|
| 140 |
|
---|
| 141 | // Configure the IFrame blur event. Do that only once.
|
---|
| 142 | if ( !this._.blurSet )
|
---|
| 143 | {
|
---|
| 144 | // Non IE prefer the event into a window object.
|
---|
| 145 | var focused = CKEDITOR.env.ie ? iframe : new CKEDITOR.dom.window( iframe.$.contentWindow );
|
---|
| 146 |
|
---|
| 147 | // With addEventListener compatible browsers, we must
|
---|
| 148 | // useCapture when registering the focus/blur events to
|
---|
| 149 | // guarantee they will be firing in all situations. (#3068, #3222 )
|
---|
| 150 | CKEDITOR.event.useCapture = true;
|
---|
| 151 |
|
---|
| 152 | focused.on( 'blur', function( ev )
|
---|
| 153 | {
|
---|
| 154 | if ( !this.allowBlur() )
|
---|
| 155 | return;
|
---|
| 156 |
|
---|
| 157 | // As we are using capture to register the listener,
|
---|
| 158 | // the blur event may get fired even when focusing
|
---|
| 159 | // inside the window itself, so we must ensure the
|
---|
| 160 | // target is out of it.
|
---|
| 161 | var target;
|
---|
| 162 | if ( CKEDITOR.env.ie && !this.allowBlur()
|
---|
| 163 | || ( target = ev.data.getTarget() )
|
---|
| 164 | && target.getName && target.getName() != 'iframe' )
|
---|
| 165 | return;
|
---|
| 166 |
|
---|
| 167 | if ( this.visible && !this._.activeChild && !isShowing )
|
---|
| 168 | this.hide();
|
---|
| 169 | },
|
---|
| 170 | this );
|
---|
| 171 |
|
---|
| 172 | focused.on( 'focus', function()
|
---|
| 173 | {
|
---|
| 174 | this._.focused = true;
|
---|
| 175 | this.hideChild();
|
---|
| 176 | this.allowBlur( true );
|
---|
| 177 | },
|
---|
| 178 | this );
|
---|
| 179 |
|
---|
| 180 | CKEDITOR.event.useCapture = false;
|
---|
| 181 |
|
---|
| 182 | this._.blurSet = 1;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | panel.onEscape = CKEDITOR.tools.bind( function( keystroke )
|
---|
| 186 | {
|
---|
| 187 | if ( this.onEscape && this.onEscape( keystroke ) === false )
|
---|
| 188 | return false;
|
---|
| 189 | },
|
---|
| 190 | this );
|
---|
| 191 |
|
---|
| 192 | CKEDITOR.tools.setTimeout( function()
|
---|
| 193 | {
|
---|
| 194 | if ( rtl )
|
---|
| 195 | left -= element.$.offsetWidth;
|
---|
| 196 |
|
---|
| 197 | var panelLoad = CKEDITOR.tools.bind( function ()
|
---|
| 198 | {
|
---|
| 199 | var target = element.getFirst();
|
---|
| 200 |
|
---|
| 201 | if ( block.autoSize )
|
---|
| 202 | {
|
---|
| 203 | // We must adjust first the width or IE6 could include extra lines in the height computation
|
---|
| 204 | var widthNode = block.element.$;
|
---|
| 205 |
|
---|
| 206 | if ( CKEDITOR.env.gecko || CKEDITOR.env.opera)
|
---|
| 207 | widthNode = widthNode.parentNode;
|
---|
| 208 |
|
---|
| 209 | if ( CKEDITOR.env.ie)
|
---|
| 210 | widthNode = widthNode.document.body;
|
---|
| 211 |
|
---|
| 212 | var width = widthNode.scrollWidth;
|
---|
| 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 );
|
---|
| 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 );
|
---|
| 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 | var panelElement = panel.element,
|
---|
| 244 | panelWindow = panelElement.getWindow(),
|
---|
| 245 | windowScroll = panelWindow.getScrollPosition(),
|
---|
| 246 | viewportSize = panelWindow.getViewPaneSize(),
|
---|
| 247 | panelSize =
|
---|
| 248 | {
|
---|
| 249 | 'height' : panelElement.$.offsetHeight,
|
---|
| 250 | 'width' : panelElement.$.offsetWidth
|
---|
| 251 | };
|
---|
| 252 |
|
---|
| 253 | // If the menu is horizontal off, shift it toward
|
---|
| 254 | // the opposite language direction.
|
---|
| 255 | if ( rtl ? left < 0 : left + panelSize.width > viewportSize.width + windowScroll.x )
|
---|
| 256 | left += ( panelSize.width * ( rtl ? 1 : -1 ) );
|
---|
| 257 |
|
---|
| 258 | // Vertical off screen is simpler.
|
---|
| 259 | if ( top + panelSize.height > viewportSize.height + windowScroll.y )
|
---|
| 260 | top -= panelSize.height;
|
---|
| 261 |
|
---|
| 262 | element.setStyles(
|
---|
| 263 | {
|
---|
| 264 | top : top + 'px',
|
---|
| 265 | left : left + 'px',
|
---|
| 266 | opacity : '1'
|
---|
| 267 | } );
|
---|
| 268 |
|
---|
| 269 | } , this );
|
---|
| 270 |
|
---|
| 271 | panel.isLoaded ? panelLoad() : panel.onLoad = panelLoad;
|
---|
| 272 |
|
---|
| 273 | // Set the panel frame focus, so the blur event gets fired.
|
---|
| 274 | CKEDITOR.tools.setTimeout( function()
|
---|
| 275 | {
|
---|
| 276 | iframe.$.contentWindow.focus();
|
---|
| 277 | // We need this get fired manually because of unfired focus() function.
|
---|
| 278 | this.allowBlur( true );
|
---|
| 279 | }, 0, this);
|
---|
| 280 | }, 0, this);
|
---|
| 281 | this.visible = 1;
|
---|
| 282 |
|
---|
| 283 | if ( this.onShow )
|
---|
| 284 | this.onShow.call( this );
|
---|
| 285 |
|
---|
| 286 | isShowing = false;
|
---|
| 287 | },
|
---|
| 288 |
|
---|
| 289 | hide : function()
|
---|
| 290 | {
|
---|
| 291 | if ( this.visible && ( !this.onHide || this.onHide.call( this ) !== true ) )
|
---|
| 292 | {
|
---|
| 293 | this.hideChild();
|
---|
| 294 | this.element.setStyle( 'display', 'none' );
|
---|
| 295 | this.visible = 0;
|
---|
| 296 | }
|
---|
| 297 | },
|
---|
| 298 |
|
---|
| 299 | allowBlur : function( allow ) // Prevent editor from hiding the panel. #3222.
|
---|
| 300 | {
|
---|
| 301 | var panel = this._.panel;
|
---|
| 302 | if ( allow != undefined )
|
---|
| 303 | panel.allowBlur = allow;
|
---|
| 304 |
|
---|
| 305 | return panel.allowBlur;
|
---|
| 306 | },
|
---|
| 307 |
|
---|
| 308 | showAsChild : function( panel, blockName, offsetParent, corner, offsetX, offsetY )
|
---|
| 309 | {
|
---|
| 310 | // Skip reshowing of child which is already visible.
|
---|
| 311 | if ( this._.activeChild == panel && panel._.panel._.offsetParentId == offsetParent.getId() )
|
---|
| 312 | return;
|
---|
| 313 |
|
---|
| 314 | this.hideChild();
|
---|
| 315 |
|
---|
| 316 | panel.onHide = CKEDITOR.tools.bind( function()
|
---|
| 317 | {
|
---|
| 318 | // Use a timeout, so we give time for this menu to get
|
---|
| 319 | // potentially focused.
|
---|
| 320 | CKEDITOR.tools.setTimeout( function()
|
---|
| 321 | {
|
---|
| 322 | if ( !this._.focused )
|
---|
| 323 | this.hide();
|
---|
| 324 | },
|
---|
| 325 | 0, this );
|
---|
| 326 | },
|
---|
| 327 | this );
|
---|
| 328 |
|
---|
| 329 | this._.activeChild = panel;
|
---|
| 330 | this._.focused = false;
|
---|
| 331 |
|
---|
| 332 | panel.showBlock( blockName, offsetParent, corner, offsetX, offsetY );
|
---|
| 333 |
|
---|
| 334 | /* #3767 IE: Second level menu may not have borders */
|
---|
| 335 | if ( CKEDITOR.env.ie7Compat || ( CKEDITOR.env.ie8 && CKEDITOR.env.ie6Compat ) )
|
---|
| 336 | {
|
---|
| 337 | setTimeout(function()
|
---|
| 338 | {
|
---|
| 339 | panel.element.getChild( 0 ).$.style.cssText += '';
|
---|
| 340 | }, 100);
|
---|
| 341 | }
|
---|
| 342 | },
|
---|
| 343 |
|
---|
| 344 | hideChild : function()
|
---|
| 345 | {
|
---|
| 346 | var activeChild = this._.activeChild;
|
---|
| 347 |
|
---|
| 348 | if ( activeChild )
|
---|
| 349 | {
|
---|
| 350 | delete activeChild.onHide;
|
---|
| 351 | delete this._.activeChild;
|
---|
| 352 | activeChild.hide();
|
---|
| 353 | }
|
---|
| 354 | }
|
---|
| 355 | }
|
---|
| 356 | });
|
---|
| 357 |
|
---|
| 358 | CKEDITOR.on( 'instanceDestroyed', function()
|
---|
| 359 | {
|
---|
| 360 | var isLastInstance = CKEDITOR.tools.isEmpty( CKEDITOR.instances );
|
---|
| 361 |
|
---|
| 362 | for ( var i in panels )
|
---|
| 363 | if ( ! ( i in Object.prototype ) )
|
---|
| 364 | {
|
---|
| 365 | var panel = panels[ i ];
|
---|
| 366 | // Safe to destroy it since there're no more instances.(#4241)
|
---|
| 367 | if ( isLastInstance )
|
---|
| 368 | panel.destroy();
|
---|
| 369 | // Panel might be used by other instances, just hide them.(#4552)
|
---|
| 370 | else
|
---|
| 371 | panel.element.hide();
|
---|
| 372 | }
|
---|
| 373 | // Remove the registration.
|
---|
| 374 | isLastInstance && ( panels = {} );
|
---|
| 375 |
|
---|
| 376 | } );
|
---|
| 377 | })();
|
---|