|
12 | 12 | distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | 13 | See the License for the specific language governing permissions and limitations under the License. |
14 | 14 | */ |
| 15 | + (function ($) { |
| 16 | + if(typeof $.fn.each2 == "undefined"){ |
| 17 | + $.fn.extend({ |
| 18 | + /* |
| 19 | + * 4-10 times faster .each replacement |
| 20 | + * use it carefully, as it overrides jQuery context of element on each iteration |
| 21 | + */ |
| 22 | + each2 : function (c) { |
| 23 | + var j = $([0]), i = -1, l = this.length; |
| 24 | + while ( |
| 25 | + ++i < l |
| 26 | + && (j.context = j[0] = this[i]) |
| 27 | + && c.call(j[0], i, j) !== false //"this"=DOM, i=index, j=jQuery object |
| 28 | + ); |
| 29 | + return this; |
| 30 | + } |
| 31 | + }); |
| 32 | + } |
| 33 | +})(jQuery); |
| 34 | + |
15 | 35 | (function ($, undefined) { |
16 | 36 | "use strict"; |
17 | 37 | /*global document, window, jQuery, console */ |
|
182 | 202 | } |
183 | 203 |
|
184 | 204 | function measureTextWidth(e) { |
185 | | - var sizer, width; |
| 205 | + var sizer, width, |
| 206 | + style = e.currentStyle || window.getComputedStyle(e, null); |
186 | 207 | sizer = $("<div></div>").css({ |
187 | 208 | position: "absolute", |
188 | 209 | left: "-1000px", |
189 | 210 | top: "-1000px", |
190 | 211 | display: "none", |
191 | | - fontSize: e.css("fontSize"), |
192 | | - fontFamily: e.css("fontFamily"), |
193 | | - fontStyle: e.css("fontStyle"), |
194 | | - fontWeight: e.css("fontWeight"), |
195 | | - letterSpacing: e.css("letterSpacing"), |
196 | | - textTransform: e.css("textTransform"), |
| 212 | + fontSize: style.fontSize, |
| 213 | + fontFamily: style.fontFamily, |
| 214 | + fontStyle: style.fontStyle, |
| 215 | + fontWeight: style.fontWeight, |
| 216 | + letterSpacing: style.letterSpacing, |
| 217 | + textTransform: style.textTransform, |
197 | 218 | whiteSpace: "nowrap" |
198 | 219 | }); |
199 | 220 | sizer.text(e.val()); |
|
534 | 555 | return result.text; |
535 | 556 | }, |
536 | 557 | formatSelection: function (data) { |
537 | | - if (data.fullText) { |
538 | | - return data.fullText; |
539 | | - } else { |
540 | | - return data.text; |
541 | | - } |
| 558 | + return data.fullText || data.text; |
542 | 559 | }, |
543 | 560 | formatNoMatches: function () { return "No matches found"; }, |
544 | 561 | formatInputTooShort: function (input, min) { return "Please enter " + (min - input.length) + " more characters"; }, |
|
569 | 586 | } |
570 | 587 | } else if (element.is("optgroup")) { |
571 | 588 | group={text:element.attr("label"), children:[]}; |
572 | | - element.children().each(function() { process($(this), group.children); }); |
| 589 | + element.children().each2(function(i, elm) { process(elm, group.children); }); |
573 | 590 | if (group.children.length>0) { |
574 | 591 | collection.push(group); |
575 | 592 | } |
576 | 593 | } |
577 | 594 | }; |
578 | 595 |
|
579 | | - element.children().each(function() { process($(this), data.results); }); |
| 596 | + element.children().each2(function(i, elm) { process(elm, data.results); }); |
580 | 597 |
|
581 | 598 | query.callback(data); |
582 | 599 | }); |
|
694 | 711 |
|
695 | 712 | ensureHighlightVisible: function () { |
696 | 713 | var results = this.results, children, index, child, hb, rb, y, more; |
697 | | - |
698 | | - children = results.find(".select2-result"); |
| 714 | + |
699 | 715 | index = this.highlight(); |
700 | 716 |
|
701 | 717 | if (index < 0) return; |
| 718 | + |
| 719 | + children = results.find(".select2-result"); |
702 | 720 |
|
703 | 721 | child = $(children[index]); |
704 | 722 |
|
|
760 | 778 | }, |
761 | 779 |
|
762 | 780 | highlightUnderEvent: function (event) { |
763 | | - var el = $(event.target).closest(".select2-result"); |
764 | | - var choices = this.results.find('.select2-result'); |
| 781 | + var el = $(event.target).closest(".select2-result"); |
765 | 782 | if (el.length > 0) { |
| 783 | + var choices = this.results.find('.select2-result'); |
766 | 784 | this.highlight(choices.index(el)); |
767 | 785 | } |
768 | 786 | }, |
|
786 | 804 | context: this.context, |
787 | 805 | matcher: this.opts.matcher, |
788 | 806 | callback: this.bind(function (data) { |
789 | | - console.log("load more callback", data); |
790 | 807 |
|
791 | 808 | self.opts.populateResults(results, data.results); |
792 | 809 |
|
|
1113 | 1130 |
|
1114 | 1131 | // find the selected element in the result list |
1115 | 1132 |
|
1116 | | - this.results.find(".select2-result").each(function (i) { |
1117 | | - if (equal(self.id($(this).data("select2-data")), self.opts.element.val())) { |
| 1133 | + this.results.find(".select2-result").each2(function (i, elm) { |
| 1134 | + if (equal(self.id(elm.data("select2-data")), self.opts.element.val())) { |
1118 | 1135 | selected = i; |
1119 | 1136 | return false; |
1120 | 1137 | } |
|
1172 | 1189 | // val is an id |
1173 | 1190 | this.select |
1174 | 1191 | .val(val) |
1175 | | - .find(":selected").each(function () { |
1176 | | - data = {id: $(this).attr("value"), text: $(this).text()}; |
| 1192 | + .find(":selected").each2(function (i, elm) { |
| 1193 | + data = {id: elm.attr("value"), text: elm.text()}; |
1177 | 1194 | return false; |
1178 | 1195 | }); |
1179 | 1196 | this.updateSelection(data); |
|
1223 | 1240 | // install sthe selection initializer |
1224 | 1241 | opts.initSelection = function (element) { |
1225 | 1242 | var data = []; |
1226 | | - element.find(":selected").each(function () { |
1227 | | - data.push({id: $(this).attr("value"), text: $(this).text()}); |
| 1243 | + element.find(":selected").each2(function (i, elm) { |
| 1244 | + data.push({id: elm.attr("value"), text: elm.text()}); |
1228 | 1245 | }); |
1229 | 1246 | return data; |
1230 | 1247 | }; |
|
1492 | 1509 | choices = this.results.find(".select2-result"), |
1493 | 1510 | self = this; |
1494 | 1511 |
|
1495 | | - choices.each(function () { |
1496 | | - var choice = $(this), id = self.id(choice.data("select2-data")); |
| 1512 | + choices.each2(function (i, choice) { |
| 1513 | + var id = self.id(choice.data("select2-data")); |
1497 | 1514 | if (indexOf(id, val) >= 0) { |
1498 | 1515 | choice.addClass("select2-disabled"); |
1499 | 1516 | } else { |
1500 | 1517 | choice.removeClass("select2-disabled"); |
1501 | 1518 | } |
1502 | 1519 | }); |
1503 | 1520 |
|
1504 | | - choices.each(function (i) { |
1505 | | - if (!$(this).hasClass("select2-disabled")) { |
| 1521 | + choices.each2(function (i, choice) { |
| 1522 | + if (!choice.hasClass("select2-disabled")) { |
1506 | 1523 | self.highlight(i); |
1507 | 1524 | return false; |
1508 | 1525 | } |
|
0 commit comments