2 Copyright (c) 2003-2010, 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 );
\r
91 range.setEndAfter( e.data );
\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.getDirection() )
\r
103 range.setStartAfter( node );
\r
104 walker = new CKEDITOR.dom.walker( range );
\r
108 // Switch the alignment.
\r
109 var style = 'text-align';
\r
110 var align = node.getStyle( style );
\r
112 if ( align == 'left' )
\r
113 node.setStyle( style, 'right' );
\r
114 else if ( align == 'right' )
\r
115 node.setStyle( style, 'left' );
\r
120 justifyCommand.prototype = {
\r
121 exec : function( editor )
\r
123 var selection = editor.getSelection(),
\r
124 enterMode = editor.config.enterMode;
\r
129 var bookmarks = selection.createBookmarks(),
\r
130 ranges = selection.getRanges( true );
\r
132 var cssClassName = this.cssClassName,
\r
136 var useComputedState = editor.config.useComputedState;
\r
137 useComputedState = useComputedState === undefined || useComputedState;
\r
139 for ( var i = ranges.length - 1 ; i >= 0 ; i-- )
\r
141 iterator = ranges[ i ].createIterator();
\r
142 iterator.enlargeBr = enterMode != CKEDITOR.ENTER_BR;
\r
144 while ( ( block = iterator.getNextParagraph() ) )
\r
146 block.removeAttribute( 'align' );
\r
147 block.removeStyle( 'text-align' );
\r
149 // Remove any of the alignment classes from the className.
\r
150 var className = cssClassName && ( block.$.className =
\r
151 CKEDITOR.tools.ltrim( block.$.className.replace( this.cssClassRegex, '' ) ) );
\r
154 ( this.state == CKEDITOR.TRISTATE_OFF ) &&
\r
155 ( !useComputedState || ( getAlignment( block, true ) != this.value ) );
\r
157 if ( cssClassName )
\r
159 // Append the desired class name.
\r
161 block.addClass( cssClassName );
\r
162 else if ( !className )
\r
163 block.removeAttribute( 'class' );
\r
166 block.setStyle( 'text-align', this.value );
\r
172 editor.forceNextSelectionCheck();
\r
173 selection.selectBookmarks( bookmarks );
\r
177 CKEDITOR.plugins.add( 'justify',
\r
179 init : function( editor )
\r
181 var left = new justifyCommand( editor, 'justifyleft', 'left' ),
\r
182 center = new justifyCommand( editor, 'justifycenter', 'center' ),
\r
183 right = new justifyCommand( editor, 'justifyright', 'right' ),
\r
184 justify = new justifyCommand( editor, 'justifyblock', 'justify' );
\r
186 editor.addCommand( 'justifyleft', left );
\r
187 editor.addCommand( 'justifycenter', center );
\r
188 editor.addCommand( 'justifyright', right );
\r
189 editor.addCommand( 'justifyblock', justify );
\r
191 editor.ui.addButton( 'JustifyLeft',
\r
193 label : editor.lang.justify.left,
\r
194 command : 'justifyleft'
\r
196 editor.ui.addButton( 'JustifyCenter',
\r
198 label : editor.lang.justify.center,
\r
199 command : 'justifycenter'
\r
201 editor.ui.addButton( 'JustifyRight',
\r
203 label : editor.lang.justify.right,
\r
204 command : 'justifyright'
\r
206 editor.ui.addButton( 'JustifyBlock',
\r
208 label : editor.lang.justify.block,
\r
209 command : 'justifyblock'
\r
212 editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, left ) );
\r
213 editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, right ) );
\r
214 editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, center ) );
\r
215 editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, justify ) );
\r
216 editor.on( 'dirChanged', onDirChanged );
\r
219 requires : [ 'domiterator' ]
\r