Skip to content

Commit

Permalink
Upgrade to release v3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
kartik-v committed Oct 8, 2014
1 parent edb703f commit ae5982e
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 53 deletions.
110 changes: 77 additions & 33 deletions lib/select2/select2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Copyright 2012 Igor Vaynberg
Version: 3.5.0 Timestamp: Mon Jun 16 19:29:44 EDT 2014
Version: @@ver@@ Timestamp: @@timestamp@@
This software is licensed under the Apache License, Version 2.0 (the "Apache License") or the GNU
General Public License version 2 (the "GPL License"). You may choose either license to govern your
Expand Down Expand Up @@ -443,6 +443,16 @@ the specific language governing permissions and limitations under the Apache Lic
// TODO - replace query.page with query so users have access to term, page, etc.
// added query as third paramter to keep backwards compatibility
var results = options.results(data, query.page, query);
query.callback(results);
},
error: function(jqXHR, textStatus, errorThrown){
var results = {
hasError: true,
jqXHR: jqXHR,
textStatus: textStatus,
errorThrown: errorThrown
};

query.callback(results);
}
});
Expand Down Expand Up @@ -689,12 +699,15 @@ the specific language governing permissions and limitations under the Apache Lic

this.container = this.createContainer();

this.liveRegion = $("<span>", {
role: "status",
"aria-live": "polite"
})
.addClass("select2-hidden-accessible")
.appendTo(document.body);
this.liveRegion = $('.select2-hidden-accessible');
if (this.liveRegion.length == 0) {
this.liveRegion = $("<span>", {
role: "status",
"aria-live": "polite"
})
.addClass("select2-hidden-accessible")
.appendTo(document.body);
}

this.containerId="s2id_"+(opts.element.attr("id") || "autogen"+nextUid());
this.containerEventName= this.containerId
Expand Down Expand Up @@ -836,13 +849,13 @@ the specific language governing permissions and limitations under the Apache Lic

