diff options
author | Jacob Thornton <jacobthornton@gmail.com> | 2012-01-29 01:16:05 +0400 |
---|---|---|
committer | Jacob Thornton <jacobthornton@gmail.com> | 2012-01-29 01:16:05 +0400 |
commit | 6935f693b3bce10f9bfe7b7e1f3c2705ff2b6ca2 (patch) | |
tree | 29cb6f5e9fed5aaf1f65cdfa94a77faa2a5b44df /js | |
parent | f768242759903151c9957a493c5349f931d882ef (diff) |
typahead does case insensitive matching with simple sorter + accepts highlighter option
Diffstat (limited to 'js')
-rw-r--r-- | js/bootstrap-typeahead.js | 43 |
1 files changed, 29 insertions, 14 deletions
diff --git a/js/bootstrap-typeahead.js b/js/bootstrap-typeahead.js index 39331816e8..1426185afc 100644 --- a/js/bootstrap-typeahead.js +++ b/js/bootstrap-typeahead.js @@ -24,8 +24,9 @@ var Typeahead = function ( element, options ) { this.$element = $(element) this.options = $.extend({}, $.fn.typeahead.defaults, options) - this.matcher = this.options.matcher - this.sorter = this.options.sorter + this.matcher = this.options.matcher || this.matcher + this.sorter = this.options.sorter || this.sorter + this.highlighter = this.options.highlighter || this.highlighter this.$menu = $(this.options.menu).appendTo('body') this.source = this.options.source this.shown = false @@ -87,17 +88,37 @@ return this.render(items.slice(0, this.options.items)).show() } + , matcher: function (item) { + return ~item.toLowerCase().indexOf(this.query.toLowerCase()) + } + + , sorter: function (items) { + var beginswith = [] + , caseSensitive = [] + , caseInsensitive = [] + , item + + while (item = items.shift()) { + if (!item.toLowerCase().indexOf(this.query.toLowerCase())) beginswith.push(item) + else if (~item.indexOf(this.query)) caseSensitive.push(item) + else caseInsensitive.push(item) + } + + return beginswith.concat(caseSensitive, caseInsensitive) + } + + , highlighter: function (item) { + return item.replace(new RegExp('(' + this.query + ')', 'ig'), function ($1, match) { + return '<strong>' + match + '</strong>' + }) + } + , render: function (items) { var that = this - , QUERY = new RegExp('(' + this.query + ')', 'ig') items = $(items).map(function (i, item) { i = $(that.options.item).attr('data-value', item) - - i.find('a').html(item.replace(QUERY, function ($1, match) { - return '<strong>' + match + '</strong>' - })) - + i.find('a').html(that.highlighter(item)) return i[0] }) @@ -230,12 +251,6 @@ , items: 8 , menu: '<ul class="typeahead dropdown-menu"></ul>' , item: '<li><a href="#"></a></li>' - , matcher: function (item) { - return ~item.indexOf(this.query) - } - , sorter: function (items) { - return items - } } $.fn.typeahead.Constructor = Typeahead |