/*! Facetly - v0.1.0 - 2013-11-20 * https://github.com/jiabin/facetly * Copyright (c) 2013 Eymen Gunay; Licensed MIT */ var Facetly=Facetly||function($){var Utils={},Ajax={},Events={},Templates={},UI={},Lang={},Query={},App={},Public={};Utils={settings:{debug:!0,selector:"#facetly",elasticsearch:"http://localhost:9200",perPage:25,currentPage:1,excludedFields:[],facets:{},meta:{},onSerialize:function(){},init:function(e){_log("Initializing Settings"),$('meta[name^="facetly-"]').each(function(){Utils.settings.meta[this.name.replace("facetly-","")]=this.content}),Utils.settings=Utils.extend(Utils.settings,e),element=$(Utils.settings.selector),_log("Initialized Settings")}},cache:{window:window,document:document},extend:function(e,t){return $.extend(e,t)},merge:function(e,t){return $.merge(e,t)},elastic_url:function(){return Utils.settings.elasticsearch},elastic_search_url:function(){return Utils.elastic_url()},log:function(e){Utils.settings.debug&&console.log(e)},parseRoute:function(e){var t=e.delimiter||"/",a=e.path.split(t),s=e.target[a.shift()],i=s!==void 0,n=0==a.length;e.inits=e.inits||[],i?("function"==typeof s.init&&e.inits.push(s.init),n?e.parsed.call(void 0,{exists:!0,type:typeof s,obj:s,inits:e.inits}):Utils.parseRoute({path:a.join(t),target:s,delimiter:t,parsed:e.parsed,inits:e.inits})):e.parsed.call(void 0,{exists:!1})},route:function(){Utils.parseRoute({path:Utils.settings.meta.route,target:Routes,delimiter:"/",parsed:function(e){if(e.exists&&"function"==e.type){if(0!=e.inits.length)for(var t in e.inits)e.inits[t].call();e.obj.call()}}})},clone:[]};var _log=Utils.log;Ajax={send:function(e,t,a,s){$.ajax({type:e,url:t,dataType:"json",data:a,success:s})},call:function(e,t,a){Ajax.send("POST",e,t,a)},get:function(e,t,a){Ajax.send("GET",e,t,a)},facets:!1},Events={endpoints:{serializeHistogram:function(){var e=$(this).attr("data-name"),t=Utils.settings.facets[e],a=t.date_histogram.field;Query.holder[e]={};var s=$(this).val();if(s){var i=s.split(",");if(2==i.length){var n=Ajax.facets[e].entries;i[0]=void 0==n[i[0]]||void 0==n[i[0]].time?n[0].time:n[i[0]].time,i[1]=void 0==n[i[1]]||void 0==n[i[1]].time?n[n.length-1].time:n[i[1]].time;var l=$("#facetly-form ul#facet-"+e+" :input").serializeObject(),r=0;for(q in l[e]){var o=l[e][q].operator;if(""!=o&&""!=i[0]&&""!=i[1]){void 0==Query.holder[e][o]&&(Query.holder[e][o]=[]);var l={};if(l.range={},l.range[a]={from:parseInt(i[0]),to:parseInt(i[1])},t.nested)var c={nested:{path:t.nested,query:l}};else var c=l;Query.holder[e][o].push(c),r++}}App.loadResults()}}},serializeTerms:function(){var e=$(this).attr("data-name"),t=$("#facetly-form ul#facet-"+e+" :input").serializeObject(),a=Utils.settings.facets[e],s=a.terms.field;Query.holder[e]={};var i=0;for(q in t[e]){var n=t[e][q].operator,l=t[e][q].value;if(""!=n&&""!=l){if(void 0==Query.holder[e][n]&&(Query.holder[e][n]=[]),a.nested){var r={nested:{path:a.nested,query:{query_string:{}}}};r.nested.query.query_string.default_field=s,r.nested.query.query_string.query=l}else{var r={query_string:{}};r.query_string.default_field=s,r.query_string.query=l}Query.holder[e][n].push(r),i++}}App.loadResults()},clone:function(e){var li=$(this).closest("li"),name=$(this).attr("data-name"),cloneables=$("li[data-clonable='facetly-"+name+"']"),firstLi=cloneables.first(),firstLiHTML=firstLi[0].outerHTML;void 0==Utils.clone[name]&&(Utils.clone[name]=cloneables.length),Utils.clone[name]=Utils.clone[name]+1;var index=eval("("+decodeURIComponent($(this).attr("data-index"))+")");for(i in index){var pattern=RegExp(RegExp.quote(index[i].orig),"g");firstLiHTML=firstLiHTML.replace(pattern,index[i].format.replace("{#}",Utils.clone[name]))}var pattern=RegExp(RegExp.quote('data-bound="true"'),"g");firstLiHTML=firstLiHTML.replace(pattern,""),$(firstLi).after(firstLiHTML),Events.bindEvents()},remove:function(){var e=$(this).closest("li"),t=$(e).parent();return 1==$("li.custom",t).length?!1:($(e).remove(),Events.bindEvents(),void 0)}},serialize:function(){var e={bool:{}};e.bool.must=[],e.bool.should=[],e.bool.must_not=[];var t=0;for(i in Query.holder)for(ii in Query.holder[i])for(iii in Query.holder[i][ii])e.bool[ii].push(Query.holder[i][ii][iii]),t++;var e=0==t?Query.matchAllQuery():e;Query.currentQuery=e;var a={query:e,size:Utils.settings.perPage},s=Query.create(e);return Utils.settings.onSerialize&&Utils.settings.onSerialize(a,s),a},bindEvents:function(){_log("Binding Events"),$("[data-event]").each(function(){var e=this,t=e.dataset.method||"click",a=e.dataset.event,s=e.dataset.bound;s||Utils.parseRoute({path:a,target:Events.endpoints,delimiter:".",parsed:function(a){a.exists&&(e.dataset.bound=!0,$(e).on(t,function(t){a.obj.call(e,t)}))}})}),_log("Events Bound")},init:function(){Events.bindEvents()}},Templates={init:function(){_log("Compiling templates"),Templates.types.terms=Handlebars.compile(Templates.types.terms),Templates.types.date_histogram=Handlebars.compile(Templates.types.date_histogram),Handlebars.registerHelper("random",function(){var e=String.fromCharCode(65+Math.floor(26*Math.random())),t=e+Date.now();return t}),Handlebars.registerHelper("trans",function(e,t){return Lang.get(e,t)}),Handlebars.registerHelper("unique_inc",function(){return val+1}),Handlebars.registerHelper("inc",function(e){return e+1}),Handlebars.registerHelper("type",function(e,t){var a=Templates.types[e._type];return new Handlebars.SafeString(a({facet:e,name:t}))}),Handlebars.registerHelper("facets",function(e,t){return t.fn(Utils.settings.facets[e])}),Handlebars.registerHelper("thead",function(e){var t="
{{trans "no_results"}} |
{{results.hits.total}} {{trans "results"}}
',facets:' '},UI={init:function(){UI.row1=Handlebars.compile(UI.row1),UI.row1=$(UI.row1()),UI.search=Handlebars.compile(UI.search),UI.search=$(UI.search()),element.append(UI.row1),UI.row1.append(UI.search),UI.row2=Handlebars.compile(UI.row2),UI.row2=$(UI.row2()),UI.sidebar=Handlebars.compile(UI.sidebar),UI.sidebar=$(UI.sidebar()),UI.results=Handlebars.compile(UI.results),UI.results=$(UI.results()),element.append(UI.row2),UI.row2.append(UI.sidebar),UI.row2.append(UI.results)},row1:'',row2:'',search:'',sidebar:'',results:''},Lang={locale:"it",get:function(e){return Lang.translations[Lang.locale][e]},translations:{en:{must:"Must",should:"Should",must_not:"Must Not",total:"Total",other:"Others",search:"Search",results:"results",no_results:"No results found"},it:{must:"Deve",should:"Può",must_not:"Non Deve",total:"Totale",other:"Altri",search:"Ricerca",results:"risultati",no_results:"Nessun risultato"}}},Query={create:function(e){return JSON.stringify(e)},matchAllQuery:function(){var e={};return e.match_all={},e},holder:{},currentQuery:{}},App={logic:{},init:function(e){_log("Initializing Facetly"),Utils.settings.init(e),UI.init(),Templates.init(),_log("Initialized Facetly"),Utils.cache.window.tmp={},App.loadFacets(function(){Events.init(),App.loadResults(function(e){Events.init(),$(Utils.settings.selector+" .nbresults").html(e.hits.total||0)})})},loadFacets:function(e){_log("Getting Facets"),Ajax.call(Utils.elastic_search_url(),Query.create({facets:Utils.settings.facets,query:Query.matchAllQuery}),function(t){UI.sidebar.html(Templates.facets({facets:t.facets})),Ajax.facets=t.facets,UI.search.html(Templates.search());var a={},s={};for(name in t.facets){var n=t.facets[name];if(n.terms){a[name]=n,void 0==s[name]&&(s[name]=[]);for(i in n.terms){var l=n.terms[i].term;s[name].push(l)}}}var r=function(e){e(Object.keys(a))},o=function(e,t,a){a(s[e])};VS.init({container:$(Utils.settings.selector+" #search_must"),query:"",callbacks:{search:function(){},facetMatches:r,valueMatches:o}}),VS.init({container:$(Utils.settings.selector+" #search_should"),query:"",callbacks:{search:function(){},facetMatches:r,valueMatches:o}}),VS.init({container:$(Utils.settings.selector+" #search_must_not"),query:"",callbacks:{search:function(){},facetMatches:r,valueMatches:o}}),_log("Facets Loaded"),e&&e()})},loadResults:function(e){_log("Loading Results"),Ajax.call(Utils.elastic_search_url(),Query.create(Events.serialize()),function(t){UI.results.html(Templates.results({results:t})),$(Utils.settings.selector+" .nbresults").html(t.hits.total||0),_log("Results Loaded"),e&&e(t)})}};var element;return Public={init:function(e){App.init(e)},loadFacets:App.loadFacets,templates:App.Templates,currentQuery:Query.currentQuery}}(window.jQuery);RegExp.quote=function(e){return(e+"").replace(/([.?*+^$[\]\\(){}|-])/g,"\\$1")},function(e){e.fn.serializeObject=function(){var t=this,a={},s={},i={validate:/^[a-zA-Z][a-zA-Z0-9_]*(?:\[(?:\d*|[a-zA-Z0-9_]+)\])*$/,key:/[a-zA-Z0-9_]+|(?=\[\])/g,push:/^$/,fixed:/^\d+$/,named:/^[a-zA-Z0-9_]+$/};return this.build=function(e,t,a){return e[t]=a,e},this.push_counter=function(e){return void 0===s[e]&&(s[e]=0),s[e]++},e.each(e(this).serializeArray(),function(){if(i.validate.test(this.name)){for(var s,n=this.name.match(i.key),l=this.value,r=this.name;void 0!==(s=n.pop());)r=r.replace(RegExp("\\["+s+"\\]$"),""),s.match(i.push)?l=t.build([],t.push_counter(r),l):s.match(i.fixed)?l=t.build([],s,l):s.match(i.named)&&(l=t.build({},s,l));a=e.extend(!0,a,l)}}),a}}(jQuery);