| 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 | |
| 74 | for ( attrName in attrs ) |
| 75 | { |
| 76 | if ( attrs[ attrName ] ) |
| 77 | element.setAttribute( attrName, attrs[ attrName ] ); |
| 78 | else |
| 79 | element.removeAttribute( attrName ); |
| 80 | } |
| 81 | } |
| 82 | }, |
| 83 | |
| 84 | /** |
| 85 | * Alter the element style while keep record of the original styles. |
| 86 | */ |
| 87 | changeStyle : function( element, styles ) |
| 88 | { |
| 89 | this.saveStyles( element ); |
| 90 | element.setStyles( styles ); |
| 91 | }, |
| 92 | |
| 93 | |
| 94 | /** |
| 95 | * Lift the whole editor to became the first child of body element, OR |
| 96 | * _restore the editor to it's original position. |
| 97 | * @param {Boolean} isRestore |
| 98 | */ |
| 99 | moveEditor : function( isRestore ) |
| 100 | { |
| 101 | var container = this.editor.container; |
| 102 | |
| 103 | if ( isRestore ) |
| 104 | { |
| 105 | // place right after the remembered previous sibling. |
| 106 | container.insertAfter( this.lastPreviousSibling ); |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | this.lastPreviousSibling = this.editor.container.getPrevious(); |
| 111 | body.append( this.editor.container, true ); |
| 112 | } |
| 113 | }, |
| 114 | |
| 115 | saveSelection : function() |
| 116 | { |
| 117 | if ( this.editor.mode == 'wysiwyg' ) |
| 118 | { |
| 119 | var editorFocus = this.editor.focusManager.hasFocus; |
| 120 | |
| 121 | // IE failed to create selection on editor blur |
| 122 | if ( CKEDITOR.env.ie && !editorFocus ) |
| 123 | return; |
| 124 | |
| 125 | var sel = this.editor.document.getSelection(); |
| 126 | if ( sel ) |
| 127 | { |
| 128 | this._.restore.bookmark = sel.createBookmarks( true ); |
| 129 | this._.restore.focus = editorFocus; |
| 130 | } |
| 131 | } |
| 132 | }, |
| 133 | |
| 134 | restoreSelection : function() |
| 135 | { |
| 136 | if ( this._.restore.bookmark && !this.restorePending ) |
| 137 | { |
| 138 | var sel = this.editor.document.getSelection(); |
| 139 | if ( sel ) |
| 140 | { |
| 141 | sel.selectBookmarks( this._.restore.bookmark ); |
| 142 | |
| 143 | if ( this._.restore.focus ) |
| 144 | { |
| 145 | this.editor.focus(); |
| 146 | this.editor.forceNextSelectionCheck(); |
| 147 | } |
| 148 | else |
| 149 | this.editor.checkSelection(); |
| 150 | |
| 151 | delete this._.restore.focus; |
| 152 | delete this._.restore.bookmark; |
| 153 | } |
| 154 | } |
| 155 | }, |
| 156 | |
| 157 | /** |
| 158 | * Force reconstruct the current mode with the saved content. |
| 159 | * @param {Object} data Content snapshot |
| 160 | */ |
| 161 | reloadMode : function( data ) |
| 162 | { |
| 163 | //Clear holder element |
| 164 | var holderElement = this.editor.getThemeSpace( 'contents' ); |
| 165 | holderElement.setHtml( '' ); |
| 166 | // Reload current mode. |
| 167 | var currentModeEditor = this.editor._.modes[ this.editor.mode ]; |
| 168 | currentModeEditor.load( holderElement, data ); |
| 169 | }, |
| 170 | |
| 171 | saveContent : function() |
| 172 | { |
| 173 | // Kludge: Fix for Gecko and Webkit bug of iframe reloads when moved |
| 174 | // around the DOM tree. |
| 175 | if ( this.editor.mode == 'wysiwyg' && !CKEDITOR.env.ie ) |
| 176 | { |
| 177 | // Get the dirty content along with bookmark |
| 178 | this._.restore.content = this.editor.getSnapshot(); |
| 179 | this.restorePending = true; |
| 180 | } |
| 181 | }, |
| 182 | |
| 183 | restoreContent : function() |
| 184 | { |
| 185 | if ( this._.restore.content ) |
| 186 | { |
| 187 | this.editor.on( 'mode', function() |
| 188 | { |
| 189 | this.restorePending = false; |
| 190 | this.restoreScrollPosition(); |
| 191 | this.restoreSelection(); |
| 192 | this.editor.removeListener( 'mode', arguments.callee ); |
| 193 | delete this._.restore.content; |
| 194 | }, this ); |
| 195 | |
| 196 | // reload document content |
| 197 | this.reloadMode( this._.restore.content ); |
| 198 | } |
| 199 | }, |
| 200 | |
| 201 | getEditorElement : function() |
| 202 | { |
| 203 | var element = ( this.editor.mode == 'wysiwyg' ) ? |
| 204 | this.editor.document.$.documentElement |
| 205 | : this.editor.sourceField; |
| 206 | |
| 207 | return element.$ || element; |
| 208 | }, |
| 209 | |
| 210 | saveScrollPosition : function( ) |
| 211 | { |
| 212 | if ( CKEDITOR.env.ie && this.editor.mode == 'wysiwyg' ) |
| 213 | return; |
| 214 | var element = this.getEditorElement(); |
| 215 | this._.restore.scroll = |
| 216 | { |
| 217 | 'element' : element, |
| 218 | 'left' : element.scrollLeft + element.clientWidth, |
| 219 | 'top' : element.scrollTop + element.clientHeight |
| 220 | }; |
| 221 | }, |
| 222 | |
| 223 | restoreScrollPosition : function() |
| 224 | { |
| 225 | if ( this._.restore.scroll && !this.restorePending ) |
| 226 | { |
| 227 | var element = this.getEditorElement(); |
| 228 | var translatedScroll = |
| 229 | { |
| 230 | 'left' : this._.restore.scroll.left - element.clientWidth , |
| 231 | 'top' : this._.restore.scroll.top - element.clientHeight |
| 232 | }; |
| 233 | element.scrollLeft = translatedScroll.left; |
| 234 | element.scrollTop = translatedScroll.top; |
| 235 | |
| 236 | delete this._.restore.scroll; |
| 237 | } |
| 238 | }, |
| 239 | |
| 240 | save: function() |
| 241 | { |
| 242 | this.saveSelection(); |
| 243 | this.saveContent(); |
| 244 | this.saveScrollPosition(); |
| 245 | }, |
| 246 | |
| 247 | restore : function() |
| 248 | { |
| 249 | this.restoreContent(); |
| 250 | this.restoreScrollPosition(); |
| 251 | this.restoreSelection(); |
| 252 | }, |
| 253 | |
| 254 | exec : function( editor ) |
| 255 | { |
| 256 | |
| 257 | if( this.state == CKEDITOR.TRISTATE_ON || !hasMaximized ) |
| 258 | { |
| 259 | this.save(); |
| 260 | |
| 261 | if ( this.state == CKEDITOR.TRISTATE_ON ) |
| 262 | { |
| 263 | this.moveEditor( true ); |
| 264 | this.restoreAllStyles(); |
| 265 | hasMaximized = false; |
| 266 | } |
| 267 | else |
| 268 | { |
| 269 | this.moveEditor(); |
| 270 | // fix IE and Opera viewport scrolling |
| 271 | if ( CKEDITOR.env.ie || CKEDITOR.env.opera ) |
| 272 | { |
| 273 | var view = new CKEDITOR.dom.element( |
| 274 | CKEDITOR.document.$.documentElement ); |
| 275 | this.changeStyle( view, { |
| 276 | 'overflow' :'hidden' |
| 277 | } ); |
| 278 | } |
| 279 | |
| 280 | this.changeStyle( body, { |
| 281 | 'top' : '0px', |
| 282 | 'left' : '0px', |
| 283 | 'height' : '100%', |
| 284 | 'width' : '100%', |
| 285 | 'overflow' : 'hidden', |
| 286 | 'position' : 'absolute', |
| 287 | 'margin' : '0px' |
| 288 | } ); |
| 289 | |
| 290 | if ( CKEDITOR.env.webkit ) |
| 291 | { |
| 292 | this.changeStyle( editor.container, { |
| 293 | 'position' : 'absolute', |
| 294 | 'height' : '100%', |
| 295 | 'width' : '100%', |
| 296 | 'z-index' : '10' |
| 297 | } ); |
| 298 | } |
| 299 | |
| 300 | this.changeStyle( editor.container.getFirst(), { |
| 301 | 'width' : '100%', |
| 302 | // Kludge: Fix IE and Opera table over-height on fit parent. |
| 303 | 'height' : ( ( CKEDITOR.env.ie && |
| 304 | CKEDITOR.document.$.compatMode != 'BackCompat' ) |
| 305 | || CKEDITOR.env.opera ) ? '90%' : '100%', |
| 306 | 'z-index' : '10' |
| 307 | } ); |
| 308 | |
| 309 | // Fix IE 'source' mode bug of #2764 |
| 310 | if( editor.mode == 'source' && CKEDITOR.env.ie ) |
| 311 | { |
| 312 | var element = this.editor.sourceField; |
| 313 | this.changeStyle( element , { |
| 314 | 'width' : element.getParent().$.clientWidth, |
| 315 | 'height' : element.getParent().$.clientHeight |
| 316 | } ); |
| 317 | } |
| 318 | |
| 319 | hasMaximized = true; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | this.restore(); |
| 324 | // refresh command state |
| 325 | this.toggleState(); |
| 326 | } |
| 327 | }; |
| 328 | |
| 329 | CKEDITOR.plugins.add( 'maximize', |
| 330 | { |
| 331 | requires : [ 'editingblock' ], |
| 332 | |
| 333 | init : function( editor ) |
| 334 | { |
| 335 | editor.addCommand( 'maximize', new maximizeCommand( editor ) ); |
| 336 | editor.ui.addButton( 'Maximize', { |
| 337 | label : editor.lang.maximize, |
| 338 | command : 'maximize' |
| 339 | } ); |
| 340 | |
| 341 | if( editor.config.startupMaximize ) |
| 342 | { |
| 343 | editor.on( 'mode', function() |
| 344 | { |
| 345 | // defer the execution to avoid DUP event registration in |
| 346 | // event-handler. |
| 347 | setTimeout( function() |
| 348 | { |
| 349 | editor.getCommand( 'maximize' ).exec(); |
| 350 | }, 0 ); |
| 351 | |
| 352 | editor.removeListener( 'mode', arguments.callee ); |
| 353 | } ); |
| 354 | } |
| 355 | } |
| 356 | }); |
| 357 | |
| 358 | })(); |
| 359 | |
| 360 | CKEDITOR.config.startupMaximize = false; |
| 361 | No newline at end of file |