1
0
mirror of synced 2025-02-04 06:09:23 +03:00

jQuery 1.4.2 compatibility : - use attr() instead of prop() - use .bind() and .delegate() instead of .on() - pass ajax success handler as an argument, instead of chaining call to .success()

Signed-off-by: Igor Vaynberg <igor.vaynberg@gmail.com>
This commit is contained in:
Justin DuJardin 2012-03-29 15:58:30 -07:00 committed by Igor Vaynberg
parent eddc40934c
commit b162567c22

View File

@ -88,10 +88,10 @@
} }
function installKeyUpChangeEvent(element) { function installKeyUpChangeEvent(element) {
element.on("keydown", function () { element.bind("keydown", function () {
element.data("keyup-change-value", element.val()); element.data("keyup-change-value", element.val());
}); });
element.on("keyup", function () { element.bind("keyup", function () {
if (element.val() !== element.data("keyup-change-value")) { if (element.val() !== element.data("keyup-change-value")) {
element.trigger("keyup-change"); element.trigger("keyup-change");
} }
@ -104,12 +104,12 @@
* filters out mouse events that occur when mouse is stationary but * filters out mouse events that occur when mouse is stationary but
* the elements under the pointer are scrolled. * the elements under the pointer are scrolled.
*/ */
$(document).on("mousemove", function (e) { $(document).delegate("*","mousemove", function (e) {
$(this).data("select2-lastpos", {x: e.pageX, y: e.pageY}); $(this).data("select2-lastpos", {x: e.pageX, y: e.pageY});
}); });
function installFilteredMouseMove(element) { function installFilteredMouseMove(element) {
var doc = $(document); var doc = $(document);
element.on("mousemove", function (e) { element.bind("mousemove", function (e) {
var lastpos = doc.data("select2-lastpos"); var lastpos = doc.data("select2-lastpos");
if (lastpos === undefined || lastpos.x !== e.pageX || lastpos.y !== e.pageY) { if (lastpos === undefined || lastpos.x !== e.pageX || lastpos.y !== e.pageY) {
@ -128,7 +128,7 @@
function installDebouncedScroll(threshold, element) { function installDebouncedScroll(threshold, element) {
var notify = debounce(threshold, function (e) { element.trigger("scroll-debounced", e);}); var notify = debounce(threshold, function (e) { element.trigger("scroll-debounced", e);});
element.on("scroll", function (e) { element.bind("scroll", function (e) {
if (indexOf(e.target, element.get()) >= 0) notify(e); if (indexOf(e.target, element.get()) >= 0) notify(e);
}); });
} }
@ -164,7 +164,7 @@
* blurs any Select2 container that has focus when an element outside them was clicked or received focus * blurs any Select2 container that has focus when an element outside them was clicked or received focus
*/ */
$(document).ready(function () { $(document).ready(function () {
$(document).on("mousedown focusin", function (e) { $(document).delegate("*","mousedown focusin", function (e) {
var target = $(e.target).closest("div.select2-container").get(0); var target = $(e.target).closest("div.select2-container").get(0);
$(document).find("div.select2-container-active").each(function () { $(document).find("div.select2-container-active").each(function () {
if (this !== target) $(this).data("select2").blur(); if (this !== target) $(this).data("select2").blur();
@ -187,7 +187,7 @@
}; };
AbstractSelect2.prototype.init = function (opts) { AbstractSelect2.prototype.init = function (opts) {
var results, search; var results, search, results_selector = ".select2-results";
// prepare options // prepare options
this.opts = this.prepareOpts(opts); this.opts = this.prepareOpts(opts);
@ -199,26 +199,25 @@
} }
// swap container for the element // swap container for the element
this.opts.element.data("select2", this) this.opts.element.data("select2", this)
.hide() .hide()
.after(this.container); .after(this.container);
this.container.data("select2", this); this.container.data("select2", this);
this.dropdown = this.container.find(".select2-drop"); this.dropdown = this.container.find(".select2-drop");
this.results = results = this.container.find(".select2-results"); this.results = results = this.container.find(results_selector);
this.search = search = this.container.find("input[type=text]"); this.search = search = this.container.find("input[type=text]");
// initialize the container
this.resultsPage = 0; this.resultsPage = 0;
// initialize the container
this.initContainer(); this.initContainer();
installFilteredMouseMove(this.results); installFilteredMouseMove(this.results);
results.on("mousemove-filtered", this.bind(this.highlightUnderEvent)); this.container.delegate(results_selector,"mousemove-filtered", this.bind(this.highlightUnderEvent));
installDebouncedScroll(80, this.results); installDebouncedScroll(80, this.results);
results.on("scroll-debounced", this.bind(this.loadMoreIfNeeded)); this.container.delegate(results_selector,"scroll-debounced", this.bind(this.loadMoreIfNeeded));
// if jquery.mousewheel plugin is installed we can prevent out-of-bounds scrolling of results via mousewheel // if jquery.mousewheel plugin is installed we can prevent out-of-bounds scrolling of results via mousewheel
if ($.fn.mousewheel) { if ($.fn.mousewheel) {
@ -235,9 +234,9 @@
} }
installKeyUpChangeEvent(search); installKeyUpChangeEvent(search);
search.on("keyup-change", this.bind(this.updateResults)); search.bind("keyup-change", this.bind(this.updateResults));
results.on("click", this.bind(function (e) { this.container.delegate(results_selector,"click", this.bind(function (e) {
if ($(e.target).closest(".select2-result:not(.select2-disabled)").length > 0) { if ($(e.target).closest(".select2-result:not(.select2-disabled)").length > 0) {
this.highlightUnderEvent(e); this.highlightUnderEvent(e);
this.selectHighlighted(e); this.selectHighlighted(e);
@ -319,15 +318,14 @@
$.ajax({ $.ajax({
url: options.url, url: options.url,
dataType: options.dataType, dataType: options.dataType,
data: data data: data,
}).success( success: function (data) {
function (data) {
if (requestNumber < requestSequence) { if (requestNumber < requestSequence) {
return; return;
} }
query.callback(options.results(data, query.page)); query.callback(options.results(data, query.page));
} }
); });
}, quietMillis); }, quietMillis);
}; };
}()); }());
@ -649,11 +647,11 @@
SingleSelect2.prototype.initContainer = function () { SingleSelect2.prototype.initContainer = function () {
var selection, container = this.container, clickingInside = false, var selection, container = this.container, clickingInside = false,
selected; selector = ".select2-choice", selected;
this.selection = selection = container.find(".select2-choice"); this.selection = selection = container.find(selector);
this.search.on("keydown", this.bind(function (e) { this.search.bind("keydown", this.bind(function (e) {
switch (e.which) { switch (e.which) {
case KEY.UP: case KEY.UP:
case KEY.DOWN: case KEY.DOWN:
@ -672,7 +670,7 @@
} }
})); }));
selection.on("click", this.bind(function (e) { container.delegate(selector,"click", this.bind(function (e) {
clickingInside = true; clickingInside = true;
if (this.opened()) { if (this.opened()) {
@ -685,7 +683,7 @@
clickingInside = false; clickingInside = false;
})); }));
selection.on("keydown", this.bind(function (e) { container.delegate(selector,"keydown", this.bind(function (e) {
if (e.which === KEY.TAB || KEY.isControl(e) || KEY.isFunctionKey(e) || e.which === KEY.ESC) { if (e.which === KEY.TAB || KEY.isControl(e) || KEY.isFunctionKey(e) || e.which === KEY.ESC) {
return; return;
} }
@ -699,19 +697,17 @@
killEvent(e); killEvent(e);
} }
})); }));
selection.on("focus", function () { container.addClass("select2-container-active"); }); container.delegate(selector,"focus", function () { container.addClass("select2-container-active"); });
selection.on("blur", this.bind(function () { container.delegate(selector,"blur", this.bind(function () {
if (clickingInside) return; if (clickingInside) return;
if (!this.opened()) this.blur(); if (!this.opened()) this.blur();
})); }));
selection.find("abbr") selection.delegate("abbr","click", this.bind(function (e) {
.on("click", this.bind(function (e) {
this.val(""); this.val("");
killEvent(e); killEvent(e);
this.close(); this.close();
} }));
));
if (this.select) { if (this.select) {
selected = this.select.find(":selected"); selected = this.select.find(":selected");
@ -839,12 +835,12 @@
MultiSelect2.prototype.initContainer = function () { MultiSelect2.prototype.initContainer = function () {
var selection, data; var selector = ".select2-choices", selection, data;
this.searchContainer = this.container.find(".select2-search-field"); this.searchContainer = this.container.find(".select2-search-field");
this.selection = selection = this.container.find(".select2-choices"); this.selection = selection = this.container.find(selector);
this.search.on("keydown", this.bind(function (e) { this.search.bind("keydown", this.bind(function (e) {
if (e.which === KEY.BACKSPACE && this.search.val() === "") { if (e.which === KEY.BACKSPACE && this.search.val() === "") {
this.close(); this.close();
@ -895,9 +891,9 @@
} }
})); }));
this.search.on("keyup", this.bind(this.resizeSearch)); this.search.bind("keyup", this.bind(this.resizeSearch));
this.selection.on("click", this.bind(function (e) { this.container.delegate(selector,"click", this.bind(function (e) {
if (this.select) { if (this.select) {
this.open(); this.open();
} }
@ -905,7 +901,7 @@
e.preventDefault(); e.preventDefault();
})); }));
this.search.on("focus", this.bind(function () { this.container.delegate(selector,"focus", this.bind(function () {
this.container.addClass("select2-container-active"); this.container.addClass("select2-container-active");
this.clearPlaceholder(); this.clearPlaceholder();
})); }));
@ -989,13 +985,13 @@
choice = $(parts.join("")); choice = $(parts.join(""));
choice.find("a") choice.find("a")
.on("click dblclick", this.bind(function (e) { .bind("click dblclick", this.bind(function (e) {
this.unselect($(e.target)); this.unselect($(e.target));
this.selection.find(".select2-search-choice-focus").removeClass("select2-search-choice-focus"); this.selection.find(".select2-search-choice-focus").removeClass("select2-search-choice-focus");
killEvent(e); killEvent(e);
this.close(); this.close();
this.focusSearch(); this.focusSearch();
})).on("focus", this.bind(function () { })).bind("focus", this.bind(function () {
this.container.addClass("select2-container-active"); this.container.addClass("select2-container-active");
})); }));
@ -1132,7 +1128,7 @@
opts.element = $(this); opts.element = $(this);
if (opts.element.get(0).tagName.toLowerCase() === "select") { if (opts.element.get(0).tagName.toLowerCase() === "select") {
multiple = opts.element.prop("multiple"); multiple = opts.element.attr("multiple");
} else { } else {
multiple = opts.multiple || false; multiple = opts.multiple || false;
} }