2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
7 * @file Justify commands.
\r
12 function getState( editor, path )
\r
14 var firstBlock = path.block || path.blockLimit;
\r
16 if ( !firstBlock || firstBlock.getName() == 'body' )
\r
17 return CKEDITOR.TRISTATE_OFF;
\r
19 return ( getAlignment( firstBlock, editor.config.useComputedState ) == this.value ) ?
\r
20 CKEDITOR.TRISTATE_ON :
\r
21 CKEDITOR.TRISTATE_OFF;
\r
24 function getAlignment( element, useComputedState )
\r
26 useComputedState = useComputedState === undefined || useComputedState;
\r
29 if ( useComputedState )
\r
30 align = element.getComputedStyle( 'text-align' );
\r
33 while ( !element.hasAttribute || !( element.hasAttribute( 'align' ) || element.getStyle( 'text-align' ) ) )
\r
35 var parent = element.getParent();
\r
40 align = element.getStyle( 'text-align' ) || element.getAttribute( 'align' ) || '';
\r
43 align && ( align = align.replace( /-moz-|-webkit-|start|auto/i, '' ) );
\r
45 !align && useComputedState && ( align = element.getComputedStyle( 'direction' ) == 'rtl' ? 'right' : 'left' );
\r
50 function onSelectionChange( evt )
\r
52 if ( evt.editor.readOnly )
\r
55 var command = evt.editor.getCommand( this.name );
\r
56 command.state = getState.call( this, evt.editor, evt.data.path );
\r
57 command.fire( 'state' );
\r
60 function justifyCommand( editor, name, value )
\r
65 var classes = editor.config.justifyClasses;
\r
71 this.cssClassName = classes[0];
\r
74 this.cssClassName = classes[1];
\r
77 this.cssClassName = classes[2];
\r
80 this.cssClassName = classes[3];
\r
84 this.cssClassRegex = new RegExp( '(?:^|\\s+)(?:' + classes.join( '|' ) + ')(?=$|\\s)' );
\r
88 function onDirChanged( e )
\r
90 var editor = e.editor;
\r
92 var range = new CKEDITOR.dom.range( editor.document );
\r
93 range.setStartBefore( e.data.node );
\r
94 range.setEndAfter( e.data.node );
\r
96 var walker = new CKEDITOR.dom.walker( range ),
\r
99 while ( ( node = walker.next() ) )
\r
101 if ( node.type == CKEDITOR.NODE_ELEMENT )
\r
103 // A child with the defined dir is to be ignored.
\r
104 if ( !node.equals( e.data.node ) && node.getDirection() )
\r
106 range.setStartAfter( node );
\r
107 walker = new CKEDITOR.dom.walker( range );
\r
111 // Switch the alignment.
\r
112 var classes = editor.config.justifyClasses;
\r
115 // The left align class.
\r
116 if ( node.hasClass( classes[ 0 ] ) )
\r
118 node.removeClass( classes[ 0 ] );
\r
119 node.addClass( classes[ 2 ] );
\r
121 // The right align class.
\r
122 else if ( node.hasClass( classes[ 2 ] ) )
\r
124 node.removeClass( classes[ 2 ] );
\r
125 node.addClass( classes[ 0 ] );
\r
129 // Always switch CSS margins.
\r
130 var style = 'text-align';
\r
131 var align = node.getStyle( style );
\r
133 if ( align == 'left' )
\r
134 node.setStyle( style, 'right' );
\r
135 else if ( align == 'right' )
\r
136 node.setStyle( style, 'left' );
\r
141 justifyCommand.prototype = {
\r
142 exec : function( editor )
\r
144 var selection = editor.getSelection(),
\r
145 enterMode = editor.config.enterMode;
\r
150 var bookmarks = selection.createBookmarks(),
\r
151 ranges = selection.getRanges( true );
\r
153 var cssClassName = this.cssClassName,
\r
157 var useComputedState = editor.config.useComputedState;
\r
158 useComputedState = useComputedState === undefined || useComputedState;
\r
160 for ( var i = ranges.length - 1 ; i >= 0 ; i-- )
\r
162 iterator = ranges[ i ].createIterator();
\r
163 iterator.enlargeBr = enterMode != CKEDITOR.ENTER_BR;
\r
165 while ( ( block = iterator.getNextParagraph( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) ) )
\r
167 block.removeAttribute( 'align' );
\r
168 block.removeStyle( 'text-align' );
\r
170 // Remove any of the alignment classes from the className.
\r
171 var className = cssClassName && ( block.$.className =
\r
172 CKEDITOR.tools.ltrim( block.$.className.replace( this.cssClassRegex, '' ) ) );
\r
175 ( this.state == CKEDITOR.TRISTATE_OFF ) &&
\r
176 ( !useComputedState || ( getAlignment( block, true ) != this.value ) );
\r
178 if ( cssClassName )
\r
180 // Append the desired class name.
\r
182 block.addClass( cssClassName );
\r
183 else if ( !className )
\r
184 block.removeAttribute( 'class' );
\r
187 block.setStyle( 'text-align', this.value );
\r
193 editor.forceNextSelectionCheck();
\r
194 selection.selectBookmarks( bookmarks );
\r
198 CKEDITOR.plugins.add( 'justify',
\r
200 init : function( editor )
\r
202 var left = new justifyCommand( editor, 'justifyleft', 'left' ),
\r
203 center = new justifyCommand( editor, 'justifycenter', 'center' ),
\r
204 right = new justifyCommand( editor, 'justifyright', 'right' ),
\r
205 justify = new justifyCommand( editor, 'justifyblock', 'justify' );
\r
207 editor.addCommand( 'justifyleft', left );
\r
208 editor.addCommand( 'justifycenter', center );
\r
209 editor.addCommand( 'justifyright', right );
\r
210 editor.addCommand( 'justifyblock', justify );
\r
212 editor.ui.addButton( 'JustifyLeft',
\r
214 label : editor.lang.justify.left,
\r
215 command : 'justifyleft'
\r
217 editor.ui.addButton( 'JustifyCenter',
\r
219 label : editor.lang.justify.center,
\r
220 command : 'justifycenter'
\r
222 editor.ui.addButton( 'JustifyRight',
\r
224 label : editor.lang.justify.right,
\r
225 command : 'justifyright'
\r
227 editor.ui.addButton( 'JustifyBlock',
\r
229 label : editor.lang.justify.block,
\r
230 command : 'justifyblock'
\r
233 editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, left ) );
\r
234 editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, right ) );
\r
235 editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, center ) );
\r
236 editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, justify ) );
\r
237 editor.on( 'dirChanged', onDirChanged );
\r
240 requires : [ 'domiterator' ]
\r
245 * List of classes to use for aligning the contents. If it's null, no classes will be used
\r
246 * and instead the corresponding CSS values will be used. The array should contain 4 members, in the following order: left, center, right, justify.
\r
247 * @name CKEDITOR.config.justifyClasses
\r
251 * // Use the classes 'AlignLeft', 'AlignCenter', 'AlignRight', 'AlignJustify'
\r
252 * config.justifyClasses = [ 'AlignLeft', 'AlignCenter', 'AlignRight', 'AlignJustify' ];
\r