[389] | 1 | /* |
---|
| 2 | * FCKeditor - The text editor for internet |
---|
| 3 | * Copyright (C) 2003-2006 Frederico Caldeira Knabben |
---|
| 4 | * |
---|
| 5 | * Licensed under the terms of the GNU Lesser General Public License: |
---|
| 6 | * http://www.opensource.org/licenses/lgpl-license.php |
---|
| 7 | * |
---|
| 8 | * For further information visit: |
---|
| 9 | * http://www.fckeditor.net/ |
---|
| 10 | * |
---|
| 11 | * "Support Open Source software. What about a donation today?" |
---|
| 12 | * |
---|
| 13 | * File Name: fckselection_gecko.js |
---|
| 14 | * Active selection functions. (Gecko specific implementation) |
---|
| 15 | * |
---|
| 16 | * File Authors: |
---|
| 17 | * Frederico Caldeira Knabben (fredck@fckeditor.net) |
---|
| 18 | */ |
---|
| 19 | |
---|
| 20 | // Get the selection type (like document.select.type in IE). |
---|
| 21 | FCKSelection.GetType = function() |
---|
| 22 | { |
---|
| 23 | // if ( ! this._Type ) |
---|
| 24 | // { |
---|
| 25 | // By default set the type to "Text". |
---|
| 26 | this._Type = 'Text' ; |
---|
| 27 | |
---|
| 28 | // Check if the actual selection is a Control (IMG, TABLE, HR, etc...). |
---|
| 29 | var oSel ; |
---|
| 30 | try { oSel = FCK.EditorWindow.getSelection() ; } |
---|
| 31 | catch (e) {} |
---|
| 32 | |
---|
| 33 | if ( oSel && oSel.rangeCount == 1 ) |
---|
| 34 | { |
---|
| 35 | var oRange = oSel.getRangeAt(0) ; |
---|
| 36 | if ( oRange.startContainer == oRange.endContainer && (oRange.endOffset - oRange.startOffset) == 1 && oRange.startContainer.nodeType != Node.TEXT_NODE ) |
---|
| 37 | this._Type = 'Control' ; |
---|
| 38 | } |
---|
| 39 | // } |
---|
| 40 | return this._Type ; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | // Retrieves the selected element (if any), just in the case that a single |
---|
| 44 | // element (object like and image or a table) is selected. |
---|
| 45 | FCKSelection.GetSelectedElement = function() |
---|
| 46 | { |
---|
| 47 | if ( this.GetType() == 'Control' ) |
---|
| 48 | { |
---|
| 49 | var oSel = FCK.EditorWindow.getSelection() ; |
---|
| 50 | return oSel.anchorNode.childNodes[ oSel.anchorOffset ] ; |
---|
| 51 | } |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | FCKSelection.GetParentElement = function() |
---|
| 55 | { |
---|
| 56 | if ( this.GetType() == 'Control' ) |
---|
| 57 | return FCKSelection.GetSelectedElement().parentNode ; |
---|
| 58 | else |
---|
| 59 | { |
---|
| 60 | var oSel = FCK.EditorWindow.getSelection() ; |
---|
| 61 | if ( oSel ) |
---|
| 62 | { |
---|
| 63 | var oNode = oSel.anchorNode ; |
---|
| 64 | |
---|
| 65 | while ( oNode && oNode.nodeType != 1 ) |
---|
| 66 | oNode = oNode.parentNode ; |
---|
| 67 | |
---|
| 68 | return oNode ; |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | FCKSelection.SelectNode = function( element ) |
---|
| 74 | { |
---|
| 75 | // FCK.Focus() ; |
---|
| 76 | |
---|
| 77 | var oRange = FCK.EditorDocument.createRange() ; |
---|
| 78 | oRange.selectNode( element ) ; |
---|
| 79 | |
---|
| 80 | var oSel = FCK.EditorWindow.getSelection() ; |
---|
| 81 | oSel.removeAllRanges() ; |
---|
| 82 | oSel.addRange( oRange ) ; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | FCKSelection.Collapse = function( toStart ) |
---|
| 86 | { |
---|
| 87 | var oSel = FCK.EditorWindow.getSelection() ; |
---|
| 88 | |
---|
| 89 | if ( toStart == null || toStart === true ) |
---|
| 90 | oSel.collapseToStart() ; |
---|
| 91 | else |
---|
| 92 | oSel.collapseToEnd() ; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | // The "nodeTagName" parameter must be Upper Case. |
---|
| 96 | FCKSelection.HasAncestorNode = function( nodeTagName ) |
---|
| 97 | { |
---|
| 98 | var oContainer = this.GetSelectedElement() ; |
---|
| 99 | if ( ! oContainer && FCK.EditorWindow ) |
---|
| 100 | { |
---|
| 101 | try { oContainer = FCK.EditorWindow.getSelection().getRangeAt(0).startContainer ; } |
---|
| 102 | catch(e){} |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | while ( oContainer ) |
---|
| 106 | { |
---|
| 107 | if ( oContainer.nodeType == 1 && oContainer.tagName == nodeTagName ) return true ; |
---|
| 108 | oContainer = oContainer.parentNode ; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | return false ; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | // The "nodeTagName" parameter must be Upper Case. |
---|
| 115 | FCKSelection.MoveToAncestorNode = function( nodeTagName ) |
---|
| 116 | { |
---|
| 117 | var oNode ; |
---|
| 118 | |
---|
| 119 | var oContainer = this.GetSelectedElement() ; |
---|
| 120 | if ( ! oContainer ) |
---|
| 121 | oContainer = FCK.EditorWindow.getSelection().getRangeAt(0).startContainer ; |
---|
| 122 | |
---|
| 123 | while ( oContainer ) |
---|
| 124 | { |
---|
| 125 | if ( oContainer.tagName == nodeTagName ) |
---|
| 126 | return oContainer ; |
---|
| 127 | oContainer = oContainer.parentNode ; |
---|
| 128 | } |
---|
| 129 | return null ; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | FCKSelection.Delete = function() |
---|
| 133 | { |
---|
| 134 | // Gets the actual selection. |
---|
| 135 | var oSel = FCK.EditorWindow.getSelection() ; |
---|
| 136 | |
---|
| 137 | // Deletes the actual selection contents. |
---|
| 138 | for ( var i = 0 ; i < oSel.rangeCount ; i++ ) |
---|
| 139 | { |
---|
| 140 | oSel.getRangeAt(i).deleteContents() ; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | return oSel ; |
---|
| 144 | } |
---|