Sort streams and subjects in a better way.

Modify the Bootstrap default sorter to sort by:
1. Starts-with, in a case-sensitive way
2. Starts-with, in a case-insensitive way
3. Matches anywhere, case-sensitive
4. Matches anywhere, case-insensitive

(This fixes the Keegan-reported issue of "Testing!" taking precedence
over "test" when he types in "tes")

(imported from commit b2a0127956fe7a8bf1cbf30752a6ddc2c49b7198)
This commit is contained in:
Waseem Daher 2012-11-26 16:39:30 -05:00
parent 830f9df3f2
commit ab8496b9ba
2 changed files with 42 additions and 2 deletions

View File

@ -113,7 +113,8 @@ exports.initialize = function () {
return stream_list; return stream_list;
}, },
items: 3, items: 3,
highlighter: composebox_typeahead_highlighter highlighter: composebox_typeahead_highlighter,
sorter: typeahead_helper.sort_streams
}); });
$( "#subject" ).typeahead({ $( "#subject" ).typeahead({
@ -125,7 +126,8 @@ exports.initialize = function () {
return []; return [];
}, },
items: 3, items: 3,
highlighter: composebox_typeahead_highlighter highlighter: composebox_typeahead_highlighter,
sorter: typeahead_helper.sort_subjects
}); });
$( "#private_message_recipient" ).typeahead({ $( "#private_message_recipient" ).typeahead({

View File

@ -82,6 +82,44 @@ exports.update_your_recipients = function (recipients) {
}); });
}; };
exports.sorter = function (query, objs, get_item) {
// Based on Bootstrap typeahead's default sorter, but taking into
// account case sensitivity on "begins with"
var beginswithCaseSensitive = [];
var beginswithCaseInsensitive = [];
var caseSensitive = [];
var caseInsensitive = [];
var obj = objs.shift();
while (obj) {
var item = get_item(obj);
if (item.indexOf(query) === 0)
beginswithCaseSensitive.push(obj);
else if (item.toLowerCase().indexOf(query.toLowerCase()) !== -1)
beginswithCaseInsensitive.push(obj);
else if (item.indexOf(query) !== -1)
caseSensitive.push(obj);
else
caseInsensitive.push(obj);
obj = objs.shift();
}
return beginswithCaseSensitive.concat(beginswithCaseInsensitive,
caseSensitive,
caseInsensitive);
};
function identity(item) {
return item;
}
exports.sort_streams = function (items) {
return typeahead_helper.sorter(this.query, items, identity);
};
exports.sort_subjects = function (items) {
return typeahead_helper.sorter(this.query, items, identity);
};
return exports; return exports;
}()); }());