1 define("dijit/_base/focus", [
2 "dojo/_base/array", // array.forEach
3 "dojo/dom", // dom.isDescendant
4 "dojo/_base/lang", // lang.isArray
5 "dojo/topic", // publish
6 "dojo/_base/window", // win.doc win.doc.selection win.global win.global.getSelection win.withGlobal
8 ".." // for exporting symbols to dijit
9 ], function(array, dom, lang, topic, win, focus, dijit){
14 // Deprecated module to monitor currently focused node and stack of currently focused widgets.
15 // New code should access dijit/focus directly.
19 // Currently focused item on screen
22 // _prevFocus: DomNode
23 // Previously focused item on screen
26 isCollapsed: function(){
28 // Returns true if there is no text selected
29 return dijit.getBookmark().isCollapsed;
32 getBookmark: function(){
34 // Retrieves a bookmark that can be used with moveToBookmark to return to the same range
35 var bm, rg, tg, sel = win.doc.selection, cf = focus.curNode;
37 if(win.global.getSelection){
38 //W3C Range API for selections.
39 sel = win.global.getSelection();
42 tg = cf? cf.tagName : "";
44 //Create a fake rangelike item to restore selections.
45 tg = tg.toLowerCase();
46 if(tg == "textarea" ||
47 (tg == "input" && (!cf.type || cf.type.toLowerCase() == "text"))){
49 start: cf.selectionStart,
54 return {isCollapsed: (sel.end <= sel.start), mark: sel}; //Object.
57 bm = {isCollapsed:true};
59 bm.mark = sel.getRangeAt(0).cloneRange();
62 rg = sel.getRangeAt(0);
63 bm = {isCollapsed: false, mark: rg.cloneRange()};
67 // If the current focus was a input of some sort and no selection, don't bother saving
68 // a native bookmark. This is because it causes issues with dialog/page selection restore.
69 // So, we need to create psuedo bookmarks to work with.
70 tg = cf ? cf.tagName : "";
71 tg = tg.toLowerCase();
72 if(cf && tg && (tg == "button" || tg == "textarea" || tg == "input")){
73 if(sel.type && sel.type.toLowerCase() == "none"){
79 rg = sel.createRange();
81 isCollapsed: rg.text && rg.text.length?false:true,
91 //'IE' way for selections.
93 // createRange() throws exception when dojo in iframe
94 //and nothing selected, see #9632
95 rg = sel.createRange();
96 bm.isCollapsed = !(sel.type == 'Text' ? rg.htmlText.length : rg.length);
98 bm.isCollapsed = true;
101 if(sel.type.toUpperCase() == 'CONTROL'){
104 var i=0,len=rg.length;
106 bm.mark.push(rg.item(i++));
109 bm.isCollapsed = true;
113 bm.mark = rg.getBookmark();
116 console.warn("No idea how to store the current selection for this browser!");
121 moveToBookmark: function(/*Object*/ bookmark){
123 // Moves current selection to a bookmark
125 // This should be a returned object from dijit.getBookmark()
128 mark = bookmark.mark;
130 if(win.global.getSelection){
131 //W3C Rangi API (FF, WebKit, Opera, etc)
132 var sel = win.global.getSelection();
133 if(sel && sel.removeAllRanges){
136 n.selectionStart = mark.start;
137 n.selectionEnd = mark.end;
139 sel.removeAllRanges();
143 console.warn("No idea how to restore selection for this browser!");
145 }else if(_doc.selection && mark){
150 }else if(lang.isArray(mark)){
151 rg = _doc.body.createControlRange();
152 //rg.addElement does not have call/apply method, so can not call it directly
153 //rg is not available in "range.addElement(item)", so can't use that either
154 array.forEach(mark, function(n){
158 rg = _doc.body.createTextRange();
159 rg.moveToBookmark(mark);
166 getFocus: function(/*Widget?*/ menu, /*Window?*/ openedForWindow){
168 // Called as getFocus(), this returns an Object showing the current focus
169 // and selected text.
171 // Called as getFocus(widget), where widget is a (widget representing) a button
172 // that was just pressed, it returns where focus was before that button
173 // was pressed. (Pressing the button may have either shifted focus to the button,
174 // or removed focus altogether.) In this case the selected text is not returned,
175 // since it can't be accurately determined.
177 // menu: dijit._Widget or {domNode: DomNode} structure
178 // The button that was just pressed. If focus has disappeared or moved
179 // to this button, returns the previous focus. In this case the bookmark
180 // information is already lost, and null is returned.
183 // iframe in which menu was opened
186 // A handle to restore focus/selection, to be passed to `dijit.focus`
187 var node = !focus.curNode || (menu && dom.isDescendant(focus.curNode, menu.domNode)) ? dijit._prevFocus : focus.curNode;
190 bookmark: node && (node == focus.curNode) && win.withGlobal(openedForWindow || win.global, dijit.getBookmark),
191 openedForWindow: openedForWindow
195 // _activeStack: dijit._Widget[]
196 // List of currently active widgets (focused widget and it's ancestors)
199 registerIframe: function(/*DomNode*/ iframe){
201 // Registers listeners on the specified iframe so that any click
202 // or focus event on that iframe (or anything in it) is reported
203 // as a focus/click event on the <iframe> itself.
205 // Currently only used by editor.
207 // Handle to pass to unregisterIframe()
208 return focus.registerIframe(iframe);
211 unregisterIframe: function(/*Object*/ handle){
213 // Unregisters listeners on the specified iframe created by registerIframe.
214 // After calling be sure to delete or null out the handle itself.
216 // Handle returned by registerIframe()
218 handle && handle.remove();
221 registerWin: function(/*Window?*/targetWindow, /*DomNode?*/ effectiveNode){
223 // Registers listeners on the specified window (either the main
224 // window or an iframe's window) to detect when the user has clicked somewhere
225 // or focused somewhere.
227 // Users should call registerIframe() instead of this method.
229 // If specified this is the window associated with the iframe,
230 // i.e. iframe.contentWindow.
232 // If specified, report any focus events inside targetWindow as
233 // an event on effectiveNode, rather than on evt.target.
235 // Handle to pass to unregisterWin()
237 return focus.registerWin(targetWindow, effectiveNode);
240 unregisterWin: function(/*Handle*/ handle){
242 // Unregisters listeners on the specified window (either the main
243 // window or an iframe's window) according to handle returned from registerWin().
244 // After calling be sure to delete or null out the handle itself.
246 handle && handle.remove();
250 // Override focus singleton's focus function so that dijit.focus()
251 // has backwards compatible behavior of restoring selection (although
252 // probably no one is using that).
253 focus.focus = function(/*Object || DomNode */ handle){
255 // Sets the focused node and the selection according to argument.
256 // To set focus to an iframe's content, pass in the iframe itself.
258 // object returned by get(), or a DomNode
260 if(!handle){ return; }
262 var node = "node" in handle ? handle.node : handle, // because handle is either DomNode or a composite object
263 bookmark = handle.bookmark,
264 openedForWindow = handle.openedForWindow,
265 collapsed = bookmark ? bookmark.isCollapsed : false;
268 // Note that for iframe's we need to use the <iframe> to follow the parentNode chain,
269 // but we need to set focus to iframe.contentWindow
271 var focusNode = (node.tagName.toLowerCase() == "iframe") ? node.contentWindow : node;
272 if(focusNode && focusNode.focus){
274 // Gecko throws sometimes if setting focus is impossible,
275 // node not displayed or something like that
279 focus._onFocusNode(node);
283 // do not need to restore if current selection is not empty
284 // (use keyboard to select a menu item) or if previous selection was collapsed
285 // as it may cause focus shift (Esp in IE).
286 if(bookmark && win.withGlobal(openedForWindow || win.global, dijit.isCollapsed) && !collapsed){
288 openedForWindow.focus();
291 win.withGlobal(openedForWindow || win.global, dijit.moveToBookmark, null, [bookmark]);
293 /*squelch IE internal error, see http://trac.dojotoolkit.org/ticket/1984 */
298 // For back compatibility, monitor changes to focused node and active widget stack,
299 // publishing events and copying changes from focus manager variables into dijit (top level) variables
300 focus.watch("curNode", function(name, oldVal, newVal){
301 dijit._curFocus = newVal;
302 dijit._prevFocus = oldVal;
304 topic.publish("focusNode", newVal); // publish
307 focus.watch("activeStack", function(name, oldVal, newVal){
308 dijit._activeStack = newVal;
311 focus.on("widget-blur", function(widget, by){
312 topic.publish("widgetBlur", widget, by); // publish
314 focus.on("widget-focus", function(widget, by){
315 topic.publish("widgetFocus", widget, by); // publish