|
433 | 433 | }); |
434 | 434 | }); |
435 | 435 |
|
436 | | - /** |
437 | | - * Closes any opened Select2s when the window is resized |
438 | | - */ |
439 | | - $(window).resize(debounce(100, function() { |
440 | | - $(".select2-container.select2-dropdown-open").select2('close'); |
441 | | - })); |
442 | | - |
443 | | - /** |
444 | | - * Closes any opened Select2s when the window is scrolled |
445 | | - */ |
446 | | - $(window).scroll(debounce(100, function() { |
447 | | - $(".select2-container.select2-dropdown-open").select2('close'); |
448 | | - })); |
449 | | - |
450 | | - |
451 | 436 | /** |
452 | 437 | * Creates a new class |
453 | 438 | * |
|
491 | 476 | this.enabled=true; |
492 | 477 | this.container = this.createContainer(); |
493 | 478 |
|
| 479 | + this.containerId="s2id"+nextUid(); |
| 480 | + this.container.attr("id", this.containerId); |
| 481 | + |
494 | 482 | // cache the body so future lookups are cheap |
495 | 483 | this.body = thunk(function() { return opts.element.closest("body"); }); |
496 | 484 |
|
|
873 | 861 | */ |
874 | 862 | // abstract |
875 | 863 | opening: function() { |
| 864 | + var cid = this.containerId, selector = "#"+ cid, |
| 865 | + scroll = "scroll." + cid, resize = "resize." + cid; |
| 866 | + |
| 867 | + this.container.parents().each(function() { |
| 868 | + $(this).bind(scroll, function() { |
| 869 | + var s2 = $(selector); |
| 870 | + if (s2.length == 0) { |
| 871 | + $(this).unbind(scroll); |
| 872 | + } |
| 873 | + s2.select2("close"); |
| 874 | + }); |
| 875 | + }); |
| 876 | + |
| 877 | + $(window).bind(resize, function() { |
| 878 | + var s2 = $(selector); |
| 879 | + if (s2.length == 0) { |
| 880 | + $(window).unbind(resize); |
| 881 | + } |
| 882 | + s2.select2("close"); |
| 883 | + }); |
| 884 | + |
876 | 885 | this.clearDropdownAlignmentPreference(); |
877 | 886 |
|
878 | 887 | if (this.search.val() === " ") { this.search.val(""); } |
|
898 | 907 | close: function () { |
899 | 908 | if (!this.opened()) return; |
900 | 909 |
|
| 910 | + var self = this; |
| 911 | + |
| 912 | + this.container.parents().each(function() { |
| 913 | + $(this).unbind("scroll." + self.containerId); |
| 914 | + }); |
| 915 | + $(window).unbind("resize." + this.containerId); |
| 916 | + |
901 | 917 | this.clearDropdownAlignmentPreference(); |
902 | 918 |
|
903 | 919 | this.dropdown.hide(); |
|
2138 | 2154 | var args = Array.prototype.slice.call(arguments, 0), |
2139 | 2155 | opts, |
2140 | 2156 | select2, |
2141 | | - value, multiple, allowedMethods = ["val", "destroy", "open", "close", "focus", "isFocused", "container", "onSortStart", "onSortEnd", "enable", "disable", "positionDropdown", "data"]; |
| 2157 | + value, multiple, allowedMethods = ["val", "destroy", "opened", "open", "close", "focus", "isFocused", "container", "onSortStart", "onSortEnd", "enable", "disable", "positionDropdown", "data"]; |
2142 | 2158 |
|
2143 | 2159 | this.each(function () { |
2144 | 2160 | if (args.length === 0 || typeof(args[0]) === "object") { |
|
0 commit comments