/* ============================================================= * bootstrap3-typeahead.js v3.1.0 * https://github.com/bassjobsen/Bootstrap-3-Typeahead * ============================================================= * Original written by @mdo and @fat * ============================================================= * Copyright 2014 Bass Jobsen @bassjobsen * * 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 (root, factory) { 'use strict'; // CommonJS module is defined if (typeof module !== 'undefined' && module.exports) { module.exports = factory(require('jquery')); } // AMD module is defined else if (typeof define === 'function' && define.amd) { define(['jquery'], function ($) { return factory ($); }); } else { factory(root.jQuery); } }(this, function ($) { 'use strict'; // jshint laxcomma: true /* TYPEAHEAD PUBLIC CLASS DEFINITION * ================================= */ var Typeahead = function (element, options) { this.$element = $(element); this.options = $.extend({}, $.fn.typeahead.defaults, options); this.matcher = this.options.matcher || this.matcher; this.sorter = this.options.sorter || this.sorter; this.select = this.options.select || this.select; this.autoSelect = typeof this.options.autoSelect == 'boolean' ? this.options.autoSelect : true; this.highlighter = this.options.highlighter || this.highlighter; this.render = this.options.render || this.render; this.updater = this.options.updater || this.updater; this.displayText = this.options.displayText || this.displayText; this.source = this.options.source; this.delay = this.options.delay; this.$menu = $(this.options.menu); this.$appendTo = this.options.appendTo ? $(this.options.appendTo) : null; this.shown = false; this.listen(); this.showHintOnFocus = typeof this.options.showHintOnFocus == 'boolean' ? this.options.showHintOnFocus : false; this.afterSelect = this.options.afterSelect; this.addItem = false; this.pathImg = this.options.pathImg || ''; }; Typeahead.prototype = { constructor: Typeahead, select: function () { var val = this.$menu.find('.active').data('value'); this.$element.data('active', val); if (this.autoSelect || val) { var newVal = this.updater(val); // Updater can be set to any random functions via "options" parameter in constructor above. // Add null check for cases when updater returns void or undefined. if (!newVal) { newVal = ''; } this.$element .val(this.displayText(newVal) || newVal) .change(); this.afterSelect(newVal); } return this.hide(); }, updater: function (item) { return item; }, setSource: function (source) { this.source = source; }, show: function () { var pos = $.extend({}, this.$element.position(), { height: this.$element[0].offsetHeight }); var scrollHeight = typeof this.options.scrollHeight == 'function' ? this.options.scrollHeight.call() : this.options.scrollHeight; var element; if (this.shown) { element = this.$menu; } else if (this.$appendTo) { element = this.$menu.appendTo(this.$appendTo); } else { element = this.$menu.insertAfter(this.$element); } // The rules for bootstrap are: 'dropup' in the parent and 'dropdown-menu-right' in the element. // Note that to get right alignment, you'll need to specify `menu` in the options to be: // '
' var dropup = $(element).parent().hasClass('dropup'); var newTop = dropup ? 'auto' : (pos.top + pos.height + scrollHeight); var right = $(element).hasClass('dropdown-menu-right'); var newLeft = right ? 'auto' : pos.left; // it seems like setting the css is a bad idea (just let Bootstrap do it), but I'll keep the old // logic in place except for the dropup/right-align cases. element.css({ top: newTop, left: newLeft }).show(); this.shown = true; return this; }, hide: function () { this.$menu.hide(); this.shown = false; return this; }, lookup: function (query) { var items; if (typeof(query) != 'undefined' && query !== null) { this.query = query; } else { this.query = this.$element.val() || ''; } if (this.query.length < this.options.minLength && !this.options.showHintOnFocus) { return this.shown ? this.hide() : this; } var worker = $.proxy(function () { if ($.isFunction(this.source)) { this.source(this.query, $.proxy(this.process, this)); } else if (this.source) { this.process(this.source); } }, this); clearTimeout(this.lookupWorker); this.lookupWorker = setTimeout(worker, this.delay); }, process: function (items) { var that = this; items = $.grep(items, function (item) { return that.matcher(item); }); items = this.sorter(items); if (!items.length && !this.options.addItem) { return this.shown ? this.hide() : this; } if (items.length > 0) { this.$element.data('active', items[0]); } else { this.$element.data('active', null); } // Add item if (this.options.addItem){ items.push(this.options.addItem); } if (this.options.items == 'all') { return this.render(items).show(); } else { return this.render(items.slice(0, this.options.items)).show(); } }, matcher: function (item) { var it = this.displayText(item); return item; //return ~it.toLowerCase(); //return ~it.toLowerCase().indexOf(this.query.toLowerCase()); }, sorter: function (items) { var beginswith = []; var caseSensitive = []; var caseInsensitive = []; var item; while ((item = items.shift())) { var it = this.displayText(item); if (!it.toLowerCase().indexOf(this.query.toLowerCase())) beginswith.push(item); else if (~it.indexOf(this.query)) caseSensitive.push(item); else caseInsensitive.push(item); } return beginswith.concat(caseSensitive, caseInsensitive); }, highlighter: function (item) { var html = $(''); var query = this.query; var i = item.toLowerCase().indexOf(query.toLowerCase()); var len = query.length; var leftPart; var middlePart; var rightPart; var strong; if (len === 0) { return html.text(item).html(); } while (i > -1) { leftPart = item.substr(0, i); middlePart = item.substr(i, len); rightPart = item.substr(i + len); strong = $('').text(middlePart); html .append(document.createTextNode(leftPart)) .append(strong); item = rightPart; i = item.toLowerCase().indexOf(query.toLowerCase()); } return html.append(document.createTextNode(item)).html(); }, render: function (items) { var that = this; var self = this; var activeFound = false; var data = []; var _category = that.options.separator; $.each(items, function (key,value) { // inject separator if (key > 0 && value[_category] !== items[key - 1][_category]){ data.push({ __type: 'divider' }); } // inject category header if (value[_category] && (key === 0 || value[_category] !== items[key - 1][_category])){ data.push({ __type: 'category', name: value[_category] }); } data.push(value); }); items = $(data).map(function (i, item) { if ((item.__type || false) == 'category'){ return $(that.options.headerHtml).text(item.name)[0]; } if ((item.__type || false) == 'divider'){ return $(that.options.headerDivider)[0]; } var text = self.displayText(item); i = $(that.options.item).data('value', item); i.find('a').html(that.highlighter(text, item)); if (text == self.$element.val()) { i.addClass('active'); self.$element.data('active', item); activeFound = true; } /** * Flag * */ if (item.flag != ''){ var img = $('