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 | (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 | CKEDITOR.plugins.add( 'maximize',
|
---|
91 | {
|
---|
92 | init : function( editor )
|
---|
93 | {
|
---|
94 | var lang = editor.lang;
|
---|
95 | var mainDocument = CKEDITOR.document;
|
---|
96 | var mainWindow = mainDocument.getWindow();
|
---|
97 |
|
---|
98 | // Saved selection and scroll position for the editing area.
|
---|
99 | var savedSelection;
|
---|
100 | var savedScroll;
|
---|
101 |
|
---|
102 | // Saved scroll position for the outer window.
|
---|
103 | var outerScroll;
|
---|
104 |
|
---|
105 | // Saved resize handler function.
|
---|
106 | var resizeHandler = getResizeHandler( mainWindow, editor );
|
---|
107 |
|
---|
108 | // Retain state after mode switches.
|
---|
109 | var savedState = CKEDITOR.TRISTATE_OFF;
|
---|
110 |
|
---|
111 | editor.addCommand( 'maximize',
|
---|
112 | {
|
---|
113 | modes : { wysiwyg : 1, source : 1 },
|
---|
114 | editorFocus : false,
|
---|
115 | exec : function()
|
---|
116 | {
|
---|
117 | var container = editor.container.getChild( [ 0, 0 ] );
|
---|
118 | var contents = editor.getThemeSpace( 'contents' );
|
---|
119 |
|
---|
120 | // Save current selection and scroll position in editing area.
|
---|
121 | if ( editor.mode == 'wysiwyg' )
|
---|
122 | {
|
---|
123 | var selection = editor.getSelection();
|
---|
124 | savedSelection = selection && selection.getRanges();
|
---|
125 | savedScroll = mainWindow.getScrollPosition();
|
---|
126 | }
|
---|
127 | else
|
---|
128 | {
|
---|
129 | var $textarea = editor.textarea.$;
|
---|
130 | savedSelection = !CKEDITOR.env.ie && [ $textarea.selectionStart, $textarea.selectionEnd ];
|
---|
131 | savedScroll = [ $textarea.scrollLeft, $textarea.scrollTop ];
|
---|
132 | }
|
---|
133 |
|
---|
134 | if ( this.state == CKEDITOR.TRISTATE_OFF ) // Go fullscreen if the state is off.
|
---|
135 | {
|
---|
136 | // Add event handler for resizing.
|
---|
137 | mainWindow.on( 'resize', resizeHandler );
|
---|
138 |
|
---|
139 | // Save the scroll bar position.
|
---|
140 | outerScroll = mainWindow.getScrollPosition();
|
---|
141 |
|
---|
142 | // Save and reset the styles for the entire node tree.
|
---|
143 | var currentNode = editor.container;
|
---|
144 | while ( ( currentNode = currentNode.getParent() ) )
|
---|
145 | {
|
---|
146 | currentNode.setCustomData( 'maximize_saved_styles', saveStyles( currentNode ) );
|
---|
147 | currentNode.setStyle( 'z-index', editor.config.baseFloatZIndex - 1 );
|
---|
148 | }
|
---|
149 | contents.setCustomData( 'maximize_saved_styles', saveStyles( contents, true ) );
|
---|
150 | container.setCustomData( 'maximize_saved_styles', saveStyles( container, true ) );
|
---|
151 |
|
---|
152 | // Hide scroll bars.
|
---|
153 | if ( CKEDITOR.env.ie )
|
---|
154 | {
|
---|
155 | mainDocument.$.documentElement.style.overflow =
|
---|
156 | mainDocument.getBody().$.style.overflow = 'hidden';
|
---|
157 | }
|
---|
158 | else
|
---|
159 | {
|
---|
160 | mainDocument.getBody().setStyles(
|
---|
161 | {
|
---|
162 | overflow : 'hidden',
|
---|
163 | width : '0px',
|
---|
164 | height : '0px'
|
---|
165 | } );
|
---|
166 | }
|
---|
167 |
|
---|
168 | // Scroll to the top left.
|
---|
169 | mainWindow.$.scrollTo( 0, 0 );
|
---|
170 |
|
---|
171 | // Resize and move to top left.
|
---|
172 | var viewPaneSize = mainWindow.getViewPaneSize();
|
---|
173 | container.setStyle( 'position', 'absolute' );
|
---|
174 | container.$.offsetLeft; // SAFARI BUG: See #2066.
|
---|
175 | container.setStyles(
|
---|
176 | {
|
---|
177 | 'z-index' : editor.config.baseFloatZIndex - 1,
|
---|
178 | left : '0px',
|
---|
179 | top : '0px'
|
---|
180 | } );
|
---|
181 | editor.resize( viewPaneSize.width, viewPaneSize.height, null, true );
|
---|
182 |
|
---|
183 | // Still not top left? Fix it. (Bug #174)
|
---|
184 | var offset = container.getDocumentPosition();
|
---|
185 | container.setStyles(
|
---|
186 | {
|
---|
187 | left : ( -1 * offset.x ) + 'px',
|
---|
188 | top : ( -1 * offset.y ) + 'px'
|
---|
189 | } );
|
---|
190 |
|
---|
191 | // Add cke_maximized class.
|
---|
192 | container.addClass( 'cke_maximized' );
|
---|
193 | }
|
---|
194 | else if ( this.state == CKEDITOR.TRISTATE_ON ) // Restore from fullscreen if the state is on.
|
---|
195 | {
|
---|
196 | // Remove event handler for resizing.
|
---|
197 | mainWindow.removeListener( 'resize', resizeHandler );
|
---|
198 |
|
---|
199 | // Restore CSS styles for the entire node tree.
|
---|
200 | var editorElements = [ contents, container ];
|
---|
201 | for ( var i = 0 ; i < editorElements.length ; i++ )
|
---|
202 | {
|
---|
203 | restoreStyles( editorElements[i], editorElements[i].getCustomData( 'maximize_saved_styles' ) );
|
---|
204 | editorElements[i].removeCustomData( 'maximize_saved_styles' );
|
---|
205 | }
|
---|
206 |
|
---|
207 | currentNode = editor.container;
|
---|
208 | while ( ( currentNode = currentNode.getParent() ) )
|
---|
209 | {
|
---|
210 | restoreStyles( currentNode, currentNode.getCustomData( 'maximize_saved_styles' ) );
|
---|
211 | currentNode.removeCustomData( 'maximize_saved_styles' );
|
---|
212 | }
|
---|
213 |
|
---|
214 | // Restore the window scroll position.
|
---|
215 | mainWindow.$.scrollTo( outerScroll.x, outerScroll.y );
|
---|
216 |
|
---|
217 | // Remove cke_maximized class.
|
---|
218 | container.removeClass( 'cke_maximized' );
|
---|
219 |
|
---|
220 | // Emit a resize event, because this time the size is modified in
|
---|
221 | // restoreStyles.
|
---|
222 | editor.fire( 'resize' );
|
---|
223 | }
|
---|
224 |
|
---|
225 | this.toggleState();
|
---|
226 |
|
---|
227 | // Toggle button label.
|
---|
228 | var button = this.uiItems[ 0 ];
|
---|
229 | var label = ( this.state == CKEDITOR.TRISTATE_OFF )
|
---|
230 | ? lang.maximize : lang.minimize;
|
---|
231 | var buttonNode = editor.element.getDocument().getById( button._.id );
|
---|
232 | buttonNode.getChild( 1 ).setHtml( label );
|
---|
233 | buttonNode.setAttribute( 'title', label );
|
---|
234 | buttonNode.setAttribute( 'href', 'javascript:void("' + label + '");' );
|
---|
235 |
|
---|
236 | // Restore selection and scroll position in editing area.
|
---|
237 | if ( editor.mode == 'wysiwyg' )
|
---|
238 | {
|
---|
239 | if ( savedSelection )
|
---|
240 | {
|
---|
241 | editor.getSelection().selectRanges(savedSelection);
|
---|
242 | var element = editor.getSelection().getStartElement();
|
---|
243 | element && element.scrollIntoView( true );
|
---|
244 | }
|
---|
245 |
|
---|
246 | else
|
---|
247 | mainWindow.$.scrollTo( savedScroll.x, savedScroll.y );
|
---|
248 | }
|
---|
249 | else
|
---|
250 | {
|
---|
251 | if ( savedSelection )
|
---|
252 | {
|
---|
253 | $textarea.selectionStart = savedSelection[0];
|
---|
254 | $textarea.selectionEnd = savedSelection[1];
|
---|
255 | }
|
---|
256 | $textarea.scrollLeft = savedScroll[0];
|
---|
257 | $textarea.scrollTop = savedScroll[1];
|
---|
258 | }
|
---|
259 |
|
---|
260 | savedSelection = savedScroll = null;
|
---|
261 | savedState = this.state;
|
---|
262 | },
|
---|
263 | canUndo : false
|
---|
264 | } );
|
---|
265 |
|
---|
266 | editor.ui.addButton( 'Maximize',
|
---|
267 | {
|
---|
268 | label : lang.maximize,
|
---|
269 | command : 'maximize'
|
---|
270 | } );
|
---|
271 |
|
---|
272 | // Restore the command state after mode change.
|
---|
273 | editor.on( 'mode', function()
|
---|
274 | {
|
---|
275 | editor.getCommand( 'maximize' ).setState( savedState );
|
---|
276 | }, null, null, 100 );
|
---|
277 | }
|
---|
278 | } );
|
---|
279 | })();
|
---|