You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1 lines
8.7 KiB

!function(a,b,c,d){a.fn.search=function(c,e){var f,g=a(this),h=a.extend(!0,{},a.fn.search.settings,e),i=h.className,j=h.selector,k=h.error,l=h.namespace,m="."+l,n=l+"-module",o=g.selector||"",p=(new Date).getTime(),q=[],r=arguments[0],s="string"==typeof r,t=[].slice.call(arguments,1);return a(this).each(function(){var e,l=a(this),u=l.find(j.prompt),v=l.find(j.searchButton),w=l.find(j.results),x=(l.find(j.result),l.find(j.category),this),y=l.data(n);e={initialize:function(){e.verbose("Initializing module");var a=u[0],b=a.oninput!==d?"input":a.onpropertychange!==d?"propertychange":"keyup";u.on("focus"+m,e.event.focus).on("blur"+m,e.event.blur).on("keydown"+m,e.handleKeyboard),h.automatic&&u.on(b+m,e.search.throttle),v.on("click"+m,e.search.query),w.on("click"+m,j.result,e.results.select),e.instantiate()},instantiate:function(){e.verbose("Storing instance of module",e),y=e,l.data(n,e)},destroy:function(){e.verbose("Destroying instance"),l.removeData(n)},event:{focus:function(){l.addClass(i.focus),e.results.show()},blur:function(){e.search.cancel(),l.removeClass(i.focus),e.results.hide()}},handleKeyboard:function(b){var c,d=l.find(j.result),f=l.find(j.category),g=b.which,h={backspace:8,enter:13,escape:27,upArrow:38,downArrow:40},k=i.active,m=d.index(d.filter("."+k)),n=d.size();if(g==h.escape&&(e.verbose("Escape key pressed, blurring search field"),u.trigger("blur")),w.filter(":visible").size()>0)if(g==h.enter){if(e.verbose("Enter key pressed, selecting active result"),d.filter("."+k).exists())return a.proxy(e.results.select,d.filter("."+k))(),b.preventDefault(),!1}else g==h.upArrow?(e.verbose("Up key pressed, changing active result"),c=0>m-1?m:m-1,f.removeClass(k),d.removeClass(k).eq(c).addClass(k).closest(f).addClass(k),b.preventDefault()):g==h.downArrow&&(e.verbose("Down key pressed, changing active result"),c=m+1>=n?m:m+1,f.removeClass(k),d.removeClass(k).eq(c).addClass(k).closest(f).addClass(k),b.preventDefault());else g==h.enter&&(e.verbose("Enter key pressed, executing query"),e.search.query(),v.addClass(i.down),u.one("keyup",function(){v.removeClass(i.down)}))},search:{cancel:function(){var a=l.data("xhr")||!1;a&&"resolved"!=a.state()&&(e.debug("Cancelling last search"),a.abort())},throttle:function(){var a=u.val(),b=a.length;clearTimeout(e.timer),b>=h.minCharacters?e.timer=setTimeout(e.search.query,h.searchThrottle):e.results.hide()},query:function(){var b=u.val(),d=e.search.cache.read(b);d?(e.debug("Reading result for '"+b+"' from cache"),e.results.add(d)):(e.debug("Querying for '"+b+"'"),"object"==typeof c?e.search.local(b):e.search.remote(b),a.proxy(h.onSearchQuery,l)(b))},local:function(b){var d,f=[],g=[],j=a.isArray(h.searchFields)?h.searchFields:[h.searchFields],k=new RegExp("(?:s|^)"+b,"i"),m=new RegExp(b,"i");l.addClass(i.loading),a.each(j,function(b,d){a.each(c,function(b,c){"string"==typeof c[d]&&-1==a.inArray(c,f)&&-1==a.inArray(c,g)&&(k.test(c[d])?f.push(c):m.test(c[d])&&g.push(c))})}),d=e.results.generate({results:a.merge(f,g)}),l.removeClass(i.loading),e.search.cache.write(b,d),e.results.add(d)},remote:function(b){var d,f={stateContext:l,url:c,urlData:{query:b},success:function(a){d=e.results.generate(a),e.search.cache.write(b,d),e.results.add(d)},failure:e.error};e.search.cancel(),e.debug("Executing search"),a.extend(!0,f,h.apiSettings),a.api(f)},cache:{read:function(a){var b=l.data("cache");return h.cache&&"object"==typeof b&&b[a]!==d?b[a]:!1},write:function(a,b){var c=l.data("cache")!==d?l.data("cache"):{};c[a]=b,l.data("cache",c)}}},results:{generate:function(b){e.debug("Generating html from response",b);var c=h.templates[h.type],d="";return a.isPlainObject(b.results)&&!a.isEmptyObject(b.results)||a.isArray(b.results)&&b.results.length>0?(h.maxResults>0&&(b.results=a.makeArray(b.results).slice(0,h.maxResults)),b.results.length>0&&(a.isFunction(c)?d=c(b):e.error(k.noTemplate,!1))):d=e.message(k.noResults,"empty"),a.proxy(h.onResults,l)(b),d},add:function(b){("default"==h.onResultsAdd||"default"==a.proxy(h.onResultsAdd,w)(b))&&w.html(b),e.results.show()},show:function(){0===w.filter(":visible").size()&&u.filter(":focus").size()>0&&""!==w.html()&&(w.stop().fadeIn(200),a.proxy(h.onResultsOpen,w)())},hide:function(){w.filter(":visible").size()>0&&(w.stop().fadeOut(200),a.proxy(h.onResultsClose,w)())},select:function(c){e.debug("Search result selected");var d=a(this),f=d.find(".title"),g=f.html();if("default"==h.onSelect||"default"==a.proxy(h.onSelect,this)(c)){var i=d.find("a[href]").eq(0),j=i.attr("href")||!1,k=i.attr("target")||!1;e.results.hide(),u.val(g),j&&("_blank"==k||c.ctrlKey?b.open(j):b.location.href=j)}}},setting:function(b,c){return e.debug("Changing setting",b,c),c===d?h[b]:(a.isPlainObject(b)?a.extend(!0,h,b):h[b]=c,void 0)},internal:function(b,c){return e.debug("Changing internal",b,c),c===d?e[b]:(a.isPlainObject(b)?a.extend(!0,e,b):e[b]=c,void 0)},debug:function(){h.debug&&(h.performance?e.performance.log(arguments):(e.debug=Function.prototype.bind.call(console.info,console,h.name+":"),e.debug.apply(console,arguments)))},verbose:function(){h.verbose&&h.debug&&(h.performance?e.performance.log(arguments):(e.verbose=Function.prototype.bind.call(console.info,console,h.name+":"),e.verbose.apply(console,arguments)))},error:function(){e.error=Function.prototype.bind.call(console.error,console,h.name+":"),e.error.apply(console,arguments)},performance:{log:function(a){var b,c,d;h.performance&&(b=(new Date).getTime(),d=p||b,c=b-d,p=b,q.push({Element:x,Name:a[0],Arguments:[].slice.call(a,1)||"","Execution Time":c})),clearTimeout(e.performance.timer),e.performance.timer=setTimeout(e.performance.display,100)},display:function(){var b=h.name+":",c=0;p=!1,clearTimeout(e.performance.timer),a.each(q,function(a,b){c+=b["Execution Time"]}),b+=" "+c+"ms",o&&(b+=" '"+o+"'"),g.size()>1&&(b+=" ("+g.size()+")"),(console.group!==d||console.table!==d)&&q.length>0&&(console.groupCollapsed(b),console.table?console.table(q):a.each(q,function(a,b){console.log(b.Name+": "+b["Execution Time"]+"ms")}),console.groupEnd()),q=[]}},invoke:function(b,c,g){var h,i,j;return c=c||t,g=x||g,"string"==typeof b&&y!==d&&(b=b.split(/[\. ]/),h=b.length-1,a.each(b,function(c,f){var g=c!=h?f+b[c+1].charAt(0).toUpperCase()+b[c+1].slice(1):b;if(a.isPlainObject(y[f])&&c!=h)y=y[f];else{if(!a.isPlainObject(y[g])||c==h)return y[f]!==d?(i=y[f],!1):y[g]!==d?(i=y[g],!1):(e.error(k.method),!1);y=y[g]}})),a.isFunction(i)?j=i.apply(g,c):i!==d&&(j=i),a.isArray(f)?f.push(j):"string"==typeof f?f=[f,j]:j!==d&&(f=j),i}},s?(y===d&&e.initialize(),e.invoke(r)):(y!==d&&e.destroy(),e.initialize())}),f!==d?f:this},a.fn.search.settings={name:"Search Module",namespace:"search",debug:!0,verbose:!0,performance:!0,onSelect:"default",onResultsAdd:"default",onSearchQuery:function(){},onResults:function(){},onResultsOpen:function(){},onResultsClose:function(){},automatic:"true",type:"simple",minCharacters:3,searchThrottle:300,maxResults:7,cache:!0,searchFields:["title","description"],apiSettings:{},className:{active:"active",down:"down",focus:"focus",empty:"empty",loading:"loading"},error:{noResults:"Your search returned no results",logging:"Error in debug logging, exiting.",noTemplate:"A valid template name was not specified.",serverError:"There was an issue with querying the server.",method:"The method you called is not defined."},selector:{prompt:".prompt",searchButton:".search.button",results:".results",category:".category",result:".result"},templates:{message:function(a,b){var c="";return a!==d&&b!==d&&(c+='<div class="message '+b+'">',c+="empty"==b?'<div class="header">No Results</div class="header"><div class="description">'+a+'</div class="description">':' <div class="description">'+a+"</div>",c+="</div>"),c},categories:function(b){var c="";return b.results!==d?(a.each(b.results,function(b,e){e.results!==d&&e.results.length>0&&(c+='<div class="category"><div class="name">'+e.name+"</div>",a.each(e.results,function(a,b){c+='<div class="result">',c+='<a href="'+b.url+'"></a>',b.image!==d&&(c+='<div class="image"> <img src="'+b.image+'">'+"</div>"),c+='<div class="info">',b.price!==d&&(c+='<div class="price">'+b.price+"</div>"),b.title!==d&&(c+='<div class="title">'+b.title+"</div>"),b.description!==d&&(c+='<div class="description">'+b.description+"</div>"),c+="</div></div>"}),c+="</div>")}),b.resultPage&&(c+='<a href="'+b.resultPage.url+'" class="all">'+b.resultPage.text+"</a>"),c):!1},simple:function(b){var c="";return b.results!==d?(a.each(b.results,function(a,b){c+='<a class="result" href="'+b.url+'">',b.image!==d&&(c+='<div class="image"> <img src="'+b.image+'">'+"</div>"),c+='<div class="info">',b.price!==d&&(c+='<div class="price">'+b.price+"</div>"),b.title!==d&&(c+='<div class="title">'+b.title+"</div>"),b.description!==d&&(c+='<div class="description">'+b.description+"</div>"),c+="</div></a>"}),b.resultPage&&(c+='<a href="'+b.resultPage.url+'" class="all">'+b.resultPage.text+"</a>"),c):!1}}}}(jQuery,window,document);