indvd00m@0: /* ============================================================= indvd00m@0: * bootstrap-typeahead.js v2.3.1 indvd00m@0: * http://twitter.github.com/bootstrap/javascript.html#typeahead indvd00m@0: * ============================================================= indvd00m@0: * Copyright 2012 Twitter, Inc. indvd00m@0: * indvd00m@0: * Licensed under the Apache License, Version 2.0 (the "License"); indvd00m@0: * you may not use this file except in compliance with the License. indvd00m@0: * You may obtain a copy of the License at indvd00m@0: * indvd00m@0: * http://www.apache.org/licenses/LICENSE-2.0 indvd00m@0: * indvd00m@0: * Unless required by applicable law or agreed to in writing, software indvd00m@0: * distributed under the License is distributed on an "AS IS" BASIS, indvd00m@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. indvd00m@0: * See the License for the specific language governing permissions and indvd00m@0: * limitations under the License. indvd00m@0: * ============================================================ */ indvd00m@0: indvd00m@0: indvd00m@0: !function($){ indvd00m@0: indvd00m@0: "use strict"; // jshint ;_; indvd00m@0: indvd00m@0: indvd00m@0: /* TYPEAHEAD PUBLIC CLASS DEFINITION indvd00m@0: * ================================= */ indvd00m@0: indvd00m@0: var Typeahead = function (element, options) { indvd00m@0: this.$element = $(element) indvd00m@0: this.options = $.extend({}, $.fn.typeahead.defaults, options) indvd00m@0: this.matcher = this.options.matcher || this.matcher indvd00m@0: this.sorter = this.options.sorter || this.sorter indvd00m@0: this.highlighter = this.options.highlighter || this.highlighter indvd00m@0: this.updater = this.options.updater || this.updater indvd00m@0: this.source = this.options.source indvd00m@0: this.$menu = $(this.options.menu) indvd00m@0: this.shown = false indvd00m@0: this.listen() indvd00m@0: } indvd00m@0: indvd00m@0: Typeahead.prototype = { indvd00m@0: indvd00m@0: constructor: Typeahead indvd00m@0: indvd00m@0: , select: function () { indvd00m@0: var val = this.$menu.find('.active').attr('data-value') indvd00m@0: this.$element indvd00m@0: .val(this.updater(val)) indvd00m@0: .change() indvd00m@0: return this.hide() indvd00m@0: } indvd00m@0: indvd00m@0: , updater: function (item) { indvd00m@0: return item indvd00m@0: } indvd00m@0: indvd00m@0: , show: function () { indvd00m@0: var pos = $.extend({}, this.$element.position(), { indvd00m@0: height: this.$element[0].offsetHeight indvd00m@0: }) indvd00m@0: indvd00m@0: this.$menu indvd00m@0: .insertAfter(this.$element) indvd00m@0: .css({ indvd00m@0: top: pos.top + pos.height indvd00m@0: , left: pos.left indvd00m@0: }) indvd00m@0: .show() indvd00m@0: indvd00m@0: this.shown = true indvd00m@0: return this indvd00m@0: } indvd00m@0: indvd00m@0: , hide: function () { indvd00m@0: this.$menu.hide() indvd00m@0: this.shown = false indvd00m@0: return this indvd00m@0: } indvd00m@0: indvd00m@0: , lookup: function (event) { indvd00m@0: var items indvd00m@0: indvd00m@0: this.query = this.$element.val() indvd00m@0: indvd00m@0: if (!this.query || this.query.length < this.options.minLength) { indvd00m@0: return this.shown ? this.hide() : this indvd00m@0: } indvd00m@0: indvd00m@0: items = $.isFunction(this.source) ? this.source(this.query, $.proxy(this.process, this)) : this.source indvd00m@0: indvd00m@0: return items ? this.process(items) : this indvd00m@0: } indvd00m@0: indvd00m@0: , process: function (items) { indvd00m@0: var that = this indvd00m@0: indvd00m@0: items = $.grep(items, function (item) { indvd00m@0: return that.matcher(item) indvd00m@0: }) indvd00m@0: indvd00m@0: items = this.sorter(items) indvd00m@0: indvd00m@0: if (!items.length) { indvd00m@0: return this.shown ? this.hide() : this indvd00m@0: } indvd00m@0: indvd00m@0: return this.render(items.slice(0, this.options.items)).show() indvd00m@0: } indvd00m@0: indvd00m@0: , matcher: function (item) { indvd00m@0: return ~item.toLowerCase().indexOf(this.query.toLowerCase()) indvd00m@0: } indvd00m@0: indvd00m@0: , sorter: function (items) { indvd00m@0: var beginswith = [] indvd00m@0: , caseSensitive = [] indvd00m@0: , caseInsensitive = [] indvd00m@0: , item indvd00m@0: indvd00m@0: while (item = items.shift()) { indvd00m@0: if (!item.toLowerCase().indexOf(this.query.toLowerCase())) beginswith.push(item) indvd00m@0: else if (~item.indexOf(this.query)) caseSensitive.push(item) indvd00m@0: else caseInsensitive.push(item) indvd00m@0: } indvd00m@0: indvd00m@0: return beginswith.concat(caseSensitive, caseInsensitive) indvd00m@0: } indvd00m@0: indvd00m@0: , highlighter: function (item) { indvd00m@0: var query = this.query.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&') indvd00m@0: return item.replace(new RegExp('(' + query + ')', 'ig'), function ($1, match) { indvd00m@0: return '' + match + '' indvd00m@0: }) indvd00m@0: } indvd00m@0: indvd00m@0: , render: function (items) { indvd00m@0: var that = this indvd00m@0: indvd00m@0: items = $(items).map(function (i, item) { indvd00m@0: i = $(that.options.item).attr('data-value', item) indvd00m@0: i.find('a').html(that.highlighter(item)) indvd00m@0: return i[0] indvd00m@0: }) indvd00m@0: indvd00m@0: items.first().addClass('active') indvd00m@0: this.$menu.html(items) indvd00m@0: return this indvd00m@0: } indvd00m@0: indvd00m@0: , next: function (event) { indvd00m@0: var active = this.$menu.find('.active').removeClass('active') indvd00m@0: , next = active.next() indvd00m@0: indvd00m@0: if (!next.length) { indvd00m@0: next = $(this.$menu.find('li')[0]) indvd00m@0: } indvd00m@0: indvd00m@0: next.addClass('active') indvd00m@0: } indvd00m@0: indvd00m@0: , prev: function (event) { indvd00m@0: var active = this.$menu.find('.active').removeClass('active') indvd00m@0: , prev = active.prev() indvd00m@0: indvd00m@0: if (!prev.length) { indvd00m@0: prev = this.$menu.find('li').last() indvd00m@0: } indvd00m@0: indvd00m@0: prev.addClass('active') indvd00m@0: } indvd00m@0: indvd00m@0: , listen: function () { indvd00m@0: this.$element indvd00m@0: .on('focus', $.proxy(this.focus, this)) indvd00m@0: .on('blur', $.proxy(this.blur, this)) indvd00m@0: .on('keypress', $.proxy(this.keypress, this)) indvd00m@0: .on('keyup', $.proxy(this.keyup, this)) indvd00m@0: indvd00m@0: if (this.eventSupported('keydown')) { indvd00m@0: this.$element.on('keydown', $.proxy(this.keydown, this)) indvd00m@0: } indvd00m@0: indvd00m@0: this.$menu indvd00m@0: .on('click', $.proxy(this.click, this)) indvd00m@0: .on('mouseenter', 'li', $.proxy(this.mouseenter, this)) indvd00m@0: .on('mouseleave', 'li', $.proxy(this.mouseleave, this)) indvd00m@0: } indvd00m@0: indvd00m@0: , eventSupported: function(eventName) { indvd00m@0: var isSupported = eventName in this.$element indvd00m@0: if (!isSupported) { indvd00m@0: this.$element.setAttribute(eventName, 'return;') indvd00m@0: isSupported = typeof this.$element[eventName] === 'function' indvd00m@0: } indvd00m@0: return isSupported indvd00m@0: } indvd00m@0: indvd00m@0: , move: function (e) { indvd00m@0: if (!this.shown) return indvd00m@0: indvd00m@0: switch(e.keyCode) { indvd00m@0: case 9: // tab indvd00m@0: case 13: // enter indvd00m@0: case 27: // escape indvd00m@0: e.preventDefault() indvd00m@0: break indvd00m@0: indvd00m@0: case 38: // up arrow indvd00m@0: e.preventDefault() indvd00m@0: this.prev() indvd00m@0: break indvd00m@0: indvd00m@0: case 40: // down arrow indvd00m@0: e.preventDefault() indvd00m@0: this.next() indvd00m@0: break indvd00m@0: } indvd00m@0: indvd00m@0: e.stopPropagation() indvd00m@0: } indvd00m@0: indvd00m@0: , keydown: function (e) { indvd00m@0: this.suppressKeyPressRepeat = ~$.inArray(e.keyCode, [40,38,9,13,27]) indvd00m@0: this.move(e) indvd00m@0: } indvd00m@0: indvd00m@0: , keypress: function (e) { indvd00m@0: if (this.suppressKeyPressRepeat) return indvd00m@0: this.move(e) indvd00m@0: } indvd00m@0: indvd00m@0: , keyup: function (e) { indvd00m@0: switch(e.keyCode) { indvd00m@0: case 40: // down arrow indvd00m@0: case 38: // up arrow indvd00m@0: case 16: // shift indvd00m@0: case 17: // ctrl indvd00m@0: case 18: // alt indvd00m@0: break indvd00m@0: indvd00m@0: case 9: // tab indvd00m@0: case 13: // enter indvd00m@0: if (!this.shown) return indvd00m@0: this.select() indvd00m@0: break indvd00m@0: indvd00m@0: case 27: // escape indvd00m@0: if (!this.shown) return indvd00m@0: this.hide() indvd00m@0: break indvd00m@0: indvd00m@0: default: indvd00m@0: this.lookup() indvd00m@0: } indvd00m@0: indvd00m@0: e.stopPropagation() indvd00m@0: e.preventDefault() indvd00m@0: } indvd00m@0: indvd00m@0: , focus: function (e) { indvd00m@0: this.focused = true indvd00m@0: } indvd00m@0: indvd00m@0: , blur: function (e) { indvd00m@0: this.focused = false indvd00m@0: if (!this.mousedover && this.shown) this.hide() indvd00m@0: } indvd00m@0: indvd00m@0: , click: function (e) { indvd00m@0: e.stopPropagation() indvd00m@0: e.preventDefault() indvd00m@0: this.select() indvd00m@0: this.$element.focus() indvd00m@0: } indvd00m@0: indvd00m@0: , mouseenter: function (e) { indvd00m@0: this.mousedover = true indvd00m@0: this.$menu.find('.active').removeClass('active') indvd00m@0: $(e.currentTarget).addClass('active') indvd00m@0: } indvd00m@0: indvd00m@0: , mouseleave: function (e) { indvd00m@0: this.mousedover = false indvd00m@0: if (!this.focused && this.shown) this.hide() indvd00m@0: } indvd00m@0: indvd00m@0: } indvd00m@0: indvd00m@0: indvd00m@0: /* TYPEAHEAD PLUGIN DEFINITION indvd00m@0: * =========================== */ indvd00m@0: indvd00m@0: var old = $.fn.typeahead indvd00m@0: indvd00m@0: $.fn.typeahead = function (option) { indvd00m@0: return this.each(function () { indvd00m@0: var $this = $(this) indvd00m@0: , data = $this.data('typeahead') indvd00m@0: , options = typeof option == 'object' && option indvd00m@0: if (!data) $this.data('typeahead', (data = new Typeahead(this, options))) indvd00m@0: if (typeof option == 'string') data[option]() indvd00m@0: }) indvd00m@0: } indvd00m@0: indvd00m@0: $.fn.typeahead.defaults = { indvd00m@0: source: [] indvd00m@0: , items: 8 indvd00m@0: , menu: '
' indvd00m@0: , item: '