JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
c6888219cbdaea7f1257636689c3933f7e1a84f3
[ckeditor.git] / _source / plugins / indent / plugin.js
1 /*\r
2 Copyright (c) 2003-2009, 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 Increse and decrease indent commands.\r
8  */\r
9 \r
10 (function()\r
11 {\r
12         var listNodeNames = { ol : 1, ul : 1 };\r
13 \r
14         function setState( editor, state )\r
15         {\r
16                 editor.getCommand( this.name ).setState( state );\r
17         }\r
18 \r
19         function onSelectionChange( evt )\r
20         {\r
21                 var elements = evt.data.path.elements,\r
22                         listNode, listItem,\r
23                         editor = evt.editor;\r
24 \r
25                 for ( var i = 0 ; i < elements.length ; i++ )\r
26                 {\r
27                         if ( elements[i].getName() == 'li' )\r
28                         {\r
29                                 listItem = elements[i];\r
30                                 continue;\r
31                         }\r
32                         if ( listNodeNames[ elements[i].getName() ] )\r
33                         {\r
34                                 listNode = elements[i];\r
35                                 break;\r
36                         }\r
37                 }\r
38 \r
39                 if ( listNode )\r
40                 {\r
41                         if ( this.name == 'outdent' )\r
42                                 return setState.call( this, editor, CKEDITOR.TRISTATE_OFF );\r
43                         else\r
44                         {\r
45                                 while ( listItem && ( listItem = listItem.getPrevious( CKEDITOR.dom.walker.whitespaces( true ) ) ) )\r
46                                 {\r
47                                         if ( listItem.getName && listItem.getName() == 'li' )\r
48                                                 return setState.call( this, editor, CKEDITOR.TRISTATE_OFF );\r
49                                 }\r
50                                 return setState.call( this, editor, CKEDITOR.TRISTATE_DISABLED );\r
51                         }\r
52                 }\r
53 \r
54                 if ( !this.useIndentClasses && this.name == 'indent' )\r
55                         return setState.call( this, editor, CKEDITOR.TRISTATE_OFF );\r
56 \r
57                 var path = evt.data.path,\r
58                         firstBlock = path.block || path.blockLimit;\r
59                 if ( !firstBlock )\r
60                         return setState.call( this, editor, CKEDITOR.TRISTATE_DISABLED );\r
61 \r
62                 if ( this.useIndentClasses )\r
63                 {\r
64                         var indentClass = firstBlock.$.className.match( this.classNameRegex ),\r
65                                 indentStep = 0;\r
66                         if ( indentClass )\r
67                         {\r
68                                 indentClass = indentClass[1];\r
69                                 indentStep = this.indentClassMap[ indentClass ];\r
70                         }\r
71                         if ( ( this.name == 'outdent' && !indentStep ) ||\r
72                                         ( this.name == 'indent' && indentStep == editor.config.indentClasses.length ) )\r
73                                 return setState.call( this, editor, CKEDITOR.TRISTATE_DISABLED );\r
74                         return setState.call( this, editor, CKEDITOR.TRISTATE_OFF );\r
75                 }\r
76                 else\r
77                 {\r
78                         var indent = parseInt( firstBlock.getStyle( this.indentCssProperty ), 10 );\r
79                         if ( isNaN( indent ) )\r
80                                 indent = 0;\r
81                         if ( indent <= 0 )\r
82                                 return setState.call( this, editor, CKEDITOR.TRISTATE_DISABLED );\r
83                         return setState.call( this, editor, CKEDITOR.TRISTATE_OFF );\r
84                 }\r
85         }\r
86 \r
87         function indentList( editor, range, listNode )\r
88         {\r
89                 // Our starting and ending points of the range might be inside some blocks under a list item...\r
90                 // So before playing with the iterator, we need to expand the block to include the list items.\r
91                 var startContainer = range.startContainer,\r
92                         endContainer = range.endContainer;\r
93                 while ( startContainer && !startContainer.getParent().equals( listNode ) )\r
94                         startContainer = startContainer.getParent();\r
95                 while ( endContainer && !endContainer.getParent().equals( listNode ) )\r
96                         endContainer = endContainer.getParent();\r
97 \r
98                 if ( !startContainer || !endContainer )\r
99                         return;\r
100 \r
101                 // Now we can iterate over the individual items on the same tree depth.\r
102                 var block = startContainer,\r
103                         itemsToMove = [],\r
104                         stopFlag = false;\r
105                 while ( !stopFlag )\r
106                 {\r
107                         if ( block.equals( endContainer ) )\r
108                                 stopFlag = true;\r
109                         itemsToMove.push( block );\r
110                         block = block.getNext();\r
111                 }\r
112                 if ( itemsToMove.length < 1 )\r
113                         return;\r
114 \r
115                 // Do indent or outdent operations on the array model of the list, not the\r
116                 // list's DOM tree itself. The array model demands that it knows as much as\r
117                 // possible about the surrounding lists, we need to feed it the further\r
118                 // ancestor node that is still a list.\r
119                 var listParents = listNode.getParents( true );\r
120                 for ( var i = 0 ; i < listParents.length ; i++ )\r
121                 {\r
122                         if ( listParents[i].getName && listNodeNames[ listParents[i].getName() ] )\r
123                         {\r
124                                 listNode = listParents[i];\r
125                                 break;\r
126                         }\r
127                 }\r
128                 var indentOffset = this.name == 'indent' ? 1 : -1,\r
129                         startItem = itemsToMove[0],\r
130                         lastItem = itemsToMove[ itemsToMove.length - 1 ],\r
131                         database = {};\r
132 \r
133                 // Convert the list DOM tree into a one dimensional array.\r
134                 var listArray = CKEDITOR.plugins.list.listToArray( listNode, database );\r
135 \r
136                 // Apply indenting or outdenting on the array.\r
137                 var baseIndent = listArray[ lastItem.getCustomData( 'listarray_index' ) ].indent;\r
138                 for ( i = startItem.getCustomData( 'listarray_index' ) ; i <= lastItem.getCustomData( 'listarray_index' ) ; i++ )\r
139                         listArray[i].indent += indentOffset;\r
140                 for ( i = lastItem.getCustomData( 'listarray_index' ) + 1 ;\r
141                                 i < listArray.length && listArray[i].indent > baseIndent ; i++ )\r
142                         listArray[i].indent += indentOffset;\r
143 \r
144                 // Convert the array back to a DOM forest (yes we might have a few subtrees now).\r
145                 // And replace the old list with the new forest.\r
146                 var newList = CKEDITOR.plugins.list.arrayToList( listArray, database, null, editor.config.enterMode, 0 );\r
147 \r
148                 // Avoid nested <li> after outdent even they're visually same,\r
149                 // recording them for later refactoring.(#3982)\r
150                 if ( this.name == 'outdent' )\r
151                 {\r
152                         var parentLiElement;\r
153                         if ( ( parentLiElement = listNode.getParent() ) && parentLiElement.is( 'li' ) )\r
154                         {\r
155                                 var children = newList.listNode.getChildren(),\r
156                                         pendingLis = [],\r
157                                         count = children.count(),\r
158                                         child;\r
159 \r
160                                 for ( i = count - 1 ; i >= 0 ; i-- )\r
161                                 {\r
162                                         if( ( child = children.getItem( i ) ) && child.is && child.is( 'li' )  )\r
163                                                 pendingLis.push( child );\r
164                                 }\r
165                         }\r
166                 }\r
167 \r
168                 if ( newList )\r
169                         newList.listNode.replace( listNode );\r
170 \r
171                 // Move the nested <li> to be appeared after the parent.\r
172                 if ( pendingLis && pendingLis.length )\r
173                 {\r
174                         for (  i = 0; i < pendingLis.length ; i++ )\r
175                         {\r
176                                 var li = pendingLis[ i ],\r
177                                         followingList = li;\r
178 \r
179                                 // Nest preceding <ul>/<ol> inside current <li> if any.\r
180                                 while( ( followingList = followingList.getNext() ) &&\r
181                                            followingList.is &&\r
182                                            followingList.getName() in listNodeNames )\r
183                                 {\r
184                                         li.append( followingList );\r
185                                 }\r
186 \r
187                                 li.insertAfter( parentLiElement );\r
188                         }\r
189                 }\r
190 \r
191                 // Clean up the markers.\r
192                 CKEDITOR.dom.element.clearAllMarkers( database );\r
193         }\r
194 \r
195         function indentBlock( editor, range )\r
196         {\r
197                 var iterator = range.createIterator(),\r
198                         enterMode = editor.config.enterMode;\r
199                 iterator.enforceRealBlocks = true;\r
200                 iterator.enlargeBr = enterMode != CKEDITOR.ENTER_BR;\r
201                 var block;\r
202                 while ( ( block = iterator.getNextParagraph() ) )\r
203                 {\r
204 \r
205                         if ( this.useIndentClasses )\r
206                         {\r
207                                 // Transform current class name to indent step index.\r
208                                 var indentClass = block.$.className.match( this.classNameRegex ),\r
209                                         indentStep = 0;\r
210                                 if ( indentClass )\r
211                                 {\r
212                                         indentClass = indentClass[1];\r
213                                         indentStep = this.indentClassMap[ indentClass ];\r
214                                 }\r
215 \r
216                                 // Operate on indent step index, transform indent step index back to class\r
217                                 // name.\r
218                                 if ( this.name == 'outdent' )\r
219                                         indentStep--;\r
220                                 else\r
221                                         indentStep++;\r
222                                 indentStep = Math.min( indentStep, editor.config.indentClasses.length );\r
223                                 indentStep = Math.max( indentStep, 0 );\r
224                                 var className = CKEDITOR.tools.ltrim( block.$.className.replace( this.classNameRegex, '' ) );\r
225                                 if ( indentStep < 1 )\r
226                                         block.$.className = className;\r
227                                 else\r
228                                         block.addClass( editor.config.indentClasses[ indentStep - 1 ] );\r
229                         }\r
230                         else\r
231                         {\r
232                                 var currentOffset = parseInt( block.getStyle( this.indentCssProperty ), 10 );\r
233                                 if ( isNaN( currentOffset ) )\r
234                                         currentOffset = 0;\r
235                                 currentOffset += ( this.name == 'indent' ? 1 : -1 ) * editor.config.indentOffset;\r
236                                 currentOffset = Math.max( currentOffset, 0 );\r
237                                 currentOffset = Math.ceil( currentOffset / editor.config.indentOffset ) * editor.config.indentOffset;\r
238                                 block.setStyle( this.indentCssProperty, currentOffset ? currentOffset + editor.config.indentUnit : '' );\r
239                                 if ( block.getAttribute( 'style' ) === '' )\r
240                                         block.removeAttribute( 'style' );\r
241                         }\r
242                 }\r
243         }\r
244 \r
245         function indentCommand( editor, name )\r
246         {\r
247                 this.name = name;\r
248                 this.useIndentClasses = editor.config.indentClasses && editor.config.indentClasses.length > 0;\r
249                 if ( this.useIndentClasses )\r
250                 {\r
251                         this.classNameRegex = new RegExp( '(?:^|\\s+)(' + editor.config.indentClasses.join( '|' ) + ')(?=$|\\s)' );\r
252                         this.indentClassMap = {};\r
253                         for ( var i = 0 ; i < editor.config.indentClasses.length ; i++ )\r
254                                 this.indentClassMap[ editor.config.indentClasses[i] ] = i + 1;\r
255                 }\r
256                 else\r
257                         this.indentCssProperty = editor.config.contentsLangDirection == 'ltr' ? 'margin-left' : 'margin-right';\r
258         }\r
259 \r
260         indentCommand.prototype = {\r
261                 exec : function( editor )\r
262                 {\r
263                         var selection = editor.getSelection(),\r
264                                 range = selection && selection.getRanges()[0];\r
265 \r
266                         if ( !selection || !range )\r
267                                 return;\r
268 \r
269                         var bookmarks = selection.createBookmarks( true ),\r
270                                 nearestListBlock = range.getCommonAncestor();\r
271 \r
272                         while ( nearestListBlock && !( nearestListBlock.type == CKEDITOR.NODE_ELEMENT &&\r
273                                 listNodeNames[ nearestListBlock.getName() ] ) )\r
274                                 nearestListBlock = nearestListBlock.getParent();\r
275 \r
276                         if ( nearestListBlock )\r
277                                 indentList.call( this, editor, range, nearestListBlock );\r
278                         else\r
279                                 indentBlock.call( this, editor, range );\r
280 \r
281                         editor.focus();\r
282                         editor.forceNextSelectionCheck();\r
283                         selection.selectBookmarks( bookmarks );\r
284                 }\r
285         };\r
286 \r
287         CKEDITOR.plugins.add( 'indent',\r
288         {\r
289                 init : function( editor )\r
290                 {\r
291                         // Register commands.\r
292                         var indent = new indentCommand( editor, 'indent' ),\r
293                                 outdent = new indentCommand( editor, 'outdent' );\r
294                         editor.addCommand( 'indent', indent );\r
295                         editor.addCommand( 'outdent', outdent );\r
296 \r
297                         // Register the toolbar buttons.\r
298                         editor.ui.addButton( 'Indent',\r
299                                 {\r
300                                         label : editor.lang.indent,\r
301                                         command : 'indent'\r
302                                 });\r
303                         editor.ui.addButton( 'Outdent',\r
304                                 {\r
305                                         label : editor.lang.outdent,\r
306                                         command : 'outdent'\r
307                                 });\r
308 \r
309                         // Register the state changing handlers.\r
310                         editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, indent ) );\r
311                         editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, outdent ) );\r
312                 },\r
313 \r
314                 requires : [ 'domiterator', 'list' ]\r
315         } );\r
316 })();\r
317 \r
318 CKEDITOR.tools.extend( CKEDITOR.config,\r
319         {\r
320                 indentOffset : 40,\r
321                 indentUnit : 'px',\r
322                 indentClasses : null\r
323         });\r