bootstrap-typeahead.js 5.91 KB
Newer Older
Jacob Thornton's avatar
Jacob Thornton committed
1
2
/* =============================================================
 * bootstrap-typeahead.js v2.0.0
Jon Stevens's avatar
Jon Stevens committed
3
 * http://twitter.github.com/bootstrap/javascript.html#typeahead
Jacob Thornton's avatar
Jacob Thornton committed
4
 * =============================================================
Mark Otto's avatar
Mark Otto committed
5
 * Copyright 2012 Twitter, Inc.
Jacob Thornton's avatar
Jacob Thornton committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============================================================ */

!function( $ ){

  "use strict"

  var Typeahead = function ( element, options ) {
    this.$element = $(element)
    this.options = $.extend({}, $.fn.typeahead.defaults, options)
    this.$menu = $(this.options.menu).appendTo('body')
    this.data = this.options.data
    this.shown = false
    this.listen()
  }

  Typeahead.prototype = {

    constructor: Typeahead

37
  , matcher: function (item, query) {
38
      // ;_; http://jsperf.com/asdfdfasdfa
39
      return ~item.toLowerCase().indexOf(query)
Jacob Thornton's avatar
Jacob Thornton committed
40
41
    }

42
43
44
45
  , select: function () {
      var val = this.$menu.find('.active').attr('data-value')
      this.$element.val(val)
      return this.hide()
Jacob Thornton's avatar
Jacob Thornton committed
46
47
48
    }

  , show: function () {
49
50
51
52
53
54
55
56
57
      var pos = $.extend({}, this.$element.offset(), {
        height: this.$element[0].offsetHeight
      })

      this.$menu.css({
        top: pos.top + pos.height
      , left: pos.left
      })

Jacob Thornton's avatar
Jacob Thornton committed
58
      this.$menu.show()
59
      this.shown = true
Jacob Thornton's avatar
Jacob Thornton committed
60
61
62
63
64
      return this
    }

  , hide: function () {
      this.$menu.hide()
65
      this.shown = false
Jacob Thornton's avatar
Jacob Thornton committed
66
67
68
69
      return this
    }

  , lookup: function (event) {
70
      var that = this
71
        , items
72
73
74
        , q

      this.query = this.$element.val()
75

76
      if (!this.query) {
77
78
        return this.shown ? this.hide() : this
      }
Jacob Thornton's avatar
Jacob Thornton committed
79

80
81
      q = this.query.toLowerCase()

82
      items = this.data.filter(function (item) {
83
        if (that.matcher(item, q)) return item
Jacob Thornton's avatar
Jacob Thornton committed
84
85
86
87
88
89
      })

      if (!items.length) {
        return this.shown ? this.hide() : this
      }

90
      return this.render(items.slice(0, this.options.items)).show()
Jacob Thornton's avatar
Jacob Thornton committed
91
92
    }

93
  , render: function (items) {
Jacob Thornton's avatar
Jacob Thornton committed
94
      var that = this
95
        , QUERY = new RegExp('(' + this.query + ')', 'ig')
Jacob Thornton's avatar
Jacob Thornton committed
96
97

      items = $(items).map(function (i, item) {
98
        i = $(that.options.item).attr('data-value', item)
99
100
101
102
103

        i.find('a').html(item.replace(QUERY, function ($1, match) {
          return '<strong>' + match + '</strong>'
        }))

104
        return i[0]
Jacob Thornton's avatar
Jacob Thornton committed
105
106
107
      })

      items.first().addClass('active')
108
      this.$menu.html(items)
Jacob Thornton's avatar
Jacob Thornton committed
109
110
111
112
113
      return this
    }

  , next: function (event) {
      var active = this.$menu.find('.active').removeClass('active')
114
115
116
117
118
        , next = active.next()

      if (!next.length) {
        next = $(this.$menu.find('li')[0])
      }
Jacob Thornton's avatar
Jacob Thornton committed
119
120
121
122
123
124

      next.addClass('active')
    }

  , prev: function (event) {
      var active = this.$menu.find('.active').removeClass('active')
125
        , prev = active.prev()
Jacob Thornton's avatar
Jacob Thornton committed
126

127
128
129
130
131
      if (!prev.length) {
        prev = this.$menu.find('li').last()
      }

      prev.addClass('active')
Jacob Thornton's avatar
Jacob Thornton committed
132
133
    }

134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
  , 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))
    }

149
150
151
152
153
154
155
156
  , keyup: function (e) {
      e.stopPropagation()
      e.preventDefault()

      switch(e.keyCode) {
        case 40: // down arrow
        case 38: // up arrow
          break
Jacob Thornton's avatar
Jacob Thornton committed
157
158
159
160
161
162
163
164
165
166
167
168
169

        case 9: // tab
        case 13: // enter
          this.select()
          break

        case 27: // escape
          this.hide()
          break

        default:
          this.lookup()
      }
170

Jacob Thornton's avatar
Jacob Thornton committed
171
172
  }

173
174
175
176
  , keypress: function (e) {
      e.stopPropagation()

      switch(e.keyCode) {
Jacob Thornton's avatar
Jacob Thornton committed
177
178
179
        case 9: // tab
        case 13: // enter
        case 27: // escape
180
          e.preventDefault()
Jacob Thornton's avatar
Jacob Thornton committed
181
182
183
          break

        case 38: // up arrow
184
          if (!this.shown) return
185
          e.preventDefault()
Jacob Thornton's avatar
Jacob Thornton committed
186
187
188
189
          this.prev()
          break

        case 40: // down arrow
190
          if (!this.shown) return
191
          e.preventDefault()
Jacob Thornton's avatar
Jacob Thornton committed
192
193
194
          this.next()
          break
      }
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
    }

  , blur: function (e) {
      var that = this
      e.stopPropagation()
      e.preventDefault()
      setTimeout(function () { that.hide() }, 150)
    }

  , click: function (e) {
      e.stopPropagation()
      e.preventDefault()
      this.select()
    }

  , mouseenter: function (e) {
      this.$menu.find('.active').removeClass('active')
      $(e.currentTarget).addClass('active')
    }
Jacob Thornton's avatar
Jacob Thornton committed
214
215
216

  }

217

Jacob Thornton's avatar
Jacob Thornton committed
218
  /* TYPEAHEAD PLUGIN DEFINITION
219
   * =========================== */
Jacob Thornton's avatar
Jacob Thornton committed
220
221
222
223
224
225
226
227
228
229
230
231

  $.fn.typeahead = function ( option ) {
    return this.each(function () {
      var $this = $(this)
        , data = $this.data('typeahead')
        , options = typeof option == 'object' && option
      if (!data) $this.data('typeahead', (data = new Typeahead(this, options)))
      if (typeof option == 'string') data[option]()
    })
  }

  $.fn.typeahead.defaults = {
232
233
234
    items: 8
  , menu: '<ul class="typeahead dropdown-menu"></ul>'
  , item: '<li><a href="#"></a></li>'
Jacob Thornton's avatar
Jacob Thornton committed
235
236
237
238
  }

  $.fn.typeahead.Constructor = Typeahead

239
240
241
242
243
244
245
246
247
248
249
250
251

 /* TYPEAHEAD DATA-API
  * ================== */

  $(function () {
    $('body').on('focus.typeahead.data-api', '[data-provide="typeahead"]', function (e) {
      var $this = $(this)
      if ($this.data('typeahead')) return
      e.preventDefault()
      $this.typeahead($this.data())
    })
  })

252
}( window.jQuery )