1 (function($, rf) {
  2 
  3     rf.ui = rf.ui || {};
  4 
  5     rf.ui.MenuKeyNavigation = {
  6 
  7         __updateItemsList : function() {
  8             var items = $('.' + this.options.cssClasses.listContainerCss + ':first',
  9                 this.popup.popup).find('>.' + this.options.cssClasses.itemCss).not(
 10                 '.' + this.options.cssClasses.disabledItemCss);
 11             return (this.items = items);
 12         },
 13 
 14         __selectPrev : function() {
 15             if (-1 == this.currentSelectedItemIndex) {
 16                 this.currentSelectedItemIndex = this.items.length - 1;
 17             } else {
 18                 this.__deselectCurrentItem();
 19             }
 20 
 21             if (this.currentSelectedItemIndex > 0) {
 22                 this.currentSelectedItemIndex--;
 23             } else {
 24                 this.currentSelectedItemIndex = this.items.length - 1;
 25             }
 26 
 27             this.__selectCurrentItem();
 28         },
 29 
 30         __selectNext : function() {
 31             if (-1 != this.currentSelectedItemIndex) {
 32                 this.__deselectCurrentItem();
 33             }
 34             if (this.currentSelectedItemIndex < this.items.length - 1) {
 35                 this.currentSelectedItemIndex++;
 36             } else {
 37                 this.currentSelectedItemIndex = 0;
 38             }
 39 
 40             this.__selectCurrentItem();
 41         },
 42 
 43         __deselectCurrentItem : function() {
 44             this.__deselectByIndex(this.currentSelectedItemIndex);
 45         },
 46 
 47         __selectCurrentItem : function() {
 48             this.__selectByIndex(this.currentSelectedItemIndex);
 49         },
 50 
 51         __selectFirstItem : function() {
 52             this.currentSelectedItemIndex = 0;
 53             this.__selectCurrentItem();
 54         },
 55 
 56         __selectByIndex : function(index) {
 57             if (-1 != index) {
 58                 rf.component(this.items.eq(index)).select();
 59             }
 60         },
 61 
 62         __deselectByIndex : function(index) {
 63             if (index > -1) {
 64                 rf.component(this.items.eq(index)).unselect();
 65             }
 66 
 67         },
 68 
 69         __openGroup : function() {
 70             var item = this.__getItemByIndex(this.currentSelectedItemIndex);
 71             if (this.__isGroup(item)) {
 72                 rf.component(item).show();
 73                 rf.component(item).__selectFirstItem();
 74                 this.active = false;
 75             }
 76         },
 77 
 78         __closeGroup : function() {
 79             var item = this.__getItemByIndex(this.currentSelectedItemIndex);
 80             if (this.__isGroup(item)) {
 81                 rf.component(item).__deselectCurrentItem();
 82                 rf.component(item).hide();
 83                 this.active = true;
 84             }
 85         },
 86 
 87         __returnToParentMenu : function() {
 88             var item = this.__getItemByIndex(this.currentSelectedItemIndex);
 89             var menu;
 90             menu = this.__getParentMenu() || this.__getParentMenuFromItem(item);
 91             if (menu != null && this.id != rf.component(menu).id) {
 92                 this.hide();
 93                 rf.component(menu).popupElement.focus();
 94             } else {
 95                 this.hide();
 96             }
 97         },
 98 
 99         __activateMenuItem : function() {
100             var item = this.__getCurrentItem();
101             if (item) {
102                 menuItemId = item.attr('id');
103                 this.activateItem(menuItemId);
104             }
105         },
106 
107         __getItemByIndex : function(index) {
108             if (index > -1) {
109                 return this.items.eq(index);
110             } else {
111                 return null;
112             }
113         },
114 
115         __getCurrentItem : function() {
116             return this.__getItemByIndex(this.currentSelectedItemIndex);
117         },
118 
119         __keydownHandler : function(e) {
120             var code;
121 
122             if (e.keyCode) {
123                 code = e.keyCode;
124             } else if (e.which) {
125                 code = e.which;
126             }
127 
128             activeMenu = rf.ui.MenuManager.getActiveSubMenu();
129 
130             if (this.popup.isVisible()) {
131                 switch (code) {
132                     case rf.KEYS.DOWN:
133                         e.preventDefault();
134                         activeMenu.__selectNext();
135                         //this.__setInputFocus();
136                         break;
137 
138                     case rf.KEYS.UP:
139                         e.preventDefault();
140                         activeMenu.__selectPrev();
141                         //this.__setInputFocus();
142                         break;
143                     case rf.KEYS.LEFT:
144                         e.preventDefault();
145                         activeMenu.__returnToParentMenu();
146                         break;
147 
148                     case rf.KEYS.RIGHT:
149                         e.preventDefault();
150                         activeMenu.__openGroup();
151                         break;
152 
153                     case rf.KEYS.ESC:
154                         e.preventDefault();
155                         activeMenu.__returnToParentMenu();
156                         break;
157 
158                     case rf.KEYS.RETURN:
159                         e.preventDefault();
160                         activeMenu.__activateMenuItem();
161                         //this.__setInputFocus();
162                         //return false;
163                         break;
164                 }
165                 e.stopPropagation();
166             }
167         }
168     }
169 })(RichFaces.jQuery, RichFaces);