[1575] | 1 | /*
|
---|
| 2 | * FCKeditor - The text editor for Internet - http://www.fckeditor.net
|
---|
| 3 | * Copyright (C) 2003-2009 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 | * Active selection functions. (IE specific implementation)
|
---|
| 22 | */
|
---|
| 23 |
|
---|
| 24 | // Get the selection type.
|
---|
| 25 | FCKSelection.GetType = function()
|
---|
| 26 | {
|
---|
| 27 | // It is possible that we can still get a text range object even when type=='None' is returned by IE.
|
---|
| 28 | // So we'd better check the object returned by createRange() rather than by looking at the type.
|
---|
| 29 | try
|
---|
| 30 | {
|
---|
| 31 | var ieType = FCKSelection.GetSelection().type ;
|
---|
| 32 | if ( ieType == 'Control' || ieType == 'Text' )
|
---|
| 33 | return ieType ;
|
---|
| 34 |
|
---|
| 35 | if ( this.GetSelection().createRange().parentElement )
|
---|
| 36 | return 'Text' ;
|
---|
| 37 | }
|
---|
| 38 | catch(e)
|
---|
| 39 | {
|
---|
| 40 | // Nothing to do, it will return None properly.
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | return 'None' ;
|
---|
| 44 | } ;
|
---|
| 45 |
|
---|
| 46 | // Retrieves the selected element (if any), just in the case that a single
|
---|
| 47 | // element (object like and image or a table) is selected.
|
---|
| 48 | FCKSelection.GetSelectedElement = function()
|
---|
| 49 | {
|
---|
| 50 | if ( this.GetType() == 'Control' )
|
---|
| 51 | {
|
---|
| 52 | var oRange = this.GetSelection().createRange() ;
|
---|
| 53 |
|
---|
| 54 | if ( oRange && oRange.item )
|
---|
| 55 | return this.GetSelection().createRange().item(0) ;
|
---|
| 56 | }
|
---|
| 57 | return null ;
|
---|
| 58 | } ;
|
---|
| 59 |
|
---|
| 60 | FCKSelection.GetParentElement = function()
|
---|
| 61 | {
|
---|
| 62 | switch ( this.GetType() )
|
---|
| 63 | {
|
---|
| 64 | case 'Control' :
|
---|
| 65 | var el = FCKSelection.GetSelectedElement() ;
|
---|
| 66 | return el ? el.parentElement : null ;
|
---|
| 67 |
|
---|
| 68 | case 'None' :
|
---|
| 69 | return null ;
|
---|
| 70 |
|
---|
| 71 | default :
|
---|
| 72 | return this.GetSelection().createRange().parentElement() ;
|
---|
| 73 | }
|
---|
| 74 | } ;
|
---|
| 75 |
|
---|
| 76 | FCKSelection.GetBoundaryParentElement = function( startBoundary )
|
---|
| 77 | {
|
---|
| 78 | switch ( this.GetType() )
|
---|
| 79 | {
|
---|
| 80 | case 'Control' :
|
---|
| 81 | var el = FCKSelection.GetSelectedElement() ;
|
---|
| 82 | return el ? el.parentElement : null ;
|
---|
| 83 |
|
---|
| 84 | case 'None' :
|
---|
| 85 | return null ;
|
---|
| 86 |
|
---|
| 87 | default :
|
---|
| 88 | var doc = FCK.EditorDocument ;
|
---|
| 89 |
|
---|
| 90 | var range = doc.selection.createRange() ;
|
---|
| 91 | range.collapse( startBoundary !== false ) ;
|
---|
| 92 |
|
---|
| 93 | var el = range.parentElement() ;
|
---|
| 94 |
|
---|
| 95 | // It may happen that range is comming from outside "doc", so we
|
---|
| 96 | // must check it (#1204).
|
---|
| 97 | return FCKTools.GetElementDocument( el ) == doc ? el : null ;
|
---|
| 98 | }
|
---|
| 99 | } ;
|
---|
| 100 |
|
---|
| 101 | FCKSelection.SelectNode = function( node )
|
---|
| 102 | {
|
---|
| 103 | FCK.Focus() ;
|
---|
| 104 | this.GetSelection().empty() ;
|
---|
| 105 | var oRange ;
|
---|
| 106 | try
|
---|
| 107 | {
|
---|
| 108 | // Try to select the node as a control.
|
---|
| 109 | oRange = FCK.EditorDocument.body.createControlRange() ;
|
---|
| 110 | oRange.addElement( node ) ;
|
---|
| 111 | oRange.select() ;
|
---|
| 112 | }
|
---|
| 113 | catch(e)
|
---|
| 114 | {
|
---|
| 115 | // If failed, select it as a text range.
|
---|
| 116 | oRange = FCK.EditorDocument.body.createTextRange() ;
|
---|
| 117 | oRange.moveToElementText( node ) ;
|
---|
| 118 | oRange.select() ;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | } ;
|
---|
| 122 |
|
---|
| 123 | FCKSelection.Collapse = function( toStart )
|
---|
| 124 | {
|
---|
| 125 | FCK.Focus() ;
|
---|
| 126 | if ( this.GetType() == 'Text' )
|
---|
| 127 | {
|
---|
| 128 | var oRange = this.GetSelection().createRange() ;
|
---|
| 129 | oRange.collapse( toStart == null || toStart === true ) ;
|
---|
| 130 | oRange.select() ;
|
---|
| 131 | }
|
---|
| 132 | } ;
|
---|
| 133 |
|
---|
| 134 | // The "nodeTagName" parameter must be Upper Case.
|
---|
| 135 | FCKSelection.HasAncestorNode = function( nodeTagName )
|
---|
| 136 | {
|
---|
| 137 | var oContainer ;
|
---|
| 138 |
|
---|
| 139 | if ( this.GetSelection().type == "Control" )
|
---|
| 140 | {
|
---|
| 141 | oContainer = this.GetSelectedElement() ;
|
---|
| 142 | }
|
---|
| 143 | else
|
---|
| 144 | {
|
---|
| 145 | var oRange = this.GetSelection().createRange() ;
|
---|
| 146 | oContainer = oRange.parentElement() ;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | while ( oContainer )
|
---|
| 150 | {
|
---|
| 151 | if ( oContainer.nodeName.IEquals( nodeTagName ) ) return true ;
|
---|
| 152 | oContainer = oContainer.parentNode ;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | return false ;
|
---|
| 156 | } ;
|
---|
| 157 |
|
---|
| 158 | // The "nodeTagName" parameter must be UPPER CASE.
|
---|
| 159 | // It can be also an array of names
|
---|
| 160 | FCKSelection.MoveToAncestorNode = function( nodeTagName )
|
---|
| 161 | {
|
---|
| 162 | var oNode, oRange ;
|
---|
| 163 |
|
---|
| 164 | if ( ! FCK.EditorDocument )
|
---|
| 165 | return null ;
|
---|
| 166 |
|
---|
| 167 | if ( this.GetSelection().type == "Control" )
|
---|
| 168 | {
|
---|
| 169 | oRange = this.GetSelection().createRange() ;
|
---|
| 170 | for ( i = 0 ; i < oRange.length ; i++ )
|
---|
| 171 | {
|
---|
| 172 | if (oRange(i).parentNode)
|
---|
| 173 | {
|
---|
| 174 | oNode = oRange(i).parentNode ;
|
---|
| 175 | break ;
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 | else
|
---|
| 180 | {
|
---|
| 181 | oRange = this.GetSelection().createRange() ;
|
---|
| 182 | oNode = oRange.parentElement() ;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | while ( oNode && !oNode.nodeName.Equals( nodeTagName ) )
|
---|
| 186 | oNode = oNode.parentNode ;
|
---|
| 187 |
|
---|
| 188 | return oNode ;
|
---|
| 189 | } ;
|
---|
| 190 |
|
---|
| 191 | FCKSelection.Delete = function()
|
---|
| 192 | {
|
---|
| 193 | // Gets the actual selection.
|
---|
| 194 | var oSel = this.GetSelection() ;
|
---|
| 195 |
|
---|
| 196 | // Deletes the actual selection contents.
|
---|
| 197 | if ( oSel.type.toLowerCase() != "none" )
|
---|
| 198 | {
|
---|
| 199 | oSel.clear() ;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | return oSel ;
|
---|
| 203 | } ;
|
---|
| 204 |
|
---|
| 205 | /**
|
---|
| 206 | * Returns the native selection object.
|
---|
| 207 | */
|
---|
| 208 | FCKSelection.GetSelection = function()
|
---|
| 209 | {
|
---|
| 210 | this.Restore() ;
|
---|
| 211 | return FCK.EditorDocument.selection ;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | FCKSelection.Save = function( lock )
|
---|
| 215 | {
|
---|
| 216 | var editorDocument = FCK.EditorDocument ;
|
---|
| 217 |
|
---|
| 218 | if ( !editorDocument )
|
---|
| 219 | return ;
|
---|
| 220 |
|
---|
| 221 | // Avoid saving again a selection while a dialog is open #2616
|
---|
| 222 | if ( this.locked )
|
---|
| 223 | return ;
|
---|
| 224 | this.locked = !!lock ;
|
---|
| 225 |
|
---|
| 226 | var selection = editorDocument.selection ;
|
---|
| 227 | var range ;
|
---|
| 228 |
|
---|
| 229 | if ( selection )
|
---|
| 230 | {
|
---|
| 231 | // The call might fail if the document doesn't have the focus (#1801),
|
---|
| 232 | // but we don't want to modify the current selection (#2495) with a call to FCK.Focus() ;
|
---|
| 233 | try {
|
---|
| 234 | range = selection.createRange() ;
|
---|
| 235 | }
|
---|
| 236 | catch(e) {}
|
---|
| 237 |
|
---|
| 238 | // Ensure that the range comes from the editor document.
|
---|
| 239 | if ( range )
|
---|
| 240 | {
|
---|
| 241 | if ( range.parentElement && FCKTools.GetElementDocument( range.parentElement() ) != editorDocument )
|
---|
| 242 | range = null ;
|
---|
| 243 | else if ( range.item && FCKTools.GetElementDocument( range.item(0) )!= editorDocument )
|
---|
| 244 | range = null ;
|
---|
| 245 | }
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | this.SelectionData = range ;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | FCKSelection._GetSelectionDocument = function( selection )
|
---|
| 252 | {
|
---|
| 253 | var range = selection.createRange() ;
|
---|
| 254 | if ( !range )
|
---|
| 255 | return null;
|
---|
| 256 | else if ( range.item )
|
---|
| 257 | return FCKTools.GetElementDocument( range.item( 0 ) ) ;
|
---|
| 258 | else
|
---|
| 259 | return FCKTools.GetElementDocument( range.parentElement() ) ;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | FCKSelection.Restore = function()
|
---|
| 263 | {
|
---|
| 264 | if ( this.SelectionData )
|
---|
| 265 | {
|
---|
| 266 | FCK.IsSelectionChangeLocked = true ;
|
---|
| 267 |
|
---|
| 268 | try
|
---|
| 269 | {
|
---|
| 270 | // Don't repeat the restore process if the editor document is already selected.
|
---|
| 271 | if ( String( this._GetSelectionDocument( FCK.EditorDocument.selection ).body.contentEditable ) == 'true' )
|
---|
| 272 | {
|
---|
| 273 | FCK.IsSelectionChangeLocked = false ;
|
---|
| 274 | return ;
|
---|
| 275 | }
|
---|
| 276 | this.SelectionData.select() ;
|
---|
| 277 | }
|
---|
| 278 | catch ( e ) {}
|
---|
| 279 |
|
---|
| 280 | FCK.IsSelectionChangeLocked = false ;
|
---|
| 281 | }
|
---|
| 282 | }
|
---|
| 283 |
|
---|
| 284 | FCKSelection.Release = function()
|
---|
| 285 | {
|
---|
| 286 | this.locked = false ;
|
---|
| 287 | delete this.SelectionData ;
|
---|
| 288 | }
|
---|