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
28 var align = useComputedState ?
\r
29 element.getComputedStyle( 'text-align' ) :
\r
30 element.getStyle( 'text-align' ) || element.getAttribute( 'align' ) || '';
\r
32 align && ( align = align.replace( /-moz-|-webkit-|start|auto/i, '' ) );
\r
34 !align && useComputedState && ( align = element.getComputedStyle( 'direction' ) == 'rtl' ? 'right' : 'left' );
\r
39 function onSelectionChange( evt )
\r
41 var command = evt.editor.getCommand( this.name );
\r
42 command.state = getState.call( this, evt.editor, evt.data.path );
\r
43 command.fire( 'state' );
\r
46 function justifyCommand( editor, name, value )
\r
51 var classes = editor.config.justifyClasses;
\r
57 this.cssClassName = classes[0];
\r
60 this.cssClassName = classes[1];
\r
63 this.cssClassName = classes[2];
\r
66 this.cssClassName = classes[3];
\r
70 this.cssClassRegex = new RegExp( '(?:^|\\s+)(?:' + classes.join( '|' ) + ')(?=$|\\s)' );
\r
74 justifyCommand.prototype = {
\r
75 exec : function( editor )
\r
77 var selection = editor.getSelection(),
\r
78 enterMode = editor.config.enterMode;
\r
83 var bookmarks = selection.createBookmarks(),
\r
84 ranges = selection.getRanges( true );
\r
86 var cssClassName = this.cssClassName,
\r
90 var useComputedState = editor.config.useComputedState;
\r
91 useComputedState = useComputedState === undefined || useComputedState;
\r
93 for ( var i = ranges.length - 1 ; i >= 0 ; i-- )
\r
95 iterator = ranges[ i ].createIterator();
\r
96 iterator.enlargeBr = enterMode != CKEDITOR.ENTER_BR;
\r
98 while ( ( block = iterator.getNextParagraph() ) )
\r
100 block.removeAttribute( 'align' );
\r
101 block.removeStyle( 'text-align' );
\r
103 // Remove any of the alignment classes from the className.
\r
104 var className = cssClassName && ( block.$.className =
\r
105 CKEDITOR.tools.ltrim( block.$.className.replace( this.cssClassRegex, '' ) ) );
\r
108 ( this.state == CKEDITOR.TRISTATE_OFF ) &&
\r
109 ( !useComputedState || ( getAlignment( block, true ) != this.value ) );
\r
111 if ( cssClassName )
\r
113 // Append the desired class name.
\r
115 block.addClass( cssClassName );
\r
116 else if ( !className )
\r
117 block.removeAttribute( 'class' );
\r
120 block.setStyle( 'text-align', this.value );
\r
126 editor.forceNextSelectionCheck();
\r
127 selection.selectBookmarks( bookmarks );
\r
131 CKEDITOR.plugins.add( 'justify',
\r
133 init : function( editor )
\r
135 var left = new justifyCommand( editor, 'justifyleft', 'left' ),
\r
136 center = new justifyCommand( editor, 'justifycenter', 'center' ),
\r
137 right = new justifyCommand( editor, 'justifyright', 'right' ),
\r
138 justify = new justifyCommand( editor, 'justifyblock', 'justify' );
\r
140 editor.addCommand( 'justifyleft', left );
\r
141 editor.addCommand( 'justifycenter', center );
\r
142 editor.addCommand( 'justifyright', right );
\r
143 editor.addCommand( 'justifyblock', justify );
\r
145 editor.ui.addButton( 'JustifyLeft',
\r
147 label : editor.lang.justify.left,
\r
148 command : 'justifyleft'
\r
150 editor.ui.addButton( 'JustifyCenter',
\r
152 label : editor.lang.justify.center,
\r
153 command : 'justifycenter'
\r
155 editor.ui.addButton( 'JustifyRight',
\r
157 label : editor.lang.justify.right,
\r
158 command : 'justifyright'
\r
160 editor.ui.addButton( 'JustifyBlock',
\r
162 label : editor.lang.justify.block,
\r
163 command : 'justifyblock'
\r
166 editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, left ) );
\r
167 editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, right ) );
\r
168 editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, center ) );
\r
169 editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, justify ) );
\r
172 requires : [ 'domiterator' ]
\r
176 CKEDITOR.tools.extend( CKEDITOR.config,
\r
178 justifyClasses : null
\r