JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.4b
[ckeditor.git] / _source / plugins / justify / plugin.js
1 /*\r
2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 /**\r
7  * @file Justify commands.\r
8  */\r
9 \r
10 (function()\r
11 {\r
12         function getState( editor, path )\r
13         {\r
14                 var firstBlock = path.block || path.blockLimit;\r
15 \r
16                 if ( !firstBlock || firstBlock.getName() == 'body' )\r
17                         return CKEDITOR.TRISTATE_OFF;\r
18 \r
19                 return ( getAlignment( firstBlock, editor.config.useComputedState ) == this.value ) ?\r
20                         CKEDITOR.TRISTATE_ON :\r
21                         CKEDITOR.TRISTATE_OFF;\r
22         }\r
23 \r
24         function getAlignment( element, useComputedState )\r
25         {\r
26                 useComputedState = useComputedState === undefined || useComputedState;\r
27 \r
28                 var align = useComputedState ?\r
29                         element.getComputedStyle( 'text-align' ) :\r
30                         element.getStyle( 'text-align' ) || element.getAttribute( 'align' ) || '';\r
31 \r
32                 align && ( align = align.replace( /-moz-|-webkit-|start|auto/i, '' ) );\r
33 \r
34                 !align && useComputedState && ( align = element.getComputedStyle( 'direction' ) == 'rtl' ? 'right' : 'left' );\r
35 \r
36                 return align;\r
37         }\r
38 \r
39         function onSelectionChange( evt )\r
40         {\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
44         }\r
45 \r
46         function justifyCommand( editor, name, value )\r
47         {\r
48                 this.name = name;\r
49                 this.value = value;\r
50 \r
51                 var classes = editor.config.justifyClasses;\r
52                 if ( classes )\r
53                 {\r
54                         switch ( value )\r
55                         {\r
56                                 case 'left' :\r
57                                         this.cssClassName = classes[0];\r
58                                         break;\r
59                                 case 'center' :\r
60                                         this.cssClassName = classes[1];\r
61                                         break;\r
62                                 case 'right' :\r
63                                         this.cssClassName = classes[2];\r
64                                         break;\r
65                                 case 'justify' :\r
66                                         this.cssClassName = classes[3];\r
67                                         break;\r
68                         }\r
69 \r
70                         this.cssClassRegex = new RegExp( '(?:^|\\s+)(?:' + classes.join( '|' ) + ')(?=$|\\s)' );\r
71                 }\r
72         }\r
73 \r
74         justifyCommand.prototype = {\r
75                 exec : function( editor )\r
76                 {\r
77                         var selection = editor.getSelection(),\r
78                                 enterMode = editor.config.enterMode;\r
79 \r
80                         if ( !selection )\r
81                                 return;\r
82 \r
83                         var bookmarks = selection.createBookmarks(),\r
84                                 ranges = selection.getRanges( true );\r
85 \r
86                         var cssClassName = this.cssClassName,\r
87                                 iterator,\r
88                                 block;\r
89 \r
90                         var useComputedState = editor.config.useComputedState;\r
91                         useComputedState = useComputedState === undefined || useComputedState;\r
92 \r
93                         for ( var i = ranges.length - 1 ; i >= 0 ; i-- )\r
94                         {\r
95                                 iterator = ranges[ i ].createIterator();\r
96                                 iterator.enlargeBr = enterMode != CKEDITOR.ENTER_BR;\r
97 \r
98                                 while ( ( block = iterator.getNextParagraph() ) )\r
99                                 {\r
100                                         block.removeAttribute( 'align' );\r
101                                         block.removeStyle( 'text-align' );\r
102 \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
106 \r
107                                         var apply =\r
108                                                 ( this.state == CKEDITOR.TRISTATE_OFF ) &&\r
109                                                 ( !useComputedState || ( getAlignment( block, true ) != this.value ) );\r
110 \r
111                                         if ( cssClassName )\r
112                                         {\r
113                                                 // Append the desired class name.\r
114                                                 if ( apply )\r
115                                                         block.addClass( cssClassName );\r
116                                                 else if ( !className )\r
117                                                         block.removeAttribute( 'class' );\r
118                                         }\r
119                                         else if ( apply )\r
120                                                 block.setStyle( 'text-align', this.value );\r
121                                 }\r
122 \r
123                         }\r
124 \r
125                         editor.focus();\r
126                         editor.forceNextSelectionCheck();\r
127                         selection.selectBookmarks( bookmarks );\r
128                 }\r
129         };\r
130 \r
131         CKEDITOR.plugins.add( 'justify',\r
132         {\r
133                 init : function( editor )\r
134                 {\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
139 \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
144 \r
145                         editor.ui.addButton( 'JustifyLeft',\r
146                                 {\r
147                                         label : editor.lang.justify.left,\r
148                                         command : 'justifyleft'\r
149                                 } );\r
150                         editor.ui.addButton( 'JustifyCenter',\r
151                                 {\r
152                                         label : editor.lang.justify.center,\r
153                                         command : 'justifycenter'\r
154                                 } );\r
155                         editor.ui.addButton( 'JustifyRight',\r
156                                 {\r
157                                         label : editor.lang.justify.right,\r
158                                         command : 'justifyright'\r
159                                 } );\r
160                         editor.ui.addButton( 'JustifyBlock',\r
161                                 {\r
162                                         label : editor.lang.justify.block,\r
163                                         command : 'justifyblock'\r
164                                 } );\r
165 \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
170                 },\r
171 \r
172                 requires : [ 'domiterator' ]\r
173         });\r
174 })();\r
175 \r
176 CKEDITOR.tools.extend( CKEDITOR.config,\r
177         {\r
178                 justifyClasses : null\r
179         } );\r