| 1 | /* |
| 2 | Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved. |
| 3 | For licensing, see LICENSE.html or http://ckeditor.com/license |
| 4 | */ |
| 5 | |
| 6 | /** |
| 7 | * @fileOverview The "maximize" plugin. Enable it will make the editor chrome |
| 8 | * being maximized and fullfill the view size. Disable it _restore |
| 9 | * editor chrome into original size. |
| 10 | */ |
| 11 | |
| 12 | ( function() |
| 13 | { |
| 14 | var body = CKEDITOR.document.getBody(); |
| 15 | |
| 16 | //Only one editor instance is allowed to be full screen at a time. |
| 17 | var hasMaximized = false; |
| 18 | |
| 19 | function maximizeCommand( editor ) |
| 20 | { |
| 21 | this.editor = editor; |
| 22 | // elements along with styles. |
| 23 | this.savedAttributes = []; |
| 24 | // Keep track of the need-to-_restore data |
| 25 | this._ = |
| 26 | { |
| 27 | restore : |
| 28 | { |
| 29 | // document content |
| 30 | content : '', |
| 31 | // selection range bookmark |
| 32 | bookmark : null, |
| 33 | // editor focus state |
| 34 | focus : false, |
| 35 | // scrollbar position |
| 36 | scroll : null |
| 37 | } |
| 38 | }; |
| 39 | // Whether pending the restoring due to asynchroniz loading of |
| 40 | // content. |
| 41 | this.restorePending = false; |
| 42 | } |
| 43 | |
| 44 | maximizeCommand.prototype = |
| 45 | { |
| 46 | /** |
| 47 | * Save the css style of the element for later restoring. |
| 48 | * |
| 49 | * @param {CKEDITOR.dom.element} element |
| 50 | */ |
| 51 | saveStyles : function( element ) |
| 52 | { |
| 53 | this.savedAttributes.push( { |
| 54 | element : element, |
| 55 | attributes : { |
| 56 | 'class' : element.getAttribute( 'class' ), |
| 57 | 'style' : element.getAttribute( 'style' ) |
| 58 | } |
| 59 | } ); |
| 60 | }, |
| 61 | |
| 62 | /** |
| 63 | * _restore the saved attributes for elements |
| 64 | */ |
| 65 | restoreAllStyles : function() |
| 66 | { |
| 67 | var i = 0, l = this.savedAttributes.length, attrs, element; |
| 68 | var attrName, attribute; |
| 69 | for ( ; i < l ; i++ ) |
| 70 | { |
| 71 | attrs = this.savedAttributes[ i ].attributes; |
| 72 | element = this.savedAttributes[ i ].element; |
| 73 | for ( attrName in attrs ) |
| 74 | { |
| 75 | if ( CKEDITOR.env.ie ) |
| 76 | { |
| 77 | // IE need pause the execution for a while when changing styles consequently. |
| 78 | window.showModalDialog( |
| 79 | 'javascript:document.write("' + |
| 80 | '<script>' + |
| 81 | 'window.setTimeout(' + |
| 82 | 'function(){window.close();}' + |
| 83 | ', 0);' + |
| 84 | '</script>")' ); |
| 85 | |
| 86 | if ( attrs[ attrName ] ) |
| 87 | element.setAttribute( attrName, attrs[ attrName ] ); |
| 88 | else |
| 89 | element.removeAttribute( attrName ); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | }, |
| 95 | |
| 96 | /** |
| 97 | * Alter the element style while keep record of the original styles. |
| 98 | */ |
| 99 | changeStyle : function( element, styles ) |
| 100 | { |
| 101 | this.saveStyles( element ); |
| 102 | element.setStyles( styles ); |
| 103 | }, |
| 104 | |
| 105 | |
| 106 | /** |
| 107 | * Lift the whole editor to became the first child of body element, OR |
| 108 | * _restore the editor to it's original position. |
| 109 | * @param {Boolean} isRestore |
| 110 | */ |
| 111 | moveEditor : function( isRestore ) |
| 112 | { |
| 113 | var container = this.editor.container; |
| 114 | |
| 115 | if ( isRestore ) |
| 116 | { |
| 117 | // place right after the remembered previous sibling. |
| 118 | container.insertAfter( this.lastPreviousSibling ); |
| 119 | } |
| 120 | else |
| 121 | { |
| 122 | this.lastPreviousSibling = this.editor.container.getPrevious(); |
| 123 | body.append( this.editor.container, true ); |
| 124 | } |
| 125 | }, |
| 126 | |
| 127 | saveSelection : function() |
| 128 | { |
| 129 | if ( this.editor.mode == 'wysiwyg' ) |
| 130 | { |
| 131 | var editorFocus = this.editor.focusManager.hasFocus; |
| 132 | |
| 133 | // IE failed to create selection on editor blur |
| 134 | if ( CKEDITOR.env.ie && !editorFocus ) |
| 135 | return; |
| 136 | |
| 137 | var sel = this.editor.document.getSelection(); |
| 138 | if ( sel ) |
| 139 | { |
| 140 | this._.restore.bookmark = sel.createBookmarks( true ); |
| 141 | this._.restore.focus = editorFocus; |
| 142 | } |
| 143 | } |
| 144 | }, |
| 145 | |
| 146 | restoreSelection : function() |
| 147 | { |
| 148 | if ( this._.restore.bookmark && !this.restorePending ) |
| 149 | { |
| 150 | var sel = this.editor.document.getSelection(); |
| 151 | if ( sel ) |
| 152 | { |
| 153 | sel.selectBookmarks( this._.restore.bookmark ); |
| 154 | |
| 155 | if ( this._.restore.focus ) |
| 156 | { |
| 157 | this.editor.focus(); |
| 158 | this.editor.forceNextSelectionCheck(); |
| 159 | } |
| 160 | else |
| 161 | this.editor.checkSelection(); |
| 162 | |
| 163 | delete this._.restore.focus; |
| 164 | delete this._.restore.bookmark; |
| 165 | } |
| 166 | } |
| 167 | }, |
| 168 | |
| 169 | /** |
| 170 | * Force reconstruct the current mode with the saved content. |
| 171 | * @param {Object} data Content snapshot |
| 172 | */ |
| 173 | reloadMode : function( data ) |
| 174 | { |
| 175 | //Clear holder element |
| 176 | var holderElement = this.editor.getThemeSpace( 'contents' ); |
| 177 | holderElement.setHtml( '' ); |
| 178 | // Reload current mode. |
| 179 | var currentModeEditor = this.editor._.modes[ this.editor.mode ]; |
| 180 | currentModeEditor.load( holderElement, data ); |
| 181 | }, |
| 182 | |
| 183 | saveContent : function() |
| 184 | { |
| 185 | // Kludge: Fix for Gecko and Webkit bug of iframe reloads when moved |
| 186 | // around the DOM tree. |
| 187 | if ( this.editor.mode == 'wysiwyg' && !CKEDITOR.env.ie ) |
| 188 | { |
| 189 | // Get the dirty content along with bookmark |
| 190 | this._.restore.content = this.editor.getSnapshot(); |
| 191 | this.restorePending = true; |
| 192 | } |
| 193 | }, |
| 194 | |
| 195 | restoreContent : function() |
| 196 | { |
| 197 | if ( this._.restore.content ) |
| 198 | { |
| 199 | this.editor.on( 'mode', function() |
| 200 | { |
| 201 | this.restorePending = false; |
| 202 | this.restoreScrollPosition(); |
| 203 | this.restoreSelection(); |
| 204 | this.editor.removeListener( 'mode', arguments.callee ); |
| 205 | delete this._.restore.content; |
| 206 | }, this ); |
| 207 | |
| 208 | // reload document content |
| 209 | this.reloadMode( this._.restore.content ); |
| 210 | } |
| 211 | }, |
| 212 | |
| 213 | getEditorElement : function() |
| 214 | { |
| 215 | var element = ( this.editor.mode == 'wysiwyg' ) ? |
| 216 | this.editor.document.$.documentElement |
| 217 | : this.editor.sourceField; |
| 218 | |
| 219 | return element.$ || element; |
| 220 | }, |
| 221 | |
| 222 | saveScrollPosition : function( ) |
| 223 | { |
| 224 | if ( CKEDITOR.env.ie && this.editor.mode == 'wysiwyg' ) |
| 225 | return; |
| 226 | var element = this.getEditorElement(); |
| 227 | this._.restore.scroll = |
| 228 | { |
| 229 | 'element' : element, |
| 230 | 'left' : element.scrollLeft + element.clientWidth, |
| 231 | 'leftScrolled' : !!element.scrollLeft, |
| 232 | 'top' : element.scrollTop + element.clientHeight, |
| 233 | 'topScrolled' : !!element.scrollTop |
| 234 | }; |
| 235 | }, |
| 236 | |
| 237 | restoreScrollPosition : function() |
| 238 | { |
| 239 | if ( this._.restore.scroll && !this.restorePending ) |
| 240 | { |
| 241 | var element = this.getEditorElement(); |
| 242 | var translatedScroll = |
| 243 | { |
| 244 | 'left' : this._.restore.scroll.left - element.clientWidth , |
| 245 | 'top' : this._.restore.scroll.top - element.clientHeight |
| 246 | }; |
| 247 | if( this._.restore.scroll.leftScrolled ) |
| 248 | element.scrollLeft = translatedScroll.left; |
| 249 | if( this._.restore.scroll.topScrolled ) |
| 250 | element.scrollTop = translatedScroll.top; |
| 251 | |
| 252 | delete this._.restore.scroll; |
| 253 | } |
| 254 | }, |
| 255 | |
| 256 | save: function() |
| 257 | { |
| 258 | this.saveSelection(); |
| 259 | this.saveContent(); |
| 260 | this.saveScrollPosition(); |
| 261 | }, |
| 262 | |
| 263 | restore : function() |
| 264 | { |
| 265 | this.restoreContent(); |
| 266 | this.restoreScrollPosition(); |
| 267 | this.restoreSelection(); |
| 268 | }, |
| 269 | |
| 270 | exec : function( editor ) |
| 271 | { |
| 272 | |
| 273 | if( this.state == CKEDITOR.TRISTATE_ON || !hasMaximized ) |
| 274 | { |
| 275 | this.save(); |
| 276 | |
| 277 | if ( this.state == CKEDITOR.TRISTATE_ON ) |
| 278 | { |
| 279 | this.moveEditor( true ); |
| 280 | |
| 281 | this.restoreAllStyles(); |
| 282 | |
| 283 | if( this.onWindowResize ) |
| 284 | new CKEDITOR.dom.window( window ).removeListener( |
| 285 | 'resize', this.onWindowResize ); |
| 286 | delete this.onWindowResize; |
| 287 | hasMaximized = false; |
| 288 | } |
| 289 | else |
| 290 | { |
| 291 | this.moveEditor(); |
| 292 | |
| 293 | // Make body fit window. |
| 294 | this.changeStyle( body, { |
| 295 | 'top' : '0px', |
| 296 | 'left' : '0px', |
| 297 | 'height' : '100%', |
| 298 | 'width' : '100%', |
| 299 | 'overflow' : 'hidden', |
| 300 | 'position' : 'absolute', |
| 301 | 'margin' : '0px', |
| 302 | 'padding' : '0px' |
| 303 | } ); |
| 304 | |
| 305 | // Fix IE and Opera viewport scrolling when body maximized. |
| 306 | if ( CKEDITOR.env.ie || CKEDITOR.env.opera ) |
| 307 | { |
| 308 | var view = new CKEDITOR.dom.element( |
| 309 | CKEDITOR.document.$.documentElement ); |
| 310 | this.changeStyle( view, { |
| 311 | 'overflow' :'hidden' |
| 312 | } ); |
| 313 | } |
| 314 | |
| 315 | // Fix webkit treate inline element as offset parent. |
| 316 | if ( CKEDITOR.env.webkit ) |
| 317 | { |
| 318 | this.changeStyle( editor.container, { |
| 319 | 'position' : 'absolute', |
| 320 | 'height' : '100%', |
| 321 | 'width' : '100%', |
| 322 | 'z-index' : '10' |
| 323 | } ); |
| 324 | } |
| 325 | |
| 326 | // Editor chrome fit body. |
| 327 | var editorTable = editor.container.getElementsByTag( 'table' ).getItem( 0 ); |
| 328 | this.changeStyle( editorTable, { |
| 329 | 'width' : '100%', |
| 330 | 'height' : '100%' |
| 331 | } ); |
| 332 | |
| 333 | // IE and Opera are requiring manual viewport size caculation. |
| 334 | if( CKEDITOR.env.ie || CKEDITOR.env.opera ) |
| 335 | { |
| 336 | var win = new CKEDITOR.dom.window( window ); |
| 337 | |
| 338 | function updateEditorHeight( isBackup ) |
| 339 | { |
| 340 | var topBar = CKEDITOR.document.getById( 'cke_toolbox_' + editor.name ), |
| 341 | topBarHeight = topBar.$.offsetHeight; |
| 342 | var bottomBar = CKEDITOR.document.getById( 'cke_path_' + editor.name ), |
| 343 | bottomBarHeight = bottomBar.$.offsetHeight; |
| 344 | |
| 345 | // Set content bar height |
| 346 | var contentBar = CKEDITOR.document.getById( 'cke_contents_' + editor.name ), |
| 347 | viewPortHeight = win.getViewPaneSize().height; |
| 348 | |
| 349 | var contentBarHeight = viewPortHeight - topBarHeight - bottomBarHeight - 5; |
| 350 | isBackup == true ? |
| 351 | this.changeStyle( contentBar, { |
| 352 | 'height' : contentBarHeight + 'px' |
| 353 | } ) : |
| 354 | contentBar.setStyle( 'height', contentBarHeight + 'px' ); |
| 355 | } |
| 356 | |
| 357 | updateEditorHeight.call( this, true ); |
| 358 | |
| 359 | if( !this.onWindowResize ) |
| 360 | { |
| 361 | this.onWindowResize = CKEDITOR.tools.bind( updateEditorHeight, this ); |
| 362 | win.on( 'resize' , this.onWindowResize ); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | // Fix IE 'source' mode bug of #2764 |
| 367 | if( editor.mode == 'source' && CKEDITOR.env.ie ) |
| 368 | { |
| 369 | var element = this.editor.sourceField; |
| 370 | this.changeStyle( element , { |
| 371 | 'width' : element.getParent().$.clientWidth, |
| 372 | 'height' : element.getParent().$.clientHeight |
| 373 | } ); |
| 374 | } |
| 375 | |
| 376 | hasMaximized = true; |
| 377 | |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | this.restore(); |
| 382 | CKEDITOR.tools.setTimeout( this.toggleState, 500, this ); |
| 383 | }, |
| 384 | |
| 385 | canUndo: false, |
| 386 | name: 'maximize', |
| 387 | modes : { wysiwyg:1, source:1 } |
| 388 | }; |
| 389 | |
| 390 | CKEDITOR.plugins.add( 'maximize', |
| 391 | { |
| 392 | requires : [ 'editingblock' ], |
| 393 | |
| 394 | init : function( editor ) |
| 395 | { |
| 396 | editor.addCommand( 'maximize', new maximizeCommand( editor ) ); |
| 397 | editor.ui.addButton( 'Maximize', { |
| 398 | label : editor.lang.maximize, |
| 399 | command : 'maximize' |
| 400 | } ); |
| 401 | |
| 402 | if( editor.config.startupMaximize ) |
| 403 | { |
| 404 | editor.on( 'mode', function( evt ) |
| 405 | { |
| 406 | evt.removeListener(); |
| 407 | editor.getCommand( 'maximize' ).exec(); |
| 408 | } ); |
| 409 | } |
| 410 | } |
| 411 | }); |
| 412 | |
| 413 | })(); |
| 414 | |
| 415 | CKEDITOR.config.startupMaximize = false;\ No newline at end of file |