-
Notifications
You must be signed in to change notification settings - Fork 3
/
mithril.solr.min.js
1 lines (1 loc) · 7.72 KB
/
mithril.solr.min.js
1
var SolrWidget=function(e,t,n){function a(e){return decodeURIComponent(e).replace(/\+/g," ")}function r(e){for(var t=[],n=0;n<e.length;n++)t.push(encodeURIComponent(e[n].name)+"="+encodeURIComponent(e[n].value));return t.join("&")}var i=function(e){return function(){return m.redraw.strategy("diff"),e.apply(this,arguments)}},s=[{name:"wt",value:"json"},{name:"facet",value:"true"}];return e.vm={init:function(){var e=m.route.param("q")?a(m.route.param("q")):"";this.q=m.prop(e),this.isSearching=!1,this.docs=this.docs||m.prop([]),this.facets=this.facets||m.prop({}),this.numFound=this.numFound||m.prop(0),this.perPage=m.prop(m.route.param("rows")||20),this.currentPage=m.prop(0),m.route.param("start")&&m.route.param("rows")&&this.currentPage(Math.ceil(m.route.param("start")/m.route.param("rows")))},getFacetProp:function(t,n){for(var a="",r=0;r<e.vm.facetFields.length;r++)e.vm.facetFields[r].name==t&&(a=e.vm.facetFields[r][n]);return a},sanitize:function(e){return e.replace(/^\"|\"$/gi,"")}},e.cache={},e.view=function(t,n){return m(".msolr",[m.component(e.SearchForm,{onSubmit:t.formSubmit,isSearching:e.vm.isSearching,q:e.vm.q,numFound:e.vm.numFound}),m(".msolr-content",[m.component(e.Pagination,{perPage:e.vm.perPage,numFound:e.vm.numFound,currentPage:e.vm.currentPage,changePage:t.changePage,nextPage:t.nextPage,prevPage:t.prevPage}),m.component(e.SearchSelectedFacets,{selectedFacets:t.selectedFacets,facetFields:n.facetFields,removeFacet:t.removeFacet}),m.component(e.SearchFacets,{facets:e.vm.facets,facetFields:n.facetFields,onSelect:t.selectFacet}),m.component(e.SearchResults,{docs:e.vm.docs,fields:n.fields,q:e.vm.q,isSearching:e.vm.isSearching}),m.component(e.Pagination,{perPage:e.vm.perPage,numFound:e.vm.numFound,currentPage:e.vm.currentPage,changePage:t.changePage,nextPage:t.nextPage,prevPage:t.prevPage})])])},e.controller=new i(function(t){var i=this;if(e.vm.init(),this.facetFields=[],e.vm.facetFields=t.facetFields,t.facetFields)for(var c=0;c<t.facetFields.length;c++)this.facetFields.push({name:"facet.field",value:t.facetFields[c].name});if(this.selectedFacets=[],m.route.param("fq")){var u=m.route.param("fq");"string"==typeof u&&(u=u.replace(/\"/g,'\\"'),u=JSON.parse('["'+u+'"]')),u.map(function(e){i.selectedFacets.push({name:"fq",value:a(e)})})}this.removeFacet=function(t,n){n&&n.preventDefault();for(var a=0;a<this.selectedFacets.length;a++)this.selectedFacets[a].value==t.fullValue&&this.selectedFacets.splice(a,1);e.vm.currentPage(0),this.route()}.bind(this),this.selectFacet=function(t,n,a){a&&a.preventDefault(),this.selectedFacets.push({name:"fq",value:t+':"'+n+'"'}),e.vm.currentPage(0),this.route()}.bind(this),this.changePage=function(t,n){n&&n.preventDefault(),e.vm.currentPage(t),this.route()}.bind(this),this.prevPage=function(t,n){n&&n.preventDefault();var a=e.vm.currentPage();--a,0>a&&(a=0),e.vm.currentPage(a),this.route()}.bind(this),this.nextPage=function(t,n){n&&n.preventDefault();var a=e.vm.currentPage();++a,a>t-1&&(a=t-1),e.vm.currentPage(a),this.route()}.bind(this),this.buildParams=function(){var t=[{name:"q",value:e.vm.q()}],a=[{name:"rows",value:e.vm.perPage()},{name:"start",value:e.vm.currentPage()*e.vm.perPage()}],r=[].concat.apply([],[this.facetFields,s,this.selectedFacets,t,a]);return r=r.filter(function(e){return e!=n})}.bind(this),this.route=function(e){e&&e.preventDefault(),queryString=r(this.buildParams()),m.route("/search?"+queryString)}.bind(this),this.search=function(t){t&&t.preventDefault(),e.vm.q()&&(e.vm.isSearching=!0,this.fetch().then(function(t){e.vm.docs(t.response.docs),e.vm.facets(t.facet_counts||""),e.vm.numFound(t.response.numFound),e.vm.isSearching=!1,m.redraw()}))}.bind(this),this.fetch=function(){var n=r(this.buildParams()),a=JSON.stringify(n);return e.cache[a]||(e.cache[a]=m.request({url:t.endPoint+"?"+n,dataType:"jsonp",callbackKey:"json.wrf",background:!0}).then(function(e){return e})),e.cache[a]}.bind(this),this.formSubmit=function(t){t&&t.preventDefault(),e.vm.q()&&this.route()}.bind(this),e.vm.q()?this.search():(e.vm.docs([]),e.vm.facets({}),e.vm.numFound(0),e.vm.currentPage(0))}),e}(SolrWidget||{},window);!function(e,t,n){var a={ellipsis:"…",edges:2,limit:10};e.Pagination={controller:function(e){this.totalPages=function(t){return Math.ceil(e.numFound()/t)}.bind(this),this.pageList=function(e,t){for(var n=[],r=this.totalPages(t),i=0,s=r,c=Math.max(parseInt(e)-a.edges,0),u=Math.min(parseInt(e)+a.edges,r),o=i;s>o;o++)0==o||o==parseInt(r)-1||r<a.limit?n.push(o):((o==u+1||o==c-1)&&n.push(a.ellipsis),u>=o&&o>=c&&n.push(o));return n}.bind(this)},view:function(e,t){var n=e.pageList(t.currentPage(),t.perPage()),r=e.totalPages(t.perPage());return m("nav.msolr-pages",{style:{display:n.length>1?"":"none"}},[m("a.previous",{onclick:t.prevPage.bind(this,r),className:0==t.currentPage()?"page-disabled":""},"Prev"),n.map(function(n){switch(n){case a.ellipsis:return m("span.page-ellipsis",m.trust(n));default:return m("a",{onclick:t.changePage.bind(e,parseInt(n)),className:n==t.currentPage()?"page-current":""},parseInt(n)+1)}}),m("a.next",{onclick:t.nextPage.bind(this,r),className:t.currentPage()==r-1?"page-disabled":""},"Next")])}}}(SolrWidget||{},window),function(e,t,n){e.SearchFacets={facetView:function(t,n,a,r){switch(e.vm.getFacetProp(a,"type")){case"string":return this.facetStringView(n,a,r);case"date":return this.facetDateView(n,a,r)}},facetStringView:function(e,t,n){for(var a=[],r=0;r<e.length;r+=2)e[r+1]>0&&a.push(m("li",m("a",{href:"#",onclick:n.onSelect.bind(this,t,e[r])},e[r],[m("span.msolr-facet-count",e[r+1])])));return m("ul",[a])},facetDateView:function(e,t,n){for(var a=[],r=0;r<e.length;r+=2)e[r+1]>0&&a.push(m("li",m("a",{href:"#",onclick:n.onSelect.bind(this,t,e[r])},new Date(e[r]).getFullYear(),[m("span.msolr-facet-count",e[r+1])])));return m("ul",[a])},view:function(t,n){var a=n.facets(),r=[];for(var i in a)if(a.hasOwnProperty(i)){var s=a[i];if("facet_fields"==i)for(var c in s)s.hasOwnProperty(c)&&s[c].length&&r.push(m(".facet-field",[m("h3",e.vm.getFacetProp(c,"displayName")),this.facetView(t,s[c],c,n)]))}return m(".msolr-filters",[r])}}}(SolrWidget||{},window),function(e,t,n){e.SearchForm={controller:function(){},view:function(e,t){return m(".msolr-form",[m("form",{onsubmit:t.onSubmit},[m("input",{type:"text",defaultValue:t.q(),onchange:m.withAttr("value",t.q),placeholder:"Enter a keyword"}),m("button",t.isSearching?"Searching":"Search")]),m("h3",{style:{display:t.numFound()>0?"":"none"}},t.numFound()+" results found for "+t.q())])}}}(SolrWidget||{},window),function(e,t,n){e.SearchResults={controller:function(){},noResults:function(e,t){return e.length<1&&t.q()&&!t.isSearching?m(".msolr-no-results","Your search did not return any results."):void 0},view:function(e,t){var n=t.docs();return m(".msolr-results",[m("ul",[n.map(function(e){return m("li",[m("h3",m("a",{href:e[t.fields.url]},e[t.fields.title])),m(".summary",[m("p",e[t.fields.summary])])])})]),this.noResults(n,t)])}}}(SolrWidget||{},window),function(e,t,n){e.SearchSelectedFacets={controller:function(e){this.selections=e.selectedFacets||[],this.groups=function(e){var t=[];return e.map(function(e){var n=e.value.indexOf(":"),a=e.value.substr(0,n);value=e.value.substr(n+1),t[a]=t[a]||[],t[a].push({name:a,fullValue:a+":"+value,value:value})}),t}},facetList:function(t,n){return t.map(function(t){return m("li",e.SearchSelectedFacets.facetDisplay(t),[m("a",{onclick:n.removeFacet.bind(this,t)}," x Remove")])})},facetDisplay:function(t){switch(e.vm.getFacetProp(t.name,"type")){case"string":return e.vm.sanitize(t.value);case"date":return new Date(e.vm.sanitize(t.value)).getFullYear()}},view:function(t,n){var a=[],r=t.groups(n.selectedFacets);for(var i in r)r.hasOwnProperty(i)&&a.push(m(".msolr-selected-facet",[m("h5",e.vm.getFacetProp(i,"displayName")),e.SearchSelectedFacets.facetList(r[i],n)]));return m(".msolr-selected-facets",[a])}}}(SolrWidget||{},window);