-
Notifications
You must be signed in to change notification settings - Fork 0
/
bone.io-textsearch.min.js
1 lines (1 loc) · 3.07 KB
/
bone.io-textsearch.min.js
1
bone.modules.textsearch=function(a){var b;return null==a&&(a={}),b={},b.IO=bone.io("textsearch",{inbound:{middleware:a.inboundmiddleware,results:function(a,c){return b.View(c.view.id).render(a)}},outbound:{middleware:a.outboundMiddleware,shortcuts:["search"]}}),b.View=bone.view('[data-ui="textsearch"]',{events:{focus:"focus",blur:"blur",keypress:"keypress",keyup:"keyup",keydown:"keydown"},defaults:{menu:'<ul class="typeahead dropdown-menu"></ul>',item:'<li><a href="#"></a></li>',minLength:1,items:8},initialize:function(){return this.options=$.extend({},this.defaults),this.$menu=$(this.options.menu),this.shown=!1,this.$menu.on("click",$.proxy(this.click,this)).on("mouseenter","li",$.proxy(this.mouseenter,this)).on("mouseleave","li",$.proxy(this.mouseleave,this))},move:function(a){if(this.shown)switch(a.keyCode){case 9:case 13:case 27:return a.preventDefault();case 38:return a.preventDefault(),this.prev();case 40:return a.preventDefault(),this.next()}},lookup:function(){return this.lookupData=this.query=this.$el.val(),null!=this.options.lookup&&(this.lookupData=this.options.lookup()),b.IO.search(this.lookupData,{view:this})},keydown:function(a){return this.suppressKeyPressRepeat=~$.inArray(a.keyCode,[40,38,9,13,27]),this.move(a)},keypress:function(a){return this.supressKeyPressRepeat?void 0:this.move(a)},keyup:function(a){switch(a.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;break;default:this.lookup()}return a.stopPropagation(),a.preventDefault()},focus:function(){return this.focused=!0},blur:function(){return this.focused=!1,!this.mousedover&&this.shown?this.hide():void 0},click:function(a){return a.stopPropagation(),a.preventDefault(),this.select(),this.$el.focus()},mouseenter:function(a){return this.mousedover=!0,this.$menu.find(".active").removeClass("active"),$(a.currentTarget).addClass("active")},mouseleave:function(){return this.mousedover=!1,!this.focused&&this.shown?this.hide():void 0},show:function(){var a;return a=$.extend({},this.$el.position(),{height:this.$el[0].offsetHeight}),this.$menu.insertAfter(this.$el).css({top:a.top+a.height,left:a.left}).show(),this.shown=!0,this},select:function(){var a;return a=this.$menu.find(".active").attr("data-value"),this.$el.val(a).change(),this.hide()},hide:function(){return this.$menu.hide(),this.shown=!1},highlighter:function(a){var b;return b=this.query.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g,"\\$&"),a.replace(new RegExp("("+b+")","ig"),function(a,b){return"<strong>"+b+"</strong>"})},render:function(a){var b,c=this;return a=a.slice(0,this.options.items),b=this,a=$(a).map(function(a,b){return a=$(c.options.item).attr("data-value",b),a.find("a").html(c.highlighter(b)),a[0]}),a.first().addClass("active"),this.$menu.html(a),this.show()},next:function(){var a,b;return a=this.$menu.find(".active").removeClass("active"),b=a.next(),b.length||(b=$(this.$menu.find("li")[0])),b.addClass("active")},prev:function(){var a,b;return a=this.$menu.find(".active").removeClass("active"),b=a.prev(),b.length||(b=this.$menu.find("li").last()),b.addClass("active")}}),b};