JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.3
[ckeditor.git] / _source / plugins / justify / plugin.js
index ad134c5..1b3aaa5 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
@@ -9,18 +9,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
 (function()\r
 {\r
-       function getState( editor, path )\r
-       {\r
-               var firstBlock = path.block || path.blockLimit;\r
-\r
-               if ( !firstBlock || firstBlock.getName() == 'body' )\r
-                       return CKEDITOR.TRISTATE_OFF;\r
-\r
-               return ( getAlignment( firstBlock, editor.config.useComputedState ) == this.value ) ?\r
-                       CKEDITOR.TRISTATE_ON :\r
-                       CKEDITOR.TRISTATE_OFF;\r
-       }\r
-\r
        function getAlignment( element, useComputedState )\r
        {\r
                useComputedState = useComputedState === undefined || useComputedState;\r
@@ -40,7 +28,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        align = element.getStyle( 'text-align' ) || element.getAttribute( 'align' ) || '';\r
                }\r
 \r
-               align && ( align = align.replace( /-moz-|-webkit-|start|auto/i, '' ) );\r
+               // Sometimes computed values doesn't tell.\r
+               align && ( align = align.replace( /(?:-(?:moz|webkit)-)?(?:start|auto)/i, '' ) );\r
 \r
                !align && useComputedState && ( align = element.getComputedStyle( 'direction' ) == 'rtl' ? 'right' : 'left' );\r
 \r
@@ -49,13 +38,15 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
        function onSelectionChange( evt )\r
        {\r
-               var command = evt.editor.getCommand( this.name );\r
-               command.state = getState.call( this, evt.editor, evt.data.path );\r
-               command.fire( 'state' );\r
+               if ( evt.editor.readOnly )\r
+                       return;\r
+\r
+               evt.editor.getCommand( this.name ).refresh( evt.data.path );\r
        }\r
 \r
        function justifyCommand( editor, name, value )\r
        {\r
+               this.editor = editor;\r
                this.name = name;\r
                this.value = value;\r
 \r
@@ -87,8 +78,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                var editor = e.editor;\r
 \r
                var range = new CKEDITOR.dom.range( editor.document );\r
-               range.setStartBefore( e.data );\r
-               range.setEndAfter( e.data );\r
+               range.setStartBefore( e.data.node );\r
+               range.setEndAfter( e.data.node );\r
 \r
                var walker = new CKEDITOR.dom.walker( range ),\r
                        node;\r
@@ -98,7 +89,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        if ( node.type == CKEDITOR.NODE_ELEMENT )\r
                        {\r
                                // A child with the defined dir is to be ignored.\r
-                               if ( !node.equals( e.data ) && node.getDirection() )\r
+                               if ( !node.equals( e.data.node ) && node.getDirection() )\r
                                {\r
                                        range.setStartAfter( node );\r
                                        walker = new CKEDITOR.dom.walker( range );\r
@@ -106,6 +97,24 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                }\r
 \r
                                // Switch the alignment.\r
+                               var classes = editor.config.justifyClasses;\r
+                               if ( classes )\r
+                               {\r
+                                       // The left align class.\r
+                                       if ( node.hasClass( classes[ 0 ] ) )\r
+                                       {\r
+                                               node.removeClass( classes[ 0 ] );\r
+                                               node.addClass( classes[ 2 ] );\r
+                                       }\r
+                                       // The right align class.\r
+                                       else if ( node.hasClass( classes[ 2 ] ) )\r
+                                       {\r
+                                               node.removeClass( classes[ 2 ] );\r
+                                               node.addClass( classes[ 0 ] );\r
+                                       }\r
+                               }\r
+\r
+                               // Always switch CSS margins.\r
                                var style = 'text-align';\r
                                var align = node.getStyle( style );\r
 \r
@@ -141,7 +150,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                iterator = ranges[ i ].createIterator();\r
                                iterator.enlargeBr = enterMode != CKEDITOR.ENTER_BR;\r
 \r
-                               while ( ( block = iterator.getNextParagraph() ) )\r
+                               while ( ( block = iterator.getNextParagraph( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) ) )\r
                                {\r
                                        block.removeAttribute( 'align' );\r
                                        block.removeStyle( 'text-align' );\r
@@ -171,6 +180,16 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        editor.focus();\r
                        editor.forceNextSelectionCheck();\r
                        selection.selectBookmarks( bookmarks );\r
+               },\r
+\r
+               refresh : function( path )\r
+               {\r
+                       var firstBlock = path.block || path.blockLimit;\r
+\r
+                       this.setState( firstBlock.getName() != 'body' &&\r
+                               getAlignment( firstBlock, this.editor.config.useComputedState ) == this.value ?\r
+                               CKEDITOR.TRISTATE_ON :\r
+                               CKEDITOR.TRISTATE_OFF );\r
                }\r
        };\r
 \r
@@ -219,3 +238,14 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                requires : [ 'domiterator' ]\r
        });\r
 })();\r
+\r
+ /**\r
+ * List of classes to use for aligning the contents. If it's null, no classes will be used\r
+ * and instead the corresponding CSS values will be used. The array should contain 4 members, in the following order: left, center, right, justify.\r
+ * @name CKEDITOR.config.justifyClasses\r
+ * @type Array\r
+ * @default null\r
+ * @example\r
+ * // Use the classes 'AlignLeft', 'AlignCenter', 'AlignRight', 'AlignJustify'\r
+ * config.justifyClasses = [ 'AlignLeft', 'AlignCenter', 'AlignRight', 'AlignJustify' ];\r
+ */\r