story tags: sort by exact tag matches first, then description matches
select2 has sortResults in newer versions but so much crap changed there that it's easier to just hack in support for this here fixes #251
This commit is contained in:
parent
46ebed367d
commit
de176d80dd
|
@ -383,7 +383,34 @@ $(document).ready(function() {
|
|||
return what.id;
|
||||
},
|
||||
matcher: function(term, text) {
|
||||
return text.toUpperCase().indexOf(term.toUpperCase()) == 0;
|
||||
return text.toUpperCase().indexOf(term.toUpperCase()) >= 0;
|
||||
},
|
||||
sortResults: function(results, container, query) {
|
||||
if (query.term) {
|
||||
var tmatches = [];
|
||||
var dmatches = [];
|
||||
|
||||
/* prefer tag matches first, then description matches */
|
||||
for (var x in results) {
|
||||
var r = results[x];
|
||||
|
||||
if (r.id.toUpperCase().indexOf(query.term.toUpperCase()) == 0)
|
||||
tmatches.push(r);
|
||||
else
|
||||
dmatches.push(r);
|
||||
}
|
||||
|
||||
tmatches = tmatches.sort(function(a, b) {
|
||||
return a.text.localeCompare(b.text);
|
||||
});
|
||||
dmatches = dmatches.sort(function(a, b) {
|
||||
return a.text.localeCompare(b.text);
|
||||
});
|
||||
|
||||
return tmatches.concat(dmatches);
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -496,6 +496,10 @@
|
|||
data.results.push({id: e.attr("value"), text: (e.attr("data-html") == undefined ? text : e.attr("data-html")) });
|
||||
}
|
||||
});
|
||||
|
||||
if (opts.sortResults)
|
||||
data.results = opts.sortResults(data.results, element, query);
|
||||
|
||||
query.callback(data);
|
||||
});
|
||||
// this is needed because inside val() we construct choices from options and there id is hardcoded
|
||||
|
|
|
@ -182,15 +182,6 @@
|
|||
Lobsters.fetchURLTitle($(this), $("#story_url"), $("#story_title"));
|
||||
return false;
|
||||
});
|
||||
|
||||
$("#story_tags_a").select2({
|
||||
formatSelection: function(what) {
|
||||
return what.id;
|
||||
},
|
||||
matcher: function(term, text, opt) {
|
||||
return (text.toLowerCase().indexOf(term.toLowerCase()) >= 0);
|
||||
},
|
||||
});
|
||||
});
|
||||
</script>
|
||||
<% end %>
|
||||
|
|
Loading…
Reference in a new issue