1
0
mirror of synced 2024-11-30 00:26:03 +03:00

Merge pull request #1365 from corinnaerin/master

Feature: Allow placeholders with nonempty values
This commit is contained in:
Igor Vaynberg 2013-05-31 15:12:32 -07:00
commit 9a3666edb5

View File

@ -875,7 +875,7 @@ the specific language governing permissions and limitations under the Apache Lic
opts.query = this.bind(function (query) { opts.query = this.bind(function (query) {
var data = { results: [], more: false }, var data = { results: [], more: false },
term = query.term, term = query.term,
children, firstChild, process; children, placeholderOption, process;
process=function(element, collection) { process=function(element, collection) {
var group; var group;
@ -896,9 +896,9 @@ the specific language governing permissions and limitations under the Apache Lic
// ignore the placeholder option if there is one // ignore the placeholder option if there is one
if (this.getPlaceholder() !== undefined && children.length > 0) { if (this.getPlaceholder() !== undefined && children.length > 0) {
firstChild = children[0]; placeholderOption = this.getPlaceholderOption();
if ($(firstChild).text() === "") { if (placeholderOption) {
children=children.not(firstChild); children=children.not(placeholderOption);
} }
} }
@ -1629,10 +1629,27 @@ the specific language governing permissions and limitations under the Apache Lic
// abstract // abstract
getPlaceholder: function () { getPlaceholder: function () {
var placeholderOption;
return this.opts.element.attr("placeholder") || return this.opts.element.attr("placeholder") ||
this.opts.element.attr("data-placeholder") || // jquery 1.4 compat this.opts.element.attr("data-placeholder") || // jquery 1.4 compat
this.opts.element.data("placeholder") || this.opts.element.data("placeholder") ||
this.opts.placeholder; this.opts.placeholder ||
((placeholderOption = this.getPlaceholderOption()) !== undefined && placeholderOption.text());
},
// abstract
getPlaceholderOption: function() {
if (this.select) {
var firstOption = this.select.children().first();
if (this.opts.placeholderOption !== undefined ) {
//Determine the placeholder option based on the specified placeholderOption setting
return (this.opts.placeholderOption === "first" && firstOption) ||
(typeof this.opts.placeholderOption === "function" && this.opts.placeholderOption(this.select));
} else if (firstOption.text() === "" && firstOption.val() === "") {
//No explicit placeholder option specified, use the first if it's blank
return firstOption;
}
}
}, },
/** /**
@ -1941,7 +1958,8 @@ the specific language governing permissions and limitations under the Apache Lic
clear: function(triggerChange) { clear: function(triggerChange) {
var data=this.selection.data("select2-data"); var data=this.selection.data("select2-data");
if (data) { // guard against queued quick consecutive clicks if (data) { // guard against queued quick consecutive clicks
this.opts.element.val(""); var placeholderOption = this.getPlaceholderOption();
this.opts.element.val(placeholderOption ? placeholderOption.val() : "");
this.selection.find("span").empty(); this.selection.find("span").empty();
this.selection.removeData("select2-data"); this.selection.removeData("select2-data");
this.setPlaceholder(); this.setPlaceholder();
@ -1959,7 +1977,7 @@ the specific language governing permissions and limitations under the Apache Lic
// single // single
initSelection: function () { initSelection: function () {
var selected; var selected;
if (this.opts.element.val() === "" && this.opts.element.text() === "") { if (this.isPlaceholderOptionSelected()) {
this.updateSelection([]); this.updateSelection([]);
this.close(); this.close();
this.setPlaceholder(); this.setPlaceholder();
@ -1975,6 +1993,14 @@ the specific language governing permissions and limitations under the Apache Lic
} }
}, },
isPlaceholderOptionSelected: function() {
var placeholderOption;
return ((placeholderOption = this.getPlaceholderOption()) !== undefined && placeholderOption.is(':selected')) ||
(this.opts.element.val() === "") ||
(this.opts.element.val() === undefined) ||
(this.opts.element.val() === null);
},
// single // single
prepareOpts: function () { prepareOpts: function () {
var opts = this.parent.prepareOpts.apply(this, arguments), var opts = this.parent.prepareOpts.apply(this, arguments),
@ -2013,9 +2039,9 @@ the specific language governing permissions and limitations under the Apache Lic
// single // single
getPlaceholder: function() { getPlaceholder: function() {
// if a placeholder is specified on a single select without the first empty option ignore it // if a placeholder is specified on a single select without a valid placeholder option ignore it
if (this.select) { if (this.select) {
if (this.select.find("option").first().text() !== "") { if (this.getPlaceholderOption() === undefined) {
return undefined; return undefined;
} }
} }
@ -2027,10 +2053,10 @@ the specific language governing permissions and limitations under the Apache Lic
setPlaceholder: function () { setPlaceholder: function () {
var placeholder = this.getPlaceholder(); var placeholder = this.getPlaceholder();
if (this.opts.element.val() === "" && placeholder !== undefined) { if (this.isPlaceholderOptionSelected() && placeholder !== undefined) {
// check for a first blank option if attached to a select // check for a placeholder option if attached to a select
if (this.select && this.select.find("option:first").text() !== "") return; if (this.select && this.getPlaceholderOption() === undefined) return;
this.selection.find("span").html(this.opts.escapeMarkup(placeholder)); this.selection.find("span").html(this.opts.escapeMarkup(placeholder));