浏览代码

improved blur tracking. closes #2

Igor Vaynberg 13 年之前
父节点
当前提交
1afffb09dc
共有 1 个文件被更改,包括 14 次插入20 次删除
  1. 14 20
      select2.js

+ 14 - 20
select2.js

@@ -20,9 +20,7 @@
     "use strict";
     /*global document, window, jQuery, console */
 
-    var uid = 0, KEY;
-
-    KEY = {
+    var KEY = {
         TAB: 9,
         ENTER: 13,
         ESC: 27,
@@ -144,6 +142,18 @@
         return width;
     }
 
+    /**
+     * blurs any Select2 container that has focus when an element outside them was clicked or received focus
+     */
+    $(document).ready(function () {
+        $(document).on("mousedown focusin", function (e) {
+            var target = $(e.target).closest("div.select2-container").get(0);
+            $(document).find("div.select2-container-active").each(function () {
+                if (this !== target) $(this).data("select2").blur();
+            });
+        });
+    });
+
     /**
      *
      * @param opts
@@ -161,9 +171,6 @@
     AbstractSelect2.prototype.init = function (opts) {
         var results, search;
 
-        this.uid = uid;
-        uid = uid + 1;
-
         // prepare options
         this.opts = this.prepareOpts(opts);
 
@@ -333,13 +340,6 @@
 
         this.alignDropdown();
         this.dropdown.show();
-
-        // register click-outside-closes-dropdown listener
-        $(document).on("click.id" + this.uid, this.bind(function (e) {
-            if ($(e.target).closest(this.container).length === 0) {
-                this.blur();
-            }
-        }));
     };
 
     AbstractSelect2.prototype.close = function () {
@@ -347,7 +347,6 @@
 
         this.dropdown.hide();
         this.container.removeClass("select2-dropdown-open");
-        $(document).off("click.id" + this.uid);
 
         if (this.select) {
             // TODO see if we can always clear here and reset on open
@@ -439,7 +438,7 @@
             more = results.find("li.select2-more-results"),
             below, // pixels the element is below the scroll fold, below==0 is when the element is starting to be visible
             offset = -1, // index of first element without data
-            page = this.resultsPage+1;
+            page = this.resultsPage + 1;
 
         if (more.length === 0) return;
 
@@ -835,11 +834,6 @@
                 }
             }
 
-            if (e.which === KEY.TAB) {
-                this.blur();
-                return;
-            }
-
             if (e.which === KEY.TAB || KEY.isControl(e) || KEY.isFunctionKey(e) || e.which === KEY.BACKSPACE || e.which === KEY.ESC) {
                 return;
             }