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 | (function()
|
---|
7 | {
|
---|
8 | function protectFormStyles( formElement )
|
---|
9 | {
|
---|
10 | if ( !formElement || formElement.type != CKEDITOR.NODE_ELEMENT || formElement.getName() != 'form' )
|
---|
11 | return [];
|
---|
12 |
|
---|
13 | var hijackRecord = [];
|
---|
14 | var hijackNames = [ 'style', 'className' ];
|
---|
15 | for ( var i = 0 ; i < hijackNames.length ; i++ )
|
---|
16 | {
|
---|
17 | var name = hijackNames[i];
|
---|
18 | var $node = formElement.$.elements.namedItem( name );
|
---|
19 | if ( $node )
|
---|
20 | {
|
---|
21 | var hijackNode = new CKEDITOR.dom.element( $node );
|
---|
22 | hijackRecord.push( [ hijackNode, hijackNode.nextSibling ] );
|
---|
23 | hijackNode.remove();
|
---|
24 | }
|
---|
25 | }
|
---|
26 |
|
---|
27 | return hijackRecord;
|
---|
28 | }
|
---|
29 |
|
---|
30 | function restoreFormStyles( formElement, hijackRecord )
|
---|
31 | {
|
---|
32 | if ( !formElement || formElement.type != CKEDITOR.NODE_ELEMENT || formElement.getName() != 'form' )
|
---|
33 | return;
|
---|
34 |
|
---|
35 | if ( hijackRecord.length > 0 )
|
---|
36 | {
|
---|
37 | for ( var i = hijackRecord.length - 1 ; i >= 0 ; i-- )
|
---|
38 | {
|
---|
39 | var node = hijackRecord[i][0];
|
---|
40 | var sibling = hijackRecord[i][1];
|
---|
41 | if ( sibling )
|
---|
42 | node.insertBefore( sibling );
|
---|
43 | else
|
---|
44 | node.appendTo( formElement );
|
---|
45 | }
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | function saveStyles( element, isInsideEditor )
|
---|
50 | {
|
---|
51 | var data = protectFormStyles( element );
|
---|
52 | var retval = {};
|
---|
53 |
|
---|
54 | var $element = element.$;
|
---|
55 |
|
---|
56 | if ( !isInsideEditor )
|
---|
57 | {
|
---|
58 | retval[ 'class' ] = $element.className || '';
|
---|
59 | $element.className = '';
|
---|
60 | }
|
---|
61 |
|
---|
62 | retval.inline = $element.style.cssText || '';
|
---|
63 | if ( !isInsideEditor ) // Reset any external styles that might interfere. (#2474)
|
---|
64 | $element.style.cssText = 'position: static; overflow: visible';
|
---|
65 |
|
---|
66 | restoreFormStyles( data );
|
---|
67 | return retval;
|
---|
68 | }
|
---|
69 |
|
---|
70 | function restoreStyles( element, savedStyles )
|
---|
71 | {
|
---|
72 | var data = protectFormStyles( element );
|
---|
73 | var $element = element.$;
|
---|
74 | if ( 'class' in savedStyles )
|
---|
75 | $element.className = savedStyles[ 'class' ];
|
---|
76 | if ( 'inline' in savedStyles )
|
---|
77 | $element.style.cssText = savedStyles.inline;
|
---|
78 | restoreFormStyles( data );
|
---|
79 | }
|
---|
80 |
|
---|
81 | function getResizeHandler( mainWindow, editor )
|
---|
82 | {
|
---|
83 | return function()
|
---|
84 | {
|
---|
85 | var viewPaneSize = mainWindow.getViewPaneSize();
|
---|
86 | editor.resize( viewPaneSize.width, viewPaneSize.height, null, true );
|
---|
87 | };
|
---|
88 | }
|
---|
89 |
|
---|
90 | function refreshCursor( editor )
|
---|
91 | {
|
---|
92 | if ( editor.focusManager.hasFocus )
|
---|
93 | {
|
---|
94 | var focusGrabber = editor.container.append( CKEDITOR.dom.element.createFromHtml(
|
---|
95 | '<span tabindex="-1" style="position:absolute; left:-10000" role="presentation"></span>' ) );
|
---|
96 |
|
---|
97 | focusGrabber.on( 'focus', function()
|
---|
98 | {
|
---|
99 | editor.focus();
|
---|
100 | } );
|
---|
101 | focusGrabber.focus();
|
---|
102 | focusGrabber.remove();
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | CKEDITOR.plugins.add( 'maximize',
|
---|
107 | {
|
---|
108 | init : function( editor )
|
---|
109 | {
|
---|
110 | var lang = editor.lang;
|
---|
111 | var mainDocument = CKEDITOR.document;
|
---|
112 | var mainWindow = mainDocument.getWindow();
|
---|
113 |
|
---|
114 | // Saved selection and scroll position for the editing area.
|
---|
115 | var savedSelection;
|
---|
116 | var savedScroll;
|
---|
117 |
|
---|
118 | // Saved scroll position for the outer window.
|
---|
119 | var outerScroll;
|
---|
120 |
|
---|
121 | // Saved resize handler function.
|
---|
122 | var resizeHandler = getResizeHandler( mainWindow, editor );
|
---|
123 |
|
---|
124 | // Retain state after mode switches.
|
---|
125 | var savedState = CKEDITOR.TRISTATE_OFF;
|
---|
126 |
|
---|
127 | editor.addCommand( 'maximize',
|
---|
128 | {
|
---|
129 | modes : { wysiwyg : 1, source : 1 },
|
---|
130 | editorFocus : false,
|
---|
131 | exec : function()
|
---|
132 | {
|
---|
133 | var container = editor.container.getChild( 1 );
|
---|
134 | var contents = editor.getThemeSpace( 'contents' );
|
---|
135 |
|
---|
136 | // Save current selection and scroll position in editing area.
|
---|
137 | if ( editor.mode == 'wysiwyg' )
|
---|
138 | {
|
---|
139 | var selection = editor.getSelection();
|
---|
140 | savedSelection = selection && selection.getRanges();
|
---|
141 | savedScroll = mainWindow.getScrollPosition();
|
---|
142 | }
|
---|
143 | else
|
---|
144 | {
|
---|
145 | var $textarea = editor.textarea.$;
|
---|
146 | savedSelection = !CKEDITOR.env.ie && [ $textarea.selectionStart, $textarea.selectionEnd ];
|
---|
147 | savedScroll = [ $textarea.scrollLeft, $textarea.scrollTop ];
|
---|
148 | }
|
---|
149 |
|
---|
150 | if ( this.state == CKEDITOR.TRISTATE_OFF ) // Go fullscreen if the state is off.
|
---|
151 | {
|
---|
152 | // Add event handler for resizing.
|
---|
153 | mainWindow.on( 'resize', resizeHandler );
|
---|
154 |
|
---|
155 | // Save the scroll bar position.
|
---|
156 | outerScroll = mainWindow.getScrollPosition();
|
---|
157 |
|
---|
158 | // Save and reset the styles for the entire node tree.
|
---|
159 | var currentNode = editor.container;
|
---|
160 | while ( ( currentNode = currentNode.getParent() ) )
|
---|
161 | {
|
---|
162 | currentNode.setCustomData( 'maximize_saved_styles', saveStyles( currentNode ) );
|
---|
163 | currentNode.setStyle( 'z-index', editor.config.baseFloatZIndex - 1 );
|
---|
164 | }
|
---|
165 | contents.setCustomData( 'maximize_saved_styles', saveStyles( contents, true ) );
|
---|
166 | container.setCustomData( 'maximize_saved_styles', saveStyles( container, true ) );
|
---|
167 |
|
---|
168 | // Hide scroll bars.
|
---|
169 | if ( CKEDITOR.env.ie )
|
---|
170 | {
|
---|
171 | mainDocument.$.documentElement.style.overflow =
|
---|
172 | mainDocument.getBody().$.style.overflow = 'hidden';
|
---|
173 | }
|
---|
174 | else
|
---|
175 | {
|
---|
176 | mainDocument.getBody().setStyles(
|
---|
177 | {
|
---|
178 | overflow : 'hidden',
|
---|
179 | width : '0px',
|
---|
180 | height : '0px'
|
---|
181 | } );
|
---|
182 | }
|
---|
183 |
|
---|
184 | // Scroll to the top left (IE needs some time for it - #4923).
|
---|
185 | CKEDITOR.env.ie ?
|
---|
186 | setTimeout( function() { mainWindow.$.scrollTo( 0, 0 ); }, 0 ) :
|
---|
187 | mainWindow.$.scrollTo( 0, 0 );
|
---|
188 |
|
---|
189 | // Resize and move to top left.
|
---|
190 | var viewPaneSize = mainWindow.getViewPaneSize();
|
---|
191 | container.setStyle( 'position', 'absolute' );
|
---|
192 | container.$.offsetLeft; // SAFARI BUG: See #2066.
|
---|
193 | container.setStyles(
|
---|
194 | {
|
---|
195 | 'z-index' : editor.config.baseFloatZIndex - 1,
|
---|
196 | left : '0px',
|
---|
197 | top : '0px'
|
---|
198 | } );
|
---|
199 | editor.resize( viewPaneSize.width, viewPaneSize.height, null, true );
|
---|
200 |
|
---|
201 | // Still not top left? Fix it. (Bug #174)
|
---|
202 | var offset = container.getDocumentPosition();
|
---|
203 | container.setStyles(
|
---|
204 | {
|
---|
205 | left : ( -1 * offset.x ) + 'px',
|
---|
206 | top : ( -1 * offset.y ) + 'px'
|
---|
207 | } );
|
---|
208 |
|
---|
209 | // Fixing positioning editor chrome in Firefox break design mode. (#5149)
|
---|
210 | CKEDITOR.env.gecko && refreshCursor( editor );
|
---|
211 |
|
---|
212 | // Add cke_maximized class.
|
---|
213 | container.addClass( 'cke_maximized' );
|
---|
214 | }
|
---|
215 | else if ( this.state == CKEDITOR.TRISTATE_ON ) // Restore from fullscreen if the state is on.
|
---|
216 | {
|
---|
217 | // Remove event handler for resizing.
|
---|
218 | mainWindow.removeListener( 'resize', resizeHandler );
|
---|
219 |
|
---|
220 | // Restore CSS styles for the entire node tree.
|
---|
221 | var editorElements = [ contents, container ];
|
---|
222 | for ( var i = 0 ; i < editorElements.length ; i++ )
|
---|
223 | {
|
---|
224 | restoreStyles( editorElements[i], editorElements[i].getCustomData( 'maximize_saved_styles' ) );
|
---|
225 | editorElements[i].removeCustomData( 'maximize_saved_styles' );
|
---|
226 | }
|
---|
227 |
|
---|
228 | currentNode = editor.container;
|
---|
229 | while ( ( currentNode = currentNode.getParent() ) )
|
---|
230 | {
|
---|
231 | restoreStyles( currentNode, currentNode.getCustomData( 'maximize_saved_styles' ) );
|
---|
232 | currentNode.removeCustomData( 'maximize_saved_styles' );
|
---|
233 | }
|
---|
234 |
|
---|
235 | // Restore the window scroll position.
|
---|
236 | CKEDITOR.env.ie ?
|
---|
237 | setTimeout( function() { mainWindow.$.scrollTo( outerScroll.x, outerScroll.y ); }, 0 ) :
|
---|
238 | mainWindow.$.scrollTo( outerScroll.x, outerScroll.y );
|
---|
239 |
|
---|
240 | // Remove cke_maximized class.
|
---|
241 | container.removeClass( 'cke_maximized' );
|
---|
242 |
|
---|
243 | // Emit a resize event, because this time the size is modified in
|
---|
244 | // restoreStyles.
|
---|
245 | editor.fire( 'resize' );
|
---|
246 | }
|
---|
247 |
|
---|
248 | this.toggleState();
|
---|
249 |
|
---|
250 | // Toggle button label.
|
---|
251 | var button = this.uiItems[ 0 ];
|
---|
252 | var label = ( this.state == CKEDITOR.TRISTATE_OFF )
|
---|
253 | ? lang.maximize : lang.minimize;
|
---|
254 | var buttonNode = editor.element.getDocument().getById( button._.id );
|
---|
255 | buttonNode.getChild( 1 ).setHtml( label );
|
---|
256 | buttonNode.setAttribute( 'title', label );
|
---|
257 | buttonNode.setAttribute( 'href', 'javascript:void("' + label + '");' );
|
---|
258 |
|
---|
259 | // Restore selection and scroll position in editing area.
|
---|
260 | if ( editor.mode == 'wysiwyg' )
|
---|
261 | {
|
---|
262 | if ( savedSelection )
|
---|
263 | {
|
---|
264 | // Fixing positioning editor chrome in Firefox break design mode. (#5149)
|
---|
265 | CKEDITOR.env.gecko && refreshCursor( editor );
|
---|
266 |
|
---|
267 | editor.getSelection().selectRanges(savedSelection);
|
---|
268 | var element = editor.getSelection().getStartElement();
|
---|
269 | element && element.scrollIntoView( true );
|
---|
270 | }
|
---|
271 |
|
---|
272 | else
|
---|
273 | mainWindow.$.scrollTo( savedScroll.x, savedScroll.y );
|
---|
274 | }
|
---|
275 | else
|
---|
276 | {
|
---|
277 | if ( savedSelection )
|
---|
278 | {
|
---|
279 | $textarea.selectionStart = savedSelection[0];
|
---|
280 | $textarea.selectionEnd = savedSelection[1];
|
---|
281 | }
|
---|
282 | $textarea.scrollLeft = savedScroll[0];
|
---|
283 | $textarea.scrollTop = savedScroll[1];
|
---|
284 | }
|
---|
285 |
|
---|
286 | savedSelection = savedScroll = null;
|
---|
287 | savedState = this.state;
|
---|
288 | },
|
---|
289 | canUndo : false
|
---|
290 | } );
|
---|
291 |
|
---|
292 | editor.ui.addButton( 'Maximize',
|
---|
293 | {
|
---|
294 | label : lang.maximize,
|
---|
295 | command : 'maximize'
|
---|
296 | } );
|
---|
297 |
|
---|
298 | // Restore the command state after mode change.
|
---|
299 | editor.on( 'mode', function()
|
---|
300 | {
|
---|
301 | editor.getCommand( 'maximize' ).setState( savedState );
|
---|
302 | }, null, null, 100 );
|
---|
303 | }
|
---|
304 | } );
|
---|
305 | })();
|
---|