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 | * Active selection functions. (Gecko specific implementation) |
---|
22 | */ |
---|
23 | |
---|
24 | // Get the selection type (like document.select.type in IE). |
---|
25 | FCKSelection.GetType = function() |
---|
26 | { |
---|
27 | // if ( ! this._Type ) |
---|
28 | // { |
---|
29 | // By default set the type to "Text". |
---|
30 | this._Type = 'Text' ; |
---|
31 | |
---|
32 | // Check if the actual selection is a Control (IMG, TABLE, HR, etc...). |
---|
33 | var oSel ; |
---|
34 | try { oSel = FCK.EditorWindow.getSelection() ; } |
---|
35 | catch (e) {} |
---|
36 | |
---|
37 | if ( oSel && oSel.rangeCount == 1 ) |
---|
38 | { |
---|
39 | var oRange = oSel.getRangeAt(0) ; |
---|
40 | if ( oRange.startContainer == oRange.endContainer && (oRange.endOffset - oRange.startOffset) == 1 && oRange.startContainer.nodeType != Node.TEXT_NODE ) |
---|
41 | this._Type = 'Control' ; |
---|
42 | } |
---|
43 | // } |
---|
44 | return this._Type ; |
---|
45 | } |
---|
46 | |
---|
47 | // Retrieves the selected element (if any), just in the case that a single |
---|
48 | // element (object like and image or a table) is selected. |
---|
49 | FCKSelection.GetSelectedElement = function() |
---|
50 | { |
---|
51 | if ( this.GetType() == 'Control' ) |
---|
52 | { |
---|
53 | var oSel = FCK.EditorWindow.getSelection() ; |
---|
54 | return oSel.anchorNode.childNodes[ oSel.anchorOffset ] ; |
---|
55 | } |
---|
56 | return null ; |
---|
57 | } |
---|
58 | |
---|
59 | FCKSelection.GetParentElement = function() |
---|
60 | { |
---|
61 | if ( this.GetType() == 'Control' ) |
---|
62 | return FCKSelection.GetSelectedElement().parentNode ; |
---|
63 | else |
---|
64 | { |
---|
65 | var oSel = FCK.EditorWindow.getSelection() ; |
---|
66 | if ( oSel ) |
---|
67 | { |
---|
68 | var oNode = oSel.anchorNode ; |
---|
69 | |
---|
70 | while ( oNode && oNode.nodeType != 1 ) |
---|
71 | oNode = oNode.parentNode ; |
---|
72 | |
---|
73 | return oNode ; |
---|
74 | } |
---|
75 | } |
---|
76 | return null ; |
---|
77 | } |
---|
78 | |
---|
79 | FCKSelection.SelectNode = function( element ) |
---|
80 | { |
---|
81 | // FCK.Focus() ; |
---|
82 | |
---|
83 | var oRange = FCK.EditorDocument.createRange() ; |
---|
84 | oRange.selectNode( element ) ; |
---|
85 | |
---|
86 | var oSel = FCK.EditorWindow.getSelection() ; |
---|
87 | oSel.removeAllRanges() ; |
---|
88 | oSel.addRange( oRange ) ; |
---|
89 | } |
---|
90 | |
---|
91 | FCKSelection.Collapse = function( toStart ) |
---|
92 | { |
---|
93 | var oSel = FCK.EditorWindow.getSelection() ; |
---|
94 | |
---|
95 | if ( toStart == null || toStart === true ) |
---|
96 | oSel.collapseToStart() ; |
---|
97 | else |
---|
98 | oSel.collapseToEnd() ; |
---|
99 | } |
---|
100 | |
---|
101 | // The "nodeTagName" parameter must be Upper Case. |
---|
102 | FCKSelection.HasAncestorNode = function( nodeTagName ) |
---|
103 | { |
---|
104 | var oContainer = this.GetSelectedElement() ; |
---|
105 | if ( ! oContainer && FCK.EditorWindow ) |
---|
106 | { |
---|
107 | try { oContainer = FCK.EditorWindow.getSelection().getRangeAt(0).startContainer ; } |
---|
108 | catch(e){} |
---|
109 | } |
---|
110 | |
---|
111 | while ( oContainer ) |
---|
112 | { |
---|
113 | if ( oContainer.nodeType == 1 && oContainer.tagName == nodeTagName ) return true ; |
---|
114 | oContainer = oContainer.parentNode ; |
---|
115 | } |
---|
116 | |
---|
117 | return false ; |
---|
118 | } |
---|
119 | |
---|
120 | // The "nodeTagName" parameter must be Upper Case. |
---|
121 | FCKSelection.MoveToAncestorNode = function( nodeTagName ) |
---|
122 | { |
---|
123 | var oNode ; |
---|
124 | |
---|
125 | var oContainer = this.GetSelectedElement() ; |
---|
126 | if ( ! oContainer ) |
---|
127 | oContainer = FCK.EditorWindow.getSelection().getRangeAt(0).startContainer ; |
---|
128 | |
---|
129 | while ( oContainer ) |
---|
130 | { |
---|
131 | if ( oContainer.nodeName == nodeTagName ) |
---|
132 | return oContainer ; |
---|
133 | |
---|
134 | oContainer = oContainer.parentNode ; |
---|
135 | } |
---|
136 | return null ; |
---|
137 | } |
---|
138 | |
---|
139 | FCKSelection.Delete = function() |
---|
140 | { |
---|
141 | // Gets the actual selection. |
---|
142 | var oSel = FCK.EditorWindow.getSelection() ; |
---|
143 | |
---|
144 | // Deletes the actual selection contents. |
---|
145 | for ( var i = 0 ; i < oSel.rangeCount ; i++ ) |
---|
146 | { |
---|
147 | oSel.getRangeAt(i).deleteContents() ; |
---|
148 | } |
---|
149 | |
---|
150 | return oSel ; |
---|
151 | } |
---|