]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dijit/_editor/selection.js
add Dojo 1.1.1
[eow] / static / dojo-release-1.1.1 / dijit / _editor / selection.js
1 if(!dojo._hasResource["dijit._editor.selection"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dijit._editor.selection"] = true;
3 dojo.provide("dijit._editor.selection");
4
5 // FIXME:
6 //              all of these methods branch internally for IE. This is probably
7 //              sub-optimal in terms of runtime performance. We should investigate the
8 //              size difference for differentiating at definition time.
9
10 dojo.mixin(dijit._editor.selection, {
11         getType: function(){
12                 // summary: Get the selection type (like dojo.doc.select.type in IE).
13                 if(dojo.doc.selection){ //IE
14                         return dojo.doc.selection.type.toLowerCase();
15                 }else{
16                         var stype = "text";
17
18                         // Check if the actual selection is a CONTROL (IMG, TABLE, HR, etc...).
19                         var oSel;
20                         try{
21                                 oSel = dojo.global.getSelection();
22                         }catch(e){ /*squelch*/ }
23
24                         if(oSel && oSel.rangeCount==1){
25                                 var oRange = oSel.getRangeAt(0);
26                                 if(     (oRange.startContainer == oRange.endContainer) &&
27                                         ((oRange.endOffset - oRange.startOffset) == 1) &&
28                                         (oRange.startContainer.nodeType != 3 /* text node*/)
29                                 ){
30                                         stype = "control";
31                                 }
32                         }
33                         return stype;
34                 }
35         },
36
37         getSelectedText: function(){
38                 // summary:
39                 //              Return the text (no html tags) included in the current selection or null if no text is selected
40                 if(dojo.doc.selection){ //IE
41                         if(dijit._editor.selection.getType() == 'control'){
42                                 return null;
43                         }
44                         return dojo.doc.selection.createRange().text;
45                 }else{
46                         var selection = dojo.global.getSelection();
47                         if(selection){
48                                 return selection.toString();
49                         }
50                 }
51                 return ''
52         },
53
54         getSelectedHtml: function(){
55                 // summary:
56                 //              Return the html of the current selection or null if unavailable
57                 if(dojo.doc.selection){ //IE
58                         if(dijit._editor.selection.getType() == 'control'){
59                                 return null;
60                         }
61                         return dojo.doc.selection.createRange().htmlText;
62                 }else{
63                         var selection = dojo.global.getSelection();
64                         if(selection && selection.rangeCount){
65                                 var frag = selection.getRangeAt(0).cloneContents();
66                                 var div = dojo.doc.createElement("div");
67                                 div.appendChild(frag);
68                                 return div.innerHTML;
69                         }
70                         return null;
71                 }
72         },
73
74         getSelectedElement: function(){
75                 // summary:
76                 //              Retrieves the selected element (if any), just in the case that
77                 //              a single element (object like and image or a table) is
78                 //              selected.
79                 if(this.getType() == "control"){
80                         if(dojo.doc.selection){ //IE
81                                 var range = dojo.doc.selection.createRange();
82                                 if(range && range.item){
83                                         return dojo.doc.selection.createRange().item(0);
84                                 }
85                         }else{
86                                 var selection = dojo.global.getSelection();
87                                 return selection.anchorNode.childNodes[ selection.anchorOffset ];
88                         }
89                 }
90                 return null;
91         },
92
93         getParentElement: function(){
94                 // summary:
95                 //              Get the parent element of the current selection
96                 if(this.getType() == "control"){
97                         var p = this.getSelectedElement();
98                         if(p){ return p.parentNode; }
99                 }else{
100                         if(dojo.doc.selection){ //IE
101                                 return dojo.doc.selection.createRange().parentElement();
102                         }else{
103                                 var selection = dojo.global.getSelection();
104                                 if(selection){
105                                         var node = selection.anchorNode;
106
107                                         while(node && (node.nodeType != 1)){ // not an element
108                                                 node = node.parentNode;
109                                         }
110
111                                         return node;
112                                 }
113                         }
114                 }
115                 return null;
116         },
117
118         hasAncestorElement: function(/*String*/tagName /* ... */){
119                 // summary:
120                 //              Check whether current selection has a  parent element which is
121                 //              of type tagName (or one of the other specified tagName)
122                 return this.getAncestorElement.apply(this, arguments) != null;
123         },
124
125         getAncestorElement: function(/*String*/tagName /* ... */){
126                 // summary:
127                 //              Return the parent element of the current selection which is of
128                 //              type tagName (or one of the other specified tagName)
129
130                 var node = this.getSelectedElement() || this.getParentElement();
131                 return this.getParentOfType(node, arguments);
132         },
133
134         isTag: function(/*DomNode*/node, /*Array*/tags){
135                 if(node && node.tagName){
136                         var _nlc = node.tagName.toLowerCase();
137                         for(var i=0; i<tags.length; i++){
138                                 var _tlc = String(tags[i]).toLowerCase();
139                                 if(_nlc == _tlc){
140                                         return _tlc;
141                                 }
142                         }
143                 }
144                 return "";
145         },
146
147         getParentOfType: function(/*DomNode*/node, /*Array*/tags){
148                 while(node){
149                         if(this.isTag(node, tags).length){
150                                 return node;
151                         }
152                         node = node.parentNode;
153                 }
154                 return null;
155         },
156
157         collapse: function(/*Boolean*/beginning) {
158                 // summary: clear current selection
159           if(window['getSelection']){
160                   var selection = dojo.global.getSelection();
161                   if(selection.removeAllRanges){ // Mozilla
162                           if(beginning){
163                                   selection.collapseToStart();
164                           }else{
165                                   selection.collapseToEnd();
166                           }
167                   }else{ // Safari
168                           // pulled from WebCore/ecma/kjs_window.cpp, line 2536
169                            selection.collapse(beginning);
170                   }
171           }else if(dojo.doc.selection){ // IE
172                   var range = dojo.doc.selection.createRange();
173                   range.collapse(beginning);
174                   range.select();
175           }
176         },
177
178         remove: function(){
179                 // summary: delete current selection
180                 var _s = dojo.doc.selection;
181                 if(_s){ //IE
182                         if(_s.type.toLowerCase() != "none"){
183                                 _s.clear();
184                         }
185                         return _s;
186                 }else{
187                         _s = dojo.global.getSelection();
188                         _s.deleteFromDocument();
189                         return _s;
190                 }
191         },
192
193         selectElementChildren: function(/*DomNode*/element,/*Boolean?*/nochangefocus){
194                 // summary:
195                 //              clear previous selection and select the content of the node
196                 //              (excluding the node itself)
197                 var _window = dojo.global;
198                 var _document = dojo.doc;
199                 element = dojo.byId(element);
200                 if(_document.selection && dojo.body().createTextRange){ // IE
201                         var range = element.ownerDocument.body.createTextRange();
202                         range.moveToElementText(element);
203                         if(!nochangefocus){
204                                 try{
205                                         range.select(); // IE throws an exception here if the widget is hidden.  See #5439
206                                 }catch(e){ /* squelch */}
207                         }
208                 }else if(_window.getSelection){
209                         var selection = _window.getSelection();
210                         if(selection.setBaseAndExtent){ // Safari
211                                 selection.setBaseAndExtent(element, 0, element, element.innerText.length - 1);
212                         }else if(selection.selectAllChildren){ // Mozilla
213                                 selection.selectAllChildren(element);
214                         }
215                 }
216         },
217
218         selectElement: function(/*DomNode*/element,/*Boolean?*/nochangefocus){
219                 // summary:
220                 //              clear previous selection and select element (including all its children)
221                 var range, _document = dojo.doc;
222                 element = dojo.byId(element);
223                 if(_document.selection && dojo.body().createTextRange){ // IE
224                         try{
225                                 range = dojo.body().createControlRange();
226                                 range.addElement(element);
227                                 if(!nochangefocus){
228                                         range.select();
229                                 }
230                         }catch(e){
231                                 this.selectElementChildren(element,nochangefocus);
232                         }
233                 }else if(dojo.global.getSelection){
234                         var selection = dojo.global.getSelection();
235                         // FIXME: does this work on Safari?
236                         if(selection.removeAllRanges){ // Mozilla
237                                 range = _document.createRange();
238                                 range.selectNode(element);
239                                 selection.removeAllRanges();
240                                 selection.addRange(range);
241                         }
242                 }
243         }
244 });
245
246 }