| 1318 |
alexandre_ |
1 |
/*
|
|
|
2 |
Copyright (c) 2004-2006, The Dojo Foundation
|
|
|
3 |
All Rights Reserved.
|
|
|
4 |
|
|
|
5 |
Licensed under the Academic Free License version 2.1 or above OR the
|
|
|
6 |
modified BSD license. For more information on Dojo licensing, see:
|
|
|
7 |
|
|
|
8 |
http://dojotoolkit.org/community/licensing.shtml
|
|
|
9 |
*/
|
|
|
10 |
|
|
|
11 |
dojo.require("dojo.html.common");
|
|
|
12 |
dojo.provide("dojo.html.selection");
|
|
|
13 |
dojo.require("dojo.dom");
|
|
|
14 |
dojo.require("dojo.lang.common");
|
|
|
15 |
dojo.html.selectionType = {NONE:0, TEXT:1, CONTROL:2};
|
|
|
16 |
dojo.html.clearSelection = function () {
|
|
|
17 |
var _window = dojo.global();
|
|
|
18 |
var _document = dojo.doc();
|
|
|
19 |
try {
|
|
|
20 |
if (_window["getSelection"]) {
|
|
|
21 |
if (dojo.render.html.safari) {
|
|
|
22 |
_window.getSelection().collapse();
|
|
|
23 |
} else {
|
|
|
24 |
_window.getSelection().removeAllRanges();
|
|
|
25 |
}
|
|
|
26 |
} else {
|
|
|
27 |
if (_document.selection) {
|
|
|
28 |
if (_document.selection.empty) {
|
|
|
29 |
_document.selection.empty();
|
|
|
30 |
} else {
|
|
|
31 |
if (_document.selection.clear) {
|
|
|
32 |
_document.selection.clear();
|
|
|
33 |
}
|
|
|
34 |
}
|
|
|
35 |
}
|
|
|
36 |
}
|
|
|
37 |
return true;
|
|
|
38 |
}
|
|
|
39 |
catch (e) {
|
|
|
40 |
dojo.debug(e);
|
|
|
41 |
return false;
|
|
|
42 |
}
|
|
|
43 |
};
|
|
|
44 |
dojo.html.disableSelection = function (element) {
|
|
|
45 |
element = dojo.byId(element) || dojo.body();
|
|
|
46 |
var h = dojo.render.html;
|
|
|
47 |
if (h.mozilla) {
|
|
|
48 |
element.style.MozUserSelect = "none";
|
|
|
49 |
} else {
|
|
|
50 |
if (h.safari) {
|
|
|
51 |
element.style.KhtmlUserSelect = "none";
|
|
|
52 |
} else {
|
|
|
53 |
if (h.ie) {
|
|
|
54 |
element.unselectable = "on";
|
|
|
55 |
} else {
|
|
|
56 |
return false;
|
|
|
57 |
}
|
|
|
58 |
}
|
|
|
59 |
}
|
|
|
60 |
return true;
|
|
|
61 |
};
|
|
|
62 |
dojo.html.enableSelection = function (element) {
|
|
|
63 |
element = dojo.byId(element) || dojo.body();
|
|
|
64 |
var h = dojo.render.html;
|
|
|
65 |
if (h.mozilla) {
|
|
|
66 |
element.style.MozUserSelect = "";
|
|
|
67 |
} else {
|
|
|
68 |
if (h.safari) {
|
|
|
69 |
element.style.KhtmlUserSelect = "";
|
|
|
70 |
} else {
|
|
|
71 |
if (h.ie) {
|
|
|
72 |
element.unselectable = "off";
|
|
|
73 |
} else {
|
|
|
74 |
return false;
|
|
|
75 |
}
|
|
|
76 |
}
|
|
|
77 |
}
|
|
|
78 |
return true;
|
|
|
79 |
};
|
|
|
80 |
dojo.html.selectElement = function (element) {
|
|
|
81 |
dojo.deprecated("dojo.html.selectElement", "replaced by dojo.html.selection.selectElementChildren", 0.5);
|
|
|
82 |
};
|
|
|
83 |
dojo.html.selectInputText = function (element) {
|
|
|
84 |
var _window = dojo.global();
|
|
|
85 |
var _document = dojo.doc();
|
|
|
86 |
element = dojo.byId(element);
|
|
|
87 |
if (_document["selection"] && dojo.body()["createTextRange"]) {
|
|
|
88 |
var range = element.createTextRange();
|
|
|
89 |
range.moveStart("character", 0);
|
|
|
90 |
range.moveEnd("character", element.value.length);
|
|
|
91 |
range.select();
|
|
|
92 |
} else {
|
|
|
93 |
if (_window["getSelection"]) {
|
|
|
94 |
var selection = _window.getSelection();
|
|
|
95 |
element.setSelectionRange(0, element.value.length);
|
|
|
96 |
}
|
|
|
97 |
}
|
|
|
98 |
element.focus();
|
|
|
99 |
};
|
|
|
100 |
dojo.html.isSelectionCollapsed = function () {
|
|
|
101 |
dojo.deprecated("dojo.html.isSelectionCollapsed", "replaced by dojo.html.selection.isCollapsed", 0.5);
|
|
|
102 |
return dojo.html.selection.isCollapsed();
|
|
|
103 |
};
|
|
|
104 |
dojo.lang.mixin(dojo.html.selection, {getType:function () {
|
|
|
105 |
if (dojo.doc()["selection"]) {
|
|
|
106 |
return dojo.html.selectionType[dojo.doc().selection.type.toUpperCase()];
|
|
|
107 |
} else {
|
|
|
108 |
var stype = dojo.html.selectionType.TEXT;
|
|
|
109 |
var oSel;
|
|
|
110 |
try {
|
|
|
111 |
oSel = dojo.global().getSelection();
|
|
|
112 |
}
|
|
|
113 |
catch (e) {
|
|
|
114 |
}
|
|
|
115 |
if (oSel && oSel.rangeCount == 1) {
|
|
|
116 |
var oRange = oSel.getRangeAt(0);
|
|
|
117 |
if (oRange.startContainer == oRange.endContainer && (oRange.endOffset - oRange.startOffset) == 1 && oRange.startContainer.nodeType != dojo.dom.TEXT_NODE) {
|
|
|
118 |
stype = dojo.html.selectionType.CONTROL;
|
|
|
119 |
}
|
|
|
120 |
}
|
|
|
121 |
return stype;
|
|
|
122 |
}
|
|
|
123 |
}, isCollapsed:function () {
|
|
|
124 |
var _window = dojo.global();
|
|
|
125 |
var _document = dojo.doc();
|
|
|
126 |
if (_document["selection"]) {
|
|
|
127 |
return _document.selection.createRange().text == "";
|
|
|
128 |
} else {
|
|
|
129 |
if (_window["getSelection"]) {
|
|
|
130 |
var selection = _window.getSelection();
|
|
|
131 |
if (dojo.lang.isString(selection)) {
|
|
|
132 |
return selection == "";
|
|
|
133 |
} else {
|
|
|
134 |
return selection.isCollapsed || selection.toString() == "";
|
|
|
135 |
}
|
|
|
136 |
}
|
|
|
137 |
}
|
|
|
138 |
}, getSelectedElement:function () {
|
|
|
139 |
if (dojo.html.selection.getType() == dojo.html.selectionType.CONTROL) {
|
|
|
140 |
if (dojo.doc()["selection"]) {
|
|
|
141 |
var range = dojo.doc().selection.createRange();
|
|
|
142 |
if (range && range.item) {
|
|
|
143 |
return dojo.doc().selection.createRange().item(0);
|
|
|
144 |
}
|
|
|
145 |
} else {
|
|
|
146 |
var selection = dojo.global().getSelection();
|
|
|
147 |
return selection.anchorNode.childNodes[selection.anchorOffset];
|
|
|
148 |
}
|
|
|
149 |
}
|
|
|
150 |
}, getParentElement:function () {
|
|
|
151 |
if (dojo.html.selection.getType() == dojo.html.selectionType.CONTROL) {
|
|
|
152 |
var p = dojo.html.selection.getSelectedElement();
|
|
|
153 |
if (p) {
|
|
|
154 |
return p.parentNode;
|
|
|
155 |
}
|
|
|
156 |
} else {
|
|
|
157 |
if (dojo.doc()["selection"]) {
|
|
|
158 |
return dojo.doc().selection.createRange().parentElement();
|
|
|
159 |
} else {
|
|
|
160 |
var selection = dojo.global().getSelection();
|
|
|
161 |
if (selection) {
|
|
|
162 |
var node = selection.anchorNode;
|
|
|
163 |
while (node && node.nodeType != dojo.dom.ELEMENT_NODE) {
|
|
|
164 |
node = node.parentNode;
|
|
|
165 |
}
|
|
|
166 |
return node;
|
|
|
167 |
}
|
|
|
168 |
}
|
|
|
169 |
}
|
|
|
170 |
}, getSelectedText:function () {
|
|
|
171 |
if (dojo.doc()["selection"]) {
|
|
|
172 |
if (dojo.html.selection.getType() == dojo.html.selectionType.CONTROL) {
|
|
|
173 |
return null;
|
|
|
174 |
}
|
|
|
175 |
return dojo.doc().selection.createRange().text;
|
|
|
176 |
} else {
|
|
|
177 |
var selection = dojo.global().getSelection();
|
|
|
178 |
if (selection) {
|
|
|
179 |
return selection.toString();
|
|
|
180 |
}
|
|
|
181 |
}
|
|
|
182 |
}, getSelectedHtml:function () {
|
|
|
183 |
if (dojo.doc()["selection"]) {
|
|
|
184 |
if (dojo.html.selection.getType() == dojo.html.selectionType.CONTROL) {
|
|
|
185 |
return null;
|
|
|
186 |
}
|
|
|
187 |
return dojo.doc().selection.createRange().htmlText;
|
|
|
188 |
} else {
|
|
|
189 |
var selection = dojo.global().getSelection();
|
|
|
190 |
if (selection && selection.rangeCount) {
|
|
|
191 |
var frag = selection.getRangeAt(0).cloneContents();
|
|
|
192 |
var div = document.createElement("div");
|
|
|
193 |
div.appendChild(frag);
|
|
|
194 |
return div.innerHTML;
|
|
|
195 |
}
|
|
|
196 |
return null;
|
|
|
197 |
}
|
|
|
198 |
}, hasAncestorElement:function (tagName) {
|
|
|
199 |
return (dojo.html.selection.getAncestorElement.apply(this, arguments) != null);
|
|
|
200 |
}, getAncestorElement:function (tagName) {
|
|
|
201 |
var node = dojo.html.selection.getSelectedElement() || dojo.html.selection.getParentElement();
|
|
|
202 |
while (node) {
|
|
|
203 |
if (dojo.html.selection.isTag(node, arguments).length > 0) {
|
|
|
204 |
return node;
|
|
|
205 |
}
|
|
|
206 |
node = node.parentNode;
|
|
|
207 |
}
|
|
|
208 |
return null;
|
|
|
209 |
}, isTag:function (node, tags) {
|
|
|
210 |
if (node && node.tagName) {
|
|
|
211 |
for (var i = 0; i < tags.length; i++) {
|
|
|
212 |
if (node.tagName.toLowerCase() == String(tags[i]).toLowerCase()) {
|
|
|
213 |
return String(tags[i]).toLowerCase();
|
|
|
214 |
}
|
|
|
215 |
}
|
|
|
216 |
}
|
|
|
217 |
return "";
|
|
|
218 |
}, selectElement:function (element) {
|
|
|
219 |
var _window = dojo.global();
|
|
|
220 |
var _document = dojo.doc();
|
|
|
221 |
element = dojo.byId(element);
|
|
|
222 |
if (_document.selection && dojo.body().createTextRange) {
|
|
|
223 |
try {
|
|
|
224 |
var range = dojo.body().createControlRange();
|
|
|
225 |
range.addElement(element);
|
|
|
226 |
range.select();
|
|
|
227 |
}
|
|
|
228 |
catch (e) {
|
|
|
229 |
dojo.html.selection.selectElementChildren(element);
|
|
|
230 |
}
|
|
|
231 |
} else {
|
|
|
232 |
if (_window["getSelection"]) {
|
|
|
233 |
var selection = _window.getSelection();
|
|
|
234 |
if (selection["removeAllRanges"]) {
|
|
|
235 |
var range = _document.createRange();
|
|
|
236 |
range.selectNode(element);
|
|
|
237 |
selection.removeAllRanges();
|
|
|
238 |
selection.addRange(range);
|
|
|
239 |
}
|
|
|
240 |
}
|
|
|
241 |
}
|
|
|
242 |
}, selectElementChildren:function (element) {
|
|
|
243 |
var _window = dojo.global();
|
|
|
244 |
var _document = dojo.doc();
|
|
|
245 |
element = dojo.byId(element);
|
|
|
246 |
if (_document.selection && dojo.body().createTextRange) {
|
|
|
247 |
var range = dojo.body().createTextRange();
|
|
|
248 |
range.moveToElementText(element);
|
|
|
249 |
range.select();
|
|
|
250 |
} else {
|
|
|
251 |
if (_window["getSelection"]) {
|
|
|
252 |
var selection = _window.getSelection();
|
|
|
253 |
if (selection["setBaseAndExtent"]) {
|
|
|
254 |
selection.setBaseAndExtent(element, 0, element, element.innerText.length - 1);
|
|
|
255 |
} else {
|
|
|
256 |
if (selection["selectAllChildren"]) {
|
|
|
257 |
selection.selectAllChildren(element);
|
|
|
258 |
}
|
|
|
259 |
}
|
|
|
260 |
}
|
|
|
261 |
}
|
|
|
262 |
}, getBookmark:function () {
|
|
|
263 |
var bookmark;
|
|
|
264 |
var _document = dojo.doc();
|
|
|
265 |
if (_document["selection"]) {
|
|
|
266 |
var range = _document.selection.createRange();
|
|
|
267 |
bookmark = range.getBookmark();
|
|
|
268 |
} else {
|
|
|
269 |
var selection;
|
|
|
270 |
try {
|
|
|
271 |
selection = dojo.global().getSelection();
|
|
|
272 |
}
|
|
|
273 |
catch (e) {
|
|
|
274 |
}
|
|
|
275 |
if (selection) {
|
|
|
276 |
var range = selection.getRangeAt(0);
|
|
|
277 |
bookmark = range.cloneRange();
|
|
|
278 |
} else {
|
|
|
279 |
dojo.debug("No idea how to store the current selection for this browser!");
|
|
|
280 |
}
|
|
|
281 |
}
|
|
|
282 |
return bookmark;
|
|
|
283 |
}, moveToBookmark:function (bookmark) {
|
|
|
284 |
var _document = dojo.doc();
|
|
|
285 |
if (_document["selection"]) {
|
|
|
286 |
var range = _document.selection.createRange();
|
|
|
287 |
range.moveToBookmark(bookmark);
|
|
|
288 |
range.select();
|
|
|
289 |
} else {
|
|
|
290 |
var selection;
|
|
|
291 |
try {
|
|
|
292 |
selection = dojo.global().getSelection();
|
|
|
293 |
}
|
|
|
294 |
catch (e) {
|
|
|
295 |
}
|
|
|
296 |
if (selection && selection["removeAllRanges"]) {
|
|
|
297 |
selection.removeAllRanges();
|
|
|
298 |
selection.addRange(bookmark);
|
|
|
299 |
} else {
|
|
|
300 |
dojo.debug("No idea how to restore selection for this browser!");
|
|
|
301 |
}
|
|
|
302 |
}
|
|
|
303 |
}, collapse:function (beginning) {
|
|
|
304 |
if (dojo.global()["getSelection"]) {
|
|
|
305 |
var selection = dojo.global().getSelection();
|
|
|
306 |
if (selection.removeAllRanges) {
|
|
|
307 |
if (beginning) {
|
|
|
308 |
selection.collapseToStart();
|
|
|
309 |
} else {
|
|
|
310 |
selection.collapseToEnd();
|
|
|
311 |
}
|
|
|
312 |
} else {
|
|
|
313 |
dojo.global().getSelection().collapse(beginning);
|
|
|
314 |
}
|
|
|
315 |
} else {
|
|
|
316 |
if (dojo.doc().selection) {
|
|
|
317 |
var range = dojo.doc().selection.createRange();
|
|
|
318 |
range.collapse(beginning);
|
|
|
319 |
range.select();
|
|
|
320 |
}
|
|
|
321 |
}
|
|
|
322 |
}, remove:function () {
|
|
|
323 |
if (dojo.doc().selection) {
|
|
|
324 |
var selection = dojo.doc().selection;
|
|
|
325 |
if (selection.type.toUpperCase() != "NONE") {
|
|
|
326 |
selection.clear();
|
|
|
327 |
}
|
|
|
328 |
return selection;
|
|
|
329 |
} else {
|
|
|
330 |
var selection = dojo.global().getSelection();
|
|
|
331 |
for (var i = 0; i < selection.rangeCount; i++) {
|
|
|
332 |
selection.getRangeAt(i).deleteContents();
|
|
|
333 |
}
|
|
|
334 |
return selection;
|
|
|
335 |
}
|
|
|
336 |
}});
|
|
|
337 |
|