dojo.provide("dijit._base.focus"); // summary: // These functions are used to query or set the focus and selection. // // Also, they trace when widgets become actived/deactivated, // so that the widget can fire _onFocus/_onBlur events. // "Active" here means something similar to "focused", but // "focus" isn't quite the right word because we keep track of // a whole stack of "active" widgets. Example: Combobutton --> Menu --> // MenuItem. The onBlur event for Combobutton doesn't fire due to focusing // on the Menu or a MenuItem, since they are considered part of the // Combobutton widget. It only happens when focus is shifted // somewhere completely different. dojo.mixin(dijit, { // _curFocus: DomNode // Currently focused item on screen _curFocus: null, // _prevFocus: DomNode // Previously focused item on screen _prevFocus: null, isCollapsed: function(){ // summary: // Returns true if there is no text selected var _document = dojo.doc; if(_document.selection){ // IE var s=_document.selection; if(s.type=='Text'){ return !s.createRange().htmlText.length; // Boolean }else{ //Control range return !s.createRange().length; // Boolean } }else{ var _window = dojo.global; var selection = _window.getSelection(); if(dojo.isString(selection)){ // Safari // TODO: this is dead code; safari is taking the else branch. remove after 1.3. return !selection; // Boolean }else{ // Mozilla/W3 return !selection || selection.isCollapsed || !selection.toString(); // Boolean } } }, getBookmark: function(){ // summary: // Retrieves a bookmark that can be used with moveToBookmark to return to the same range var bookmark, selection = dojo.doc.selection; if(selection){ // IE var range = selection.createRange(); if(selection.type.toUpperCase()=='CONTROL'){ if(range.length){ bookmark=[]; var i=0,len=range.length; while(i to follow the parentNode chain, // but we need to set focus to iframe.contentWindow if(node){ var focusNode = (node.tagName.toLowerCase()=="iframe") ? node.contentWindow : node; if(focusNode && focusNode.focus){ try{ // Gecko throws sometimes if setting focus is impossible, // node not displayed or something like that focusNode.focus(); }catch(e){/*quiet*/} } dijit._onFocusNode(node); } // set the selection // do not need to restore if current selection is not empty // (use keyboard to select a menu item) if(bookmark && dojo.withGlobal(openedForWindow||dojo.global, dijit.isCollapsed)){ if(openedForWindow){ openedForWindow.focus(); } try{ dojo.withGlobal(openedForWindow||dojo.global, dijit.moveToBookmark, null, [bookmark]); }catch(e){ /*squelch IE internal error, see http://trac.dojotoolkit.org/ticket/1984 */ } } }, // _activeStack: Array // List of currently active widgets (focused widget and it's ancestors) _activeStack: [], registerIframe: function(/*DomNode*/ iframe){ // summary: // Registers listeners on the specified iframe so that any click // or focus event on that iframe (or anything in it) is reported // as a focus/click event on the