-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathfocus.js
207 lines (181 loc) · 7.2 KB
/
focus.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
define([
"dojo/_base/array", // array.forEach
"dojo/dom", // dom.isDescendant
"dojo/_base/lang", // lang.isArray
"dojo/topic", // publish
"dojo/_base/window", // win.doc win.doc.selection win.global win.global.getSelection win.withGlobal
"../focus",
"../selection",
"../main" // for exporting symbols to dijit
], function(array, dom, lang, topic, win, focus, selection, dijit){
// module:
// dijit/_base/focus
var exports = {
// summary:
// Deprecated module to monitor currently focused node and stack of currently focused widgets.
// New code should access dijit/focus directly.
// _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
return dijit.getBookmark().isCollapsed;
},
getBookmark: function(){
// summary:
// Retrieves a bookmark that can be used with moveToBookmark to return to the same range
var sel = win.global == window ? selection : new selection.SelectionManager(win.global);
return sel.getBookmark();
},
moveToBookmark: function(/*Object*/ bookmark){
// summary:
// Moves current selection to a bookmark
// bookmark:
// This should be a returned object from dijit.getBookmark()
var sel = win.global == window ? selection : new selection.SelectionManager(win.global);
return sel.moveToBookmark(bookmark);
},
getFocus: function(/*Widget?*/ menu, /*Window?*/ openedForWindow){
// summary:
// Called as getFocus(), this returns an Object showing the current focus
// and selected text.
//
// Called as getFocus(widget), where widget is a (widget representing) a button
// that was just pressed, it returns where focus was before that button
// was pressed. (Pressing the button may have either shifted focus to the button,
// or removed focus altogether.) In this case the selected text is not returned,
// since it can't be accurately determined.
//
// menu: dijit/_WidgetBase|{domNode: DomNode} structure
// The button that was just pressed. If focus has disappeared or moved
// to this button, returns the previous focus. In this case the bookmark
// information is already lost, and null is returned.
//
// openedForWindow:
// iframe in which menu was opened
//
// returns:
// A handle to restore focus/selection, to be passed to `dijit.focus`
var node = !focus.curNode || (menu && dom.isDescendant(focus.curNode, menu.domNode)) ? dijit._prevFocus : focus.curNode;
return {
node: node,
bookmark: node && (node == focus.curNode) && win.withGlobal(openedForWindow || win.global, dijit.getBookmark),
openedForWindow: openedForWindow
}; // Object
},
// _activeStack: dijit/_WidgetBase[]
// 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 `<iframe>` itself.
// description:
// Currently only used by editor.
// returns:
// Handle to pass to unregisterIframe()
return focus.registerIframe(iframe);
},
unregisterIframe: function(/*Object*/ handle){
// summary:
// Unregisters listeners on the specified iframe created by registerIframe.
// After calling be sure to delete or null out the handle itself.
// handle:
// Handle returned by registerIframe()
handle && handle.remove();
},
registerWin: function(/*Window?*/targetWindow, /*DomNode?*/ effectiveNode){
// summary:
// Registers listeners on the specified window (either the main
// window or an iframe's window) to detect when the user has clicked somewhere
// or focused somewhere.
// description:
// Users should call registerIframe() instead of this method.
// targetWindow:
// If specified this is the window associated with the iframe,
// i.e. iframe.contentWindow.
// effectiveNode:
// If specified, report any focus events inside targetWindow as
// an event on effectiveNode, rather than on evt.target.
// returns:
// Handle to pass to unregisterWin()
return focus.registerWin(targetWindow, effectiveNode);
},
unregisterWin: function(/*Handle*/ handle){
// summary:
// Unregisters listeners on the specified window (either the main
// window or an iframe's window) according to handle returned from registerWin().
// After calling be sure to delete or null out the handle itself.
handle && handle.remove();
}
};
// Override focus singleton's focus function so that dijit.focus()
// has backwards compatible behavior of restoring selection (although
// probably no one is using that).
focus.focus = function(/*Object|DomNode */ handle){
// summary:
// Sets the focused node and the selection according to argument.
// To set focus to an iframe's content, pass in the iframe itself.
// handle:
// object returned by get(), or a DomNode
if(!handle){ return; }
var node = "node" in handle ? handle.node : handle, // because handle is either DomNode or a composite object
bookmark = handle.bookmark,
openedForWindow = handle.openedForWindow,
collapsed = bookmark ? bookmark.isCollapsed : false;
// Set the focus
// Note that for iframe's we need to use the <iframe> 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*/}
}
focus._onFocusNode(node);
}
// set the selection
// do not need to restore if current selection is not empty
// (use keyboard to select a menu item) or if previous selection was collapsed
// as it may cause focus shift (Esp in IE).
if(bookmark && win.withGlobal(openedForWindow || win.global, dijit.isCollapsed) && !collapsed){
if(openedForWindow){
openedForWindow.focus();
}
try{
win.withGlobal(openedForWindow || win.global, dijit.moveToBookmark, null, [bookmark]);
}catch(e2){
/*squelch IE internal error, see http://trac.dojotoolkit.org/ticket/1984 */
}
}
};
// For back compatibility, monitor changes to focused node and active widget stack,
// publishing events and copying changes from focus manager variables into dijit (top level) variables
focus.watch("curNode", function(name, oldVal, newVal){
dijit._curFocus = newVal;
dijit._prevFocus = oldVal;
if(newVal){
topic.publish("focusNode", newVal); // publish
}
});
focus.watch("activeStack", function(name, oldVal, newVal){
dijit._activeStack = newVal;
});
focus.on("widget-blur", function(widget, by){
topic.publish("widgetBlur", widget, by); // publish
});
focus.on("widget-focus", function(widget, by){
topic.publish("widgetFocus", widget, by); // publish
});
lang.mixin(dijit, exports);
/*===== return exports; =====*/
return dijit; // for back compat :-(
});