2
* FCKeditor - The text editor for Internet - http://www.fckeditor.net
3
* Copyright (C) 2003-2009 Frederico Caldeira Knabben
7
* Licensed under the terms of any of the following licenses at your
10
* - GNU General Public License Version 2 or later (the "GPL")
11
* http://www.gnu.org/licenses/gpl.html
13
* - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14
* http://www.gnu.org/licenses/lgpl.html
16
* - Mozilla Public License Version 1.1 or later (the "MPL")
17
* http://www.mozilla.org/MPL/MPL-1.1.html
21
* FCKJustifyCommand Class: controls block justification.
24
var FCKJustifyCommand = function( alignValue )
26
this.AlignValue = alignValue ;
28
// Detect whether this is the instance for the default alignment.
29
var contentDir = FCKConfig.ContentLangDirection.toLowerCase() ;
30
this.IsDefaultAlign = ( alignValue == 'left' && contentDir == 'ltr' ) ||
31
( alignValue == 'right' && contentDir == 'rtl' ) ;
33
// Get the class name to be used by this instance.
34
var cssClassName = this._CssClassName = ( function()
36
var classes = FCKConfig.JustifyClasses ;
42
return classes[0] || null ;
44
return classes[1] || null ;
46
return classes[2] || null ;
48
return classes[3] || null ;
54
if ( cssClassName && cssClassName.length > 0 )
55
this._CssClassRegex = new RegExp( '(?:^|\\s+)' + cssClassName + '(?=$|\\s)' ) ;
58
FCKJustifyCommand._GetClassNameRegex = function()
60
var regex = FCKJustifyCommand._ClassRegex ;
61
if ( regex != undefined )
66
var classes = FCKConfig.JustifyClasses ;
69
for ( var i = 0 ; i < 4 ; i++ )
71
var className = classes[i] ;
72
if ( className && className.length > 0 )
73
names.push( className ) ;
77
if ( names.length > 0 )
78
regex = new RegExp( '(?:^|\\s+)(?:' + names.join( '|' ) + ')(?=$|\\s)' ) ;
82
return FCKJustifyCommand._ClassRegex = regex ;
85
FCKJustifyCommand.prototype =
89
// Save an undo snapshot before doing anything.
90
FCKUndo.SaveUndoStep() ;
92
var range = new FCKDomRange( FCK.EditorWindow ) ;
93
range.MoveToSelection() ;
95
var currentState = this.GetState() ;
96
if ( currentState == FCK_TRISTATE_DISABLED )
99
// Store a bookmark of the selection since the paragraph iterator might
100
// change the DOM tree and break selections.
101
var bookmark = range.CreateBookmark() ;
103
var cssClassName = this._CssClassName ;
105
// Apply alignment setting for each paragraph.
106
var iterator = new FCKDomRangeIterator( range ) ;
108
while ( ( block = iterator.GetNextParagraph() ) )
110
block.removeAttribute( 'align' ) ;
114
// Remove the any of the alignment classes from the className.
115
var className = block.className.replace( FCKJustifyCommand._GetClassNameRegex(), '' ) ;
117
// Append the desired class name.
118
if ( currentState == FCK_TRISTATE_OFF )
120
if ( className.length > 0 )
122
block.className = className + cssClassName ;
124
else if ( className.length == 0 )
125
FCKDomTools.RemoveAttribute( block, 'class' ) ;
129
var style = block.style ;
130
if ( currentState == FCK_TRISTATE_OFF )
131
style.textAlign = this.AlignValue ;
134
style.textAlign = '' ;
135
if ( style.cssText.length == 0 )
136
block.removeAttribute( 'style' ) ;
141
// Restore previous selection.
142
range.MoveToBookmark( bookmark ) ;
146
FCK.Events.FireEvent( 'OnSelectionChange' ) ;
149
GetState : function()
151
// Disabled if not WYSIWYG.
152
if ( FCK.EditMode != FCK_EDITMODE_WYSIWYG || ! FCK.EditorWindow )
153
return FCK_TRISTATE_DISABLED ;
155
// Retrieve the first selected block.
156
var path = new FCKElementPath( FCKSelection.GetBoundaryParentElement( true ) ) ;
157
var firstBlock = path.Block || path.BlockLimit ;
159
if ( !firstBlock || firstBlock.nodeName.toLowerCase() == 'body' )
160
return FCK_TRISTATE_OFF ;
162
// Check if the desired style is already applied to the block.
164
if ( FCKBrowserInfo.IsIE )
165
currentAlign = firstBlock.currentStyle.textAlign ;
167
currentAlign = FCK.EditorWindow.getComputedStyle( firstBlock, '' ).getPropertyValue( 'text-align' );
168
currentAlign = currentAlign.replace( /(-moz-|-webkit-|start|auto)/i, '' );
169
if ( ( !currentAlign && this.IsDefaultAlign ) || currentAlign == this.AlignValue )
170
return FCK_TRISTATE_ON ;
171
return FCK_TRISTATE_OFF ;