1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-18 23:42:25 +02:00
invoiceninja/public/js/bootstrap-combobox.min.js
2019-01-30 22:25:37 +11:00

3 lines
7.8 KiB
JavaScript

!function(t){"use strict";var e=function(e,s){this.options=t.extend({},t.fn.combobox.defaults,s),this.template=this.options.template||this.template,this.$source=t(e),this.$container=this.setup(),this.$element=this.$container.find("input[type=text]"),this.$target=this.$container.find("input[type=hidden]"),this.$button=this.$container.find(".dropdown-toggle"),this.$menu=t(this.options.menu).appendTo("body"),this.matcher=this.options.matcher||this.matcher,this.sorter=this.options.sorter||this.sorter,this.highlighter=this.options.highlighter||this.highlighter,this.shown=!1,this.selected=!1,this.refresh(),this.transferAttributes(),this.listen()};e.prototype={constructor:e,setup:function(){var e=t(this.template());return this.$source.before(e),this.$source.hide(),e},disable:function(){this.$element.prop("disabled",!0),this.$button.attr("disabled",!0),this.disabled=!0,this.$container.addClass("combobox-disabled")},enable:function(){this.$element.prop("disabled",!1),this.$button.attr("disabled",!1),this.disabled=!1,this.$container.removeClass("combobox-disabled")},parse:function(){var e=this,s={},i=[],o=!1,n="";return this.$source.find("option").each(function(){var r=t(this);return""===r.val()?void(e.options.placeholder=r.text()):(s[r.text()]=r.val(),i.push(r.text()),void(r.prop("selected")&&(o=r.text(),n=r.val())))}),this.map=s,o&&(this.$element.val(o),this.$target.val(n),this.$container.addClass("combobox-selected"),this.selected=!0),i},transferAttributes:function(){this.options.placeholder=this.$source.attr("data-placeholder")||this.options.placeholder,"undefined"!==this.options.appendId&&this.$element.attr("id",this.$source.attr("id")+this.options.appendId),this.$element.attr("placeholder",this.options.placeholder),this.$target.prop("name",this.$source.prop("name")),this.$target.val(this.$source.val()),this.$source.removeAttr("name"),this.$element.attr("required",this.$source.attr("required")),this.$element.attr("rel",this.$source.attr("rel")),this.$element.attr("title",this.$source.attr("title")),this.$element.attr("class",this.$source.attr("class")),this.$element.attr("tabindex",this.$source.attr("tabindex")),this.$source.removeAttr("tabindex"),void 0!==this.$source.attr("disabled")&&this.disable()},select:function(){var t=this.$menu.find(".active").attr("data-value");return this.$element.val(this.updater(t)).trigger("change"),this.$target.val(this.map[t]).trigger("change"),this.$source.val(this.map[t]).trigger("change"),this.$container.addClass("combobox-selected"),this.selected=!0,this.hide()},updater:function(t){return t},show:function(){var e=t.extend({},this.$element.position(),{height:this.$element[0].offsetHeight});return this.$menu.insertAfter(this.$element).css({top:e.top+e.height,left:e.left}).show(),t(".dropdown-menu").on("mousedown",t.proxy(this.scrollSafety,this)),this.shown=!0,this},hide:function(){return this.$menu.hide(),t(".dropdown-menu").off("mousedown",t.proxy(this.scrollSafety,this)),this.$element.on("blur",t.proxy(this.blur,this)),this.shown=!1,this},lookup:function(t){return this.query=this.$element.val(),this.process(this.source)},process:function(e){var s=this;return e=t.grep(e,function(t){return s.matcher(t)}),e=this.sorter(e),e.length?this.render(e.slice(0,this.options.items)).show():this.shown?this.hide():this},template:function(){return"2"==this.options.bsVersion?'<div class="combobox-container"><input type="hidden" /> <div class="input-append"> <input type="text" autocomplete="false" /> <span class="add-on dropdown-toggle" data-dropdown="dropdown"> <span class="caret"/> <i class="icon-remove"/> </span> </div> </div>':'<div class="combobox-container"> <input type="hidden" /> <div class="input-group"> <input type="text" autocomplete="false" /> <span class="input-group-addon dropdown-toggle" data-dropdown="dropdown"> <span class="caret" /> <span class="glyphicon glyphicon-remove" /> </span> </div> </div>'},matcher:function(t){return~t.toLowerCase().indexOf(this.query.toLowerCase())},sorter:function(t){for(var e,s=[],i=[],o=[];e=t.shift();)e.toLowerCase().indexOf(this.query.toLowerCase())?~e.indexOf(this.query)?i.push(e):o.push(e):s.push(e);return s.concat(i,o)},highlighter:function(t){var e=this.query.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g,"\\$&");return t.replace(new RegExp("("+e+")","ig"),function(t,e){return"<strong>"+e+"</strong>"})},render:function(e){var s=this;return e=t(e).map(function(e,i){return e=t(s.options.item).attr("data-value",i),e.find("a").html(s.highlighter(i)),e[0]}),e.first().addClass("active"),this.$menu.html(e),this},next:function(e){var s=this.$menu.find(".active").removeClass("active"),i=s.next();i.length||(i=t(this.$menu.find("li")[0])),i.addClass("active")},prev:function(t){var e=this.$menu.find(".active").removeClass("active"),s=e.prev();s.length||(s=this.$menu.find("li").last()),s.addClass("active")},toggle:function(){this.disabled||(this.$container.hasClass("combobox-selected")?(this.clearTarget(),this.triggerChange(),this.clearElement()):this.shown?this.hide():(this.clearElement(),this.lookup()))},scrollSafety:function(t){"UL"==t.target.tagName&&this.$element.off("blur")},clearElement:function(){this.$element.val("").focus()},clearTarget:function(){this.$source.val(""),this.$target.val(""),this.$container.removeClass("combobox-selected"),this.selected=!1},triggerChange:function(){this.$source.trigger("change")},refresh:function(){this.source=this.parse(),this.options.items=this.source.length},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("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)),this.$button.on("click",t.proxy(this.toggle,this))},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:t.preventDefault(),this.prev(),this.fixMenuScroll();break;case 40:t.preventDefault(),this.next(),this.fixMenuScroll()}t.stopPropagation()}},fixMenuScroll:function(){var t=this.$menu.find(".active");if(t.length){var e=t.position().top,s=e+t.height(),i=this.$menu.scrollTop(),o=this.$menu.height();s>o?this.$menu.scrollTop(i+s-o):e<0&&this.$menu.scrollTop(i+e)}},keydown:function(e){this.suppressKeyPressRepeat=~t.inArray(e.keyCode,[40,38,9,13,27]),this.move(e)},keypress:function(t){this.suppressKeyPressRepeat||this.move(t)},keyup:function(t){switch(t.keyCode){case 40:this.shown||this.toggle();break;case 39:case 38:case 37:case 36:case 35: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();break;default:this.clearTarget(),this.lookup()}t.stopPropagation(),t.preventDefault()},focus:function(t){this.focused=!0},blur:function(t){var e=this;this.focused=!1;var s=this.$element.val();this.selected||""===s||(this.$element.val(""),this.$source.val("").trigger("change"),this.$target.val("").trigger("change")),!this.mousedover&&this.shown&&setTimeout(function(){e.hide()},200)},click:function(t){t.stopPropagation(),t.preventDefault(),this.select(),this.$element.focus()},mouseenter:function(e){this.mousedover=!0,this.$menu.find(".active").removeClass("active"),t(e.currentTarget).addClass("active")},mouseleave:function(t){this.mousedover=!1}},t.fn.combobox=function(s){return this.each(function(){var i=t(this),o=i.data("combobox"),n="object"==typeof s&&s;o||i.data("combobox",o=new e(this,n)),"string"==typeof s&&o[s]()})},t.fn.combobox.defaults={bsVersion:"3",menu:'<ul class="typeahead typeahead-long dropdown-menu"></ul>',item:'<li><a href="#"></a></li>'},t.fn.combobox.Constructor=e}(window.jQuery);
//# sourceMappingURL=bootstrap-combobox.min.js.map