2150 |
mathias |
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 document.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 |
},
|
|
|
52 |
|
|
|
53 |
getSelectedHtml: function(){
|
|
|
54 |
// summary:
|
|
|
55 |
// Return the html of the current selection or null if unavailable
|
|
|
56 |
if(dojo.doc["selection"]){ //IE
|
|
|
57 |
if(dijit._editor.selection.getType() == 'control'){
|
|
|
58 |
return null;
|
|
|
59 |
}
|
|
|
60 |
return dojo.doc.selection.createRange().htmlText;
|
|
|
61 |
}else{
|
|
|
62 |
var selection = dojo.global.getSelection();
|
|
|
63 |
if(selection && selection.rangeCount){
|
|
|
64 |
var frag = selection.getRangeAt(0).cloneContents();
|
|
|
65 |
var div = document.createElement("div");
|
|
|
66 |
div.appendChild(frag);
|
|
|
67 |
return div.innerHTML;
|
|
|
68 |
}
|
|
|
69 |
return null;
|
|
|
70 |
}
|
|
|
71 |
},
|
|
|
72 |
|
|
|
73 |
getSelectedElement: function(){
|
|
|
74 |
// summary:
|
|
|
75 |
// Retrieves the selected element (if any), just in the case that
|
|
|
76 |
// a single element (object like and image or a table) is
|
|
|
77 |
// selected.
|
|
|
78 |
if(this.getType() == "control"){
|
|
|
79 |
if(dojo.doc["selection"]){ //IE
|
|
|
80 |
var range = dojo.doc.selection.createRange();
|
|
|
81 |
if(range && range.item){
|
|
|
82 |
return dojo.doc.selection.createRange().item(0);
|
|
|
83 |
}
|
|
|
84 |
}else{
|
|
|
85 |
var selection = dojo.global.getSelection();
|
|
|
86 |
return selection.anchorNode.childNodes[ selection.anchorOffset ];
|
|
|
87 |
}
|
|
|
88 |
}
|
|
|
89 |
},
|
|
|
90 |
|
|
|
91 |
getParentElement: function(){
|
|
|
92 |
// summary:
|
|
|
93 |
// Get the parent element of the current selection
|
|
|
94 |
if(this.getType() == "control"){
|
|
|
95 |
var p = this.getSelectedElement();
|
|
|
96 |
if(p){ return p.parentNode; }
|
|
|
97 |
}else{
|
|
|
98 |
if(dojo.doc["selection"]){ //IE
|
|
|
99 |
return dojo.doc.selection.createRange().parentElement();
|
|
|
100 |
}else{
|
|
|
101 |
var selection = dojo.global.getSelection();
|
|
|
102 |
if(selection){
|
|
|
103 |
var node = selection.anchorNode;
|
|
|
104 |
|
|
|
105 |
while(node && (node.nodeType != 1)){ // not an element
|
|
|
106 |
node = node.parentNode;
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
return node;
|
|
|
110 |
}
|
|
|
111 |
}
|
|
|
112 |
}
|
|
|
113 |
},
|
|
|
114 |
|
|
|
115 |
hasAncestorElement: function(/*String*/tagName /* ... */){
|
|
|
116 |
// summary:
|
|
|
117 |
// Check whether current selection has a parent element which is
|
|
|
118 |
// of type tagName (or one of the other specified tagName)
|
|
|
119 |
return (this.getAncestorElement.apply(this, arguments) != null);
|
|
|
120 |
},
|
|
|
121 |
|
|
|
122 |
getAncestorElement: function(/*String*/tagName /* ... */){
|
|
|
123 |
// summary:
|
|
|
124 |
// Return the parent element of the current selection which is of
|
|
|
125 |
// type tagName (or one of the other specified tagName)
|
|
|
126 |
|
|
|
127 |
var node = this.getSelectedElement() || this.getParentElement();
|
|
|
128 |
return this.getParentOfType(node, arguments);
|
|
|
129 |
},
|
|
|
130 |
|
|
|
131 |
isTag: function(/*DomNode*/node, /*Array*/tags){
|
|
|
132 |
if(node && node.tagName){
|
|
|
133 |
var _nlc = node.tagName.toLowerCase();
|
|
|
134 |
for(var i=0; i<tags.length; i++){
|
|
|
135 |
var _tlc = String(tags[i]).toLowerCase();
|
|
|
136 |
if(_nlc == _tlc){
|
|
|
137 |
return _tlc;
|
|
|
138 |
}
|
|
|
139 |
}
|
|
|
140 |
}
|
|
|
141 |
return "";
|
|
|
142 |
},
|
|
|
143 |
|
|
|
144 |
getParentOfType: function(/*DomNode*/node, /*Array*/tags){
|
|
|
145 |
while(node){
|
|
|
146 |
if(this.isTag(node, tags).length){
|
|
|
147 |
return node;
|
|
|
148 |
}
|
|
|
149 |
node = node.parentNode;
|
|
|
150 |
}
|
|
|
151 |
return null;
|
|
|
152 |
},
|
|
|
153 |
|
|
|
154 |
remove: function(){
|
|
|
155 |
// summary: delete current selection
|
|
|
156 |
var _s = dojo.doc.selection;
|
|
|
157 |
if(_s){ //IE
|
|
|
158 |
if(_s.type.toLowerCase() != "none"){
|
|
|
159 |
_s.clear();
|
|
|
160 |
}
|
|
|
161 |
return _s;
|
|
|
162 |
}else{
|
|
|
163 |
_s = dojo.global.getSelection();
|
|
|
164 |
_s.deleteFromDocument();
|
|
|
165 |
return _s;
|
|
|
166 |
}
|
|
|
167 |
},
|
|
|
168 |
|
|
|
169 |
selectElementChildren: function(/*DomNode*/element,/*Boolean?*/nochangefocus){
|
|
|
170 |
// summary:
|
|
|
171 |
// clear previous selection and select the content of the node
|
|
|
172 |
// (excluding the node itself)
|
|
|
173 |
var _window = dojo.global;
|
|
|
174 |
var _document = dojo.doc;
|
|
|
175 |
element = dojo.byId(element);
|
|
|
176 |
if(_document.selection && dojo.body().createTextRange){ // IE
|
|
|
177 |
var range = element.ownerDocument.body.createTextRange();
|
|
|
178 |
range.moveToElementText(element);
|
|
|
179 |
if(!nochangefocus){
|
|
|
180 |
range.select();
|
|
|
181 |
}
|
|
|
182 |
}else if(_window["getSelection"]){
|
|
|
183 |
var selection = _window.getSelection();
|
|
|
184 |
if(selection["setBaseAndExtent"]){ // Safari
|
|
|
185 |
selection.setBaseAndExtent(element, 0, element, element.innerText.length - 1);
|
|
|
186 |
}else if(selection["selectAllChildren"]){ // Mozilla
|
|
|
187 |
selection.selectAllChildren(element);
|
|
|
188 |
}
|
|
|
189 |
}
|
|
|
190 |
},
|
|
|
191 |
|
|
|
192 |
selectElement: function(/*DomNode*/element,/*Boolean?*/nochangefocus){
|
|
|
193 |
// summary:
|
|
|
194 |
// clear previous selection and select element (including all its children)
|
|
|
195 |
var _document = dojo.doc;
|
|
|
196 |
element = dojo.byId(element);
|
|
|
197 |
if(_document.selection && dojo.body().createTextRange){ // IE
|
|
|
198 |
try{
|
|
|
199 |
var range = dojo.body().createControlRange();
|
|
|
200 |
range.addElement(element);
|
|
|
201 |
if(!nochangefocus){
|
|
|
202 |
range.select();
|
|
|
203 |
}
|
|
|
204 |
}catch(e){
|
|
|
205 |
this.selectElementChildren(element,nochangefocus);
|
|
|
206 |
}
|
|
|
207 |
}else if(dojo.global["getSelection"]){
|
|
|
208 |
var selection = dojo.global.getSelection();
|
|
|
209 |
// FIXME: does this work on Safari?
|
|
|
210 |
if(selection["removeAllRanges"]){ // Mozilla
|
|
|
211 |
var range = _document.createRange() ;
|
|
|
212 |
range.selectNode(element) ;
|
|
|
213 |
selection.removeAllRanges() ;
|
|
|
214 |
selection.addRange(range) ;
|
|
|
215 |
}
|
|
|
216 |
}
|
|
|
217 |
}
|
|
|
218 |
});
|
|
|
219 |
|
|
|
220 |
}
|