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 var command = evt.editor.getCommand( this.name );
\r
53 command.state = getState.call( this, evt.editor, evt.data.path );
\r
54 command.fire( 'state' );
\r
57 function justifyCommand( editor, name, value )
\r
62 var classes = editor.config.justifyClasses;
\r
68 this.cssClassName = classes[0];
\r
71 this.cssClassName = classes[1];
\r
74 this.cssClassName = classes[2];
\r
77 this.cssClassName = classes[3];
\r
81 this.cssClassRegex = new RegExp( '(?:^|\\s+)(?:' + classes.join( '|' ) + ')(?=$|\\s)' );
\r
85 function onDirChanged( e )
\r
87 var editor = e.editor;
\r
89 var range = new CKEDITOR.dom.range( editor.document );
\r
90 range.setStartBefore( e.data.node );
\r
91 range.setEndAfter( e.data.node );
\r
93 var walker = new CKEDITOR.dom.walker( range ),
\r
96 while ( ( node = walker.next() ) )
\r
98 if ( node.type == CKEDITOR.NODE_ELEMENT )
\r
100 // A child with the defined dir is to be ignored.
\r
101 if ( !node.equals( e.data.node ) && node.getDirection() )
\r
103 range.setStartAfter( node );
\r
104 walker = new CKEDITOR.dom.walker( range );
\r
108 // Switch the alignment.
\r
109 var classes = editor.config.justifyClasses;
\r
112 // The left align class.
\r
113 if ( node.hasClass( classes[ 0 ] ) )
\r
115 node.removeClass( classes[ 0 ] );
\r
116 node.addClass( classes[ 2 ] );
\r
118 // The right align class.
\r
119 else if ( node.hasClass( classes[ 2 ] ) )
\r
121 node.removeClass( classes[ 2 ] );
\r
122 node.addClass( classes[ 0 ] );
\r
126 // Always switch CSS margins.
\r
127 var style = 'text-align';
\r
128 var align = node.getStyle( style );
\r
130 if ( align == 'left' )
\r
131 node.setStyle( style, 'right' );
\r
132 else if ( align == 'right' )
\r
133 node.setStyle( style, 'left' );
\r
138 justifyCommand.prototype = {
\r
139 exec : function( editor )
\r
141 var selection = editor.getSelection(),
\r
142 enterMode = editor.config.enterMode;
\r
147 var bookmarks = selection.createBookmarks(),
\r
148 ranges = selection.getRanges( true );
\r
150 var cssClassName = this.cssClassName,
\r
154 var useComputedState = editor.config.useComputedState;
\r
155 useComputedState = useComputedState === undefined || useComputedState;
\r
157 for ( var i = ranges.length - 1 ; i >= 0 ; i-- )
\r
159 iterator = ranges[ i ].createIterator();
\r
160 iterator.enlargeBr = enterMode != CKEDITOR.ENTER_BR;
\r
162 while ( ( block = iterator.getNextParagraph( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) ) )
\r
164 block.removeAttribute( 'align' );
\r
165 block.removeStyle( 'text-align' );
\r
167 // Remove any of the alignment classes from the className.
\r
168 var className = cssClassName && ( block.$.className =
\r
169 CKEDITOR.tools.ltrim( block.$.className.replace( this.cssClassRegex, '' ) ) );
\r
172 ( this.state == CKEDITOR.TRISTATE_OFF ) &&
\r
173 ( !useComputedState || ( getAlignment( block, true ) != this.value ) );
\r
175 if ( cssClassName )
\r
177 // Append the desired class name.
\r
179 block.addClass( cssClassName );
\r
180 else if ( !className )
\r
181 block.removeAttribute( 'class' );
\r
184 block.setStyle( 'text-align', this.value );
\r
190 editor.forceNextSelectionCheck();
\r
191 selection.selectBookmarks( bookmarks );
\r
195 CKEDITOR.plugins.add( 'justify',
\r
197 init : function( editor )
\r
199 var left = new justifyCommand( editor, 'justifyleft', 'left' ),
\r
200 center = new justifyCommand( editor, 'justifycenter', 'center' ),
\r
201 right = new justifyCommand( editor, 'justifyright', 'right' ),
\r
202 justify = new justifyCommand( editor, 'justifyblock', 'justify' );
\r
204 editor.addCommand( 'justifyleft', left );
\r
205 editor.addCommand( 'justifycenter', center );
\r
206 editor.addCommand( 'justifyright', right );
\r
207 editor.addCommand( 'justifyblock', justify );
\r
209 editor.ui.addButton( 'JustifyLeft',
\r
211 label : editor.lang.justify.left,
\r
212 command : 'justifyleft'
\r
214 editor.ui.addButton( 'JustifyCenter',
\r
216 label : editor.lang.justify.center,
\r
217 command : 'justifycenter'
\r
219 editor.ui.addButton( 'JustifyRight',
\r
221 label : editor.lang.justify.right,
\r
222 command : 'justifyright'
\r
224 editor.ui.addButton( 'JustifyBlock',
\r
226 label : editor.lang.justify.block,
\r
227 command : 'justifyblock'
\r
230 editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, left ) );
\r
231 editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, right ) );
\r
232 editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, center ) );
\r
233 editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, justify ) );
\r
234 editor.on( 'dirChanged', onDirChanged );
\r
237 requires : [ 'domiterator' ]
\r