forked from bassjobsen/Bootstrap-3-Typeahead
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbootstrap3-typeahead.min.js
1 lines (1 loc) · 9.22 KB
/
bootstrap3-typeahead.min.js
1
!function(t,e){"use strict";"undefined"!=typeof module&&module.exports?module.exports=e(require("jquery")):"function"==typeof define&&define.amd?define(["jquery"],function(t){return e(t)}):e(t.jQuery)}(this,function(t){"use strict";var e=function(e,i){this.$element=t(e),this.options=t.extend({},t.fn.typeahead.defaults,i),this.matcher=this.options.matcher||this.matcher,this.sorter=this.options.sorter||this.sorter,this.select=this.options.select||this.select,this.autoSelect="boolean"==typeof this.options.autoSelect?this.options.autoSelect:!0,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=t(this.options.menu),this.notFoundString=this.options.notFoundString||void 0,this.$appendTo=this.options.appendTo?t(this.options.appendTo):null,this.fitToElement="boolean"==typeof this.options.fitToElement?this.options.fitToElement:!1,this.shown=!1,this.listen(),this.showHintOnFocus="boolean"==typeof this.options.showHintOnFocus||"all"===this.options.showHintOnFocus?this.options.showHintOnFocus:!1,this.afterSelect=this.options.afterSelect,this.addItem=!1,this.value=this.$element.val()||this.$element.text()};e.prototype={constructor:e,select:function(){var t=this.$menu.find(".active").data("value");if(this.$element.data("active",t),this.autoSelect||t){var e=this.updater(t);e||(e=""),this.$element.val(this.displayText(e)||e).text(this.displayText(e)||e).change(),this.afterSelect(e)}return this.hide()},updater:function(t){return t},setSource:function(t){this.source=t},show:function(){var e,i=t.extend({},this.$element.position(),{height:this.$element[0].offsetHeight}),s="function"==typeof this.options.scrollHeight?this.options.scrollHeight.call():this.options.scrollHeight;if(this.shown?e=this.$menu:this.$appendTo?(e=this.$menu.appendTo(this.$appendTo),this.hasSameParent=this.$appendTo.is(this.$element.parent())):(e=this.$menu.insertAfter(this.$element),this.hasSameParent=!0),!this.hasSameParent){e.css("position","fixed");var n=this.$element.offset();i.top=n.top,i.left=n.left}var o=t(e).parent().hasClass("dropup"),h=o?"auto":i.top+i.height+s,a=t(e).hasClass("dropdown-menu-right"),r=a?"auto":i.left;return e.css({top:h,left:r}).show(),this.options.fitToElement===!0&&e.css("width",this.$element.outerWidth()+"px"),this.shown=!0,this},hide:function(){return this.$menu.hide(),this.shown=!1,this},lookup:function(e){if("undefined"!=typeof e&&null!==e?this.query=e:this.query=this.$element.val()||this.$element.text()||"",this.query.length<this.options.minLength&&!this.options.showHintOnFocus)return this.shown?this.hide():this;var i=t.proxy(function(){t.isFunction(this.source)?this.source(this.query,t.proxy(this.process,this)):this.source&&this.process(this.source)},this);clearTimeout(this.lookupWorker),this.lookupWorker=setTimeout(i,this.delay)},process:function(e){var i=this;return e=t.grep(e,function(t){return i.matcher(t)}),e=this.sorter(e),e.length||this.options.addItem?(e.length>0?this.notFoundString&&-1!==e[0].indexOf(this.notFoundString)||this.$element.data("active",e[0]):this.$element.data("active",null),this.options.addItem&&e.push(this.options.addItem),"all"==this.options.items?this.render(e).show():this.render(e.slice(0,this.options.items)).show()):this.shown?this.hide():this},matcher:function(t){var e=this.displayText(t);return~e.toLowerCase().indexOf(this.query.toLowerCase())},sorter:function(t){for(var e,i=[],s=[],n=[];e=t.shift();){var o=this.displayText(e);o.toLowerCase().indexOf(this.query.toLowerCase())?~o.indexOf(this.query)?s.push(e):n.push(e):i.push(e)}return i.concat(s,n)},highlighter:function(e){var i,s,n,o,h=t("<div></div>"),a=this.query,r=e.toLowerCase().indexOf(a.toLowerCase()),u=a.length;if(0===u)return h.text(e).html();for(;r>-1;)i=e.substr(0,r),s=e.substr(r,u),n=e.substr(r+u),o=t("<strong></strong>").text(s),h.append(document.createTextNode(i)).append(o),e=n,r=e.toLowerCase().indexOf(a.toLowerCase());return h.append(document.createTextNode(e)).html()},render:function(e){var i=this,s=this,n=!1,o=[],h=i.options.separator;return t.each(e,function(t,i){t>0&&i[h]!==e[t-1][h]&&o.push({__type:"divider"}),!i[h]||0!==t&&i[h]===e[t-1][h]||o.push({__type:"category",name:i[h]}),o.push(i)}),e=t(o).map(function(e,o){if("category"==(o.__type||!1))return t(i.options.headerHtml).text(o.name)[0];if("divider"==(o.__type||!1))return t(i.options.headerDivider)[0];var h=s.displayText(o);return e=t(i.options.item).data("value",o),e.find("a").html(i.highlighter(h,o)),i.notFoundString&&h.indexOf(i.notFoundString)>-1?(e.addClass("disabled unclickable"),e.find("a").addClass("disabled unclickable"),e.find("a").click(function(t){t.preventDefault()})):h==s.$element.val()&&(e.addClass("active"),s.$element.data("active",o),n=!0),e[0]}),this.autoSelect&&!n&&(this.notFoundString&&-1!==e.first().data("value").indexOf(this.notFoundString)||(e.filter(":not(.dropdown-header)").first().addClass("active"),this.$element.data("active",e.first().data("value")))),this.$menu.html(e),this},displayText:function(t){return"undefined"!=typeof t&&"undefined"!=typeof t.name&&t.name||t},next:function(e){var i=this.$menu.find(".active").removeClass("active"),s=i.next();s.length||(s=t(this.$menu.find("li")[0])),this.notFoundString&&-1!==s.data("value").indexOf(this.notFoundString)||s.addClass("active")},prev:function(t){var e=this.$menu.find(".active").removeClass("active"),i=e.prev();i.length||(i=this.$menu.find("li").last()),this.notFoundString&&-1!==i.data("value").indexOf(this.notFoundString)||i.addClass("active")},listen:function(){this.$element.on("focus",t.proxy(this.focus,this)).on("blur",t.proxy(this.blur,this)).on("keypress",t.proxy(this.keypress,this)).on("input",t.proxy(this.input,this)).on("keyup",t.proxy(this.keyup,this)),this.eventSupported("keydown")&&this.$element.on("keydown",t.proxy(this.keydown,this)),this.$menu.on("click",t.proxy(this.click,this)).on("mouseenter","li",t.proxy(this.mouseenter,this)).on("mouseleave","li",t.proxy(this.mouseleave,this)).on("mousedown",t.proxy(this.mousedown,this))},destroy:function(){this.$element.data("typeahead",null),this.$element.data("active",null),this.$element.off("focus").off("blur").off("keypress").off("input").off("keyup"),this.eventSupported("keydown")&&this.$element.off("keydown"),this.$menu.remove(),this.destroyed=!0},eventSupported:function(t){var e=t in this.$element;return e||(this.$element.setAttribute(t,"return;"),e="function"==typeof this.$element[t]),e},move:function(t){if(this.shown)switch(t.keyCode){case 9:case 13:case 27:t.preventDefault();break;case 38:if(t.shiftKey)return;t.preventDefault(),this.prev();break;case 40:if(t.shiftKey)return;t.preventDefault(),this.next()}},keydown:function(e){this.suppressKeyPressRepeat=~t.inArray(e.keyCode,[40,38,9,13,27]),this.shown||40!=e.keyCode?this.move(e):this.lookup()},keypress:function(t){this.suppressKeyPressRepeat||this.move(t)},input:function(t){var e=this.$element.val()||this.$element.text();this.value!==e&&(this.value=e,this.lookup())},keyup:function(t){if(!this.destroyed)switch(t.keyCode){case 40:case 38:case 16:case 17:case 18:break;case 9:case 13:if(!this.shown)return;this.select();break;case 27:if(!this.shown)return;this.hide()}},focus:function(t){this.focused||(this.focused=!0,this.options.showHintOnFocus&&this.skipShowHintOnFocus!==!0&&("all"===this.options.showHintOnFocus?this.lookup(""):this.lookup())),this.skipShowHintOnFocus&&(this.skipShowHintOnFocus=!1)},blur:function(t){this.mousedover||this.mouseddown||!this.shown?this.mouseddown&&(this.skipShowHintOnFocus=!0,this.$element.focus(),this.mouseddown=!1):(this.hide(),this.focused=!1)},click:function(t){t.preventDefault(),this.notFoundString&&(t.target.text&&-1!==t.target.text.indexOf(this.notFoundString)||t.target.parentNode.text&&-1!==t.target.parentNode.text.indexOf(this.notFoundString))||(this.skipShowHintOnFocus=!0,this.select(),this.$element.focus(),this.hide())},mouseenter:function(e){this.mousedover=!0,this.$menu.find(".active").removeClass("active");var i=t(e.currentTarget);this.notFoundString&&-1!==i.data("value").indexOf(this.notFoundString)||i.addClass("active")},mouseleave:function(t){this.mousedover=!1,!this.focused&&this.shown&&this.hide()},mousedown:function(t){this.mouseddown=!0,this.$menu.one("mouseup",function(t){this.mouseddown=!1}.bind(this))}};var i=t.fn.typeahead;t.fn.typeahead=function(i){var s=arguments;return"string"==typeof i&&"getActive"==i?this.data("active"):this.each(function(){var n=t(this),o=n.data("typeahead"),h="object"==typeof i&&i;o||n.data("typeahead",o=new e(this,h)),"string"==typeof i&&o[i]&&(s.length>1?o[i].apply(o,Array.prototype.slice.call(s,1)):o[i]())})},t.fn.typeahead.defaults={source:[],items:8,menu:'<ul class="typeahead dropdown-menu" role="listbox"></ul>',item:'<li><a class="dropdown-item" href="#" role="option"></a></li>',minLength:1,scrollHeight:0,autoSelect:!0,afterSelect:t.noop,addItem:!1,delay:0,separator:"category",headerHtml:'<li class="dropdown-header"></li>',headerDivider:'<li class="divider" role="separator"></li>'},t.fn.typeahead.Constructor=e,t.fn.typeahead.noConflict=function(){return t.fn.typeahead=i,this},t(document).on("focus.typeahead.data-api",'[data-provide="typeahead"]',function(e){var i=t(this);i.data("typeahead")||i.typeahead(i.data())})});