Debian Codegraph @

dijit/focus.js

raw symbols SCIP
1 define([
2 "dojo/aspect",
3 "dojo/_base/declare", // declare
4 "dojo/dom", // domAttr.get dom.isDescendant
5 "dojo/dom-attr", // domAttr.get dom.isDescendant
6 "dojo/dom-class",
7 "dojo/dom-construct", // connect to domConstruct.empty, domConstruct.destroy
8 "dojo/Evented",
9 "dojo/_base/lang", // lang.hitch
10 "dojo/on",
11 "dojo/domReady",
12 "dojo/sniff", // has("ie")
13 "dojo/Stateful",
14 "dojo/_base/window", // win.body
15 "dojo/window", // winUtils.get
16 "./a11y", // a11y.isTabNavigable
17 "./registry", // registry.byId
18 "./main" // to set dijit.focus
19 ], function(aspect, declare, dom, domAttr, domClass, domConstruct, Evented, lang, on, domReady, has, Stateful, win, winUtils,
20 a11y, registry, dijit){
21
22 // module:
23 // dijit/focus
24
25 // Time of the last focusin event
26 var lastFocusin;
27
28 // Time of the last touch/mousedown or focusin event
29 var lastTouchOrFocusin;
30
31 var FocusManager = declare([Stateful, Evented], {
32 // summary:
33 // Tracks the currently focused node, and which widgets are currently "active".
34 // Access via require(["dijit/focus"], function(focus){ ... }).
35 //
36 // A widget is considered active if it or a descendant widget has focus,
37 // or if a non-focusable node of this widget or a descendant was recently clicked.
38 //
39 // Call focus.watch("curNode", callback) to track the current focused DOMNode,
40 // or focus.watch("activeStack", callback) to track the currently focused stack of widgets.
41 //
42 // Call focus.on("widget-blur", func) or focus.on("widget-focus", ...) to monitor when
43 // when widgets become active/inactive
44 //
45 // Finally, focus(node) will focus a node, suppressing errors if the node doesn't exist.
46
47 // curNode: DomNode
48 // Currently focused item on screen
49 curNode: null,
50
51 // activeStack: dijit/_WidgetBase[]
52 // List of currently active widgets (focused widget and it's ancestors)
53 activeStack: [],
54
55 constructor: function(){
56 // Don't leave curNode/prevNode pointing to bogus elements
57 var check = lang.hitch(this, function(node){
58 if(dom.isDescendant(this.curNode, node)){
59 this.set("curNode", null);
60 }
61 if(dom.isDescendant(this.prevNode, node)){
62 this.set("prevNode", null);
63 }
64 });
65 aspect.before(domConstruct, "empty", check);
66 aspect.before(domConstruct, "destroy", check);
67 },
68
69 registerIframe: function(/*DomNode*/ iframe){
70 // summary:
71 // Registers listeners on the specified iframe so that any click
72 // or focus event on that iframe (or anything in it) is reported
73 // as a focus/click event on the `<iframe>` itself.
74 // description:
75 // Currently only used by editor.
76 // returns:
77 // Handle with remove() method to deregister.
78 return this.registerWin(iframe.contentWindow, iframe);
79 },
80
81 registerWin: function(/*Window?*/targetWindow, /*DomNode?*/ effectiveNode){
82 // summary:
83 // Registers listeners on the specified window (either the main
84 // window or an iframe's window) to detect when the user has clicked somewhere
85 // or focused somewhere.
86 // description:
87 // Users should call registerIframe() instead of this method.
88 // targetWindow:
89 // If specified this is the window associated with the iframe,
90 // i.e. iframe.contentWindow.
91 // effectiveNode:
92 // If specified, report any focus events inside targetWindow as
93 // an event on effectiveNode, rather than on evt.target.
94 // returns:
95 // Handle with remove() method to deregister.
96
97 // TODO: make this function private in 2.0; Editor/users should call registerIframe(),
98
99 // Listen for blur and focus events on targetWindow's document.
100 var _this = this,
101 body = targetWindow.document && targetWindow.document.body;
102
103 if(body){
104 // Listen for touches or mousedowns... could also use dojo/touch.press here.
105 var event = has("pointer-events") ? "pointerdown" : has("MSPointer") ? "MSPointerDown" :
106 has("touch-events") ? "mousedown, touchstart" : "mousedown";
107 var mdh = on(targetWindow.document, event, function(evt){
108 // workaround weird IE bug where the click is on an orphaned node
109 // (first time clicking a Select/DropDownButton inside a TooltipDialog).
110 // actually, strangely this is happening on latest chrome too.
111 if(evt && evt.target && evt.target.parentNode == null){
112 return;
113 }
114
115 _this._onTouchNode(effectiveNode || evt.target, "mouse");
116 });
117
118 var fih = on(body, 'focusin', function(evt){
119 // When you refocus the browser window, IE gives an event with an empty srcElement
120 if(!evt.target.tagName) { return; }
121
122 // IE reports that nodes like <body> have gotten focus, even though they have tabIndex=-1,
123 // ignore those events
124 var tag = evt.target.tagName.toLowerCase();
125 if(tag == "#document" || tag == "body"){ return; }
126
127 if(a11y.isFocusable(evt.target)){
128 _this._onFocusNode(effectiveNode || evt.target);
129 }else{
130 // Previous code called _onTouchNode() for any activate event on a non-focusable node. Can
131 // probably just ignore such an event as it will be handled by onmousedown handler above, but
132 // leaving the code for now.
133 _this._onTouchNode(effectiveNode || evt.target);
134 }
135 });
136
137 var foh = on(body, 'focusout', function(evt){
138 _this._onBlurNode(effectiveNode || evt.target);
139 });
140
141 return {
142 remove: function(){
143 mdh.remove();
144 fih.remove();
145 foh.remove();
146 mdh = fih = foh = null;
147 body = null; // prevent memory leak (apparent circular reference via closure)
148 }
149 };
150 }
151 },
152
153 _onBlurNode: function(/*DomNode*/ node){
154 // summary:
155 // Called when focus leaves a node.
156 // Usually ignored, _unless_ it *isn't* followed by touching another node,
157 // which indicates that we tabbed off the last field on the page,
158 // in which case every widget is marked inactive
159
160 var now = (new Date()).getTime();
161
162 // IE9+ and chrome have a problem where focusout events come after the corresponding focusin event.
163 // For chrome problem see https://bugs.dojotoolkit.org/ticket/17668.
164 // IE problem happens when moving focus from the Editor's <iframe> to a normal DOMNode.
165 if(now < lastFocusin + 100){
166 return;
167 }
168
169 // If the blur event isn't followed by a focus event, it means the user clicked on something unfocusable,
170 // so clear focus.
171 if(this._clearFocusTimer){
172 clearTimeout(this._clearFocusTimer);
173 }
174 this._clearFocusTimer = setTimeout(lang.hitch(this, function(){
175 this.set("prevNode", this.curNode);
176 this.set("curNode", null);
177 }), 0);
178
179 // Unset timer to zero-out widget stack; we'll reset it below if appropriate.
180 if(this._clearActiveWidgetsTimer){
181 clearTimeout(this._clearActiveWidgetsTimer);
182 }
183
184 if(now < lastTouchOrFocusin + 100){
185 // This blur event is coming late (after the call to _onTouchNode() rather than before.
186 // So let _onTouchNode() handle setting the widget stack.
187 // See https://bugs.dojotoolkit.org/ticket/17668
188 return;
189 }
190
191 // If the blur event isn't followed (or preceded) by a focus or touch event then mark all widgets as inactive.
192 this._clearActiveWidgetsTimer = setTimeout(lang.hitch(this, function(){
193 delete this._clearActiveWidgetsTimer;
194 this._setStack([]);
195 }), 0);
196 },
197
198 _onTouchNode: function(/*DomNode*/ node, /*String*/ by){
199 // summary:
200 // Callback when node is focused or touched.
201 // Note that _onFocusNode() calls _onTouchNode().
202 // node:
203 // The node that was touched.
204 // by:
205 // "mouse" if the focus/touch was caused by a mouse down event
206
207 // Keep track of time of last focusin or touch event.
208 lastTouchOrFocusin = (new Date()).getTime();
209
210 if(this._clearActiveWidgetsTimer){
211 // forget the recent blur event
212 clearTimeout(this._clearActiveWidgetsTimer);
213 delete this._clearActiveWidgetsTimer;
214 }
215
216 // if the click occurred on the scrollbar of a dropdown, treat it as a click on the dropdown,
217 // even though the scrollbar is technically on the popup wrapper (see #10631)
218 if(domClass.contains(node, "dijitPopup")){
219 node = node.firstChild;
220 }
221
222 // compute stack of active widgets (ex: ComboButton --> Menu --> MenuItem)
223 var newStack=[];
224 try{
225 while(node){
226 var popupParent = domAttr.get(node, "dijitPopupParent");
227 if(popupParent){
228 node=registry.byId(popupParent).domNode;
229 }else if(node.tagName && node.tagName.toLowerCase() == "body"){
230 // is this the root of the document or just the root of an iframe?
231 if(node === win.body()){
232 // node is the root of the main document
233 break;
234 }
235 // otherwise, find the iframe this node refers to (can't access it via parentNode,
236 // need to do this trick instead). window.frameElement is supported in IE/FF/Webkit
237 node=winUtils.get(node.ownerDocument).frameElement;
238 }else{
239 // if this node is the root node of a widget, then add widget id to stack,
240 // except ignore clicks on disabled widgets (actually focusing a disabled widget still works,
241 // to support MenuItem)
242 var id = node.getAttribute && node.getAttribute("widgetId"),
243 widget = id && registry.byId(id);
244 if(widget && !(by == "mouse" && widget.get("disabled"))){
245 newStack.unshift(id);
246 }
247 node=node.parentNode;
248 }
249 }
250 }catch(e){ /* squelch */ }
251
252 this._setStack(newStack, by);
253 },
254
255 _onFocusNode: function(/*DomNode*/ node){
256 // summary:
257 // Callback when node is focused
258
259 if(!node){
260 return;
261 }
262
263 if(node.nodeType == 9){
264 // Ignore focus events on the document itself. This is here so that
265 // (for example) clicking the up/down arrows of a spinner
266 // (which don't get focus) won't cause that widget to blur. (FF issue)
267 return;
268 }
269
270 // Keep track of time of last focusin event.
271 lastFocusin = (new Date()).getTime();
272
273 // There was probably a blur event right before this event, but since we have a new focus,
274 // forget about the blur
275 if(this._clearFocusTimer){
276 clearTimeout(this._clearFocusTimer);
277 delete this._clearFocusTimer;
278 }
279
280 this._onTouchNode(node);
281
282 if(node == this.curNode){ return; }
283 this.set("prevNode", this.curNode);
284 this.set("curNode", node);
285 },
286
287 _setStack: function(/*String[]*/ newStack, /*String*/ by){
288 // summary:
289 // The stack of active widgets has changed. Send out appropriate events and records new stack.
290 // newStack:
291 // array of widget id's, starting from the top (outermost) widget
292 // by:
293 // "mouse" if the focus/touch was caused by a mouse down event
294
295 var oldStack = this.activeStack, lastOldIdx = oldStack.length - 1, lastNewIdx = newStack.length - 1;
296
297 if(newStack[lastNewIdx] == oldStack[lastOldIdx]){
298 // no changes, return now to avoid spurious notifications about changes to activeStack
299 return;
300 }
301
302 this.set("activeStack", newStack);
303
304 var widget, i;
305
306 // for all elements that have gone out of focus, set focused=false
307 for(i = lastOldIdx; i >= 0 && oldStack[i] != newStack[i]; i--){
308 widget = registry.byId(oldStack[i]);
309 if(widget){
310 widget._hasBeenBlurred = true; // TODO: used by form widgets, should be moved there
311 widget.set("focused", false);
312 if(widget._focusManager == this){
313 widget._onBlur(by);
314 }
315 this.emit("widget-blur", widget, by);
316 }
317 }
318
319 // for all element that have come into focus, set focused=true
320 for(i++; i <= lastNewIdx; i++){
321 widget = registry.byId(newStack[i]);
322 if(widget){
323 widget.set("focused", true);
324 if(widget._focusManager == this){
325 widget._onFocus(by);
326 }
327 this.emit("widget-focus", widget, by);
328 }
329 }
330 },
331
332 focus: function(node){
333 // summary:
334 // Focus the specified node, suppressing errors if they occur
335 if(node){
336 try{ node.focus(); }catch(e){/*quiet*/}
337 }
338 }
339 });
340
341 var singleton = new FocusManager();
342
343 // register top window and all the iframes it contains
344 domReady(function(){
345 var handle = singleton.registerWin(winUtils.get(document));
346 if(has("ie")){
347 on(window, "unload", function(){
348 if(handle){ // because this gets called twice when doh.robot is running
349 handle.remove();
350 handle = null;
351 }
352 });
353 }
354 });
355
356 // Setup dijit.focus as a pointer to the singleton but also (for backwards compatibility)
357 // as a function to set focus. Remove for 2.0.
358 dijit.focus = function(node){
359 singleton.focus(node); // indirection here allows dijit/_base/focus.js to override behavior
360 };
361 for(var attr in singleton){
362 if(!/^_/.test(attr)){
363 dijit.focus[attr] = typeof singleton[attr] == "function" ? lang.hitch(singleton, attr) : singleton[attr];
364 }
365 }
366 singleton.watch(function(attr, oldVal, newVal){
367 dijit.focus[attr] = newVal;
368 });
369
370 return singleton;
371 });