Debian Codegraph @

dijit/_MenuBase.js

raw symbols SCIP
1 define([
2 "dojo/_base/array", // array.indexOf
3 "dojo/_base/declare", // declare
4 "dojo/dom", // dom.isDescendant domClass.replace
5 "dojo/dom-attr",
6 "dojo/dom-class", // domClass.replace
7 "dojo/_base/lang", // lang.hitch
8 "dojo/mouse", // mouse.enter, mouse.leave
9 "dojo/on",
10 "dojo/window",
11 "./a11yclick",
12 "./registry",
13 "./_Widget",
14 "./_CssStateMixin",
15 "./_KeyNavContainer",
16 "./_TemplatedMixin"
17 ], function(array, declare, dom, domAttr, domClass, lang, mouse, on, winUtils, a11yclick,
18 registry, _Widget, _CssStateMixin, _KeyNavContainer, _TemplatedMixin){
19
20 // module:
21 // dijit/_MenuBase
22
23 return declare("dijit._MenuBase", [_Widget, _TemplatedMixin, _KeyNavContainer, _CssStateMixin], {
24 // summary:
25 // Abstract base class for Menu and MenuBar.
26 // Subclass should implement _onUpArrow(), _onDownArrow(), _onLeftArrow(), and _onRightArrow().
27
28 // selected: dijit/MenuItem
29 // Currently selected (a.k.a. highlighted) MenuItem, or null if no MenuItem is selected.
30 // If a submenu is open, will be set to MenuItem that displayed the submenu. OTOH, if
31 // this Menu is in passive mode (i.e. hasn't been clicked yet), will be null, because
32 // "selected" is not merely "hovered".
33 selected: null,
34 _setSelectedAttr: function(item){
35 if(this.selected != item){
36 if(this.selected){
37 this.selected._setSelected(false);
38 this._onChildDeselect(this.selected);
39 }
40 if(item){
41 item._setSelected(true);
42 }
43 this._set("selected", item);
44 }
45 },
46
47 // activated: [readonly] Boolean
48 // This Menu has been clicked (mouse or via space/arrow key) or opened as a submenu,
49 // so mere mouseover will open submenus. Focusing a menu via TAB does NOT automatically make it active
50 // since TAB is a navigation operation and not a selection one.
51 // For Windows apps, pressing the ALT key focuses the menubar menus (similar to TAB navigation) but the
52 // menu is not active (ie no dropdown) until an item is clicked.
53 activated: false,
54 _setActivatedAttr: function(val){
55 domClass.toggle(this.domNode, "dijitMenuActive", val);
56 domClass.toggle(this.domNode, "dijitMenuPassive", !val);
57 this._set("activated", val);
58 },
59
60 // parentMenu: [readonly] Widget
61 // pointer to menu that displayed me
62 parentMenu: null,
63
64 // popupDelay: Integer
65 // After a menu has been activated (by clicking on it etc.), number of milliseconds before hovering
66 // (without clicking) another MenuItem causes that MenuItem's popup to automatically open.
67 popupDelay: 500,
68
69 // passivePopupDelay: Integer
70 // For a passive (unclicked) Menu, number of milliseconds before hovering (without clicking) will cause
71 // the popup to open. Default is Infinity, meaning you need to click the menu to open it.
72 passivePopupDelay: Infinity,
73
74 // autoFocus: Boolean
75 // A toggle to control whether or not a Menu gets focused when opened as a drop down from a MenuBar
76 // or DropDownButton/ComboButton. Note though that it always get focused when opened via the keyboard.
77 autoFocus: false,
78
79 childSelector: function(/*DOMNode*/ node){
80 // summary:
81 // Selector (passed to on.selector()) used to identify MenuItem child widgets, but exclude inert children
82 // like MenuSeparator. If subclass overrides to a string (ex: "> *"), the subclass must require dojo/query.
83 // tags:
84 // protected
85
86 var widget = registry.byNode(node);
87 return node.parentNode == this.containerNode && widget && widget.focus;
88 },
89
90 postCreate: function(){
91 var self = this,
92 matches = typeof this.childSelector == "string" ? this.childSelector : lang.hitch(this, "childSelector");
93 this.own(
94 on(this.containerNode, on.selector(matches, mouse.enter), function(){
95 self.onItemHover(registry.byNode(this));
96 }),
97 on(this.containerNode, on.selector(matches, mouse.leave), function(){
98 self.onItemUnhover(registry.byNode(this));
99 }),
100 on(this.containerNode, on.selector(matches, a11yclick), function(evt){
101 self.onItemClick(registry.byNode(this), evt);
102 evt.stopPropagation();
103 }),
104 on(this.containerNode, on.selector(matches, "focusin"), function(){
105 self._onItemFocus(registry.byNode(this));
106 })
107 );
108 this.inherited(arguments);
109 },
110
111 onKeyboardSearch: function(/*MenuItem*/ item, /*Event*/ evt, /*String*/ searchString, /*Number*/ numMatches){
112 // summary:
113 // Attach point for notification about when a menu item has been searched for
114 // via the keyboard search mechanism.
115 // tags:
116 // protected
117 this.inherited(arguments);
118 if(!!item && (numMatches == -1 || (!!item.popup && numMatches == 1))){
119 this.onItemClick(item, evt);
120 }
121 },
122
123 _keyboardSearchCompare: function(/*dijit/_WidgetBase*/ item, /*String*/ searchString){
124 // summary:
125 // Compares the searchString to the widget's text label, returning:
126 // -1: a high priority match and stop searching
127 // 0: no match
128 // 1: a match but keep looking for a higher priority match
129 // tags:
130 // private
131 if(!!item.shortcutKey){
132 // accessKey matches have priority
133 return searchString == item.shortcutKey.toLowerCase() ? -1 : 0;
134 }
135 return this.inherited(arguments) ? 1 : 0; // change return value of -1 to 1 so that searching continues
136 },
137
138 onExecute: function(){
139 // summary:
140 // Attach point for notification about when a menu item has been executed.
141 // This is an internal mechanism used for Menus to signal to their parent to
142 // close them, because they are about to execute the onClick handler. In
143 // general developers should not attach to or override this method.
144 // tags:
145 // protected
146 },
147
148 onCancel: function(/*Boolean*/ /*===== closeAll =====*/){
149 // summary:
150 // Attach point for notification about when the user cancels the current menu
151 // This is an internal mechanism used for Menus to signal to their parent to
152 // close them. In general developers should not attach to or override this method.
153 // tags:
154 // protected
155 },
156
157 _moveToPopup: function(/*Event*/ evt){
158 // summary:
159 // This handles the right arrow key (left arrow key on RTL systems),
160 // which will either open a submenu, or move to the next item in the
161 // ancestor MenuBar
162 // tags:
163 // private
164
165 if(this.focusedChild && this.focusedChild.popup && !this.focusedChild.disabled){
166 this.onItemClick(this.focusedChild, evt);
167 }else{
168 var topMenu = this._getTopMenu();
169 if(topMenu && topMenu._isMenuBar){
170 topMenu.focusNext();
171 }
172 }
173 },
174
175 _onPopupHover: function(/*Event*/ /*===== evt =====*/){
176 // summary:
177 // This handler is called when the mouse moves over the popup.
178 // tags:
179 // private
180
181 // if the mouse hovers over a menu popup that is in pending-close state,
182 // then stop the close operation.
183 // This can't be done in onItemHover since some popup targets don't have MenuItems (e.g. ColorPicker)
184
185 // highlight the parent menu item pointing to this popup (in case user temporarily moused over another MenuItem)
186 this.set("selected", this.currentPopupItem);
187
188 // cancel the pending close (if there is one) (in case user temporarily moused over another MenuItem)
189 this._stopPendingCloseTimer();
190 },
191
192 onItemHover: function(/*MenuItem*/ item){
193 // summary:
194 // Called when cursor is over a MenuItem.
195 // tags:
196 // protected
197
198 // Don't do anything unless user has "activated" the menu by:
199 // 1) clicking it
200 // 2) opening it from a parent menu (which automatically activates it)
201
202 if(this.activated){
203 this.set("selected", item);
204 if(item.popup && !item.disabled && !this.hover_timer){
205 this.hover_timer = this.defer(function(){
206 this._openItemPopup(item);
207 }, this.popupDelay);
208 }
209 }else if(this.passivePopupDelay < Infinity){
210 if(this.passive_hover_timer){
211 this.passive_hover_timer.remove();
212 }
213 this.passive_hover_timer = this.defer(function(){
214 this.onItemClick(item, {type: "click"});
215 }, this.passivePopupDelay);
216 }
217
218 this._hoveredChild = item;
219
220 item._set("hovering", true);
221 },
222
223 _onChildDeselect: function(item){
224 // summary:
225 // Called when a child MenuItem becomes deselected. Setup timer to close its popup.
226
227 this._stopPopupTimer();
228
229 // Setup timer to close all popups that are open and descendants of this menu.
230 // Will be canceled if user quickly moves the mouse over the popup.
231 if(this.currentPopupItem == item){
232 this._stopPendingCloseTimer();
233 this._pendingClose_timer = this.defer(function(){
234 this._pendingClose_timer = null;
235 this.currentPopupItem = null;
236 item._closePopup(); // this calls onClose
237 }, this.popupDelay);
238 }
239 },
240
241 onItemUnhover: function(/*MenuItem*/ item){
242 // summary:
243 // Callback fires when mouse exits a MenuItem
244 // tags:
245 // protected
246
247 if(this._hoveredChild == item){
248 this._hoveredChild = null;
249 }
250
251 if(this.passive_hover_timer){
252 this.passive_hover_timer.remove();
253 this.passive_hover_timer = null;
254 }
255
256 item._set("hovering", false);
257 },
258
259 _stopPopupTimer: function(){
260 // summary:
261 // Cancels the popup timer because the user has stop hovering
262 // on the MenuItem, etc.
263 // tags:
264 // private
265
266 if(this.hover_timer){
267 this.hover_timer = this.hover_timer.remove();
268 }
269 },
270
271 _stopPendingCloseTimer: function(){
272 // summary:
273 // Cancels the pending-close timer because the close has been preempted
274 // tags:
275 // private
276 if(this._pendingClose_timer){
277 this._pendingClose_timer = this._pendingClose_timer.remove();
278 }
279 },
280
281 _getTopMenu: function(){
282 // summary:
283 // Returns the top menu in this chain of Menus
284 // tags:
285 // private
286 for(var top = this; top.parentMenu; top = top.parentMenu){}
287 return top;
288 },
289
290 onItemClick: function(/*dijit/_WidgetBase*/ item, /*Event*/ evt){
291 // summary:
292 // Handle clicks on an item.
293 // tags:
294 // private
295
296 if(this.passive_hover_timer){
297 this.passive_hover_timer.remove();
298 }
299
300 this.focusChild(item);
301
302 if(item.disabled){
303 return false;
304 }
305
306 if(item.popup){
307 this.set("selected", item);
308 this.set("activated", true);
309 var byKeyboard = /^key/.test(evt._origType || evt.type) ||
310 (evt.clientX == 0 && evt.clientY == 0); // detects accessKey like ALT+SHIFT+F, where type is "click"
311 this._openItemPopup(item, byKeyboard);
312 }else{
313 // before calling user defined handler, close hierarchy of menus
314 // and restore focus to place it was when menu was opened
315 this.onExecute();
316
317 // user defined handler for click
318 item._onClick ? item._onClick(evt) : item.onClick(evt);
319 }
320 },
321
322 _openItemPopup: function(/*dijit/MenuItem*/ from_item, /*Boolean*/ focus){
323 // summary:
324 // Open the popup to the side of/underneath the current menu item, and optionally focus first item
325 // tags:
326 // protected
327
328 if(from_item == this.currentPopupItem){
329 // Specified popup is already being shown, so just return
330 return;
331 }
332 if(this.currentPopupItem){
333 // If another popup is currently shown, then close it
334 this._stopPendingCloseTimer();
335 this.currentPopupItem._closePopup();
336 }
337 this._stopPopupTimer();
338
339 var popup = from_item.popup;
340 popup.parentMenu = this;
341
342 // detect mouseover of the popup to handle lazy mouse movements that temporarily focus other menu items\c
343 this.own(this._mouseoverHandle = on.once(popup.domNode, "mouseover", lang.hitch(this, "_onPopupHover")));
344
345 var self = this;
346 from_item._openPopup({
347 parent: this,
348 orient: this._orient || ["after", "before"],
349 onCancel: function(){ // called when the child menu is canceled
350 if(focus){
351 // put focus back on my node before focused node is hidden
352 self.focusChild(from_item);
353 }
354
355 // close the submenu (be sure this is done _after_ focus is moved)
356 self._cleanUp();
357 },
358 onExecute: lang.hitch(this, "_cleanUp", true),
359 onClose: function(){
360 // Remove handler created by onItemHover
361 if(self._mouseoverHandle){
362 self._mouseoverHandle.remove();
363 delete self._mouseoverHandle;
364 }
365 }
366 }, focus);
367
368 this.currentPopupItem = from_item;
369
370 // TODO: focusing a popup should clear tabIndex on Menu (and it's child MenuItems), so that neither
371 // TAB nor SHIFT-TAB returns to the menu. Only ESC or ENTER should return to the menu.
372 },
373
374 onOpen: function(/*Event*/ /*===== e =====*/){
375 // summary:
376 // Callback when this menu is opened.
377 // This is called by the popup manager as notification that the menu
378 // was opened.
379 // tags:
380 // private
381
382 this.isShowingNow = true;
383 this.set("activated", true);
384 },
385
386 onClose: function(){
387 // summary:
388 // Callback when this menu is closed.
389 // This is called by the popup manager as notification that the menu
390 // was closed.
391 // tags:
392 // private
393
394 this.set("activated", false);
395 this.set("selected", null);
396 this.isShowingNow = false;
397 this.parentMenu = null;
398 },
399
400 _closeChild: function(){
401 // summary:
402 // Called when submenu is clicked or focus is lost. Close hierarchy of menus.
403 // tags:
404 // private
405 this._stopPopupTimer();
406
407 if(this.currentPopupItem){
408 // If focus is on a descendant MenuItem then move focus to me,
409 // because IE doesn't like it when you display:none a node with focus,
410 // and also so keyboard users don't lose control.
411 // Likely, immediately after a user defined onClick handler will move focus somewhere
412 // else, like a Dialog.
413 if(this.focused){
414 domAttr.set(this.selected.focusNode, "tabIndex", this.tabIndex);
415 this.selected.focusNode.focus();
416 }
417
418 // Close all popups that are open and descendants of this menu
419 this.currentPopupItem._closePopup();
420 this.currentPopupItem = null;
421 }
422 },
423
424 _onItemFocus: function(/*MenuItem*/ item){
425 // summary:
426 // Called when child of this Menu gets focus from:
427 //
428 // 1. clicking it
429 // 2. tabbing into it
430 // 3. being opened by a parent menu.
431 //
432 // This is not called just from mouse hover.
433
434 if(this._hoveredChild && this._hoveredChild != item){
435 this.onItemUnhover(this._hoveredChild); // any previous mouse movement is trumped by focus selection
436 }
437 this.set("selected", item);
438 },
439
440 _onBlur: function(){
441 // summary:
442 // Called when focus is moved away from this Menu and it's submenus.
443 // tags:
444 // protected
445
446 this._cleanUp(true);
447 this.inherited(arguments);
448 },
449
450 _cleanUp: function(/*Boolean*/ clearSelectedItem){
451 // summary:
452 // Called when the user is done with this menu. Closes hierarchy of menus.
453 // tags:
454 // private
455
456 this._closeChild(); // don't call this.onClose since that's incorrect for MenuBar's that never close
457 if(typeof this.isShowingNow == 'undefined'){ // non-popup menu doesn't call onClose
458 this.set("activated", false);
459 }
460
461 if(clearSelectedItem){
462 this.set("selected", null);
463 }
464 }
465 });
466 });