// abstract
destroy: function () {
var element=this.opts.element, select2 = element.data("select2");
var element=this.opts.element, select2 = element.data("select2"), self = this;

this.close();

if (element.length && element[0].detachEvent) {
if (element.length && element[0].detachEvent && self._sync) {
element.each(function () {
this.detachEvent("onpropertychange", this._sync);
this.detachEvent("onpropertychange", self._sync);
});
}
if (this.propertyObserver) {
Expand Down Expand Up @@ -1027,7 +1040,7 @@ the specific language governing permissions and limitations under the Apache Lic

query.callback(data);
});
// this is needed because inside val() we construct choices from options and there id is hardcoded
// this is needed because inside val() we construct choices from options and their id is hardcoded
opts.id=function(e) { return e.id; };
} else {
if (!("query" in opts)) {
Expand Down Expand Up @@ -1212,9 +1225,10 @@ the specific language governing permissions and limitations under the Apache Lic
// abstract
positionDropdown: function() {
var $dropdown = this.dropdown,
offset = this.container.offset(),
height = this.container.outerHeight(false),
width = this.container.outerWidth(false),
container = this.container,
offset = container.offset(),
height = container.outerHeight(false),
width = container.outerWidth(false),
dropHeight = $dropdown.outerHeight(false),
$window = $(window),
windowWidth = $window.width(),
Expand All @@ -1226,7 +1240,12 @@ the specific language governing permissions and limitations under the Apache Lic
enoughRoomBelow = dropTop + dropHeight <= viewportBottom,
enoughRoomAbove = (offset.top - dropHeight) >= $window.scrollTop(),
dropWidth = $dropdown.outerWidth(false),
enoughRoomOnRight = dropLeft + dropWidth <= viewPortRight,
enoughRoomOnRight = function() {
return dropLeft + dropWidth <= viewPortRight;
},
enoughRoomOnLeft = function() {
return offset.left + viewPortRight + container.outerWidth(false) > dropWidth;
},
aboveNow = $dropdown.hasClass("select2-drop-above"),
bodyOffset,
above,
Expand Down Expand Up @@ -1261,7 +1280,6 @@ the specific language governing permissions and limitations under the Apache Lic
dropTop = offset.top + height;
dropLeft = offset.left;
dropWidth = $dropdown.outerWidth(false);
enoughRoomOnRight = dropLeft + dropWidth <= viewPortRight;
$dropdown.show();

// fix so the cursor does not move to the left within the search-textbox in IE
Expand All @@ -1276,7 +1294,6 @@ the specific language governing permissions and limitations under the Apache Lic
dropWidth = $dropdown.outerWidth(false) + (resultsListNode.scrollHeight === resultsListNode.clientHeight ? 0 : scrollBarDimensions.width);
dropWidth > width ? width = dropWidth : dropWidth = width;
dropHeight = $dropdown.outerHeight(false);
enoughRoomOnRight = dropLeft + dropWidth <= viewPortRight;
}
else {
this.container.removeClass('select2-drop-auto-width');
Expand All @@ -1292,7 +1309,7 @@ the specific language governing permissions and limitations under the Apache Lic
dropLeft -= bodyOffset.left;
}

if (!enoughRoomOnRight) {
if (!enoughRoomOnRight() && enoughRoomOnLeft()) {
dropLeft = offset.left + this.container.outerWidth(false) - dropWidth;
}

Expand Down Expand Up @@ -1515,7 +1532,7 @@ the specific language governing permissions and limitations under the Apache Lic
}
}

rb = results.offset().top + results.outerHeight(true);
rb = results.offset().top + results.outerHeight(false);
if (hb > rb) {
results.scrollTop(results.scrollTop() + (hb - rb));
}
Expand Down Expand Up @@ -1638,7 +1655,7 @@ the specific language governing permissions and limitations under the Apache Lic
self.postprocessResults(data, false, false);

if (data.more===true) {
more.detach().appendTo(results).text(evaluate(self.opts.formatLoadMore, self.opts.element, page+1));
more.detach().appendTo(results).html(self.opts.escapeMarkup(evaluate(self.opts.formatLoadMore, self.opts.element, page+1)));
window.setTimeout(function() { self.loadMoreIfNeeded(); }, 10);
} else {
more.remove();
Expand Down Expand Up @@ -1766,6 +1783,12 @@ the specific language governing permissions and limitations under the Apache Lic
return;
}

// handle ajax error
if(data.hasError !== undefined && checkFormatter(opts.formatAjaxError, "formatAjaxError")) {
render("<li class='select2-ajax-error'>" + evaluate(opts.formatAjaxError, opts.element, data.jqXHR, data.textStatus, data.errorThrown) + "</li>");
return;
}

// save context, if any
this.context = (data.context===undefined) ? null : data.context;
// create a default choice and prepend it to the list
Expand Down Expand Up @@ -2101,6 +2124,9 @@ the specific language governing permissions and limitations under the Apache Lic
this.search.on("keydown", this.bind(function (e) {
if (!this.isInterfaceEnabled()) return;

// filter 229 keyCodes (input method editor is processing key input)
if (229 == e.keyCode) return;

if (e.which === KEY.PAGE_UP || e.which === KEY.PAGE_DOWN) {
// prevent the page from scrolling
killEvent(e);
Expand Down Expand Up @@ -2655,8 +2681,7 @@ the specific language governing permissions and limitations under the Apache Lic
this.selection = selection = this.container.find(selector);

var _this = this;
this.selection.on("click", ".select2-search-choice:not(.select2-locked)", function (e) {
//killEvent(e);
this.selection.on("click", ".select2-container:not(.select2-container-disabled) .select2-search-choice:not(.select2-locked)", function (e) {
_this.search[0].focus();
_this.selectChoice($(this));
});
Expand Down Expand Up @@ -2953,7 +2978,7 @@ the specific language governing permissions and limitations under the Apache Lic
// multi
onSelect: function (data, options) {

if (!this.triggerSelect(data)) { return; }
if (!this.triggerSelect(data) || data.text === "") { return; }

this.addSelectedChoice(data);

Expand Down Expand Up @@ -3212,7 +3237,7 @@ the specific language governing permissions and limitations under the Apache Lic
if (equal(this.opts.id(current[i]), this.opts.id(old[j]))) {
current.splice(i, 1);
if(i>0){
i--;
i--;
}
old.splice(j, 1);
j--;
Expand Down Expand Up @@ -3395,29 +3420,33 @@ the specific language governing permissions and limitations under the Apache Lic
dropdownCssClass: "",
formatResult: function(result, container, query, escapeMarkup) {
var markup=[];
markMatch(result.text, query.term, markup, escapeMarkup);
markMatch(this.text(result), query.term, markup, escapeMarkup);
return markup.join("");
},
formatSelection: function (data, container, escapeMarkup) {
return data ? escapeMarkup(data.text) : undefined;
return data ? escapeMarkup(this.text(data)) : undefined;
},
sortResults: function (results, container, query) {
return results;
},
formatResultCssClass: function(data) {return data.css;},
formatSelectionCssClass: function(data, container) {return undefined;},
formatMatches: function (matches) { if (matches === 1) { return "One result is available, press enter to select it."; } return matches + " results are available, use up and down arrow keys to navigate."; },
formatNoMatches: function () { return "No matches found"; },
formatInputTooShort: function (input, min) { var n = min - input.length; return "Please enter " + n + " or more character" + (n == 1? "" : "s"); },
formatInputTooLong: function (input, max) { var n = input.length - max; return "Please delete " + n + " character" + (n == 1? "" : "s"); },
formatSelectionTooBig: function (limit) { return "You can only select " + limit + " item" + (limit == 1 ? "" : "s"); },
formatLoadMore: function (pageNumber) { return "Loading more results…"; },
formatSearching: function () { return "Searching…"; },
minimumResultsForSearch: 0,
minimumInputLength: 0,
maximumInputLength: null,
maximumSelectionSize: 0,
id: function (e) { return e == undefined ? null : e.id; },
text: function (e) {
if (e && this.data && this.data.text) {
if ($.isFunction(this.data.text)) {
return this.data.text(e);
} else {
return e[this.data.text];
}
} else {
return e.text;
}
},
matcher: function(term, text) {
return stripDiacritics(''+text).toUpperCase().indexOf(stripDiacritics(''+term).toUpperCase()) >= 0;
},
Expand Down Expand Up @@ -3451,6 +3480,21 @@ the specific language governing permissions and limitations under the Apache Lic
}
};

$.fn.select2.locales = [];

$.fn.select2.locales['en'] = {
formatMatches: function (matches) { if (matches === 1) { return "One result is available, press enter to select it."; } return matches + " results are available, use up and down arrow keys to navigate."; },
formatNoMatches: function () { return "No matches found"; },
formatAjaxError: function (jqXHR, textStatus, errorThrown) { return "Loading failed"; },
formatInputTooShort: function (input, min) { var n = min - input.length; return "Please enter " + n + " or more character" + (n == 1 ? "" : "s"); },
formatInputTooLong: function (input, max) { var n = input.length - max; return "Please delete " + n + " character" + (n == 1 ? "" : "s"); },
formatSelectionTooBig: function (limit) { return "You can only select " + limit + " item" + (limit == 1 ? "" : "s"); },
formatLoadMore: function (pageNumber) { return "Loading more results…"; },
formatSearching: function () { return "Searching…"; }
};

$.extend($.fn.select2.defaults, $.fn.select2.locales['en']);

$.fn.select2.ajaxDefaults = {
transport: $.ajax,
params: {
Expand Down
Loading

0 comments on commit ae5982e

Please sign in to comment.