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

« back to all changes in this revision

Viewing changes to templates/lib/extra/ckeditor/plugins/justify/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
/**
 
7
 * @fileOverview Justify commands.
 
8
 */
 
9
 
 
10
( function() {
 
11
        function getAlignment( element, useComputedState ) {
 
12
                useComputedState = useComputedState === undefined || useComputedState;
 
13
 
 
14
                var align;
 
15
                if ( useComputedState )
 
16
                        align = element.getComputedStyle( 'text-align' );
 
17
                else {
 
18
                        while ( !element.hasAttribute || !( element.hasAttribute( 'align' ) || element.getStyle( 'text-align' ) ) ) {
 
19
                                var parent = element.getParent();
 
20
                                if ( !parent )
 
21
                                        break;
 
22
                                element = parent;
 
23
                        }
 
24
                        align = element.getStyle( 'text-align' ) || element.getAttribute( 'align' ) || '';
 
25
                }
 
26
 
 
27
                // Sometimes computed values doesn't tell.
 
28
                align && ( align = align.replace( /(?:-(?:moz|webkit)-)?(?:start|auto)/i, '' ) );
 
29
 
 
30
                !align && useComputedState && ( align = element.getComputedStyle( 'direction' ) == 'rtl' ? 'right' : 'left' );
 
31
 
 
32
                return align;
 
33
        }
 
34
 
 
35
        function justifyCommand( editor, name, value ) {
 
36
                this.editor = editor;
 
37
                this.name = name;
 
38
                this.value = value;
 
39
                this.context = 'p';
 
40
 
 
41
                var classes = editor.config.justifyClasses,
 
42
                        blockTag = editor.config.enterMode == CKEDITOR.ENTER_P ? 'p' : 'div';
 
43
 
 
44
                if ( classes ) {
 
45
                        switch ( value ) {
 
46
                                case 'left':
 
47
                                        this.cssClassName = classes[ 0 ];
 
48
                                        break;
 
49
                                case 'center':
 
50
                                        this.cssClassName = classes[ 1 ];
 
51
                                        break;
 
52
                                case 'right':
 
53
                                        this.cssClassName = classes[ 2 ];
 
54
                                        break;
 
55
                                case 'justify':
 
56
                                        this.cssClassName = classes[ 3 ];
 
57
                                        break;
 
58
                        }
 
59
 
 
60
                        this.cssClassRegex = new RegExp( '(?:^|\\s+)(?:' + classes.join( '|' ) + ')(?=$|\\s)' );
 
61
                        this.requiredContent = blockTag + '(' + this.cssClassName + ')';
 
62
                }
 
63
                else
 
64
                        this.requiredContent = blockTag + '{text-align}';
 
65
 
 
66
                this.allowedContent = {
 
67
                        'caption div h1 h2 h3 h4 h5 h6 p pre td th li': {
 
68
                                // Do not add elements, but only text-align style if element is validated by other rule.
 
69
                                propertiesOnly: true,
 
70
                                styles: this.cssClassName ? null : 'text-align',
 
71
                                classes: this.cssClassName || null
 
72
                        }
 
73
                };
 
74
 
 
75
                // In enter mode BR we need to allow here for div, because when non other
 
76
                // feature allows div justify is the only plugin that uses it.
 
77
                if ( editor.config.enterMode == CKEDITOR.ENTER_BR )
 
78
                        this.allowedContent.div = true;
 
79
        }
 
80
 
 
81
        function onDirChanged( e ) {
 
82
                var editor = e.editor;
 
83
 
 
84
                var range = editor.createRange();
 
85
                range.setStartBefore( e.data.node );
 
86
                range.setEndAfter( e.data.node );
 
87
 
 
88
                var walker = new CKEDITOR.dom.walker( range ),
 
89
                        node;
 
90
 
 
91
                while ( ( node = walker.next() ) ) {
 
92
                        if ( node.type == CKEDITOR.NODE_ELEMENT ) {
 
93
                                // A child with the defined dir is to be ignored.
 
94
                                if ( !node.equals( e.data.node ) && node.getDirection() ) {
 
95
                                        range.setStartAfter( node );
 
96
                                        walker = new CKEDITOR.dom.walker( range );
 
97
                                        continue;
 
98
                                }
 
99
 
 
100
                                // Switch the alignment.
 
101
                                var classes = editor.config.justifyClasses;
 
102
                                if ( classes ) {
 
103
                                        // The left align class.
 
104
                                        if ( node.hasClass( classes[ 0 ] ) ) {
 
105
                                                node.removeClass( classes[ 0 ] );
 
106
                                                node.addClass( classes[ 2 ] );
 
107
                                        }
 
108
                                        // The right align class.
 
109
                                        else if ( node.hasClass( classes[ 2 ] ) ) {
 
110
                                                node.removeClass( classes[ 2 ] );
 
111
                                                node.addClass( classes[ 0 ] );
 
112
                                        }
 
113
                                }
 
114
 
 
115
                                // Always switch CSS margins.
 
116
                                var style = 'text-align';
 
117
                                var align = node.getStyle( style );
 
118
 
 
119
                                if ( align == 'left' )
 
120
                                        node.setStyle( style, 'right' );
 
121
                                else if ( align == 'right' )
 
122
                                        node.setStyle( style, 'left' );
 
123
                        }
 
124
                }
 
125
        }
 
126
 
 
127
        justifyCommand.prototype = {
 
128
                exec: function( editor ) {
 
129
                        var selection = editor.getSelection(),
 
130
                                enterMode = editor.config.enterMode;
 
131
 
 
132
                        if ( !selection )
 
133
                                return;
 
134
 
 
135
                        var bookmarks = selection.createBookmarks(),
 
136
                                ranges = selection.getRanges();
 
137
 
 
138
                        var cssClassName = this.cssClassName,
 
139
                                iterator, block;
 
140
 
 
141
                        var useComputedState = editor.config.useComputedState;
 
142
                        useComputedState = useComputedState === undefined || useComputedState;
 
143
 
 
144
                        for ( var i = ranges.length - 1; i >= 0; i-- ) {
 
145
                                iterator = ranges[ i ].createIterator();
 
146
                                iterator.enlargeBr = enterMode != CKEDITOR.ENTER_BR;
 
147
 
 
148
                                while ( ( block = iterator.getNextParagraph( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) ) ) {
 
149
                                        if ( block.isReadOnly() )
 
150
                                                continue;
 
151
 
 
152
                                        block.removeAttribute( 'align' );
 
153
                                        block.removeStyle( 'text-align' );
 
154
 
 
155
                                        // Remove any of the alignment classes from the className.
 
156
                                        var className = cssClassName && ( block.$.className = CKEDITOR.tools.ltrim( block.$.className.replace( this.cssClassRegex, '' ) ) );
 
157
 
 
158
                                        var apply = ( this.state == CKEDITOR.TRISTATE_OFF ) && ( !useComputedState || ( getAlignment( block, true ) != this.value ) );
 
159
 
 
160
                                        if ( cssClassName ) {
 
161
                                                // Append the desired class name.
 
162
                                                if ( apply )
 
163
                                                        block.addClass( cssClassName );
 
164
                                                else if ( !className )
 
165
                                                        block.removeAttribute( 'class' );
 
166
                                        } else if ( apply )
 
167
                                                block.setStyle( 'text-align', this.value );
 
168
                                }
 
169
 
 
170
                        }
 
171
 
 
172
                        editor.focus();
 
173
                        editor.forceNextSelectionCheck();
 
174
                        selection.selectBookmarks( bookmarks );
 
175
                },
 
176
 
 
177
                refresh: function( editor, path ) {
 
178
                        var firstBlock = path.block || path.blockLimit;
 
179
 
 
180
                        this.setState( firstBlock.getName() != 'body' && getAlignment( firstBlock, this.editor.config.useComputedState ) == this.value ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF );
 
181
                }
 
182
        };
 
183
 
 
184
        CKEDITOR.plugins.add( 'justify', {
 
185
                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%
 
186
                icons: 'justifyblock,justifycenter,justifyleft,justifyright', // %REMOVE_LINE_CORE%
 
187
                hidpi: true, // %REMOVE_LINE_CORE%
 
188
                init: function( editor ) {
 
189
                        if ( editor.blockless )
 
190
                                return;
 
191
 
 
192
                        var left = new justifyCommand( editor, 'justifyleft', 'left' ),
 
193
                                center = new justifyCommand( editor, 'justifycenter', 'center' ),
 
194
                                right = new justifyCommand( editor, 'justifyright', 'right' ),
 
195
                                justify = new justifyCommand( editor, 'justifyblock', 'justify' );
 
196
 
 
197
                        editor.addCommand( 'justifyleft', left );
 
198
                        editor.addCommand( 'justifycenter', center );
 
199
                        editor.addCommand( 'justifyright', right );
 
200
                        editor.addCommand( 'justifyblock', justify );
 
201
 
 
202
                        if ( editor.ui.addButton ) {
 
203
                                editor.ui.addButton( 'JustifyLeft', {
 
204
                                        label: editor.lang.justify.left,
 
205
                                        command: 'justifyleft',
 
206
                                        toolbar: 'align,10'
 
207
                                } );
 
208
                                editor.ui.addButton( 'JustifyCenter', {
 
209
                                        label: editor.lang.justify.center,
 
210
                                        command: 'justifycenter',
 
211
                                        toolbar: 'align,20'
 
212
                                } );
 
213
                                editor.ui.addButton( 'JustifyRight', {
 
214
                                        label: editor.lang.justify.right,
 
215
                                        command: 'justifyright',
 
216
                                        toolbar: 'align,30'
 
217
                                } );
 
218
                                editor.ui.addButton( 'JustifyBlock', {
 
219
                                        label: editor.lang.justify.block,
 
220
                                        command: 'justifyblock',
 
221
                                        toolbar: 'align,40'
 
222
                                } );
 
223
                        }
 
224
 
 
225
                        editor.on( 'dirChanged', onDirChanged );
 
226
                }
 
227
        } );
 
228
} )();
 
229
 
 
230
/**
 
231
 * List of classes to use for aligning the contents. If it's `null`, no classes will be used
 
232
 * and instead the corresponding CSS values will be used.
 
233
 *
 
234
 * The array should contain 4 members, in the following order: left, center, right, justify.
 
235
 *
 
236
 *              // Use the classes 'AlignLeft', 'AlignCenter', 'AlignRight', 'AlignJustify'
 
237
 *              config.justifyClasses = [ 'AlignLeft', 'AlignCenter', 'AlignRight', 'AlignJustify' ];
 
238
 *
 
239
 * @cfg {Array} [justifyClasses=null]
 
240
 * @member CKEDITOR.config
 
241
 */