1
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-01-17 21:49:09 +01:00

more efficient matcher + bold matched query in item

This commit is contained in:
Jacob Thornton 2012-01-21 22:06:36 -08:00
parent 4fe11342d0
commit 6e490628d1

View File

@ -36,7 +36,7 @@
, matcher: function (item, query) { , matcher: function (item, query) {
// ;_; http://jsperf.com/asdfdfasdfa // ;_; http://jsperf.com/asdfdfasdfa
return ~item.toLowerCase().indexOf(query.toLowerCase()) return ~item.toLowerCase().indexOf(query)
} }
, select: function () { , select: function () {
@ -67,16 +67,20 @@
} }
, lookup: function (event) { , lookup: function (event) {
var query = this.$element.val() var that = this
, that = this
, items , items
, q
if (!query) { this.query = this.$element.val()
if (!this.query) {
return this.shown ? this.hide() : this return this.shown ? this.hide() : this
} }
q = this.query.toLowerCase()
items = this.data.filter(function (item) { items = this.data.filter(function (item) {
if (that.matcher(item, query)) return item if (that.matcher(item, q)) return item
}) })
if (!items.length) { if (!items.length) {
@ -88,10 +92,15 @@
, render: function (items) { , render: function (items) {
var that = this var that = this
, QUERY = new RegExp('(' + this.query + ')', 'ig')
items = $(items).map(function (i, item) { items = $(items).map(function (i, item) {
i = $(that.options.item).attr('data-value', item) i = $(that.options.item).attr('data-value', item)
i.find('a').text(item)
i.find('a').html(item.replace(QUERY, function ($1, match) {
return '<strong>' + match + '</strong>'
}))
return i[0] return i[0]
}) })
@ -122,6 +131,21 @@
prev.addClass('active') prev.addClass('active')
} }
, listen: function () {
this.$element
.on('blur', $.proxy(this.blur, this))
.on('keypress', $.proxy(this.keypress, this))
.on('keyup', $.proxy(this.keyup, this))
if ($.browser.webkit || $.browser.msie) {
this.$element.on('keydown', $.proxy(this.keypress, this))
}
this.$menu
.on('click', $.proxy(this.click, this))
.on('mouseenter', 'li', $.proxy(this.mouseenter, this))
}
, keyup: function (e) { , keyup: function (e) {
e.stopPropagation() e.stopPropagation()
e.preventDefault() e.preventDefault()
@ -188,20 +212,6 @@
$(e.currentTarget).addClass('active') $(e.currentTarget).addClass('active')
} }
, listen: function () {
this.$element
.on('blur', $.proxy(this.blur, this))
.on('keypress', $.proxy(this.keypress, this))
.on('keyup', $.proxy(this.keyup, this))
if ($.browser.webkit || $.browser.msie) {
this.$element.on('keydown', $.proxy(this.keypress, this))
}
this.$menu
.on('click', $.proxy(this.click, this))
.on('mouseenter', 'li', $.proxy(this.mouseenter, this))
}
} }