| 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 |
| 104 | .insertAfter( this.lastPreviousSibling ); |
| 105 | } |
| 106 | else |
| 107 | { |
| 108 | this.lastPreviousSibling = this.editor.container.getPrevious(); |
| 109 | body.append( this.editor.container, true ); |
| 110 | } |
| 111 | }, |
| 112 | |
| 113 | saveSelection : function () |
| 114 | { |
| 115 | if ( this.editor.mode == 'wysiwyg' ) |
| 116 | { |
| 117 | var editorFocus = this.editor.focusManager.hasFocus; |
| 118 | |
| 119 | // IE failed to create selection on editor blur |
| 120 | if ( CKEDITOR.env.ie && !editorFocus ) |
| 121 | return; |
| 122 | |
| 123 | var sel = this.editor.document.getSelection(); |
| 124 | if ( sel ) |
| 125 | { |
| 126 | this.restore.bookmark = sel.createBookmarks( true ); |
| 127 | this.restore.focus = editorFocus; |
| 128 | } |
| 129 | } |
| 130 | }, |
| 131 | |
| 132 | restoreSelection : function () |
| 133 | { |
| 134 | if ( this.restore.bookmark && !this.restorePending ) |
| 135 | { |
| 136 | var sel = this.editor.document.getSelection(); |
| 137 | if ( sel ) |
| 138 | { |
| 139 | sel.selectBookmarks( this.restore.bookmark ); |
| 140 | |
| 141 | if ( this.restore.focus ) |
| 142 | { |
| 143 | this.editor.focus(); |
| 144 | this.editor.forceNextSelectionCheck(); |
| 145 | } |
| 146 | else |
| 147 | this.editor.forceSelectionCheck(); |
| 148 | |
| 149 | delete this.restore.focus; |
| 150 | delete this.restore.bookmark; |
| 151 | } |
| 152 | } |
| 153 | }, |
| 154 | |
| 155 | saveContent : function () |
| 156 | { |
| 157 | // Kludge: Fix for Gecko and Webkit bug of iframe reloads when moved |
| 158 | // around the DOM tree. |
| 159 | if ( this.editor.mode == 'wysiwyg' && !CKEDITOR.env.ie ) |
| 160 | { |
| 161 | // Get the dirty content along with bookmark |
| 162 | this.restore.content = this.editor.getSnapshot(); |
| 163 | this.restorePending = true; |
| 164 | } |
| 165 | }, |
| 166 | |
| 167 | restoreContent : function () |
| 168 | { |
| 169 | if ( this.restore.content ) |
| 170 | { |
| 171 | this.editor.on( 'mode', function() |
| 172 | { |
| 173 | this.restorePending = false; |
| 174 | this.restoreScrollPosition(); |
| 175 | this.restoreSelection(); |
| 176 | this.editor.removeListener( 'mode', arguments.callee ); |
| 177 | delete this.restore.content; |
| 178 | }, this ); |
| 179 | |
| 180 | // reload document content |
| 181 | this.editor.setMode( 'wysiwyg', true, this.restore.content ); |
| 182 | } |
| 183 | }, |
| 184 | |
| 185 | getEditorElement : function() |
| 186 | { |
| 187 | var element = ( this.editor.mode == 'wysiwyg' ) ? |
| 188 | this.editor.document.$.documentElement |
| 189 | : this.editor.editarea; |
| 190 | |
| 191 | return element.$ || element; |
| 192 | }, |
| 193 | |
| 194 | saveScrollPosition : function ( ) |
| 195 | { |
| 196 | if ( CKEDITOR.env.ie && this.editor.mode == 'wysiwyg' ) |
| 197 | return; |
| 198 | var element = this.getEditorElement(); |
| 199 | this.restore.scroll = |
| 200 | { |
| 201 | 'element' : element, |
| 202 | 'left' : element.scrollLeft + element.clientWidth, |
| 203 | 'top' : element.scrollTop + element.clientHeight |
| 204 | }; |
| 205 | }, |
| 206 | |
| 207 | restoreScrollPosition : function () |
| 208 | { |
| 209 | if ( this.restore.scroll && !this.restorePending ) |
| 210 | { |
| 211 | var element = this.getEditorElement(); |
| 212 | var translatedScroll = |
| 213 | { |
| 214 | 'left' : this.restore.scroll.left - element.clientWidth , |
| 215 | 'top' : this.restore.scroll.top - element.clientHeight |
| 216 | }; |
| 217 | element.scrollLeft = translatedScroll.left; |
| 218 | element.scrollTop = translatedScroll.top; |
| 219 | |
| 220 | delete this.restore.scroll; |
| 221 | } |
| 222 | }, |
| 223 | |
| 224 | save: function() |
| 225 | { |
| 226 | this.saveSelection(); |
| 227 | this.saveContent(); |
| 228 | this.saveScrollPosition(); |
| 229 | }, |
| 230 | |
| 231 | recover : function() |
| 232 | { |
| 233 | this.restoreContent(); |
| 234 | this.restoreScrollPosition(); |
| 235 | this.restoreSelection(); |
| 236 | }, |
| 237 | |
| 238 | exec : function( editor ) |
| 239 | { |
| 240 | |
| 241 | if( this.state == CKEDITOR.TRISTATE_ON || !hasMaximized ) |
| 242 | { |
| 243 | this.save(); |
| 244 | |
| 245 | if ( this.state == CKEDITOR.TRISTATE_ON ) |
| 246 | { |
| 247 | this.moveEditor( true ); |
| 248 | this.restoreAllStyles(); |
| 249 | hasMaximized = false; |
| 250 | } |
| 251 | else |
| 252 | { |
| 253 | this.moveEditor(); |
| 254 | // fix IE and Opera viewport scrolling |
| 255 | if ( CKEDITOR.env.ie || CKEDITOR.env.opera ) |
| 256 | { |
| 257 | var view = new CKEDITOR.dom.element( |
| 258 | CKEDITOR.document.$.documentElement ); |
| 259 | this.changeStyle( view, { |
| 260 | 'overflow' :'hidden' |
| 261 | } ); |
| 262 | } |
| 263 | |
| 264 | this.changeStyle( body, { |
| 265 | 'top' : '0px', |
| 266 | 'left' : '0px', |
| 267 | 'height' : '100%', |
| 268 | 'width' : '100%', |
| 269 | 'overflow' : 'hidden', |
| 270 | 'position' : 'absolute', |
| 271 | 'margin' : '0px' |
| 272 | } ); |
| 273 | |
| 274 | if ( CKEDITOR.env.webkit ) |
| 275 | { |
| 276 | this.changeStyle( editor.container, { |
| 277 | 'position' : 'absolute', |
| 278 | 'height' : '100%', |
| 279 | 'width' : '100%', |
| 280 | 'z-index' : '10' |
| 281 | } ); |
| 282 | } |
| 283 | |
| 284 | this.changeStyle( editor.container.getFirst(), { |
| 285 | 'width' : '100%', |
| 286 | // Kludge: Fix IE and Opera table over-height on fit parent. |
| 287 | 'height' : ( ( CKEDITOR.env.ie && |
| 288 | CKEDITOR.document.$.compatMode != 'BackCompat' ) |
| 289 | || CKEDITOR.env.opera ) ? '90%' : '100%', |
| 290 | 'z-index' : '10' |
| 291 | } ); |
| 292 | |
| 293 | // Fix IE 'source' mode bug of #2764 |
| 294 | if( editor.mode == 'source' && CKEDITOR.env.ie ) |
| 295 | { |
| 296 | var element = this.editor.editarea; |
| 297 | this.changeStyle( element , { |
| 298 | 'width' : element.getParent().$.clientWidth, |
| 299 | 'height' : element.getParent().$.clientHeight |
| 300 | } ); |
| 301 | } |
| 302 | |
| 303 | hasMaximized = true; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | this.recover(); |
| 308 | // refresh command state |
| 309 | this.toggleState(); |
| 310 | } |
| 311 | }; |
| 312 | |
| 313 | CKEDITOR.plugins.add( 'maximize', |
| 314 | { |
| 315 | requires : [ 'editingblock' ], |
| 316 | |
| 317 | init : function( editor ) |
| 318 | { |
| 319 | editor.addCommand( 'maximize', new maximizeCommand( editor ) ); |
| 320 | editor.ui.addButton( 'Maximize', { |
| 321 | label : editor.lang.maximize, |
| 322 | command : 'maximize' |
| 323 | } ); |
| 324 | |
| 325 | if(editor.config.startupMaximize) |
| 326 | { |
| 327 | editor.on( 'mode', function() |
| 328 | { |
| 329 | // defer the execution to avoid DUP event registration in |
| 330 | // event-handler. |
| 331 | setTimeout( function() |
| 332 | { |
| 333 | editor.getCommand( 'maximize' ).exec(); |
| 334 | }, 0 ); |
| 335 | |
| 336 | editor.removeListener( 'mode', arguments.callee ); |
| 337 | }); |
| 338 | } |
| 339 | } |
| 340 | }); |
| 341 | |
| 342 | })(); |
| 343 | |
| 344 | CKEDITOR.config.startupMaximize = false; |