~patrick-hetu/+junk/ptittrain_site

« back to all changes in this revision

Viewing changes to apps/ptittrain/static/bootstrap/docs/assets/js/bootstrap-typeahead.js

  • Committer: Patrick Hetu
  • Date: 2012-08-05 23:21:32 UTC
  • Revision ID: patrick.hetu@gmail.com-20120805232132-fryckvbj84wzg3tk
remove unnessesary files and fix static+version

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* =============================================================
2
 
 * bootstrap-typeahead.js v2.0.2
3
 
 * http://twitter.github.com/bootstrap/javascript.html#typeahead
4
 
 * =============================================================
5
 
 * Copyright 2012 Twitter, Inc.
6
 
 *
7
 
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 
 * you may not use this file except in compliance with the License.
9
 
 * You may obtain a copy of the License at
10
 
 *
11
 
 * http://www.apache.org/licenses/LICENSE-2.0
12
 
 *
13
 
 * Unless required by applicable law or agreed to in writing, software
14
 
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 
 * See the License for the specific language governing permissions and
17
 
 * limitations under the License.
18
 
 * ============================================================ */
19
 
 
20
 
!function( $ ){
21
 
 
22
 
  "use strict"
23
 
 
24
 
  var Typeahead = function ( element, options ) {
25
 
    this.$element = $(element)
26
 
    this.options = $.extend({}, $.fn.typeahead.defaults, options)
27
 
    this.matcher = this.options.matcher || this.matcher
28
 
    this.sorter = this.options.sorter || this.sorter
29
 
    this.highlighter = this.options.highlighter || this.highlighter
30
 
    this.$menu = $(this.options.menu).appendTo('body')
31
 
    this.source = this.options.source
32
 
    this.shown = false
33
 
    this.listen()
34
 
  }
35
 
 
36
 
  Typeahead.prototype = {
37
 
 
38
 
    constructor: Typeahead
39
 
 
40
 
  , select: function () {
41
 
      var val = this.$menu.find('.active').attr('data-value')
42
 
      this.$element.val(val)
43
 
      this.$element.change();
44
 
      return this.hide()
45
 
    }
46
 
 
47
 
  , show: function () {
48
 
      var pos = $.extend({}, this.$element.offset(), {
49
 
        height: this.$element[0].offsetHeight
50
 
      })
51
 
 
52
 
      this.$menu.css({
53
 
        top: pos.top + pos.height
54
 
      , left: pos.left
55
 
      })
56
 
 
57
 
      this.$menu.show()
58
 
      this.shown = true
59
 
      return this
60
 
    }
61
 
 
62
 
  , hide: function () {
63
 
      this.$menu.hide()
64
 
      this.shown = false
65
 
      return this
66
 
    }
67
 
 
68
 
  , lookup: function (event) {
69
 
      var that = this
70
 
        , items
71
 
        , q
72
 
 
73
 
      this.query = this.$element.val()
74
 
 
75
 
      if (!this.query) {
76
 
        return this.shown ? this.hide() : this
77
 
      }
78
 
 
79
 
      items = $.grep(this.source, function (item) {
80
 
        if (that.matcher(item)) return item
81
 
      })
82
 
 
83
 
      items = this.sorter(items)
84
 
 
85
 
      if (!items.length) {
86
 
        return this.shown ? this.hide() : this
87
 
      }
88
 
 
89
 
      return this.render(items.slice(0, this.options.items)).show()
90
 
    }
91
 
 
92
 
  , matcher: function (item) {
93
 
      return ~item.toLowerCase().indexOf(this.query.toLowerCase())
94
 
    }
95
 
 
96
 
  , sorter: function (items) {
97
 
      var beginswith = []
98
 
        , caseSensitive = []
99
 
        , caseInsensitive = []
100
 
        , item
101
 
 
102
 
      while (item = items.shift()) {
103
 
        if (!item.toLowerCase().indexOf(this.query.toLowerCase())) beginswith.push(item)
104
 
        else if (~item.indexOf(this.query)) caseSensitive.push(item)
105
 
        else caseInsensitive.push(item)
106
 
      }
107
 
 
108
 
      return beginswith.concat(caseSensitive, caseInsensitive)
109
 
    }
110
 
 
111
 
  , highlighter: function (item) {
112
 
      return item.replace(new RegExp('(' + this.query + ')', 'ig'), function ($1, match) {
113
 
        return '<strong>' + match + '</strong>'
114
 
      })
115
 
    }
116
 
 
117
 
  , render: function (items) {
118
 
      var that = this
119
 
 
120
 
      items = $(items).map(function (i, item) {
121
 
        i = $(that.options.item).attr('data-value', item)
122
 
        i.find('a').html(that.highlighter(item))
123
 
        return i[0]
124
 
      })
125
 
 
126
 
      items.first().addClass('active')
127
 
      this.$menu.html(items)
128
 
      return this
129
 
    }
130
 
 
131
 
  , next: function (event) {
132
 
      var active = this.$menu.find('.active').removeClass('active')
133
 
        , next = active.next()
134
 
 
135
 
      if (!next.length) {
136
 
        next = $(this.$menu.find('li')[0])
137
 
      }
138
 
 
139
 
      next.addClass('active')
140
 
    }
141
 
 
142
 
  , prev: function (event) {
143
 
      var active = this.$menu.find('.active').removeClass('active')
144
 
        , prev = active.prev()
145
 
 
146
 
      if (!prev.length) {
147
 
        prev = this.$menu.find('li').last()
148
 
      }
149
 
 
150
 
      prev.addClass('active')
151
 
    }
152
 
 
153
 
  , listen: function () {
154
 
      this.$element
155
 
        .on('blur',     $.proxy(this.blur, this))
156
 
        .on('keypress', $.proxy(this.keypress, this))
157
 
        .on('keyup',    $.proxy(this.keyup, this))
158
 
 
159
 
      if ($.browser.webkit || $.browser.msie) {
160
 
        this.$element.on('keydown', $.proxy(this.keypress, this))
161
 
      }
162
 
 
163
 
      this.$menu
164
 
        .on('click', $.proxy(this.click, this))
165
 
        .on('mouseenter', 'li', $.proxy(this.mouseenter, this))
166
 
    }
167
 
 
168
 
  , keyup: function (e) {
169
 
      switch(e.keyCode) {
170
 
        case 40: // down arrow
171
 
        case 38: // up arrow
172
 
          break
173
 
 
174
 
        case 9: // tab
175
 
        case 13: // enter
176
 
          if (!this.shown) return
177
 
          this.select()
178
 
          break
179
 
 
180
 
        case 27: // escape
181
 
          if (!this.shown) return
182
 
          this.hide()
183
 
          break
184
 
 
185
 
        default:
186
 
          this.lookup()
187
 
      }
188
 
 
189
 
      e.stopPropagation()
190
 
      e.preventDefault()
191
 
  }
192
 
 
193
 
  , keypress: function (e) {
194
 
      if (!this.shown) return
195
 
 
196
 
      switch(e.keyCode) {
197
 
        case 9: // tab
198
 
        case 13: // enter
199
 
        case 27: // escape
200
 
          e.preventDefault()
201
 
          break
202
 
 
203
 
        case 38: // up arrow
204
 
          e.preventDefault()
205
 
          this.prev()
206
 
          break
207
 
 
208
 
        case 40: // down arrow
209
 
          e.preventDefault()
210
 
          this.next()
211
 
          break
212
 
      }
213
 
 
214
 
      e.stopPropagation()
215
 
    }
216
 
 
217
 
  , blur: function (e) {
218
 
      var that = this
219
 
      setTimeout(function () { that.hide() }, 150)
220
 
    }
221
 
 
222
 
  , click: function (e) {
223
 
      e.stopPropagation()
224
 
      e.preventDefault()
225
 
      this.select()
226
 
    }
227
 
 
228
 
  , mouseenter: function (e) {
229
 
      this.$menu.find('.active').removeClass('active')
230
 
      $(e.currentTarget).addClass('active')
231
 
    }
232
 
 
233
 
  }
234
 
 
235
 
 
236
 
  /* TYPEAHEAD PLUGIN DEFINITION
237
 
   * =========================== */
238
 
 
239
 
  $.fn.typeahead = function ( option ) {
240
 
    return this.each(function () {
241
 
      var $this = $(this)
242
 
        , data = $this.data('typeahead')
243
 
        , options = typeof option == 'object' && option
244
 
      if (!data) $this.data('typeahead', (data = new Typeahead(this, options)))
245
 
      if (typeof option == 'string') data[option]()
246
 
    })
247
 
  }
248
 
 
249
 
  $.fn.typeahead.defaults = {
250
 
    source: []
251
 
  , items: 8
252
 
  , menu: '<ul class="typeahead dropdown-menu"></ul>'
253
 
  , item: '<li><a href="#"></a></li>'
254
 
  }
255
 
 
256
 
  $.fn.typeahead.Constructor = Typeahead
257
 
 
258
 
 
259
 
 /* TYPEAHEAD DATA-API
260
 
  * ================== */
261
 
 
262
 
  $(function () {
263
 
    $('body').on('focus.typeahead.data-api', '[data-provide="typeahead"]', function (e) {
264
 
      var $this = $(this)
265
 
      if ($this.data('typeahead')) return
266
 
      e.preventDefault()
267
 
      $this.typeahead($this.data())
268
 
    })
269
 
  })
270
 
 
271
 
}( window.jQuery );
 
 
b'\\ No newline at end of file'