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. (IE specific implementation)
22 */
23
24// Get the selection type.
25FCKSelection.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 = FCK.EditorDocument.selection.type ;
32		if ( ieType == 'Control' || ieType == 'Text' )
33			return ieType ;
34
35		if ( FCK.EditorDocument.selection.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.
48FCKSelection.GetSelectedElement = function()
49{
50	if ( this.GetType() == 'Control' )
51	{
52		var oRange = FCK.EditorDocument.selection.createRange() ;
53
54		if ( oRange && oRange.item )
55			return FCK.EditorDocument.selection.createRange().item(0) ;
56	}
57	return null ;
58} ;
59
60FCKSelection.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 FCK.EditorDocument.selection.createRange().parentElement() ;
73	}
74} ;
75
76FCKSelection.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
101FCKSelection.SelectNode = function( node )
102{
103	FCK.Focus() ;
104	FCK.EditorDocument.selection.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	}
112	catch(e)
113	{
114		// If failed, select it as a text range.
115		oRange = FCK.EditorDocument.body.createTextRange() ;
116		oRange.moveToElementText( node ) ;
117	}
118
119	oRange.select() ;
120} ;
121
122FCKSelection.Collapse = function( toStart )
123{
124	FCK.Focus() ;
125	if ( this.GetType() == 'Text' )
126	{
127		var oRange = FCK.EditorDocument.selection.createRange() ;
128		oRange.collapse( toStart == null || toStart === true ) ;
129		oRange.select() ;
130	}
131} ;
132
133// The "nodeTagName" parameter must be Upper Case.
134FCKSelection.HasAncestorNode = function( nodeTagName )
135{
136	var oContainer ;
137
138	if ( FCK.EditorDocument.selection.type == "Control" )
139	{
140		oContainer = this.GetSelectedElement() ;
141	}
142	else
143	{
144		var oRange  = FCK.EditorDocument.selection.createRange() ;
145		oContainer = oRange.parentElement() ;
146	}
147
148	while ( oContainer )
149	{
150		if ( oContainer.tagName == nodeTagName ) return true ;
151		oContainer = oContainer.parentNode ;
152	}
153
154	return false ;
155} ;
156
157// The "nodeTagName" parameter must be UPPER CASE.
158FCKSelection.MoveToAncestorNode = function( nodeTagName )
159{
160	var oNode, oRange ;
161
162	if ( ! FCK.EditorDocument )
163		return null ;
164
165	if ( FCK.EditorDocument.selection.type == "Control" )
166	{
167		oRange = FCK.EditorDocument.selection.createRange() ;
168		for ( i = 0 ; i < oRange.length ; i++ )
169		{
170			if (oRange(i).parentNode)
171			{
172				oNode = oRange(i).parentNode ;
173				break ;
174			}
175		}
176	}
177	else
178	{
179		oRange  = FCK.EditorDocument.selection.createRange() ;
180		oNode = oRange.parentElement() ;
181	}
182
183	while ( oNode && oNode.nodeName != nodeTagName )
184		oNode = oNode.parentNode ;
185
186	return oNode ;
187} ;
188
189FCKSelection.Delete = function()
190{
191	// Gets the actual selection.
192	var oSel = FCK.EditorDocument.selection ;
193
194	// Deletes the actual selection contents.
195	if ( oSel.type.toLowerCase() != "none" )
196	{
197		oSel.clear() ;
198	}
199
200	return oSel ;
201} ;
202