Permalink
Browse files

improve keyboard handling. fixes #389

  • Loading branch information...
1 parent 908c358 commit 4b1204c5dedf1fe321b3be926a1fb1353a2bfd54 @ivaynberg ivaynberg committed Sep 13, 2012
Showing with 7 additions and 38 deletions.
  1. +7 −38 select2.js
View
@@ -1537,56 +1537,25 @@
selection.bind("keydown", this.bind(function(e) {
if (!this.enabled) return;
- if (e.which === KEY.PAGE_UP || e.which === KEY.PAGE_DOWN) {
- // prevent the page from scrolling
+ if (e.which == KEY.DOWN || e.which == KEY.UP
+ || (e.which == KEY.ENTER && this.opts.openOnEnter)) {
+ this.open();
killEvent(e);
return;
}
- if (e.which === KEY.TAB || KEY.isControl(e) || KEY.isFunctionKey(e)
- || e.which === KEY.ESC) {
- return;
- }
-
- if (this.opts.openOnEnter === false && e.which === KEY.ENTER) {
- return;
- }
-
if (e.which == KEY.DELETE || e.which == KEY.BACKSPACE) {
if (this.opts.allowClear) {
this.clear();
}
killEvent(e);
return;
}
-
+ }));
+ selection.bind("keypress", this.bind(function(e) {
+ var key = String.fromCharCode(e.which);
+ this.search.val(key);
this.open();
-
- if (e.which === KEY.ENTER) {
- // do not propagate the event otherwise we open, and propagate enter which closes
- killEvent(e);
- return;
- }
-
- // do not set the search input value for non-alpha-numeric keys
- // otherwise pressing down results in a '(' being set in the search field
- if (e.which < 48 ) { // '0' == 48
- killEvent(e);
- return;
- }
-
- var keyWritten = String.fromCharCode(e.which).toLowerCase();
-
- if (e.shiftKey) {
- keyWritten = keyWritten.toUpperCase();
- }
-
- // focus the field before calling val so the cursor ends up after the value instead of before
- this.search.focus();
- this.search.val(keyWritten);
-
- // prevent event propagation so it doesnt replay on the now focussed search field and result in double key entry
- killEvent(e);
}));
this.setPlaceholder();

0 comments on commit 4b1204c

Please sign in to comment.