r2733 - added type-ahead support to match native select behavior. Multiple same-keypresses will i...

r2733 - added type-ahead support to match native select behavior. Multiple same-keypresses will i...


Author: scottjehl
Date: Fri Jun 12 13:14:26 2009
New Revision: 2733
Modified:
branches/labs/selectmenu/ui.selectmenu.js
Log:
added type-ahead support to match native select behavior. Multiple
same-keypresses will iterate to next item starting with that letter.
Modified: branches/labs/selectmenu/ui.selectmenu.js
==============================================================================
--- branches/labs/selectmenu/ui.selectmenu.js    (original)
+++ branches/labs/selectmenu/ui.selectmenu.js    Fri Jun 12 13:14:26 2009
@@ -66,6 +66,9 @@
                    //trigger click event on next (should be abstracted out)
                    
self.list.find('li.ui-selectmenu-item-selected').next().trigger('mouseup');    
                    break;    
+                default:
+                //    self._typeAhead(event.keyCode);
+                    break;    
            }
            return ret;
        })
@@ -187,6 +190,10 @@
                    ret = true;
                    self.close();
                    break;    
+                default:
+                    ret = false;
+                    self._typeAhead(event.keyCode);
+                    break;        
            }
            return ret;
        });
@@ -223,6 +230,35 @@
        $('label[for='+this.newelement.attr('id')+']')
            .attr('for',this.element.attr('id'))
            .unbind('click');
+    },
+    
+    _prevChar: ['',0],
+    
+    _typeAhead: function(code){
+        var self = this;
+        var C = String.fromCharCode(code);
+        c = C.toLowerCase();
+        var focusFound = false;
+        this.list.find('li a').each(function(i){    
+            if(!focusFound){
+                var thisText = $(this).text();
+                if( thisText.indexOf(C) == 0 || thisText.indexOf(c) == 0){
+                        if(self._prevChar[0] == C){
+                            if(self._prevChar[1] < i){
+                                focusFound = true;
+                                $(this).focus();
+                                self._prevChar[1] = i;
+                            }    
+                        }
+                        else{
+                            focusFound = true;
+                            $(this).focus();
+                            self._prevChar[1] = i;
+                        }    
+                }
+            }
+        });
+        this._prevChar[0] = C;
    },
    
    _closeOthers: function(){