/** bootstrap 2 typeahead plugin. with bootstrap 3 it's been dropped in favor of a more advanced separate plugin. pretty good for simple cases such as autocomplete feature of the search box and required for tag input plugin. */ /* ============================================================= * bootstrap-typeahead.js v2.3.2 * http://twitter.github.com/bootstrap/javascript.html#typeahead * ============================================================= * copyright 2012 twitter, inc. * * 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"; // jshint ;_; /* typeahead public class definition * ================================= */ var typeahead = function (element, options) { this.$element = $(element) this.options = $.extend({}, $.fn.bs_typeahead.defaults, options) this.matcher = this.options.matcher || this.matcher this.sorter = this.options.sorter || this.sorter this.highlighter = this.options.highlighter || this.highlighter this.updater = this.options.updater || this.updater this.source = this.options.source this.$menu = $(this.options.menu) this.shown = false this.listen() } typeahead.prototype = { constructor: typeahead , select: function () { var val = this.$menu.find('.active').attr('data-value') this.$element .val(this.updater(val)) .change() return this.hide() } , updater: function (item) { return item } , show: function () { var pos = $.extend({}, this.$element.position(), { height: this.$element[0].offsetheight }) this.$menu .insertafter(this.$element) .css({ top: pos.top + pos.height , left: pos.left }) .show() this.shown = true return this } , hide: function () { this.$menu.hide() this.shown = false return this } , lookup: function (event) { var items this.query = this.$element.val() if (!this.query || this.query.length < this.options.minlength) { return this.shown ? this.hide() : this } 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) }) items = this.sorter(items) if (!items.length) { return this.shown ? this.hide() : this } return this.render(items.slice(0, this.options.items)).show() } , matcher: function (item) { return ~item.tolowercase().indexof(this.query.tolowercase()) } , sorter: function (items) { var beginswith = [] , casesensitive = [] , caseinsensitive = [] , item while (item = items.shift()) { if (!item.tolowercase().indexof(this.query.tolowercase())) beginswith.push(item) else if (~item.indexof(this.query)) casesensitive.push(item) else caseinsensitive.push(item) } return beginswith.concat(casesensitive, caseinsensitive) } , highlighter: function (item) { var query = this.query.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&') return item.replace(new regexp('(' + query + ')', 'ig'), function ($1, match) { return '' + match + '' }) } , render: function (items) { var that = this items = $(items).map(function (i, item) { i = $(that.options.item).attr('data-value', item) i.find('a').html(that.highlighter(item)) return i[0] }) items.first().addclass('active') this.$menu.html(items) return this } , next: function (event) { var active = this.$menu.find('.active').removeclass('active') , next = active.next() if (!next.length) { next = $(this.$menu.find('li')[0]) } next.addclass('active') } , prev: function (event) { var active = this.$menu.find('.active').removeclass('active') , prev = active.prev() if (!prev.length) { prev = this.$menu.find('li').last() } prev.addclass('active') } , listen: function () { this.$element .on('focus', $.proxy(this.focus, this)) .on('blur', $.proxy(this.blur, this)) .on('keypress', $.proxy(this.keypress, this)) .on('keyup', $.proxy(this.keyup, this)) if (this.eventsupported('keydown')) { this.$element.on('keydown', $.proxy(this.keydown, this)) } this.$menu .on('click', $.proxy(this.click, this)) .on('mouseenter', 'li', $.proxy(this.mouseenter, this)) .on('mouseleave', 'li', $.proxy(this.mouseleave, this)) } , eventsupported: function(eventname) { var issupported = eventname in this.$element if (!issupported) { this.$element.setattribute(eventname, 'return;') issupported = typeof this.$element[eventname] === 'function' } return issupported } , 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 case 38: // up arrow case 16: // shift case 17: // ctrl case 18: // alt break case 9: // tab case 13: // enter if (!this.shown) return this.select() break case 27: // escape if (!this.shown) return this.hide() break default: this.lookup() } e.stoppropagation() e.preventdefault() } , focus: function (e) { this.focused = true } , blur: function (e) { this.focused = false if (!this.mousedover && this.shown) this.hide() } , click: function (e) { e.stoppropagation() e.preventdefault() this.select() this.$element.focus() } , mouseenter: function (e) { this.mousedover = true this.$menu.find('.active').removeclass('active') $(e.currenttarget).addclass('active') } , mouseleave: function (e) { this.mousedover = false if (!this.focused && this.shown) this.hide() } } /* typeahead plugin definition * =========================== */ var old = $.fn.bs_typeahead $.fn.bs_typeahead = function (option) { return this.each(function () { var $this = $(this) , data = $this.data('bs_typeahead') , options = typeof option == 'object' && option if (!data) $this.data('bs_typeahead', (data = new typeahead(this, options))) if (typeof option == 'string') data[option]() }) } $.fn.bs_typeahead.defaults = { source: [] , items: 8 , menu: '' , item: '
  • ' , minlength: 1 } $.fn.bs_typeahead.constructor = typeahead /* typeahead no conflict * =================== */ $.fn.bs_typeahead.noconflict = function () { $.fn.bs_typeahead = old return this } /* typeahead data-api * ================== */ $(document).on('focus.bs_typeahead.data-api', '[data-provide="bs_typeahead"]', function (e) { var $this = $(this) if ($this.data('bs_typeahead')) return $this.bs_typeahead($this.data()) }) }(window.jquery);