1
0
mirror of synced 2024-11-30 00:26:03 +03:00

close on scroll and resize. fixes #149

This commit is contained in:
Igor Vaynberg 2012-08-06 23:44:39 -07:00
parent 921bf06434
commit d92a572694

View File

@ -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 * Creates a new class
* *
@ -491,6 +476,9 @@
this.enabled=true; this.enabled=true;
this.container = this.createContainer(); this.container = this.createContainer();
this.containerId="s2id"+nextUid();
this.container.attr("id", this.containerId);
// cache the body so future lookups are cheap // cache the body so future lookups are cheap
this.body = thunk(function() { return opts.element.closest("body"); }); this.body = thunk(function() { return opts.element.closest("body"); });
@ -873,6 +861,27 @@
*/ */
// abstract // abstract
opening: function() { 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(); this.clearDropdownAlignmentPreference();
if (this.search.val() === " ") { this.search.val(""); } if (this.search.val() === " ") { this.search.val(""); }
@ -898,6 +907,13 @@
close: function () { close: function () {
if (!this.opened()) return; 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.clearDropdownAlignmentPreference();
this.dropdown.hide(); this.dropdown.hide();
@ -2138,7 +2154,7 @@
var args = Array.prototype.slice.call(arguments, 0), var args = Array.prototype.slice.call(arguments, 0),
opts, opts,
select2, 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 () { this.each(function () {
if (args.length === 0 || typeof(args[0]) === "object") { if (args.length === 0 || typeof(args[0]) === "object") {