root / trunk / web / dojo / dijit / _editor / selection.js
History | View | Annotate | Download (4.93 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
4 | see: http://dojotoolkit.org/license for details
|
||
5 | */
|
||
6 | |||
7 | |||
8 | if(!dojo._hasResource["dijit._editor.selection"]){ |
||
9 | dojo._hasResource["dijit._editor.selection"]=true; |
||
10 | dojo.provide("dijit._editor.selection");
|
||
11 | dojo.mixin(dijit._editor.selection,{getType:function(){ |
||
12 | if(dojo.isIE){
|
||
13 | return dojo.doc.selection.type.toLowerCase();
|
||
14 | }else{
|
||
15 | var _1="text"; |
||
16 | var _2;
|
||
17 | try{
|
||
18 | _2=dojo.global.getSelection(); |
||
19 | } |
||
20 | catch(e){
|
||
21 | } |
||
22 | if(_2&&_2.rangeCount==1){ |
||
23 | var _3=_2.getRangeAt(0); |
||
24 | if((_3.startContainer==_3.endContainer)&&((_3.endOffset-_3.startOffset)==1)&&(_3.startContainer.nodeType!=3)){ |
||
25 | _1="control";
|
||
26 | } |
||
27 | } |
||
28 | return _1;
|
||
29 | } |
||
30 | },getSelectedText:function(){ |
||
31 | if(dojo.isIE){
|
||
32 | if(dijit._editor.selection.getType()=="control"){ |
||
33 | return null; |
||
34 | } |
||
35 | return dojo.doc.selection.createRange().text;
|
||
36 | }else{
|
||
37 | var _4=dojo.global.getSelection();
|
||
38 | if(_4){
|
||
39 | return _4.toString();
|
||
40 | } |
||
41 | } |
||
42 | return ""; |
||
43 | },getSelectedHtml:function(){ |
||
44 | if(dojo.isIE){
|
||
45 | if(dijit._editor.selection.getType()=="control"){ |
||
46 | return null; |
||
47 | } |
||
48 | return dojo.doc.selection.createRange().htmlText;
|
||
49 | }else{
|
||
50 | var _5=dojo.global.getSelection();
|
||
51 | if(_5&&_5.rangeCount){
|
||
52 | var i;
|
||
53 | var _6=""; |
||
54 | for(i=0;i<_5.rangeCount;i++){ |
||
55 | var _7=_5.getRangeAt(i).cloneContents();
|
||
56 | var _8=dojo.doc.createElement("div"); |
||
57 | _8.appendChild(_7); |
||
58 | _6+=_8.innerHTML; |
||
59 | } |
||
60 | return _6;
|
||
61 | } |
||
62 | return null; |
||
63 | } |
||
64 | },getSelectedElement:function(){ |
||
65 | if(dijit._editor.selection.getType()=="control"){ |
||
66 | if(dojo.isIE){
|
||
67 | var _9=dojo.doc.selection.createRange();
|
||
68 | if(_9&&_9.item){
|
||
69 | return dojo.doc.selection.createRange().item(0); |
||
70 | } |
||
71 | }else{
|
||
72 | var _a=dojo.global.getSelection();
|
||
73 | return _a.anchorNode.childNodes[_a.anchorOffset];
|
||
74 | } |
||
75 | } |
||
76 | return null; |
||
77 | },getParentElement:function(){ |
||
78 | if(dijit._editor.selection.getType()=="control"){ |
||
79 | var p=this.getSelectedElement(); |
||
80 | if(p){
|
||
81 | return p.parentNode;
|
||
82 | } |
||
83 | }else{
|
||
84 | if(dojo.isIE){
|
||
85 | var r=dojo.doc.selection.createRange();
|
||
86 | r.collapse(true);
|
||
87 | return r.parentElement();
|
||
88 | }else{
|
||
89 | var _b=dojo.global.getSelection();
|
||
90 | if(_b){
|
||
91 | var _c=_b.anchorNode;
|
||
92 | while(_c&&(_c.nodeType!=1)){ |
||
93 | _c=_c.parentNode; |
||
94 | } |
||
95 | return _c;
|
||
96 | } |
||
97 | } |
||
98 | } |
||
99 | return null; |
||
100 | },hasAncestorElement:function(_d){ |
||
101 | return this.getAncestorElement.apply(this,arguments)!=null; |
||
102 | },getAncestorElement:function(_e){ |
||
103 | var _f=this.getSelectedElement()||this.getParentElement(); |
||
104 | return this.getParentOfType(_f,arguments); |
||
105 | },isTag:function(_10,_11){ |
||
106 | if(_10&&_10.tagName){
|
||
107 | var _12=_10.tagName.toLowerCase();
|
||
108 | for(var i=0;i<_11.length;i++){ |
||
109 | var _13=String(_11[i]).toLowerCase();
|
||
110 | if(_12==_13){
|
||
111 | return _13;
|
||
112 | } |
||
113 | } |
||
114 | } |
||
115 | return ""; |
||
116 | },getParentOfType:function(_14,_15){ |
||
117 | while(_14){
|
||
118 | if(this.isTag(_14,_15).length){ |
||
119 | return _14;
|
||
120 | } |
||
121 | _14=_14.parentNode; |
||
122 | } |
||
123 | return null; |
||
124 | },collapse:function(_16){ |
||
125 | if(window.getSelection){
|
||
126 | var _17=dojo.global.getSelection();
|
||
127 | if(_17.removeAllRanges){
|
||
128 | if(_16){
|
||
129 | _17.collapseToStart(); |
||
130 | }else{
|
||
131 | _17.collapseToEnd(); |
||
132 | } |
||
133 | }else{
|
||
134 | _17.collapse(_16); |
||
135 | } |
||
136 | }else{
|
||
137 | if(dojo.isIE){
|
||
138 | var _18=dojo.doc.selection.createRange();
|
||
139 | _18.collapse(_16); |
||
140 | _18.select(); |
||
141 | } |
||
142 | } |
||
143 | },remove:function(){ |
||
144 | var sel=dojo.doc.selection;
|
||
145 | if(dojo.isIE){
|
||
146 | if(sel.type.toLowerCase()!="none"){ |
||
147 | sel.clear(); |
||
148 | } |
||
149 | return sel;
|
||
150 | }else{
|
||
151 | sel=dojo.global.getSelection(); |
||
152 | sel.deleteFromDocument(); |
||
153 | return sel;
|
||
154 | } |
||
155 | },selectElementChildren:function(_19,_1a){ |
||
156 | var win=dojo.global;
|
||
157 | var doc=dojo.doc;
|
||
158 | var _1b;
|
||
159 | _19=dojo.byId(_19); |
||
160 | if(doc.selection&&dojo.isIE&&dojo.body().createTextRange){
|
||
161 | _1b=_19.ownerDocument.body.createTextRange(); |
||
162 | _1b.moveToElementText(_19); |
||
163 | if(!_1a){
|
||
164 | try{
|
||
165 | _1b.select(); |
||
166 | } |
||
167 | catch(e){
|
||
168 | } |
||
169 | } |
||
170 | }else{
|
||
171 | if(win.getSelection){
|
||
172 | var _1c=dojo.global.getSelection();
|
||
173 | if(dojo.isOpera){
|
||
174 | if(_1c.rangeCount){
|
||
175 | _1b=_1c.getRangeAt(0);
|
||
176 | }else{
|
||
177 | _1b=doc.createRange(); |
||
178 | } |
||
179 | _1b.setStart(_19,0);
|
||
180 | _1b.setEnd(_19,(_19.nodeType==3)?_19.length:_19.childNodes.length);
|
||
181 | _1c.addRange(_1b); |
||
182 | }else{
|
||
183 | _1c.selectAllChildren(_19); |
||
184 | } |
||
185 | } |
||
186 | } |
||
187 | },selectElement:function(_1d,_1e){ |
||
188 | var _1f;
|
||
189 | var doc=dojo.doc;
|
||
190 | var win=dojo.global;
|
||
191 | _1d=dojo.byId(_1d); |
||
192 | if(dojo.isIE&&dojo.body().createTextRange){
|
||
193 | try{
|
||
194 | _1f=dojo.body().createControlRange(); |
||
195 | _1f.addElement(_1d); |
||
196 | if(!_1e){
|
||
197 | _1f.select(); |
||
198 | } |
||
199 | } |
||
200 | catch(e){
|
||
201 | this.selectElementChildren(_1d,_1e);
|
||
202 | } |
||
203 | }else{
|
||
204 | if(dojo.global.getSelection){
|
||
205 | var _20=win.getSelection();
|
||
206 | _1f=doc.createRange(); |
||
207 | if(_20.removeAllRanges){
|
||
208 | if(dojo.isOpera){
|
||
209 | if(_20.getRangeAt(0)){ |
||
210 | _1f=_20.getRangeAt(0);
|
||
211 | } |
||
212 | } |
||
213 | _1f.selectNode(_1d); |
||
214 | _20.removeAllRanges(); |
||
215 | _20.addRange(_1f); |
||
216 | } |
||
217 | } |
||
218 | } |
||
219 | },inSelection:function(_21){ |
||
220 | if(_21){
|
||
221 | var _22;
|
||
222 | var doc=dojo.doc;
|
||
223 | var _23;
|
||
224 | if(dojo.global.getSelection){
|
||
225 | var sel=dojo.global.getSelection();
|
||
226 | if(sel&&sel.rangeCount>0){ |
||
227 | _23=sel.getRangeAt(0);
|
||
228 | } |
||
229 | if(_23&&_23.compareBoundaryPoints&&doc.createRange){
|
||
230 | try{
|
||
231 | _22=doc.createRange(); |
||
232 | _22.setStart(_21,0);
|
||
233 | if(_23.compareBoundaryPoints(_23.START_TO_END,_22)===1){ |
||
234 | return true; |
||
235 | } |
||
236 | } |
||
237 | catch(e){
|
||
238 | } |
||
239 | } |
||
240 | }else{
|
||
241 | if(doc.selection){
|
||
242 | _23=doc.selection.createRange(); |
||
243 | try{
|
||
244 | _22=_21.ownerDocument.body.createControlRange(); |
||
245 | if(_22){
|
||
246 | _22.addElement(_21); |
||
247 | } |
||
248 | } |
||
249 | catch(e1){
|
||
250 | try{
|
||
251 | _22=_21.ownerDocument.body.createTextRange(); |
||
252 | _22.moveToElementText(_21); |
||
253 | } |
||
254 | catch(e2){
|
||
255 | } |
||
256 | } |
||
257 | if(_23&&_22){
|
||
258 | if(_23.compareEndPoints("EndToStart",_22)===1){ |
||
259 | return true; |
||
260 | } |
||
261 | } |
||
262 | } |
||
263 | } |
||
264 | } |
||
265 | return false; |
||
266 | }}); |
||
267 | } |