From ef364d2118ec0e32e8817f2a05841a4a8c43ffc3 Mon Sep 17 00:00:00 2001 From: Igor Vaynberg Date: Fri, 8 Feb 2013 09:33:18 -0800 Subject: [PATCH] better handling of first character typed. fixes #196 --- select2.css | 1 - select2.js | 238 +++++++++++++++++++++++++--------------------------- 2 files changed, 115 insertions(+), 124 deletions(-) diff --git a/select2.css b/select2.css index d8b43d87..456e049f 100644 --- a/select2.css +++ b/select2.css @@ -261,7 +261,6 @@ Version: @@ver@@ Timestamp: @@timestamp@@ } .select2-dropdown-open .select2-choice { - border: 1px solid #aaa; border-bottom-color: transparent; -webkit-box-shadow: 0 1px 0 #fff inset; -moz-box-shadow: 0 1px 0 #fff inset; diff --git a/select2.js b/select2.js index 2ffb25b6..a135d424 100644 --- a/select2.js +++ b/select2.js @@ -211,6 +211,34 @@ the specific language governing permissions and limitations under the Apache Lic }); } + function focus($el) { + if ($el[0] === document.activeElement) return; + + /* set the focus in a 0 timeout - that way the focus is set after the processing + of the current event has finished - which seems like the only reliable way + to set focus */ + window.setTimeout(function() { + var el=$el[0], pos=$el.val().length, range; + + $el.focus(); + + /* after the focus is set move the caret to the end, necessary when we val() + just before setting focus */ + if(el.setSelectionRange) + { + el.setSelectionRange(pos, pos); + } + else if (el.createTextRange) { + range = el.createTextRange(); + range.collapse(true); + range.moveEnd('character', pos); + range.moveStart('character', pos); + range.select(); + } + + }, 0); + } + function killEvent(event) { event.preventDefault(); event.stopPropagation(); @@ -1075,7 +1103,7 @@ the specific language governing permissions and limitations under the Apache Lic $("#select2-drop-mask").hide(); this.dropdown.removeAttr("id"); // only the active dropdown has the select2-drop id this.dropdown.hide(); - this.container.removeClass("select2-dropdown-open").removeClass("select2-container-active"); + this.container.removeClass("select2-dropdown-open"); this.results.empty(); this.clearSearch(); @@ -1355,7 +1383,6 @@ the specific language governing permissions and limitations under the Apache Lic this.close(); this.container.removeClass("select2-container-active"); - this.dropdown.removeClass("select2-drop-active"); // synonymous to .is(':focus'), which is available in jquery >= 1.6 if (this.search[0] === document.activeElement) { this.search.blur(); } this.clearSearch(); @@ -1364,18 +1391,7 @@ the specific language governing permissions and limitations under the Apache Lic // abstract focusSearch: function () { - // need to do it here as well as in timeout so it works in IE - this.search.show(); - this.search.focus(); - - /* we do this in a timeout so that current event processing can complete before this code is executed. - this makes sure the search field is focussed even if the current event would blur it */ - window.setTimeout(this.bind(function () { - // reset the value so IE places the cursor at the end of the input box - this.search.show(); - this.search.focus(); - this.search.val(this.search.val()); - }), 10); + focus(this.search); }, // abstract @@ -1467,11 +1483,12 @@ the specific language governing permissions and limitations under the Apache Lic var container = $(document.createElement("div")).attr({ "class": "select2-container" }).html([ - " ", + "", " ", "
" , "
", - "
" , + "", + "