1/* 2 * FCKeditor - The text editor for Internet - http://www.fckeditor.net 3 * Copyright (C) 2003-2007 Frederico Caldeira Knabben 4 * 5 * == BEGIN LICENSE == 6 * 7 * Licensed under the terms of any of the following licenses at your 8 * choice: 9 * 10 * - GNU General Public License Version 2 or later (the "GPL") 11 * http://www.gnu.org/licenses/gpl.html 12 * 13 * - GNU Lesser General Public License Version 2.1 or later (the "LGPL") 14 * http://www.gnu.org/licenses/lgpl.html 15 * 16 * - Mozilla Public License Version 1.1 or later (the "MPL") 17 * http://www.mozilla.org/MPL/MPL-1.1.html 18 * 19 * == END LICENSE == 20 * 21 * FCKIndentCommand Class: controls block indentation. 22 */ 23 24var FCKIndentCommand = function( name, offset ) 25{ 26 this.Name = name ; 27 this.Offset = offset ; 28 this.IndentCSSProperty = FCKConfig.ContentLangDirection.IEquals( 'ltr' ) ? 'marginLeft' : 'marginRight' ; 29} 30 31FCKIndentCommand._InitIndentModeParameters = function() 32{ 33 if ( FCKConfig.IndentClasses && FCKConfig.IndentClasses.length > 0 ) 34 { 35 this._UseIndentClasses = true ; 36 this._IndentClassMap = {} ; 37 for ( var i = 0 ; i < FCKConfig.IndentClasses.length ;i++ ) 38 this._IndentClassMap[FCKConfig.IndentClasses[i]] = i + 1 ; 39 this._ClassNameRegex = new RegExp( '(?:^|\\s+)(' + FCKConfig.IndentClasses.join( '|' ) + ')(?=$|\\s)' ) ; 40 } 41 else 42 this._UseIndentClasses = false ; 43} 44 45 46FCKIndentCommand.prototype = 47{ 48 Execute : function() 49 { 50 // Save an undo snapshot before doing anything. 51 FCKUndo.SaveUndoStep() ; 52 53 var range = new FCKDomRange( FCK.EditorWindow ) ; 54 range.MoveToSelection() ; 55 var bookmark = range.CreateBookmark() ; 56 57 // Two cases to handle here: either we're in a list, or not. 58 // If we're in a list, then the indent/outdent operations would be done on the list nodes. 59 // Otherwise, apply the operation on the nearest block nodes. 60 var nearestListBlock = FCKDomTools.GetCommonParentNode( range.StartNode || range.StartContainer , 61 range.EndNode || range.EndContainer, 62 ['ul', 'ol'] ) ; 63 if ( nearestListBlock ) 64 this._IndentList( range, nearestListBlock ) ; 65 else 66 this._IndentBlock( range ) ; 67 68 range.MoveToBookmark( bookmark ) ; 69 range.Select() ; 70 71 FCK.Focus() ; 72 FCK.Events.FireEvent( 'OnSelectionChange' ) ; 73 }, 74 75 GetState : function() 76 { 77 // Disabled if not WYSIWYG. 78 if ( FCK.EditMode != FCK_EDITMODE_WYSIWYG || ! FCK.EditorWindow ) 79 return FCK_TRISTATE_DISABLED ; 80 81 // Initialize parameters if not already initialzed. 82 if ( FCKIndentCommand._UseIndentClasses == undefined ) 83 FCKIndentCommand._InitIndentModeParameters() ; 84 85 // If we're not in a list, and the starting block's indentation is zero, and the current 86 // command is the outdent command, then we should return FCK_TRISTATE_DISABLED. 87 var startContainer = FCKSelection.GetBoundaryParentElement( true ) ; 88 var endContainer = FCKSelection.GetBoundaryParentElement( false ) ; 89 var listNode = FCKDomTools.GetCommonParentNode( startContainer, endContainer, ['ul','ol'] ) ; 90 91 if ( listNode ) 92 { 93 if ( this.Name.IEquals( 'outdent' ) ) 94 return FCK_TRISTATE_OFF ; 95 var firstItem = FCKTools.GetElementAscensor( startContainer, 'li' ) ; 96 if ( !firstItem || !firstItem.previousSibling ) 97 return FCK_TRISTATE_DISABLED ; 98 return FCK_TRISTATE_OFF ; 99 } 100 if ( ! FCKIndentCommand._UseIndentClasses && this.Name.IEquals( 'indent' ) ) 101 return FCK_TRISTATE_OFF; 102 103 var path = new FCKElementPath( startContainer ) ; 104 var firstBlock = path.Block || path.BlockLimit ; 105 if ( !firstBlock ) 106 return FCK_TRISTATE_DISABLED ; 107 108 if ( FCKIndentCommand._UseIndentClasses ) 109 { 110 var indentClass = firstBlock.className.match( FCKIndentCommand._ClassNameRegex ) ; 111 var indentStep = 0 ; 112 if ( indentClass != null ) 113 { 114 indentClass = indentClass[1] ; 115 indentStep = FCKIndentCommand._IndentClassMap[indentClass] ; 116 } 117 if ( ( this.Name == 'outdent' && indentStep == 0 ) || 118 ( this.Name == 'indent' && indentStep == FCKConfig.IndentClasses.length ) ) 119 return FCK_TRISTATE_DISABLED ; 120 return FCK_TRISTATE_OFF ; 121 } 122 else 123 { 124 var indent = parseInt( firstBlock.style[this.IndentCSSProperty], 10 ) ; 125 if ( isNaN( indent ) ) 126 indent = 0 ; 127 if ( indent <= 0 ) 128 return FCK_TRISTATE_DISABLED ; 129 return FCK_TRISTATE_OFF ; 130 } 131 }, 132 133 _IndentBlock : function( range ) 134 { 135 var iterator = new FCKDomRangeIterator( range ) ; 136 range.Expand( 'block_contents' ) ; 137 var commonParents = FCKDomTools.GetCommonParents( range.StartContainer, range.EndContainer ) ; 138 var nearestParent = commonParents[commonParents.length - 1] ; 139 var block ; 140 141 while ( ( block = iterator.GetNextParagraph() ) ) 142 { 143 // We don't want to indent subtrees recursively, so only perform the indent operation 144 // if the block itself is the nearestParent, or the block's parent is the nearestParent. 145 if ( ! ( block == nearestParent || block.parentNode == nearestParent ) ) 146 continue ; 147 148 if ( FCKIndentCommand._UseIndentClasses ) 149 { 150 // Transform current class name to indent step index. 151 var indentClass = block.className.match( FCKIndentCommand._ClassNameRegex ) ; 152 var indentStep = 0 ; 153 if ( indentClass != null ) 154 { 155 indentClass = indentClass[1] ; 156 indentStep = FCKIndentCommand._IndentClassMap[indentClass] ; 157 } 158 159 // Operate on indent step index, transform indent step index back to class name. 160 if ( this.Name.IEquals( 'outdent' ) ) 161 indentStep-- ; 162 else if ( this.Name.IEquals( 'indent' ) ) 163 indentStep++ ; 164 indentStep = Math.min( indentStep, FCKConfig.IndentClasses.length ) ; 165 indentStep = Math.max( indentStep, 0 ) ; 166 var className = block.className.replace( FCKIndentCommand._ClassNameRegex, '' ) ; 167 if ( indentStep < 1 ) 168 block.className = className ; 169 else 170 block.className = ( className.length > 0 ? className + ' ' : '' ) + 171 FCKConfig.IndentClasses[indentStep - 1] ; 172 } 173 else 174 { 175 // Offset distance is assumed to be in pixels for now. 176 var currentOffset = parseInt( block.style[this.IndentCSSProperty], 10 ) ; 177 if ( isNaN( currentOffset ) ) 178 currentOffset = 0 ; 179 currentOffset += this.Offset ; 180 currentOffset = Math.max( currentOffset, 0 ) ; 181 currentOffset = Math.ceil( currentOffset / this.Offset ) * this.Offset ; 182 block.style[this.IndentCSSProperty] = currentOffset ? currentOffset + FCKConfig.IndentUnit : '' ; 183 if ( block.getAttribute( 'style' ) == '' ) 184 block.removeAttribute( 'style' ) ; 185 } 186 } 187 }, 188 189 _IndentList : function( range, listNode ) 190 { 191 // Our starting and ending points of the range might be inside some blocks under a list item... 192 // So before playing with the iterator, we need to expand the block to include the list items. 193 var startContainer = range.StartContainer ; 194 var endContainer = range.EndContainer ; 195 while ( startContainer && startContainer.parentNode != listNode ) 196 startContainer = startContainer.parentNode ; 197 while ( endContainer && endContainer.parentNode != listNode ) 198 endContainer = endContainer.parentNode ; 199 200 if ( ! startContainer || ! endContainer ) 201 return ; 202 203 // Now we can iterate over the individual items on the same tree depth. 204 var block = startContainer ; 205 var itemsToMove = [] ; 206 var stopFlag = false ; 207 while ( stopFlag == false ) 208 { 209 if ( block == endContainer ) 210 stopFlag = true ; 211 itemsToMove.push( block ) ; 212 block = block.nextSibling ; 213 } 214 if ( itemsToMove.length < 1 ) 215 return ; 216 217 // Do indent or outdent operations on the array model of the list, not the list's DOM tree itself. 218 // The array model demands that it knows as much as possible about the surrounding lists, we need 219 // to feed it the further ancestor node that is still a list. 220 var listParents = FCKDomTools.GetParents( listNode ) ; 221 for ( var i = 0 ; i < listParents.length ; i++ ) 222 { 223 if ( listParents[i].nodeName.IEquals( ['ul', 'ol'] ) ) 224 { 225 listNode = listParents[i] ; 226 break ; 227 } 228 } 229 var indentOffset = this.Name.IEquals( 'indent' ) ? 1 : -1 ; 230 var startItem = itemsToMove[0] ; 231 var lastItem = itemsToMove[ itemsToMove.length - 1 ] ; 232 var markerObj = {} ; 233 234 // Convert the list DOM tree into a one dimensional array. 235 var listArray = FCKDomTools.ListToArray( listNode, markerObj ) ; 236 237 // Apply indenting or outdenting on the array. 238 var baseIndent = listArray[lastItem._FCK_ListArray_Index].indent ; 239 for ( var i = startItem._FCK_ListArray_Index ; i <= lastItem._FCK_ListArray_Index ; i++ ) 240 listArray[i].indent += indentOffset ; 241 for ( var i = lastItem._FCK_ListArray_Index + 1 ; i < listArray.length && listArray[i].indent > baseIndent ; i++ ) 242 listArray[i].indent += indentOffset ; 243 244 /* For debug use only 245 var PrintArray = function( listArray, doc ) 246 { 247 var s = [] ; 248 for ( var i = 0 ; i < listArray.length ; i++ ) 249 { 250 for ( var j in listArray[i] ) 251 { 252 if ( j != 'contents' ) 253 s.push( j + ":" + listArray[i][j] + "; " ) ; 254 else 255 { 256 var docFrag = doc.createDocumentFragment() ; 257 var tmpNode = doc.createElement( 'span' ) ; 258 for ( var k = 0 ; k < listArray[i][j].length ; k++ ) 259 docFrag.appendChild( listArray[i][j][k].cloneNode( true ) ) ; 260 tmpNode.appendChild( docFrag ) ; 261 s.push( j + ":" + tmpNode.innerHTML + "; ") ; 262 } 263 } 264 s.push( '\n' ) ; 265 } 266 alert( s.join('') ) ; 267 } 268 PrintArray( listArray, FCK.EditorDocument ) ; 269 */ 270 271 // Convert the array back to a DOM forest (yes we might have a few subtrees now). 272 // And replace the old list with the new forest. 273 var newList = FCKDomTools.ArrayToList( listArray ) ; 274 if ( newList ) 275 listNode.parentNode.replaceChild( newList.listNode, listNode ) ; 276 277 // Clean up the markers. 278 FCKDomTools.ClearAllMarkers( markerObj ) ; 279 } 280} ; 281