1
0
mirror of synced 2024-11-26 14:56:07 +03:00

more flexible format helpers. fixes #217

This commit is contained in:
Igor Vaynberg 2012-07-16 00:22:44 +03:00
parent 1605a631e5
commit d6f506e16b

View File

@ -551,58 +551,53 @@
populateResults: function(container, results, query) { populateResults: function(container, results, query) {
var uidToData={}, populate, markup=[], uid, data, result, children, formatted, id=this.opts.id; var uidToData={}, populate, markup=[], uid, data, result, children, formatted, id=this.opts.id;
populate=function(results, depth) { populate=function(results, container, depth) {
var i, l, uid, result, selectable, compound; var i, l, uid, result, selectable, compound, node, label, innerContainer;
for (i = 0, l = results.length; i < l; i = i + 1) { for (i = 0, l = results.length; i < l; i = i + 1) {
result=results[i]; result=results[i];
selectable=id(result) !== undefined; selectable=id(result) !== undefined;
compound=("children" in result) && result.children.length > 0; compound=("children" in result) && result.children.length > 0;
markup.push("<li class='select2-result-depth-" + depth); node=$("<li></li>");
markup.push(" select2-result"); node.addClass("select2-results-dept-"+depth);
markup.push(selectable ? " select2-result-selectable" : " select2-result-unselectable"); node.addClass("select2-result");
if (compound) { markup.push(" select2-result-with-children"); } node.addClass(selectable ? "select2-result-selectable" : "select2-result-unselectable");
if (compound) { node.addClass("select2-result-with-children"); }
markup.push("'"); label=$("<div></div>");
label.addClass("select2-result-label");
uid=nextUid(); var formatted=opts.formatResult(result, label, query);
markup.push(" id='select2-result-"+uid+"'");
uidToData[uid]=result;
markup.push("><div class='select2-result-label'>");
formatted=opts.formatResult(result, query, markup);
// for backwards compat with <3.0 versions
if (formatted!==undefined) { if (formatted!==undefined) {
markup.push(formatted); label.html(formatted);
} }
markup.push("</div>");
node.append(label);
if (compound) { if (compound) {
markup.push("<ul class='select2-result-sub'>");
populate(result.children, depth + 1); innerContainer=$("<ul></ul>");
markup.push("</ul>"); innerContainer.addClass("select2-result-sub");
populate(result.children, innerContainer, depth+1);
node.append(innerContainer);
} }
markup.push("</li>"); node.data("select2-data", result);
} container.append(node);
}
}; };
populate(results, 0); populate(results, container, 0);
container.append(markup.join(""));
for (uid in uidToData) {
$("#select2-result-"+uid, container).data("select2-data", uidToData[uid]);
}
}, },
formatResult: function(result, query, markup) { formatResult: function(result, container, query) {
var markup=[];
markMatch(result.text, query.term, markup); markMatch(result.text, query.term, markup);
return markup.join("");
}, },
formatSelection: function (data) { formatSelection: function (data, container) {
return data.fullText || data.text; return data.text;
}, },
formatNoMatches: function () { return "No matches found"; }, formatNoMatches: function () { return "No matches found"; },
formatInputTooShort: function (input, min) { return "Please enter " + (min - input.length) + " more characters"; }, formatInputTooShort: function (input, min) { return "Please enter " + (min - input.length) + " more characters"; },
@ -1380,11 +1375,15 @@
// single // single
updateSelection: function (data) { updateSelection: function (data) {
var container=this.selection.find("span"), formatted;
this.selection.data("select2-data", data); this.selection.data("select2-data", data);
this.selection container.empty();
.find("span") formatted=this.opts.formatSelection(data, container);
.empty().append(this.opts.formatSelection(data)); if (formatted !== undefined) {
container.append(formatted);
}
this.selection.removeClass("select2-default"); this.selection.removeClass("select2-default");
@ -1710,19 +1709,20 @@
// multi // multi
addSelectedChoice: function (data) { addSelectedChoice: function (data) {
var choice, var choice=$("<li class='select2-search-choice'></li>"),
id = this.id(data), id = this.id(data),
val = this.getVal(),
parts = ["<li class='select2-search-choice'>", formatted;
"<span class='select2-tmp'></span>",
"<a href='javascript:void(0)' class='select2-search-choice-close' tabindex='-1'></a>",
"</li>"
],
val = this.getVal();
choice = $(parts.join(""));
choice.find('.select2-tmp').replaceWith(this.opts.formatSelection(data)); choice.find('.select2-tmp').replaceWith(this.opts.formatSelection(data));
formatted=this.opts.formatSelection(data, choice);
if (formatted !== undefined) {
choice.append(formatted);
}
choice.append("<a href='javascript:void(0)' class='select2-search-choice-close' tabindex='-1'></a>");
choice.find(".select2-search-choice-close") choice.find(".select2-search-choice-close")
.bind("click dblclick", this.bind(function (e) { .bind("click dblclick", this.bind(function (e) {
if (!this.enabled) return; if (!this.enabled) return;