Преглед на файлове

close on scroll and resize. fixes #149

Igor Vaynberg преди 12 години
родител
ревизия
d92a572694
променени са 1 файла, в които са добавени 32 реда и са изтрити 16 реда
  1. 32 16
      select2.js

+ 32 - 16
select2.js

@@ -433,21 +433,6 @@
         });
     });
 
-    /**
-     * Closes any opened Select2s when the window is resized
-     */
-    $(window).resize(debounce(100, function() {
-        $(".select2-container.select2-dropdown-open").select2('close');
-    }));
-
-    /**
-     * Closes any opened Select2s when the window is scrolled
-     */
-    $(window).scroll(debounce(100, function() {
-        $(".select2-container.select2-dropdown-open").select2('close');
-    }));
-
-
     /**
      * Creates a new class
      *
@@ -491,6 +476,9 @@
             this.enabled=true;
             this.container = this.createContainer();
 
+            this.containerId="s2id"+nextUid();
+            this.container.attr("id", this.containerId);
+
             // cache the body so future lookups are cheap
             this.body = thunk(function() { return opts.element.closest("body"); });
 
@@ -873,6 +861,27 @@
          */
         // abstract
         opening: function() {
+            var cid = this.containerId, selector = "#"+ cid,
+                scroll = "scroll." + cid, resize = "resize." + cid;
+
+            this.container.parents().each(function() {
+                $(this).bind(scroll, function() {
+                    var s2 = $(selector);
+                    if (s2.length == 0) {
+                        $(this).unbind(scroll);
+                    }
+                    s2.select2("close");
+                });
+            });
+
+            $(window).bind(resize, function() {
+                var s2 = $(selector);
+                if (s2.length == 0) {
+                    $(window).unbind(resize);
+                }
+                s2.select2("close");
+            });
+
             this.clearDropdownAlignmentPreference();
 
             if (this.search.val() === " ") { this.search.val(""); }
@@ -898,6 +907,13 @@
         close: function () {
             if (!this.opened()) return;
 
+            var self = this;
+
+            this.container.parents().each(function() {
+                $(this).unbind("scroll." + self.containerId);
+            });
+            $(window).unbind("resize." + this.containerId);
+
             this.clearDropdownAlignmentPreference();
 
             this.dropdown.hide();
@@ -2138,7 +2154,7 @@
         var args = Array.prototype.slice.call(arguments, 0),
             opts,
             select2,
-            value, multiple, allowedMethods = ["val", "destroy", "open", "close", "focus", "isFocused", "container", "onSortStart", "onSortEnd", "enable", "disable", "positionDropdown", "data"];
+            value, multiple, allowedMethods = ["val", "destroy", "opened", "open", "close", "focus", "isFocused", "container", "onSortStart", "onSortEnd", "enable", "disable", "positionDropdown", "data"];
 
         this.each(function () {
             if (args.length === 0 || typeof(args[0]) === "object") {