aboutsummaryrefslogtreecommitdiff
path: root/js/bootstrap-typeahead.js
diff options
context:
space:
mode:
authorJacob Thornton <[email protected]>2012-08-20 16:13:46 -0700
committerJacob Thornton <[email protected]>2012-08-20 16:13:46 -0700
commit5d63e8e2a84468351b4c1ee6cc21f48e95ea27bf (patch)
tree2d760c794e61c15850d14d5eee1c565ab4f9a871 /js/bootstrap-typeahead.js
parent5df1e2c00a295c45db7fa75dba333dc9464b128c (diff)
parentaa2cec6f2f706f8c1754f8bf79a8c9f094445bd8 (diff)
downloadbootstrap-5d63e8e2a84468351b4c1ee6cc21f48e95ea27bf.tar.xz
bootstrap-5d63e8e2a84468351b4c1ee6cc21f48e95ea27bf.zip
fMerge branch '2.1.0-wip'
Conflicts: docs/index.html
Diffstat (limited to 'js/bootstrap-typeahead.js')
-rw-r--r--js/bootstrap-typeahead.js85
1 files changed, 50 insertions, 35 deletions
diff --git a/js/bootstrap-typeahead.js b/js/bootstrap-typeahead.js
index ad44b9085..ae57221c1 100644
--- a/js/bootstrap-typeahead.js
+++ b/js/bootstrap-typeahead.js
@@ -1,5 +1,5 @@
/* =============================================================
- * bootstrap-typeahead.js v2.0.4
+ * bootstrap-typeahead.js v2.1.0
* http://twitter.github.com/bootstrap/javascript.html#typeahead
* =============================================================
* Copyright 2012 Twitter, Inc.
@@ -77,17 +77,23 @@
}
, lookup: function (event) {
- var that = this
- , items
- , q
+ var items
this.query = this.$element.val()
- if (!this.query) {
+ if (!this.query || this.query.length < this.options.minLength) {
return this.shown ? this.hide() : this
}
- items = $.grep(this.source, function (item) {
+ items = $.isFunction(this.source) ? this.source(this.query, $.proxy(this.process, this)) : this.source
+
+ return items ? this.process(items) : this
+ }
+
+ , process: function (items) {
+ var that = this
+
+ items = $.grep(items, function (item) {
return that.matcher(item)
})
@@ -169,7 +175,7 @@
.on('keyup', $.proxy(this.keyup, this))
if ($.browser.webkit || $.browser.msie) {
- this.$element.on('keydown', $.proxy(this.keypress, this))
+ this.$element.on('keydown', $.proxy(this.keydown, this))
}
this.$menu
@@ -177,6 +183,40 @@
.on('mouseenter', 'li', $.proxy(this.mouseenter, this))
}
+ , move: function (e) {
+ if (!this.shown) return
+
+ switch(e.keyCode) {
+ case 9: // tab
+ case 13: // enter
+ case 27: // escape
+ e.preventDefault()
+ break
+
+ case 38: // up arrow
+ e.preventDefault()
+ this.prev()
+ break
+
+ case 40: // down arrow
+ e.preventDefault()
+ this.next()
+ break
+ }
+
+ e.stopPropagation()
+ }
+
+ , keydown: function (e) {
+ this.suppressKeyPressRepeat = !~$.inArray(e.keyCode, [40,38,9,13,27])
+ this.move(e)
+ }
+
+ , keypress: function (e) {
+ if (this.suppressKeyPressRepeat) return
+ this.move(e)
+ }
+
, keyup: function (e) {
switch(e.keyCode) {
case 40: // down arrow
@@ -202,32 +242,6 @@
e.preventDefault()
}
- , keypress: function (e) {
- if (!this.shown) return
-
- switch(e.keyCode) {
- case 9: // tab
- case 13: // enter
- case 27: // escape
- e.preventDefault()
- break
-
- case 38: // up arrow
- if (e.type != 'keydown') break
- e.preventDefault()
- this.prev()
- break
-
- case 40: // down arrow
- if (e.type != 'keydown') break
- e.preventDefault()
- this.next()
- break
- }
-
- e.stopPropagation()
- }
-
, blur: function (e) {
var that = this
setTimeout(function () { that.hide() }, 150)
@@ -265,12 +279,13 @@
, items: 8
, menu: '<ul class="typeahead dropdown-menu"></ul>'
, item: '<li><a href="#"></a></li>'
+ , minLength: 1
}
$.fn.typeahead.Constructor = Typeahead
- /* TYPEAHEAD DATA-API
+ /* TYPEAHEAD DATA-API
* ================== */
$(function () {
@@ -282,4 +297,4 @@
})
})
-}(window.jQuery); \ No newline at end of file
+}(window.jQuery);