- /* =============================================================
- * bootstrap-typeahead.js v2.0.0
- * http://twitter.github.com/bootstrap/javascript.html#typeahead
- * =============================================================
- * Copyright 2012 Twitter, Inc.
- @@ -29,6 +29,8 @@
- this.highlighter = this.options.highlighter || this.highlighter
- this.$menu = $(this.options.menu).appendTo('body')
- this.source = this.options.source
- + this.onselect = this.options.onselect
- + this.strings = true
- this.shown = false
- this.listen()
- }
- @@ -38,8 +40,17 @@
- constructor: Typeahead
- , select: function () {
- - var val = this.$menu.find('.active').attr('data-value')
- - this.$element.val(val)
- + var val = JSON.parse(this.$menu.find('.active').attr('data-value'))
- + , text
- +
- + if (!this.strings) text = val[this.options.property]
- + else text = val
- +
- + this.$element.val(text)
- +
- + if (typeof this.onselect == "function")
- + this.onselect(val)
- +
- return this.hide()
- }
- @@ -68,6 +79,25 @@
- var that = this
- , items
- , q
- + , value
- +
- + this.query = this.$element.val()
- +
- + if (typeof this.source == "function")
- + value = this.source(this, this.query)
- + if (value)
- + this.process(value)
- + else
- + this.process(this.source)
- + }
- +
- + , process: function (results) {
- + var that = this
- + , items
- + , q
- +
- + if (results.length && typeof results[0] != "string")
- + this.strings = false
- this.query = this.$element.val()
- @@ -75,7 +105,9 @@
- return this.shown ? this.hide() : this
- }
- - items = $.grep(this.source, function (item) {
- + items = $.grep(results, function (item) {
- + if (!that.strings)
- + item = item[that.options.property]
- if (that.matcher(item)) return item
- })
- @@ -97,10 +129,14 @@
- , caseSensitive = []
- , caseInsensitive = []
- , item
- + , sortby
- while (item = items.shift()) {
- - if (!item.toLowerCase().indexOf(this.query.toLowerCase())) beginswith.push(item)
- - else if (~item.indexOf(this.query)) caseSensitive.push(item)
- + if (this.strings) sortby = item
- + else sortby = item[this.options.property]
- +
- + if (!sortby.toLowerCase().indexOf(this.query.toLowerCase())) beginswith.push(item)
- + else if (~sortby.indexOf(this.query)) caseSensitive.push(item)
- else caseInsensitive.push(item)
- }
- @@ -117,7 +153,9 @@
- var that = this
- items = $(items).map(function (i, item) {
- - i = $(that.options.item).attr('data-value', item)
- + i = $(that.options.item).attr('data-value', JSON.stringify(item))
- + if (!that.strings)
- + item = item[that.options.property]
- i.find('a').html(that.highlighter(item))
- return i[0]
- })
- @@ -251,6 +289,8 @@
- , items: 8
- , menu: '<ul class="typeahead dropdown-menu"></ul>'
- , item: '<li><a href="#"></a></li>'
- + , onselect: null
- + , property: 'value'
- }
- $.fn.typeahead.Constructor = Typeahead