~ubuntu-branches/ubuntu/utopic/ldap-account-manager/utopic-proposed

« back to all changes in this revision

Viewing changes to templates/lib/extra/ckeditor/plugins/maximize/plugin.js

  • Committer: Package Import Robot
  • Author(s): Roland Gruber
  • Date: 2014-06-12 17:51:20 UTC
  • mfrom: (1.2.24)
  • Revision ID: package-import@ubuntu.com-20140612175120-grobhwyk369g9aod
Tags: 4.6-1
new upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @license Copyright (c) 2003-2014, CKSource - Frederico Knabben. All rights reserved.
 
3
 * For licensing, see LICENSE.md or http://ckeditor.com/license
 
4
 */
 
5
 
 
6
( function() {
 
7
        function protectFormStyles( formElement ) {
 
8
                if ( !formElement || formElement.type != CKEDITOR.NODE_ELEMENT || formElement.getName() != 'form' )
 
9
                        return [];
 
10
 
 
11
                var hijackRecord = [],
 
12
                        hijackNames = [ 'style', 'className' ];
 
13
                for ( var i = 0; i < hijackNames.length; i++ ) {
 
14
                        var name = hijackNames[ i ];
 
15
                        var $node = formElement.$.elements.namedItem( name );
 
16
                        if ( $node ) {
 
17
                                var hijackNode = new CKEDITOR.dom.element( $node );
 
18
                                hijackRecord.push( [ hijackNode, hijackNode.nextSibling ] );
 
19
                                hijackNode.remove();
 
20
                        }
 
21
                }
 
22
 
 
23
                return hijackRecord;
 
24
        }
 
25
 
 
26
        function restoreFormStyles( formElement, hijackRecord ) {
 
27
                if ( !formElement || formElement.type != CKEDITOR.NODE_ELEMENT || formElement.getName() != 'form' )
 
28
                        return;
 
29
 
 
30
                if ( hijackRecord.length > 0 ) {
 
31
                        for ( var i = hijackRecord.length - 1; i >= 0; i-- ) {
 
32
                                var node = hijackRecord[ i ][ 0 ];
 
33
                                var sibling = hijackRecord[ i ][ 1 ];
 
34
                                if ( sibling )
 
35
                                        node.insertBefore( sibling );
 
36
                                else
 
37
                                        node.appendTo( formElement );
 
38
                        }
 
39
                }
 
40
        }
 
41
 
 
42
        function saveStyles( element, isInsideEditor ) {
 
43
                var data = protectFormStyles( element );
 
44
                var retval = {};
 
45
 
 
46
                var $element = element.$;
 
47
 
 
48
                if ( !isInsideEditor ) {
 
49
                        retval[ 'class' ] = $element.className || '';
 
50
                        $element.className = '';
 
51
                }
 
52
 
 
53
                retval.inline = $element.style.cssText || '';
 
54
                if ( !isInsideEditor ) // Reset any external styles that might interfere. (#2474)
 
55
                $element.style.cssText = 'position: static; overflow: visible';
 
56
 
 
57
                restoreFormStyles( data );
 
58
                return retval;
 
59
        }
 
60
 
 
61
        function restoreStyles( element, savedStyles ) {
 
62
                var data = protectFormStyles( element );
 
63
                var $element = element.$;
 
64
                if ( 'class' in savedStyles )
 
65
                        $element.className = savedStyles[ 'class' ];
 
66
                if ( 'inline' in savedStyles )
 
67
                        $element.style.cssText = savedStyles.inline;
 
68
                restoreFormStyles( data );
 
69
        }
 
70
 
 
71
        function refreshCursor( editor ) {
 
72
                if ( editor.editable().isInline() )
 
73
                        return;
 
74
 
 
75
                // Refresh all editor instances on the page (#5724).
 
76
                var all = CKEDITOR.instances;
 
77
                for ( var i in all ) {
 
78
                        var one = all[ i ];
 
79
                        if ( one.mode == 'wysiwyg' && !one.readOnly ) {
 
80
                                var body = one.document.getBody();
 
81
                                // Refresh 'contentEditable' otherwise
 
82
                                // DOM lifting breaks design mode. (#5560)
 
83
                                body.setAttribute( 'contentEditable', false );
 
84
                                body.setAttribute( 'contentEditable', true );
 
85
                        }
 
86
                }
 
87
 
 
88
                if ( editor.editable().hasFocus ) {
 
89
                        editor.toolbox.focus();
 
90
                        editor.focus();
 
91
                }
 
92
        }
 
93
 
 
94
        CKEDITOR.plugins.add( 'maximize', {
 
95
                lang: 'af,ar,bg,bn,bs,ca,cs,cy,da,de,el,en,en-au,en-ca,en-gb,eo,es,et,eu,fa,fi,fo,fr,fr-ca,gl,gu,he,hi,hr,hu,id,is,it,ja,ka,km,ko,ku,lt,lv,mk,mn,ms,nb,nl,no,pl,pt,pt-br,ro,ru,si,sk,sl,sq,sr,sr-latn,sv,th,tr,ug,uk,vi,zh,zh-cn', // %REMOVE_LINE_CORE%
 
96
                icons: 'maximize', // %REMOVE_LINE_CORE%
 
97
                hidpi: true, // %REMOVE_LINE_CORE%
 
98
                init: function( editor ) {
 
99
                        // Maximize plugin isn't available in inline mode yet.
 
100
                        if ( editor.elementMode == CKEDITOR.ELEMENT_MODE_INLINE )
 
101
                                return;
 
102
 
 
103
                        var lang = editor.lang;
 
104
                        var mainDocument = CKEDITOR.document,
 
105
                                mainWindow = mainDocument.getWindow();
 
106
 
 
107
                        // Saved selection and scroll position for the editing area.
 
108
                        var savedSelection, savedScroll;
 
109
 
 
110
                        // Saved scroll position for the outer window.
 
111
                        var outerScroll;
 
112
 
 
113
                        // Saved resize handler function.
 
114
                        function resizeHandler() {
 
115
                                var viewPaneSize = mainWindow.getViewPaneSize();
 
116
                                editor.resize( viewPaneSize.width, viewPaneSize.height, null, true );
 
117
                        }
 
118
 
 
119
                        // Retain state after mode switches.
 
120
                        var savedState = CKEDITOR.TRISTATE_OFF;
 
121
 
 
122
                        editor.addCommand( 'maximize', {
 
123
                                // Disabled on iOS (#8307).
 
124
                                modes: { wysiwyg: !CKEDITOR.env.iOS, source: !CKEDITOR.env.iOS },
 
125
                                readOnly: 1,
 
126
                                editorFocus: false,
 
127
                                exec: function() {
 
128
                                        var container = editor.container.getChild( 1 );
 
129
                                        var contents = editor.ui.space( 'contents' );
 
130
 
 
131
                                        // Save current selection and scroll position in editing area.
 
132
                                        if ( editor.mode == 'wysiwyg' ) {
 
133
                                                var selection = editor.getSelection();
 
134
                                                savedSelection = selection && selection.getRanges();
 
135
                                                savedScroll = mainWindow.getScrollPosition();
 
136
                                        } else {
 
137
                                                var $textarea = editor.editable().$;
 
138
                                                savedSelection = !CKEDITOR.env.ie && [ $textarea.selectionStart, $textarea.selectionEnd ];
 
139
                                                savedScroll = [ $textarea.scrollLeft, $textarea.scrollTop ];
 
140
                                        }
 
141
 
 
142
                                        if ( this.state == CKEDITOR.TRISTATE_OFF ) // Go fullscreen if the state is off.
 
143
                                        {
 
144
                                                // Add event handler for resizing.
 
145
                                                mainWindow.on( 'resize', resizeHandler );
 
146
 
 
147
                                                // Save the scroll bar position.
 
148
                                                outerScroll = mainWindow.getScrollPosition();
 
149
 
 
150
                                                // Save and reset the styles for the entire node tree.
 
151
                                                var currentNode = editor.container;
 
152
                                                while ( ( currentNode = currentNode.getParent() ) ) {
 
153
                                                        currentNode.setCustomData( 'maximize_saved_styles', saveStyles( currentNode ) );
 
154
                                                        // Show under floatpanels (-1) and context menu (-2).
 
155
                                                        currentNode.setStyle( 'z-index', editor.config.baseFloatZIndex - 5 );
 
156
                                                }
 
157
                                                contents.setCustomData( 'maximize_saved_styles', saveStyles( contents, true ) );
 
158
                                                container.setCustomData( 'maximize_saved_styles', saveStyles( container, true ) );
 
159
 
 
160
                                                // Hide scroll bars.
 
161
                                                var styles = {
 
162
                                                        overflow: CKEDITOR.env.webkit ? '' : 'hidden', // #6896
 
163
                                                        width: 0,
 
164
                                                        height: 0
 
165
                                                };
 
166
 
 
167
                                                mainDocument.getDocumentElement().setStyles( styles );
 
168
                                                !CKEDITOR.env.gecko && mainDocument.getDocumentElement().setStyle( 'position', 'fixed' );
 
169
                                                !( CKEDITOR.env.gecko && CKEDITOR.env.quirks ) && mainDocument.getBody().setStyles( styles );
 
170
 
 
171
                                                // Scroll to the top left (IE needs some time for it - #4923).
 
172
                                                CKEDITOR.env.ie ? setTimeout( function() {
 
173
                                                        mainWindow.$.scrollTo( 0, 0 );
 
174
                                                }, 0 ) : mainWindow.$.scrollTo( 0, 0 );
 
175
 
 
176
                                                // Resize and move to top left.
 
177
                                                // Special treatment for FF Quirks (#7284)
 
178
                                                container.setStyle( 'position', CKEDITOR.env.gecko && CKEDITOR.env.quirks ? 'fixed' : 'absolute' );
 
179
                                                container.$.offsetLeft; // SAFARI BUG: See #2066.
 
180
                                                container.setStyles( {
 
181
                                                        // Show under floatpanels (-1) and context menu (-2).
 
182
                                                        'z-index': editor.config.baseFloatZIndex - 5,
 
183
                                                        left: '0px',
 
184
                                                        top: '0px'
 
185
                                                } );
 
186
 
 
187
                                                // Add cke_maximized class before resize handle since that will change things sizes (#5580)
 
188
                                                container.addClass( 'cke_maximized' );
 
189
 
 
190
                                                resizeHandler();
 
191
 
 
192
                                                // Still not top left? Fix it. (Bug #174)
 
193
                                                var offset = container.getDocumentPosition();
 
194
                                                container.setStyles( {
 
195
                                                        left: ( -1 * offset.x ) + 'px',
 
196
                                                        top: ( -1 * offset.y ) + 'px'
 
197
                                                } );
 
198
 
 
199
                                                // Fixing positioning editor chrome in Firefox break design mode. (#5149)
 
200
                                                CKEDITOR.env.gecko && refreshCursor( editor );
 
201
 
 
202
                                        } else if ( this.state == CKEDITOR.TRISTATE_ON ) // Restore from fullscreen if the state is on.
 
203
                                        {
 
204
                                                // Remove event handler for resizing.
 
205
                                                mainWindow.removeListener( 'resize', resizeHandler );
 
206
 
 
207
                                                // Restore CSS styles for the entire node tree.
 
208
                                                var editorElements = [ contents, container ];
 
209
                                                for ( var i = 0; i < editorElements.length; i++ ) {
 
210
                                                        restoreStyles( editorElements[ i ], editorElements[ i ].getCustomData( 'maximize_saved_styles' ) );
 
211
                                                        editorElements[ i ].removeCustomData( 'maximize_saved_styles' );
 
212
                                                }
 
213
 
 
214
                                                currentNode = editor.container;
 
215
                                                while ( ( currentNode = currentNode.getParent() ) ) {
 
216
                                                        restoreStyles( currentNode, currentNode.getCustomData( 'maximize_saved_styles' ) );
 
217
                                                        currentNode.removeCustomData( 'maximize_saved_styles' );
 
218
                                                }
 
219
 
 
220
                                                // Restore the window scroll position.
 
221
                                                CKEDITOR.env.ie ? setTimeout( function() {
 
222
                                                        mainWindow.$.scrollTo( outerScroll.x, outerScroll.y );
 
223
                                                }, 0 ) : mainWindow.$.scrollTo( outerScroll.x, outerScroll.y );
 
224
 
 
225
                                                // Remove cke_maximized class.
 
226
                                                container.removeClass( 'cke_maximized' );
 
227
 
 
228
                                                // Webkit requires a re-layout on editor chrome. (#6695)
 
229
                                                if ( CKEDITOR.env.webkit ) {
 
230
                                                        container.setStyle( 'display', 'inline' );
 
231
                                                        setTimeout( function() {
 
232
                                                                container.setStyle( 'display', 'block' );
 
233
                                                        }, 0 );
 
234
                                                }
 
235
 
 
236
                                                // Emit a resize event, because this time the size is modified in
 
237
                                                // restoreStyles.
 
238
                                                editor.fire( 'resize' );
 
239
                                        }
 
240
 
 
241
                                        this.toggleState();
 
242
 
 
243
                                        // Toggle button label.
 
244
                                        var button = this.uiItems[ 0 ];
 
245
                                        // Only try to change the button if it exists (#6166)
 
246
                                        if ( button ) {
 
247
                                                var label = ( this.state == CKEDITOR.TRISTATE_OFF ) ? lang.maximize.maximize : lang.maximize.minimize;
 
248
                                                var buttonNode = CKEDITOR.document.getById( button._.id );
 
249
                                                buttonNode.getChild( 1 ).setHtml( label );
 
250
                                                buttonNode.setAttribute( 'title', label );
 
251
                                                buttonNode.setAttribute( 'href', 'javascript:void("' + label + '");' );
 
252
                                        }
 
253
 
 
254
                                        // Restore selection and scroll position in editing area.
 
255
                                        if ( editor.mode == 'wysiwyg' ) {
 
256
                                                if ( savedSelection ) {
 
257
                                                        // Fixing positioning editor chrome in Firefox break design mode. (#5149)
 
258
                                                        CKEDITOR.env.gecko && refreshCursor( editor );
 
259
 
 
260
                                                        editor.getSelection().selectRanges( savedSelection );
 
261
                                                        var element = editor.getSelection().getStartElement();
 
262
                                                        element && element.scrollIntoView( true );
 
263
                                                } else
 
264
                                                        mainWindow.$.scrollTo( savedScroll.x, savedScroll.y );
 
265
                                        } else {
 
266
                                                if ( savedSelection ) {
 
267
                                                        $textarea.selectionStart = savedSelection[ 0 ];
 
268
                                                        $textarea.selectionEnd = savedSelection[ 1 ];
 
269
                                                }
 
270
                                                $textarea.scrollLeft = savedScroll[ 0 ];
 
271
                                                $textarea.scrollTop = savedScroll[ 1 ];
 
272
                                        }
 
273
 
 
274
                                        savedSelection = savedScroll = null;
 
275
                                        savedState = this.state;
 
276
 
 
277
                                        editor.fire( 'maximize', this.state );
 
278
                                },
 
279
                                canUndo: false
 
280
                        } );
 
281
 
 
282
                        editor.ui.addButton && editor.ui.addButton( 'Maximize', {
 
283
                                label: lang.maximize.maximize,
 
284
                                command: 'maximize',
 
285
                                toolbar: 'tools,10'
 
286
                        } );
 
287
 
 
288
                        // Restore the command state after mode change, unless it has been changed to disabled (#6467)
 
289
                        editor.on( 'mode', function() {
 
290
                                var command = editor.getCommand( 'maximize' );
 
291
                                command.setState( command.state == CKEDITOR.TRISTATE_DISABLED ? CKEDITOR.TRISTATE_DISABLED : savedState );
 
292
                        }, null, null, 100 );
 
293
                }
 
294
        } );
 
295
} )();
 
296
 
 
297
/**
 
298
 * Event fired when the maximize command is called.
 
299
 * It also indicates whether an editor is maximized or not.
 
300
 *
 
301
 * @event maximize
 
302
 * @member CKEDITOR.editor
 
303
 * @param {CKEDITOR.editor} editor This editor instance.
 
304
 * @param {Number} data Current state of the command. See {@link CKEDITOR#TRISTATE_ON} and {@link CKEDITOR#TRISTATE_OFF}.
 
305
 */