bootstrap-typeahead.js 6 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
 *
 * 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)
27
28
    this.matcher = this.options.matcher
    this.sorter = this.options.sorter
Jacob Thornton's avatar
Jacob Thornton committed
29
    this.$menu = $(this.options.menu).appendTo('body')
30
    this.source = this.options.source
Jacob Thornton's avatar
Jacob Thornton committed
31
32
33
34
35
36
37
38
    this.shown = false
    this.listen()
  }

  Typeahead.prototype = {

    constructor: Typeahead

39
40
41
42
  , 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
43
44
45
    }

  , show: function () {
46
47
48
49
50
51
52
53
54
      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
55
      this.$menu.show()
56
      this.shown = true
Jacob Thornton's avatar
Jacob Thornton committed
57
58
59
60
61
      return this
    }

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

  , lookup: function (event) {
67
      var that = this
68
        , items
69
70
71
        , q

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

73
      if (!this.query) {
74
75
        return this.shown ? this.hide() : this
      }
Jacob Thornton's avatar
Jacob Thornton committed
76

77
78
      items = $.grep(this.source, function (item) {
        if (that.matcher(item)) return item
Jacob Thornton's avatar
Jacob Thornton committed
79
80
      })

81
82
      items = this.sorter(items)

Jacob Thornton's avatar
Jacob Thornton committed
83
84
85
86
      if (!items.length) {
        return this.shown ? this.hide() : this
      }

87
      return this.render(items.slice(0, this.options.items)).show()
Jacob Thornton's avatar
Jacob Thornton committed
88
89
    }

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

      items = $(items).map(function (i, item) {
95
        i = $(that.options.item).attr('data-value', item)
96
97
98
99
100

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

101
        return i[0]
Jacob Thornton's avatar
Jacob Thornton committed
102
103
104
      })

      items.first().addClass('active')
105
      this.$menu.html(items)
Jacob Thornton's avatar
Jacob Thornton committed
106
107
108
109
110
      return this
    }

  , next: function (event) {
      var active = this.$menu.find('.active').removeClass('active')
111
112
113
114
115
        , next = active.next()

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

      next.addClass('active')
    }

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

124
125
126
127
128
      if (!prev.length) {
        prev = this.$menu.find('li').last()
      }

      prev.addClass('active')
Jacob Thornton's avatar
Jacob Thornton committed
129
130
    }

131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
  , 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))
    }

146
147
148
149
150
151
152
153
  , 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
154
155
156

        case 9: // tab
        case 13: // enter
157
          if (!this.shown) return
Jacob Thornton's avatar
Jacob Thornton committed
158
159
160
161
162
163
164
165
166
167
          this.select()
          break

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

        default:
          this.lookup()
      }
168

Jacob Thornton's avatar
Jacob Thornton committed
169
170
  }

171
172
  , keypress: function (e) {
      e.stopPropagation()
173
      if (!this.shown) return
174
175

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

        case 38: // up arrow
183
          e.preventDefault()
Jacob Thornton's avatar
Jacob Thornton committed
184
185
186
187
          this.prev()
          break

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

  , 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
211
212
213

  }

214

Jacob Thornton's avatar
Jacob Thornton committed
215
  /* TYPEAHEAD PLUGIN DEFINITION
216
   * =========================== */
Jacob Thornton's avatar
Jacob Thornton committed
217
218
219
220
221
222
223
224
225
226
227
228

  $.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 = {
229
230
    source: []
  , items: 8
231
232
  , menu: '<ul class="typeahead dropdown-menu"></ul>'
  , item: '<li><a href="#"></a></li>'
233
234
235
236
237
238
  , matcher: function (item) {
      return ~item.indexOf(this.query)
    }
  , sorter: function (items) {
      return items
    }
Jacob Thornton's avatar
Jacob Thornton committed
239
240
241
242
  }

  $.fn.typeahead.Constructor = Typeahead

243
244
245
246
247
248
249
250
251
252
253
254
255

 /* 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())
    })
  })

256
}( window.jQuery )