root / trunk / web / dojo / dijit / _base / focus.js @ 10
History | View | Annotate | Download (6.44 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
4 | see: http://dojotoolkit.org/license for details
|
||
5 | */
|
||
6 | |||
7 | |||
8 | if(!dojo._hasResource["dijit._base.focus"]){ |
||
9 | dojo._hasResource["dijit._base.focus"]=true; |
||
10 | dojo.provide("dijit._base.focus");
|
||
11 | dojo.require("dojo.window");
|
||
12 | dojo.require("dijit._base.manager");
|
||
13 | dojo.mixin(dijit,{_curFocus:null,_prevFocus:null,isCollapsed:function(){ |
||
14 | return dijit.getBookmark().isCollapsed;
|
||
15 | },getBookmark:function(){ |
||
16 | var bm,rg,tg,_1=dojo.doc.selection,cf=dijit._curFocus;
|
||
17 | if(dojo.global.getSelection){
|
||
18 | _1=dojo.global.getSelection(); |
||
19 | if(_1){
|
||
20 | if(_1.isCollapsed){
|
||
21 | tg=cf?cf.tagName:"";
|
||
22 | if(tg){
|
||
23 | tg=tg.toLowerCase(); |
||
24 | if(tg=="textarea"||(tg=="input"&&(!cf.type||cf.type.toLowerCase()=="text"))){ |
||
25 | _1={start:cf.selectionStart,end:cf.selectionEnd,node:cf,pRange:true}; |
||
26 | return {isCollapsed:(_1.end<=_1.start),mark:_1}; |
||
27 | } |
||
28 | } |
||
29 | bm={isCollapsed:true}; |
||
30 | }else{
|
||
31 | rg=_1.getRangeAt(0);
|
||
32 | bm={isCollapsed:false,mark:rg.cloneRange()}; |
||
33 | } |
||
34 | } |
||
35 | }else{
|
||
36 | if(_1){
|
||
37 | tg=cf?cf.tagName:"";
|
||
38 | tg=tg.toLowerCase(); |
||
39 | if(cf&&tg&&(tg=="button"||tg=="textarea"||tg=="input")){ |
||
40 | if(_1.type&&_1.type.toLowerCase()=="none"){ |
||
41 | return {isCollapsed:true,mark:null}; |
||
42 | }else{
|
||
43 | rg=_1.createRange(); |
||
44 | return {isCollapsed:rg.text&&rg.text.length?false:true,mark:{range:rg,pRange:true}}; |
||
45 | } |
||
46 | } |
||
47 | bm={}; |
||
48 | try{
|
||
49 | rg=_1.createRange(); |
||
50 | bm.isCollapsed=!(_1.type=="Text"?rg.htmlText.length:rg.length);
|
||
51 | } |
||
52 | catch(e){
|
||
53 | bm.isCollapsed=true;
|
||
54 | return bm;
|
||
55 | } |
||
56 | if(_1.type.toUpperCase()=="CONTROL"){ |
||
57 | if(rg.length){
|
||
58 | bm.mark=[]; |
||
59 | var i=0,_2=rg.length; |
||
60 | while(i<_2){
|
||
61 | bm.mark.push(rg.item(i++)); |
||
62 | } |
||
63 | }else{
|
||
64 | bm.isCollapsed=true;
|
||
65 | bm.mark=null;
|
||
66 | } |
||
67 | }else{
|
||
68 | bm.mark=rg.getBookmark(); |
||
69 | } |
||
70 | }else{
|
||
71 | console.warn("No idea how to store the current selection for this browser!");
|
||
72 | } |
||
73 | } |
||
74 | return bm;
|
||
75 | },moveToBookmark:function(_3){ |
||
76 | var _4=dojo.doc,_5=_3.mark;
|
||
77 | if(_5){
|
||
78 | if(dojo.global.getSelection){
|
||
79 | var _6=dojo.global.getSelection();
|
||
80 | if(_6&&_6.removeAllRanges){
|
||
81 | if(_5.pRange){
|
||
82 | var r=_5;
|
||
83 | var n=r.node;
|
||
84 | n.selectionStart=r.start; |
||
85 | n.selectionEnd=r.end; |
||
86 | }else{
|
||
87 | _6.removeAllRanges(); |
||
88 | _6.addRange(_5); |
||
89 | } |
||
90 | }else{
|
||
91 | console.warn("No idea how to restore selection for this browser!");
|
||
92 | } |
||
93 | }else{
|
||
94 | if(_4.selection&&_5){
|
||
95 | var rg;
|
||
96 | if(_5.pRange){
|
||
97 | rg=_5.range; |
||
98 | }else{
|
||
99 | if(dojo.isArray(_5)){
|
||
100 | rg=_4.body.createControlRange(); |
||
101 | dojo.forEach(_5,function(n){
|
||
102 | rg.addElement(n); |
||
103 | }); |
||
104 | }else{
|
||
105 | rg=_4.body.createTextRange(); |
||
106 | rg.moveToBookmark(_5); |
||
107 | } |
||
108 | } |
||
109 | rg.select(); |
||
110 | } |
||
111 | } |
||
112 | } |
||
113 | },getFocus:function(_7,_8){ |
||
114 | var _9=!dijit._curFocus||(_7&&dojo.isDescendant(dijit._curFocus,_7.domNode))?dijit._prevFocus:dijit._curFocus;
|
||
115 | return {node:_9,bookmark:(_9==dijit._curFocus)&&dojo.withGlobal(_8||dojo.global,dijit.getBookmark),openedForWindow:_8}; |
||
116 | },focus:function(_a){ |
||
117 | if(!_a){
|
||
118 | return;
|
||
119 | } |
||
120 | var _b="node" in _a?_a.node:_a,_c=_a.bookmark,_d=_a.openedForWindow,_e=_c?_c.isCollapsed:false; |
||
121 | if(_b){
|
||
122 | var _f=(_b.tagName.toLowerCase()=="iframe")?_b.contentWindow:_b; |
||
123 | if(_f&&_f.focus){
|
||
124 | try{
|
||
125 | _f.focus(); |
||
126 | } |
||
127 | catch(e){
|
||
128 | } |
||
129 | } |
||
130 | dijit._onFocusNode(_b); |
||
131 | } |
||
132 | if(_c&&dojo.withGlobal(_d||dojo.global,dijit.isCollapsed)&&!_e){
|
||
133 | if(_d){
|
||
134 | _d.focus(); |
||
135 | } |
||
136 | try{
|
||
137 | dojo.withGlobal(_d||dojo.global,dijit.moveToBookmark,null,[_c]);
|
||
138 | } |
||
139 | catch(e2){
|
||
140 | } |
||
141 | } |
||
142 | },_activeStack:[],registerIframe:function(_10){ |
||
143 | return dijit.registerWin(_10.contentWindow,_10);
|
||
144 | },unregisterIframe:function(_11){ |
||
145 | dijit.unregisterWin(_11); |
||
146 | },registerWin:function(_12,_13){ |
||
147 | var _14=function(evt){ |
||
148 | dijit._justMouseDowned=true;
|
||
149 | setTimeout(function(){
|
||
150 | dijit._justMouseDowned=false;
|
||
151 | },0);
|
||
152 | if(dojo.isIE&&evt&&evt.srcElement&&evt.srcElement.parentNode==null){ |
||
153 | return;
|
||
154 | } |
||
155 | dijit._onTouchNode(_13||evt.target||evt.srcElement,"mouse");
|
||
156 | }; |
||
157 | var doc=dojo.isIE?_12.document.documentElement:_12.document;
|
||
158 | if(doc){
|
||
159 | if(dojo.isIE){
|
||
160 | doc.attachEvent("onmousedown",_14);
|
||
161 | var _15=function(evt){ |
||
162 | if(evt.srcElement.tagName.toLowerCase()!="#document"&&dijit.isTabNavigable(evt.srcElement)){ |
||
163 | dijit._onFocusNode(_13||evt.srcElement); |
||
164 | }else{
|
||
165 | dijit._onTouchNode(_13||evt.srcElement); |
||
166 | } |
||
167 | }; |
||
168 | doc.attachEvent("onactivate",_15);
|
||
169 | var _16=function(evt){ |
||
170 | dijit._onBlurNode(_13||evt.srcElement); |
||
171 | }; |
||
172 | doc.attachEvent("ondeactivate",_16);
|
||
173 | return function(){ |
||
174 | doc.detachEvent("onmousedown",_14);
|
||
175 | doc.detachEvent("onactivate",_15);
|
||
176 | doc.detachEvent("ondeactivate",_16);
|
||
177 | doc=null;
|
||
178 | }; |
||
179 | }else{
|
||
180 | doc.addEventListener("mousedown",_14,true); |
||
181 | var _17=function(evt){ |
||
182 | dijit._onFocusNode(_13||evt.target); |
||
183 | }; |
||
184 | doc.addEventListener("focus",_17,true); |
||
185 | var _18=function(evt){ |
||
186 | dijit._onBlurNode(_13||evt.target); |
||
187 | }; |
||
188 | doc.addEventListener("blur",_18,true); |
||
189 | return function(){ |
||
190 | doc.removeEventListener("mousedown",_14,true); |
||
191 | doc.removeEventListener("focus",_17,true); |
||
192 | doc.removeEventListener("blur",_18,true); |
||
193 | doc=null;
|
||
194 | }; |
||
195 | } |
||
196 | } |
||
197 | },unregisterWin:function(_19){ |
||
198 | _19&&_19(); |
||
199 | },_onBlurNode:function(_1a){ |
||
200 | dijit._prevFocus=dijit._curFocus; |
||
201 | dijit._curFocus=null;
|
||
202 | if(dijit._justMouseDowned){
|
||
203 | return;
|
||
204 | } |
||
205 | if(dijit._clearActiveWidgetsTimer){
|
||
206 | clearTimeout(dijit._clearActiveWidgetsTimer); |
||
207 | } |
||
208 | dijit._clearActiveWidgetsTimer=setTimeout(function(){
|
||
209 | delete dijit._clearActiveWidgetsTimer;
|
||
210 | dijit._setStack([]); |
||
211 | dijit._prevFocus=null;
|
||
212 | },100);
|
||
213 | },_onTouchNode:function(_1b,by){ |
||
214 | if(dijit._clearActiveWidgetsTimer){
|
||
215 | clearTimeout(dijit._clearActiveWidgetsTimer); |
||
216 | delete dijit._clearActiveWidgetsTimer;
|
||
217 | } |
||
218 | var _1c=[];
|
||
219 | try{
|
||
220 | while(_1b){
|
||
221 | var _1d=dojo.attr(_1b,"dijitPopupParent"); |
||
222 | if(_1d){
|
||
223 | _1b=dijit.byId(_1d).domNode; |
||
224 | }else{
|
||
225 | if(_1b.tagName&&_1b.tagName.toLowerCase()=="body"){ |
||
226 | if(_1b===dojo.body()){
|
||
227 | break;
|
||
228 | } |
||
229 | _1b=dojo.window.get(_1b.ownerDocument).frameElement; |
||
230 | }else{
|
||
231 | var id=_1b.getAttribute&&_1b.getAttribute("widgetId"),_1e=id&&dijit.byId(id); |
||
232 | if(_1e&&!(by=="mouse"&&_1e.get("disabled"))){ |
||
233 | _1c.unshift(id); |
||
234 | } |
||
235 | _1b=_1b.parentNode; |
||
236 | } |
||
237 | } |
||
238 | } |
||
239 | } |
||
240 | catch(e){
|
||
241 | } |
||
242 | dijit._setStack(_1c,by); |
||
243 | },_onFocusNode:function(_1f){ |
||
244 | if(!_1f){
|
||
245 | return;
|
||
246 | } |
||
247 | if(_1f.nodeType==9){ |
||
248 | return;
|
||
249 | } |
||
250 | dijit._onTouchNode(_1f); |
||
251 | if(_1f==dijit._curFocus){
|
||
252 | return;
|
||
253 | } |
||
254 | if(dijit._curFocus){
|
||
255 | dijit._prevFocus=dijit._curFocus; |
||
256 | } |
||
257 | dijit._curFocus=_1f; |
||
258 | dojo.publish("focusNode",[_1f]);
|
||
259 | },_setStack:function(_20,by){ |
||
260 | var _21=dijit._activeStack;
|
||
261 | dijit._activeStack=_20; |
||
262 | for(var _22=0;_22<Math.min(_21.length,_20.length);_22++){ |
||
263 | if(_21[_22]!=_20[_22]){
|
||
264 | break;
|
||
265 | } |
||
266 | } |
||
267 | var _23;
|
||
268 | for(var i=_21.length-1;i>=_22;i--){ |
||
269 | _23=dijit.byId(_21[i]); |
||
270 | if(_23){
|
||
271 | _23._focused=false;
|
||
272 | _23._hasBeenBlurred=true;
|
||
273 | if(_23._onBlur){
|
||
274 | _23._onBlur(by); |
||
275 | } |
||
276 | dojo.publish("widgetBlur",[_23,by]);
|
||
277 | } |
||
278 | } |
||
279 | for(i=_22;i<_20.length;i++){
|
||
280 | _23=dijit.byId(_20[i]); |
||
281 | if(_23){
|
||
282 | _23._focused=true;
|
||
283 | if(_23._onFocus){
|
||
284 | _23._onFocus(by); |
||
285 | } |
||
286 | dojo.publish("widgetFocus",[_23,by]);
|
||
287 | } |
||
288 | } |
||
289 | }}); |
||
290 | dojo.addOnLoad(function(){
|
||
291 | var _24=dijit.registerWin(window);
|
||
292 | if(dojo.isIE){
|
||
293 | dojo.addOnWindowUnload(function(){
|
||
294 | dijit.unregisterWin(_24); |
||
295 | _24=null;
|
||
296 | }); |
||
297 | } |
||
298 | }); |
||
299 | } |