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 justifyCommand.prototype = {
\r
86 exec : function( editor )
\r
88 var selection = editor.getSelection(),
\r
89 enterMode = editor.config.enterMode;
\r
94 var bookmarks = selection.createBookmarks(),
\r
95 ranges = selection.getRanges( true );
\r
97 var cssClassName = this.cssClassName,
\r
101 var useComputedState = editor.config.useComputedState;
\r
102 useComputedState = useComputedState === undefined || useComputedState;
\r
104 for ( var i = ranges.length - 1 ; i >= 0 ; i-- )
\r
106 iterator = ranges[ i ].createIterator();
\r
107 iterator.enlargeBr = enterMode != CKEDITOR.ENTER_BR;
\r
109 while ( ( block = iterator.getNextParagraph() ) )
\r
111 block.removeAttribute( 'align' );
\r
112 block.removeStyle( 'text-align' );
\r
114 // Remove any of the alignment classes from the className.
\r
115 var className = cssClassName && ( block.$.className =
\r
116 CKEDITOR.tools.ltrim( block.$.className.replace( this.cssClassRegex, '' ) ) );
\r
119 ( this.state == CKEDITOR.TRISTATE_OFF ) &&
\r
120 ( !useComputedState || ( getAlignment( block, true ) != this.value ) );
\r
122 if ( cssClassName )
\r
124 // Append the desired class name.
\r
126 block.addClass( cssClassName );
\r
127 else if ( !className )
\r
128 block.removeAttribute( 'class' );
\r
131 block.setStyle( 'text-align', this.value );
\r
137 editor.forceNextSelectionCheck();
\r
138 selection.selectBookmarks( bookmarks );
\r
142 CKEDITOR.plugins.add( 'justify',
\r
144 init : function( editor )
\r
146 var left = new justifyCommand( editor, 'justifyleft', 'left' ),
\r
147 center = new justifyCommand( editor, 'justifycenter', 'center' ),
\r
148 right = new justifyCommand( editor, 'justifyright', 'right' ),
\r
149 justify = new justifyCommand( editor, 'justifyblock', 'justify' );
\r
151 editor.addCommand( 'justifyleft', left );
\r
152 editor.addCommand( 'justifycenter', center );
\r
153 editor.addCommand( 'justifyright', right );
\r
154 editor.addCommand( 'justifyblock', justify );
\r
156 editor.ui.addButton( 'JustifyLeft',
\r
158 label : editor.lang.justify.left,
\r
159 command : 'justifyleft'
\r
161 editor.ui.addButton( 'JustifyCenter',
\r
163 label : editor.lang.justify.center,
\r
164 command : 'justifycenter'
\r
166 editor.ui.addButton( 'JustifyRight',
\r
168 label : editor.lang.justify.right,
\r
169 command : 'justifyright'
\r
171 editor.ui.addButton( 'JustifyBlock',
\r
173 label : editor.lang.justify.block,
\r
174 command : 'justifyblock'
\r
177 editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, left ) );
\r
178 editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, right ) );
\r
179 editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, center ) );
\r
180 editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, justify ) );
\r
183 requires : [ 'domiterator' ]
\r
187 CKEDITOR.tools.extend( CKEDITOR.config,
\r
189 justifyClasses : null
\r