From 36cca7c573e64b92def59b0aa87baa1abda602bb Mon Sep 17 00:00:00 2001 From: tom bertrand Date: Wed, 23 Dec 2015 18:06:08 -0500 Subject: [PATCH] #109 correlativeTemplate can be an array of display keys instead of the complete template --- dist/jquery.typeahead.min.js | 2 +- src/jquery.typeahead.js | 26 ++++++++++++++------------ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/dist/jquery.typeahead.min.js b/dist/jquery.typeahead.min.js index 0feca34..2056357 100644 --- a/dist/jquery.typeahead.min.js +++ b/dist/jquery.typeahead.min.js @@ -7,4 +7,4 @@ * @version 2.2.1 (2015-12-23) * @link http://www.runningcoder.org/jquerytypeahead/ */ -!function(a){"function"==typeof define&&define.amd?define(["jquery"],function(b){a(window,document,b)}):"object"==typeof exports?module.exports=a(window,document,require("jquery")):a(window,document,window.jQuery)}(function(a,b,c,d){a.Typeahead={version:"2.2.1"};var e={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,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},f=".typeahead",g={from:"ãàáäâẽèéëêìíïîõòóöôùúüûñç",to:"aaaaaeeeeeiiiiooooouuuunc"},h=~navigator.appVersion.indexOf("MSIE 9."),i=function(a,b){this.rawQuery="",this.query="",this.tmpSource={},this.source={},this.isGenerated=null,this.generatedGroupCount=0,this.groupCount=0,this.groupBy="group",this.result={},this.resultCount=0,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()};i.prototype={extendOptions:function(){this.options.dynamic&&(this.options.cache=!1,this.options.compression=!1),this.options.cache&&(this.options.cache=function(b){var c,d=["localStorage","sessionStorage"];if(b===!0)b="localStorage";else if("string"==typeof b&&!~d.indexOf(b))return!1;c="undefined"!=typeof a[b];try{a[b].setItem("typeahead","typeahead"),a[b].removeItem("typeahead")}catch(e){c=!1}return c&&b||!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=g),this.options.resultContainer&&("string"==typeof this.options.resultContainer&&(this.options.resultContainer=c(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=c.extend(!0,{},e,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 a=this,b=["focus"+f,"input"+f,"propertychange"+f,"keydown"+f,"keyup"+f,"dynamic"+f,"generateOnLoad"+f];this.container.off(f).on("click"+f+" touchstart"+f,function(b){b.stopPropagation(),a.options.dropdownFilter&&a.container.hasClass("filter")&&!c(b.target).closest("."+a.options.selector.dropdown.replace(" ","."))[0]&&a.container.removeClass("filter")}),this.node.closest("form").on("submit",function(b){return a.options.mustSelectItem&&a.helper.isEmpty(a.item)?void b.preventDefault():(a.hideLayout(),a.rawQuery="",a.query="",a.helper.executeCallback.call(a,a.options.callback.onSubmit,[a.node,this,a.item,b])?!1:void 0)});var d=!1;this.node.off(f).on(b.join(" "),function(b){switch(b.type){case"focus":a.options.backdropOnFocus&&(a.buildBackdropLayout(),a.showLayout());case"generateOnLoad":a.isGenerated&&a.options.searchOnFocus&&a.query.length>=a.options.minLength&&a.showLayout(),null!==a.isGenerated||a.options.dynamic||a.generateSource();break;case"keydown":b.keyCode&&~[9,13,27,38,39,40].indexOf(b.keyCode)&&(d=!0,a.navigate(b));break;case"keyup":h&&a.node[0].value.replace(/^\s+/,"").toString().length=a.options.minLength?a.generateSource():a.hideLayout()},a.options.delay);case"dynamic":if(!a.isGenerated)break;if(a.query.length0||a.options.emptyTemplate?a.showLayout():a.hideLayout()}}),this.options.generateOnLoad&&this.node.trigger("generateOnLoad"+f)},generateSource:function(){if(!this.isGenerated||this.options.dynamic){if(this.generatedGroupCount=0,this.isGenerated=!1,!this.helper.isEmpty(this.xhr)){for(var b in this.xhr)this.xhr.hasOwnProperty(b)&&this.xhr[b].abort();this.xhr={}}var c,d,e,f;for(c in this.options.source)if(this.options.source.hasOwnProperty(c)){if(d=this.options.source[c],this.options.cache&&(e=a[this.options.cache].getItem(this.node.selector+":"+c))){this.options.compression&&(e=LZString.decompressFromUTF16(e)),f=!1;try{e=JSON.parse(e+""),e.data&&e.ttl>(new Date).getTime()?(this.populateSource(e.data,c),f=!0):a[this.options.cache].removeItem(this.node.selector+":"+c)}catch(g){}if(f)continue}!d.data||d.url?d.url&&(this.requests[c]||(this.requests[c]=this.generateRequestObject(c))):this.populateSource("function"==typeof d.data&&d.data()||d.data,c)}this.handleRequests()}},generateRequestObject:function(a){var b=this,d=this.options.source[a];d.url instanceof Array||(d.url=[d.url]);var e={request:{url:null,dataType:"json",beforeSend:function(c,e){b.xhr[a]=c;var f=b.requests[a].extra.beforeSend||d.url[0].beforeSend;"function"==typeof f&&f.apply(null,arguments)}},extra:{path:null,group:a,callback:{done:null,fail:null,complete:null,always:null}},validForGroup:[a]};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=c.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_"+a);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(a),e.isDuplicated=!0,delete e.validForGroup;break}return e},handleRequests:function(){var a=this,b=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 a.options.source[d].url[0]){var f=a.options.source[d].url[0].call(a,a.query);if(e.request=c.extend(!0,e.request,f),"object"!=typeof e.request||!e.request.url)return;f.beforeSend&&(a.requests[d].extra.beforeSend=f.beforeSend)}var g,h=!1;if(~e.request.url.indexOf("{{query}}")&&(h||(e=c.extend(!0,{},e),h=!0),e.request.url=e.request.url.replace("{{query}}",a.query)),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=c.extend(!0,{},e),h=!0),e.request.data[i]=e.request.data[i].replace("{{query}}",a.query);break}c.ajax(e.request).done(function(c,d,f){for(var h,i=0;i/g,"");for(var j=0;j 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(),a.stopPropagation(),void c.find("a:first").trigger("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").trigger("click"):this.options.hint&&""!==this.hint.container.val()&&this.helper.getCaret(this.node[0])>=this.query.length&&b.find('a[data-index="'+this.hintIndex+'"]').trigger("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);v++)if(!t||this.dynamicFilter.validate.apply(this,[this.source[b][v]])){if(c=this.source[b][v],q="group"===p?b:c[p],"group"!==q&&q&&!this.result[q]&&(this.result[c[p]]=[]),s&&this.result[q].length>=s&&!this.options.callback.onResult)break;g=this.options.source[b].display||this.options.display;for(var w=0;we&&/\s/.test(r)){l=!0,m=r.split(" "),n=f;for(var x=0;xe&&!l)continue;if(this.options.offset&&0!==e)continue;if(this.options.source[b].ignore&&this.options.source[b].ignore.test(f))continue;if(j){if(k=j.call(this,c,c[g[w]]),k===d)break;if(!k)continue;"object"==typeof k&&(c=k)}}if(!this.filters.dropdown||this.filters.dropdown.value==c[this.filters.dropdown.key]){if(this.resultCount++,this.resultItemCount=s)break;c.matchedKey=g[w],this.result[q].push(c),this.resultItemCount++}break}}if(!this.options.callback.onResult&&(this.resultItemCount>=this.options.maxItem||s&&this.result[q].length>=s)&&"group"===q)break}}if(this.options.order){var y,g=[];for(var b in this.result)if(this.result.hasOwnProperty(b)){for(var w=0;w",{"class":this.options.selector.result}),this.container.append(this.resultContainer));var a=this.query.toLowerCase();this.options.accent&&(a=this.helper.removeAccent.call(this,a));var b=this,d=c("