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