1
0
mirror of synced 2025-02-10 17:19:23 +03:00

change event, closes #11

This commit is contained in:
Igor Vaynberg 2012-03-30 14:06:34 -07:00
parent 6bef0b5bf6
commit 26982e1952

View File

@ -83,6 +83,20 @@
return -1; return -1;
} }
/**
* Compares equality of a and b taking into account that a and b may be strings, in which case localCompare is used
* @param a
* @param b
*/
function equal(a, b) {
if (a === b) return true;
if (a === undefined || b === undefined) return false;
if (a === null || b === null) return false;
if (a.constructor === String) return a.localeCompare(b) === 0;
if (b.constructor === String) return b.localeCompare(a) === 0;
return false;
}
function getSideBorderPadding(element) { function getSideBorderPadding(element) {
return element.outerWidth() - element.width(); return element.outerWidth() - element.width();
} }
@ -347,7 +361,7 @@
query.callback({results: data}); query.callback({results: data});
return; return;
} }
filtered.result = $(data) filtered.results = $(data)
.filter(function () {return text(this).toUpperCase().indexOf(t) >= 0;}) .filter(function () {return text(this).toUpperCase().indexOf(t) >= 0;})
.get(); .get();
query.callback(filtered); query.callback(filtered);
@ -363,6 +377,13 @@
return opts; return opts;
}; };
/**
* Triggers the change event on the source element
*/
AbstractSelect2.prototype.triggerChange = function () {
this.opts.element.trigger("change");
};
AbstractSelect2.prototype.opened = function () { AbstractSelect2.prototype.opened = function () {
return this.container.hasClass("select2-dropdown-open"); return this.container.hasClass("select2-dropdown-open");
}; };
@ -707,6 +728,7 @@
this.val(""); this.val("");
killEvent(e); killEvent(e);
this.close(); this.close();
this.triggerChange();
})); }));
if (this.select) { if (this.select) {
@ -761,10 +783,14 @@
}; };
SingleSelect2.prototype.onSelect = function (data) { SingleSelect2.prototype.onSelect = function (data) {
var old = this.opts.element.val();
this.opts.element.val(data.id); this.opts.element.val(data.id);
this.updateSelection(data); this.updateSelection(data);
this.close(); this.close();
this.selection.focus(); this.selection.focus();
if (!equal(old, data.id)) { this.triggerChange(); }
}; };
SingleSelect2.prototype.updateSelection = function (data) { SingleSelect2.prototype.updateSelection = function (data) {
@ -896,9 +922,7 @@
this.search.bind("keyup", this.bind(this.resizeSearch)); this.search.bind("keyup", this.bind(this.resizeSearch));
this.container.delegate(selector, "click", this.bind(function (e) { this.container.delegate(selector, "click", this.bind(function (e) {
if (this.select) { this.open();
this.open();
}
this.focusSearch(); this.focusSearch();
e.preventDefault(); e.preventDefault();
})); }));
@ -965,6 +989,11 @@
if (this.select) { this.postprocessResults(); } if (this.select) { this.postprocessResults(); }
this.close(); this.close();
this.search.width(10); this.search.width(10);
// since its not possible to select an element that has already been
// added we do not need to check if this is a new element before firing change
this.triggerChange();
this.focusSearch(); this.focusSearch();
}; };
@ -1022,6 +1051,7 @@
if (this.select) this.postprocessResults(); if (this.select) this.postprocessResults();
} }
selected.remove(); selected.remove();
this.triggerChange();
window.setTimeout(this.bind(this.alignDropdown), 20); window.setTimeout(this.bind(this.alignDropdown), 20);
}; };