2 Copyright (c) 2003-2009, 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 var alignRemoveRegex = /(-moz-|-webkit-|start|auto)/i;
\r
14 function getState( editor, path )
\r
16 var firstBlock = path.block || path.blockLimit;
\r
18 if ( !firstBlock || firstBlock.getName() == 'body' )
\r
19 return CKEDITOR.TRISTATE_OFF;
\r
21 var currentAlign = firstBlock.getComputedStyle( 'text-align' ).replace( alignRemoveRegex, '' );
\r
22 if ( ( !currentAlign && this.isDefaultAlign ) || currentAlign == this.value )
\r
23 return CKEDITOR.TRISTATE_ON;
\r
24 return CKEDITOR.TRISTATE_OFF;
\r
27 function onSelectionChange( evt )
\r
29 var command = evt.editor.getCommand( this.name );
\r
30 command.state = getState.call( this, evt.editor, evt.data.path );
\r
31 command.fire( 'state' );
\r
34 function justifyCommand( editor, name, value )
\r
39 var contentDir = editor.config.contentsLangDirection;
\r
40 this.isDefaultAlign = ( value == 'left' && contentDir == 'ltr' ) ||
\r
41 ( value == 'right' && contentDir == 'rtl' );
\r
43 var classes = editor.config.justifyClasses;
\r
49 this.cssClassName = classes[0];
\r
52 this.cssClassName = classes[1];
\r
55 this.cssClassName = classes[2];
\r
58 this.cssClassName = classes[3];
\r
62 this.cssClassRegex = new RegExp( '(?:^|\\s+)(?:' + classes.join( '|' ) + ')(?=$|\\s)' );
\r
66 justifyCommand.prototype = {
\r
67 exec : function( editor )
\r
69 var selection = editor.getSelection();
\r
73 var bookmarks = selection.createBookmarks(),
\r
74 ranges = selection.getRanges();
\r
77 var cssClassName = this.cssClassName,
\r
80 for ( var i = ranges.length - 1 ; i >= 0 ; i-- )
\r
82 iterator = ranges[ i ].createIterator();
\r
83 while ( ( block = iterator.getNextParagraph() ) )
\r
85 block.removeAttribute( 'align' );
\r
89 // Remove any of the alignment classes from the className.
\r
90 var className = block.$.className =
\r
91 CKEDITOR.tools.ltrim( block.$.className.replace( this.cssClassRegex, '' ) );
\r
93 // Append the desired class name.
\r
94 if ( this.state == CKEDITOR.TRISTATE_OFF && !this.isDefaultAlign )
\r
95 block.addClass( cssClassName );
\r
96 else if ( !className )
\r
97 block.removeAttribute( 'class' );
\r
101 if ( this.state == CKEDITOR.TRISTATE_OFF && !this.isDefaultAlign )
\r
102 block.setStyle( 'text-align', this.value );
\r
104 block.removeStyle( 'text-align' );
\r
111 editor.forceNextSelectionCheck();
\r
112 selection.selectBookmarks( bookmarks );
\r
116 CKEDITOR.plugins.add( 'justify',
\r
118 init : function( editor )
\r
120 var left = new justifyCommand( editor, 'justifyleft', 'left' ),
\r
121 center = new justifyCommand( editor, 'justifycenter', 'center' ),
\r
122 right = new justifyCommand( editor, 'justifyright', 'right' ),
\r
123 justify = new justifyCommand( editor, 'justifyblock', 'justify' );
\r
125 editor.addCommand( 'justifyleft', left );
\r
126 editor.addCommand( 'justifycenter', center );
\r
127 editor.addCommand( 'justifyright', right );
\r
128 editor.addCommand( 'justifyblock', justify );
\r
130 editor.ui.addButton( 'JustifyLeft',
\r
132 label : editor.lang.justify.left,
\r
133 command : 'justifyleft'
\r
135 editor.ui.addButton( 'JustifyCenter',
\r
137 label : editor.lang.justify.center,
\r
138 command : 'justifycenter'
\r
140 editor.ui.addButton( 'JustifyRight',
\r
142 label : editor.lang.justify.right,
\r
143 command : 'justifyright'
\r
145 editor.ui.addButton( 'JustifyBlock',
\r
147 label : editor.lang.justify.block,
\r
148 command : 'justifyblock'
\r
151 editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, left ) );
\r
152 editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, right ) );
\r
153 editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, center ) );
\r
154 editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, justify ) );
\r
157 requires : [ 'domiterator' ]
\r
161 CKEDITOR.tools.extend( CKEDITOR.config,
\r
163 justifyClasses : null
\r