From eebc2562262ceb19e596a2aa6145f6516c6a61b3 Mon Sep 17 00:00:00 2001 From: tom bertrand Date: Fri, 22 Jan 2016 15:41:58 -0500 Subject: [PATCH] Fixed #143 missing dropdownFilter hide when clicking outside --- dist/jquery.typeahead.min.js | 4 ++-- src/jquery.typeahead.js | 12 +++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/dist/jquery.typeahead.min.js b/dist/jquery.typeahead.min.js index 2f2e444..bcd7b61 100644 --- a/dist/jquery.typeahead.min.js +++ b/dist/jquery.typeahead.min.js @@ -4,7 +4,7 @@ * Licensed under the MIT license * * @author Tom Bertrand - * @version 2.3.2 (2016-01-12) + * @version 2.3.2 (2016-01-22) * @link http://www.runningcoder.org/jquerytypeahead/ */ -!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):"object"==typeof module&&module.exports?module.exports=function(b,c){return void 0===c&&(c="undefined"!=typeof window?require("jquery"):require("jquery")(b)),a(c),c}:a(jQuery)}(function(a){window.Typeahead={version:"2.3.2"};var b={input:null,minLength:2,maxItem:8,dynamic:!1,delay:300,order:null,offset:!1,hint:!1,accent:!1,highlight:!0,group:!1,groupOrder:null,maxItemPerGroup:null,dropdownFilter:!1,dynamicFilter:null,backdrop:!1,backdropOnFocus:!1,cache:!1,ttl:36e5,compression:!1,suggestion:!1,searchOnFocus:!1,resultContainer:null,generateOnLoad:null,mustSelectItem:!1,href:null,display:["display"],template:null,correlativeTemplate:!1,emptyTemplate:!1,filter:!0,matcher:null,source:null,callback:{onInit:null,onReady:null,onShowLayout:null,onHideLayout:null,onSearch:null,onResult:null,onLayoutBuiltBefore:null,onLayoutBuiltAfter:null,onNavigateBefore:null,onNavigateAfter:null,onMouseEnter:null,onMouseLeave:null,onClickBefore:null,onClickAfter:null,onSendRequest:null,onReceiveRequest:null,onPopulateSource:null,onCacheSave:null,onSubmit:null},selector:{container:"typeahead-container",result:"typeahead-result",list:"typeahead-list",group:"typeahead-group",item:"typeahead-item",empty:"typeahead-empty",display:"typeahead-display",query:"typeahead-query",filter:"typeahead-filter",filterButton:"typeahead-filter-button",filterValue:"typeahead-filter-value",dropdown:"typeahead-dropdown",dropdownCaret:"typeahead-caret",button:"typeahead-button",backdrop:"typeahead-backdrop",hint:"typeahead-hint"},debug:!1},c=".typeahead",d={from:"ãàáäâẽèéëêìíïîõòóöôùúüûñç",to:"aaaaaeeeeeiiiiooooouuuunc"},e=~navigator.appVersion.indexOf("MSIE 9."),f=function(a,b){this.rawQuery="",this.query=a.val()||"",this.tmpSource={},this.source={},this.isGenerated=null,this.generatedGroupCount=0,this.groupCount=0,this.groupBy="group",this.result={},this.resultCount=0,this.resultCountPerGroup={},this.options=b,this.node=a,this.container=null,this.resultContainer=null,this.item=null,this.xhr={},this.hintIndex=null,this.filters={dropdown:{},dynamic:{}},this.requests={},this.backdrop={},this.hint={},this.__construct()};f.prototype={extendOptions:function(){this.options.dynamic&&(this.options.cache=!1,this.options.compression=!1),this.options.cache&&(this.options.cache=function(a){var b,c=["localStorage","sessionStorage"];if(a===!0)a="localStorage";else if("string"==typeof a&&!~c.indexOf(a))return!1;b="undefined"!=typeof window[a];try{window[a].setItem("typeahead","typeahead"),window[a].removeItem("typeahead")}catch(d){b=!1}return b&&a||!1}.call(this,this.options.cache)),this.options.compression&&("object"==typeof LZString&&this.options.cache||(this.options.compression=!1)),"undefined"==typeof this.options.maxItem||/^\d+$/.test(this.options.maxItem)&&0!==this.options.maxItem||(this.options.maxItem=1/0),this.options.maxItemPerGroup&&!/^\d+$/.test(this.options.maxItemPerGroup)&&(this.options.maxItemPerGroup=null),!this.options.display||this.options.display instanceof Array||(this.options.display=[this.options.display]),!this.options.group||this.options.group instanceof Array||(this.options.group=[this.options.group]),this.options.highlight&&!~["any",!0].indexOf(this.options.highlight)&&(this.options.highlight=!1),!this.options.dynamicFilter||this.options.dynamicFilter instanceof Array||(this.options.dynamicFilter=[this.options.dynamicFilter]),this.options.accent&&("object"==typeof this.options.accent?this.options.accent.from&&this.options.accent.to&&this.options.accent.from.length===this.options.accent.to.length:this.options.accent=d),this.options.resultContainer&&("string"==typeof this.options.resultContainer&&(this.options.resultContainer=a(this.options.resultContainer)),this.options.resultContainer instanceof jQuery&&this.options.resultContainer[0]&&(this.resultContainer=this.options.resultContainer)),this.options.group&&"string"==typeof this.options.group[0]&&this.options.maxItemPerGroup&&(this.groupBy=this.options.group[0]),this.options.callback&&this.options.callback.onClick&&(this.options.callback.onClickBefore=this.options.callback.onClick,delete this.options.callback.onClick),this.options.callback&&this.options.callback.onNavigate&&(this.options.callback.onNavigateBefore=this.options.callback.onNavigate,delete this.options.callback.onNavigate),this.options=a.extend(!0,{},b,this.options)},unifySourceFormat:function(){if(this.options.source instanceof Array)return this.options.source={group:{data:this.options.source}},this.groupCount+=1,!0;("undefined"!=typeof this.options.source.data||"undefined"!=typeof this.options.source.url)&&(this.options.source={group:this.options.source});var a;for(var b in this.options.source)if(this.options.source.hasOwnProperty(b)){if(a=this.options.source[b],("string"==typeof a||a instanceof Array)&&(a={url:a}),!a.data&&!a.url)return!1;!a.display||a.display instanceof Array||(a.display=[a.display]),a.ignore&&(a.ignore instanceof RegExp||delete a.ignore),this.options.source[b]=a,this.groupCount+=1}return!0},init:function(){this.helper.executeCallback.call(this,this.options.callback.onInit,[this.node]),this.container=this.node.closest("."+this.options.selector.container)},delegateEvents:function(){var b=this,d=["focus"+c,"input"+c,"propertychange"+c,"keydown"+c,"keyup"+c,"dynamic"+c,"generateOnLoad"+c];this.container.off(c).on("click"+c+" touchstart"+c,function(c){c.stopPropagation(),b.options.dropdownFilter&&b.container.hasClass("filter")&&!a(c.target).closest("."+b.options.selector.dropdown.replace(" ","."))[0]&&b.container.removeClass("filter")}),this.node.closest("form").on("submit",function(a){return b.options.mustSelectItem&&b.helper.isEmpty(b.item)?void a.preventDefault():(b.hideLayout(),b.rawQuery="",b.query="",b.helper.executeCallback.call(b,b.options.callback.onSubmit,[b.node,this,b.item,a])?!1:void 0)});var f=!1;this.node.off(c).on(d.join(" "),function(a){switch(a.type){case"focus":b.options.backdropOnFocus&&(b.buildBackdropLayout(),b.showLayout());case"generateOnLoad":b.options.searchOnFocus&&b.query.length>=b.options.minLength&&(b.isGenerated?b.showLayout():null===b.isGenerated&&b.generateSource());case"keydown":null!==b.isGenerated||b.options.dynamic||b.generateSource(),a.keyCode&&~[9,13,27,38,39,40].indexOf(a.keyCode)&&(f=!0,b.navigate(a));break;case"keyup":e&&b.node[0].value.replace(/^\s+/,"").toString().length=b.options.minLength?b.generateSource():b.hideLayout()},b.options.delay);case"dynamic":if(!b.isGenerated)break;if(b.searchResult(),b.query.length0||b.options.emptyTemplate?b.showLayout():b.hideLayout()}}),this.options.generateOnLoad&&this.node.trigger("generateOnLoad"+c)},generateSource:function(){if(!this.isGenerated||this.options.dynamic){if(this.generatedGroupCount=0,this.isGenerated=!1,!this.helper.isEmpty(this.xhr)){for(var a in this.xhr)this.xhr.hasOwnProperty(a)&&this.xhr[a].abort();this.xhr={}}var b,c,d,e;for(b in this.options.source)if(this.options.source.hasOwnProperty(b)){if(c=this.options.source[b],this.options.cache&&(d=window[this.options.cache].getItem("TYPEAHEAD_"+this.node.selector+":"+b))){this.options.compression&&(d=LZString.decompressFromUTF16(d)),e=!1;try{d=JSON.parse(d+""),d.data&&d.ttl>(new Date).getTime()?(this.populateSource(d.data,b),e=!0):window[this.options.cache].removeItem("TYPEAHEAD_"+this.node.selector+":"+b)}catch(f){}if(e)continue}!c.data||c.url?c.url&&(this.requests[b]||(this.requests[b]=this.generateRequestObject(b))):this.populateSource("function"==typeof c.data&&c.data()||c.data,b)}this.handleRequests()}},generateRequestObject:function(b){var c=this,d=this.options.source[b];d.url instanceof Array||(d.url=[d.url]);var e={request:{url:null,dataType:"json",beforeSend:function(a,e){c.xhr[b]=a;var f=c.requests[b].extra.beforeSend||d.url[0].beforeSend;"function"==typeof f&&f.apply(null,arguments)}},extra:{path:null,group:b,callback:{done:null,fail:null,complete:null,always:null}},validForGroup:[b]};Object.defineProperty(e.request,"beforeSend",{writable:!1}),d.url[0]instanceof Object?(d.url[0].callback&&(e.extra.callback=d.url[0].callback,delete d.url[0].callback),e.request=a.extend(!0,e.request,d.url[0])):"string"==typeof d.url[0]&&(e.request.url=d.url[0]),d.url[1]&&"string"==typeof d.url[1]&&(e.extra.path=d.url[1]),"jsonp"===e.request.dataType.toLowerCase()&&(e.request.jsonpCallback="callback_"+b);var f;for(var g in this.requests)if(this.requests.hasOwnProperty(g)&&(f=JSON.stringify(this.requests[g].request),f===JSON.stringify(e.request))){this.requests[g].validForGroup.push(b),e.isDuplicated=!0,delete e.validForGroup;break}return e},handleRequests:function(){var b=this,c=Object.keys(this.requests).length;this.helper.executeCallback.call(this,this.options.callback.onSendRequest,[this.node,this.query]);for(var d in this.requests)this.requests.hasOwnProperty(d)&&(this.requests[d].isDuplicated||!function(d,e){if("function"==typeof b.options.source[d].url[0]){var f=b.options.source[d].url[0].call(b,b.query);if(e.request=a.extend(!0,e.request,f),"object"!=typeof e.request||!e.request.url)return;f.beforeSend&&(b.requests[d].extra.beforeSend=f.beforeSend)}var g,h=!1;if(~e.request.url.indexOf("{{query}}")&&(h||(e=a.extend(!0,{},e),h=!0),e.request.url=e.request.url.replace("{{query}}",b.query.sanitize())),e.request.data)for(var i in e.request.data)if(e.request.data.hasOwnProperty(i)&&~String(e.request.data[i]).indexOf("{{query}}")){h||(e=a.extend(!0,{},e),h=!0),e.request.data[i]=e.request.data[i].replace("{{query}}",b.query.sanitize());break}a.ajax(e.request).done(function(a,d,f){for(var h,i=0;i/g,"");for(var i=0;i ul > li:not([data-search-group])"),c=b.filter(".active"),d=c[0]&&b.index(c)||null,e=null;if(13===a.keyCode){if(c.length>0)return a.preventDefault(),void c.find("a:first")[0].click();if(this.options.mustSelectItem&&this.helper.isEmpty(this.item))return;return void this.hideLayout()}if(39===a.keyCode)return void(d?b.eq(d).find("a:first")[0].click():this.options.hint&&""!==this.hint.container.val()&&this.helper.getCaret(this.node[0])>=this.query.length&&b.find('a[data-index="'+this.hintIndex+'"]')[0].click());b.length>0&&c.removeClass("active"),38===a.keyCode?(a.preventDefault(),c.length>0?d-1>=0&&(e=d-1,b.eq(e).addClass("active")):(e=b.length-1,b.last().addClass("active"))):40===a.keyCode&&(a.preventDefault(),c.length>0?d+1=this.options.maxItem)||this.options.callback.onResult);u++)if(!s||this.dynamicFilter.validate.apply(this,[this.source[b][u]])){if(c=this.source[b][u],p="group"===o?b:c[o],p&&!this.result[p]&&(this.result[p]=[],this.resultCountPerGroup[p]=0),r&&"group"===o&&this.result[p].length>=r&&!this.options.callback.onResult)break;f=this.options.source[b].display||this.options.display;for(var v=0;vd&&/\s/.test(q)){k=!0,l=q.split(" "),m=e;for(var w=0;wd&&!k)continue;if(this.options.offset&&0!==d)continue;if(this.options.source[b].ignore&&this.options.source[b].ignore.test(e))continue;if(i){if(j=i.call(this,c,c[f[v]]),void 0===j)break;if(!j)continue;"object"==typeof j&&(c=j)}}if(!this.filters.dropdown||this.filters.dropdown.value==c[this.filters.dropdown.key]){if(this.resultCount++,this.resultCountPerGroup[p]++,this.resultItemCount=r)break;c.matchedKey=f[v],this.result[p].push(c),this.resultItemCount++}break}}if(!this.options.callback.onResult){if(this.resultItemCount>=this.options.maxItem)break;if(r&&this.result[p].length>=r&&"group"===o)break}}}if(this.options.order){var x,f=[];for(var b in this.result)if(this.result.hasOwnProperty(b)){for(var v=0;v",{"class":this.options.selector.result}),this.container.append(this.resultContainer));var b=this.query.toLowerCase();this.options.accent&&(b=this.helper.removeAccent.call(this,b));var c=this,d=a("