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.5 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.errors,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,g=a(this),l=g.find(j.prompt),u=g.find(j.searchButton),v=g.find(j.results),w=(g.find(j.result),g.find(j.category),this),x=g.data(n);e={initialize:function(){e.verbose("Initializing module");var a=l[0],b=a.oninput!==d?"input":a.onpropertychange!==d?"propertychange":"keyup";l.on("focus"+m,e.event.focus).on("blur"+m,e.event.blur).on("keydown"+m,e.handleKeyboard),h.automatic&&l.on(b+m,e.search.throttle),u.on("click"+m,e.search.query),v.on("click"+m,j.result,e.results.select),e.instantiate()},instantiate:function(){e.verbose("Storing instance of module",e),x=e,g.data(n,e)},destroy:function(){e.verbose("Destroying instance"),g.removeData(n)},event:{focus:function(){g.addClass(i.focus),e.results.show()},blur:function(){e.search.cancel(),g.removeClass(i.focus),e.results.hide()}},handleKeyboard:function(b){var c,d=g.find(j.result),f=g.find(j.category),h=b.which,k={backspace:8,enter:13,escape:27,upArrow:38,downArrow:40},m=i.active,n=d.index(d.filter("."+m)),o=d.size();if(h==k.escape&&(e.verbose("Escape key pressed, blurring search field"),l.trigger("blur")),v.filter(":visible").size()>0)if(h==k.enter){if(e.verbose("Enter key pressed, selecting active result"),d.filter("."+m).exists())return a.proxy(e.results.select,d.filter("."+m))(),b.preventDefault(),!1}else h==k.upArrow?(e.verbose("Up key pressed, changing active result"),c=0>n-1?n:n-1,f.removeClass(m),d.removeClass(m).eq(c).addClass(m).closest(f).addClass(m),b.preventDefault()):h==k.downArrow&&(e.verbose("Down key pressed, changing active result"),c=n+1>=o?n:n+1,f.removeClass(m),d.removeClass(m).eq(c).addClass(m).closest(f).addClass(m),b.preventDefault());else h==k.enter&&(e.verbose("Enter key pressed, executing query"),e.search.query(),u.addClass(i.down),l.one("keyup",function(){u.removeClass(i.down)}))},search:{cancel:function(){var a=g.data("xhr")||!1;a&&"resolved"!=a.state()&&(e.debug("Cancelling last search"),a.abort())},throttle:function(){var a=l.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=l.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,g)(b))},local:function(b){var d,f=[],j=[],k=a.isArray(h.searchFields)?h.searchFields:[h.searchFields],l=new RegExp("(?:s|^)"+b,"i"),m=new RegExp(b,"i");g.addClass(i.loading),a.each(k,function(b,d){a.each(c,function(b,c){"string"==typeof c[d]&&-1==a.inArray(c,f)&&-1==a.inArray(c,j)&&(l.test(c[d])?f.push(c):m.test(c[d])&&j.push(c))})}),d=e.results.generate({results:a.merge(f,j)}),g.removeClass(i.loading),e.search.cache.write(b,d),e.results.add(d)},remote:function(b){var d,f={stateContext:g,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=g.data("cache");return h.cache&&"object"==typeof b&&b[a]!==d?b[a]:!1},write:function(a,b){var c=g.data("cache")!==d?g.data("cache"):{};c[a]=b,g.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,g)(b),d},add:function(b){("default"==h.onResultsAdd||"default"==a.proxy(h.onResultsAdd,v)(b))&&v.html(b),e.results.show()},show:function(){0===v.filter(":visible").size()&&l.filter(":focus").size()>0&&""!==v.html()&&(v.stop().fadeIn(200),a.proxy(h.onResultsOpen,v)())},hide:function(){v.filter(":visible").size()>0&&(v.stop().fadeOut(200),a.proxy(h.onResultsClose,v)())},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(),l.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.moduleName+":"),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.moduleName+":"),e.verbose.apply(console,arguments)))},error:function(){e.error=Function.prototype.bind.call(console.error,console,h.moduleName+":"),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:w,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.moduleName+":",c=0;p=!1,clearTimeout(e.performance.timer),a.each(q,function(a,b){c+=b["Execution Time"]}),b+=" "+c+"ms",o&&(b+=" '"+o+"'"),(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,f){var g,h;return c=c||t,f=w||f,"string"==typeof b&&x!==d&&(b=b.split(/[\. ]/),g=b.length-1,a.each(b,function(b,c){return a.isPlainObject(x[c])&&b!=g?(x=x[c],!0):x[c]!==d?(h=x[c],!0):(e.error(k.method),!1)})),a.isFunction(h)?h.apply(f,c):h||!1}},s?(x===d&&e.initialize(),f=e.invoke(r)):(x!==d&&e.destroy(),e.initialize())}),f?f:this},a.fn.search.settings={moduleName:"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"},errors:{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);