|
20 | 20 | "use strict"; |
21 | 21 | /*global document, window, jQuery, console */ |
22 | 22 |
|
23 | | - var uid = 0, KEY; |
24 | | - |
25 | | - KEY = { |
| 23 | + var KEY = { |
26 | 24 | TAB: 9, |
27 | 25 | ENTER: 13, |
28 | 26 | ESC: 27, |
|
144 | 142 | return width; |
145 | 143 | } |
146 | 144 |
|
| 145 | + /** |
| 146 | + * blurs any Select2 container that has focus when an element outside them was clicked or received focus |
| 147 | + */ |
| 148 | + $(document).ready(function () { |
| 149 | + $(document).on("mousedown focusin", function (e) { |
| 150 | + var target = $(e.target).closest("div.select2-container").get(0); |
| 151 | + $(document).find("div.select2-container-active").each(function () { |
| 152 | + if (this !== target) $(this).data("select2").blur(); |
| 153 | + }); |
| 154 | + }); |
| 155 | + }); |
| 156 | + |
147 | 157 | /** |
148 | 158 | * |
149 | 159 | * @param opts |
|
161 | 171 | AbstractSelect2.prototype.init = function (opts) { |
162 | 172 | var results, search; |
163 | 173 |
|
164 | | - this.uid = uid; |
165 | | - uid = uid + 1; |
166 | | - |
167 | 174 | // prepare options |
168 | 175 | this.opts = this.prepareOpts(opts); |
169 | 176 |
|
|
333 | 340 |
|
334 | 341 | this.alignDropdown(); |
335 | 342 | this.dropdown.show(); |
336 | | - |
337 | | - // register click-outside-closes-dropdown listener |
338 | | - $(document).on("click.id" + this.uid, this.bind(function (e) { |
339 | | - if ($(e.target).closest(this.container).length === 0) { |
340 | | - this.blur(); |
341 | | - } |
342 | | - })); |
343 | 343 | }; |
344 | 344 |
|
345 | 345 | AbstractSelect2.prototype.close = function () { |
346 | 346 | if (!this.opened()) return; |
347 | 347 |
|
348 | 348 | this.dropdown.hide(); |
349 | 349 | this.container.removeClass("select2-dropdown-open"); |
350 | | - $(document).off("click.id" + this.uid); |
351 | 350 |
|
352 | 351 | if (this.select) { |
353 | 352 | // TODO see if we can always clear here and reset on open |
|
439 | 438 | more = results.find("li.select2-more-results"), |
440 | 439 | below, // pixels the element is below the scroll fold, below==0 is when the element is starting to be visible |
441 | 440 | offset = -1, // index of first element without data |
442 | | - page = this.resultsPage+1; |
| 441 | + page = this.resultsPage + 1; |
443 | 442 |
|
444 | 443 | if (more.length === 0) return; |
445 | 444 |
|
|
835 | 834 | } |
836 | 835 | } |
837 | 836 |
|
838 | | - if (e.which === KEY.TAB) { |
839 | | - this.blur(); |
840 | | - return; |
841 | | - } |
842 | | - |
843 | 837 | if (e.which === KEY.TAB || KEY.isControl(e) || KEY.isFunctionKey(e) || e.which === KEY.BACKSPACE || e.which === KEY.ESC) { |
844 | 838 | return; |
845 | 839 | } |
|
0 commit comments