@@ -33,6 +33,7 @@ $.widget("moogle.contextmenu", {
3333 version : "@VERSION" ,
3434 options : {
3535 addClass : "ui-contextmenu" , // Add this class to the outer <ul>
36+ autoFocus : false , // Set keyboard focus to first entry on open
3637 autoTrigger : true , // open menu on browser's `contextmenu` event
3738 delegate : null , // selector
3839 hide : { effect : "fadeOut" , duration : "fast" } ,
@@ -64,6 +65,7 @@ $.widget("moogle.contextmenu", {
6465 this . $menu = null ;
6566 this . menuIsTemp = false ;
6667 this . currentTarget = null ;
68+ this . previousFocus = null ;
6769
6870 if ( opts . preventSelect ) {
6971 // Create a global style for all potential menu targets
@@ -261,7 +263,14 @@ $.widget("moogle.contextmenu", {
261263 event . preventDefault ( ) ;
262264 } ) ;
263265 }
264- this . _show ( this . $menu , this . options . show , function ( ) {
266+ this . _show ( this . $menu , opts . show , function ( ) {
267+ // Set focus to first active menu entry
268+ if ( opts . autoFocus ) {
269+ // var $first = self.$menu.children(".ui-menu-item:enabled:first");
270+ // self.$menu.menu("focus", event, $first).focus();
271+ self . $menu . focus ( ) ;
272+ self . previousFocus = $ ( event . target ) ;
273+ }
265274 self . _trigger . call ( self , "open" , event , ui ) ;
266275 } ) ;
267276 } ,
@@ -281,6 +290,10 @@ $.widget("moogle.contextmenu", {
281290 this . $menu
282291 . unbind ( "contextmenu" + this . eventNamespace ) ;
283292 this . _hide ( this . $menu , hideOpts , function ( ) {
293+ if ( self . previousFocus ) {
294+ self . previousFocus . focus ( ) ;
295+ self . previousFocus = null ;
296+ }
284297 self . _trigger ( "close" ) ;
285298 } ) ;
286299 } else {
0 commit comments