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
9.9 KiB
1 lines
9.9 KiB
!function(a,b,c,d){"use strict";a.fn.search=function(c){var e,f=a(this),g=f.selector||"",h=(new Date).getTime(),i=[],j=arguments[0],k="string"==typeof j,l=[].slice.call(arguments,1);return a(this).each(function(){var m,n=a.extend(!0,{},a.fn.search.settings,c),o=n.className,p=n.selector,q=n.error,r=n.namespace,s="."+r,t=r+"-module",u=a(this),v=u.find(p.prompt),w=u.find(p.searchButton),x=u.find(p.results),y=(u.find(p.result),u.find(p.category),this),z=u.data(t);m={initialize:function(){m.verbose("Initializing module");var a=v[0],b=a!==d&&a.oninput!==d?"input":a!==d&&a.onpropertychange!==d?"propertychange":"keyup";v.on("focus"+s,m.event.focus).on("blur"+s,m.event.blur).on("keydown"+s,m.handleKeyboard),n.automatic&&v.on(b+s,m.search.throttle),w.on("click"+s,m.search.query),x.on("click"+s,p.result,m.results.select),m.instantiate()},instantiate:function(){m.verbose("Storing instance of module",m),z=m,u.data(t,m)},destroy:function(){m.verbose("Destroying instance"),u.removeData(t),v.off(s),w.off(s),x.off(s)},event:{focus:function(){u.addClass(o.focus),clearTimeout(m.timer),m.search.throttle(),m.results.show()},blur:function(){m.search.cancel(),u.removeClass(o.focus),m.timer=setTimeout(m.results.hide,n.hideDelay)}},handleKeyboard:function(b){var c,d=u.find(p.result),e=u.find(p.category),f=b.which,g={backspace:8,enter:13,escape:27,upArrow:38,downArrow:40},h=o.active,i=d.index(d.filter("."+h)),j=d.size();if(f==g.escape&&(m.verbose("Escape key pressed, blurring search field"),v.trigger("blur")),x.filter(":visible").size()>0)if(f==g.enter){if(m.verbose("Enter key pressed, selecting active result"),d.filter("."+h).size()>0)return a.proxy(m.results.select,d.filter("."+h))(b),b.preventDefault(),!1}else f==g.upArrow?(m.verbose("Up key pressed, changing active result"),c=0>i-1?i:i-1,e.removeClass(h),d.removeClass(h).eq(c).addClass(h).closest(e).addClass(h),b.preventDefault()):f==g.downArrow&&(m.verbose("Down key pressed, changing active result"),c=i+1>=j?i:i+1,e.removeClass(h),d.removeClass(h).eq(c).addClass(h).closest(e).addClass(h),b.preventDefault());else f==g.enter&&(m.verbose("Enter key pressed, executing query"),m.search.query(),w.addClass(o.down),v.one("keyup",function(){w.removeClass(o.down)}))},search:{cancel:function(){var a=u.data("xhr")||!1;a&&"resolved"!=a.state()&&(m.debug("Cancelling last search"),a.abort())},throttle:function(){var a=v.val(),b=a.length;clearTimeout(m.timer),b>=n.minCharacters?m.timer=setTimeout(m.search.query,n.searchThrottle):m.results.hide()},query:function(){var b=v.val(),c=m.search.cache.read(b);c?(m.debug("Reading result for '"+b+"' from cache"),m.results.add(c)):(m.debug("Querying for '"+b+"'"),a.isPlainObject(n.source)||a.isArray(n.source)?m.search.local(b):n.apiSettings?m.search.remote(b):a.api!==d&&a.api.settings.api.search!==d?(m.debug("Searching with default search API endpoint"),n.apiSettings={action:"search"},m.search.remote(b)):m.error(q.source),a.proxy(n.onSearchQuery,u)(b))},local:function(b){var c,d=[],e=[],f=a.isArray(n.searchFields)?n.searchFields:[n.searchFields],g=new RegExp("(?:s|^)"+b,"i"),h=new RegExp(b,"i");u.addClass(o.loading),a.each(f,function(b,c){a.each(n.source,function(b,f){var i="string"==typeof f[c],j=-1==a.inArray(f,d)&&-1==a.inArray(f,e);i&&j&&(g.test(f[c])?d.push(f):h.test(f[c])&&e.push(f))})}),c=m.results.generate({results:a.merge(d,e)}),u.removeClass(o.loading),m.search.cache.write(b,c),m.results.add(c)},remote:function(b){var c,d={stateContext:u,urlData:{query:b},onSuccess:function(a){c=m.results.generate(a),m.search.cache.write(b,c),m.results.add(c)},failure:m.error};m.search.cancel(),m.debug("Executing search"),a.extend(!0,d,n.apiSettings),a.api(d)},cache:{read:function(a){var b=u.data("cache");return n.cache&&"object"==typeof b&&b[a]!==d?b[a]:!1},write:function(a,b){var c=u.data("cache")!==d?u.data("cache"):{};c[a]=b,u.data("cache",c)}}},results:{generate:function(b){m.debug("Generating html from response",b);var c=n.templates[n.type],d="";return a.isPlainObject(b.results)&&!a.isEmptyObject(b.results)||a.isArray(b.results)&&b.results.length>0?(n.maxResults>0&&(b.results=a.makeArray(b.results).slice(0,n.maxResults)),a.isFunction(c)?d=c(b):m.error(q.noTemplate,!1)):d=m.message(q.noResults,"empty"),a.proxy(n.onResults,u)(b),d},add:function(b){("default"==n.onResultsAdd||"default"==a.proxy(n.onResultsAdd,x)(b))&&x.html(b),m.results.show()},show:function(){0===x.filter(":visible").size()&&v.filter(":focus").size()>0&&""!==x.html()&&(n.transition&&a.fn.transition!==d&&u.transition("is supported")?(m.debug("Showing results with css animations"),x.transition(n.transition+" in",n.duration)):(m.debug("Showing results with javascript"),x.stop().fadeIn(n.duration,n.easing)),a.proxy(n.onResultsOpen,x)())},hide:function(){x.filter(":visible").size()>0&&(n.transition&&a.fn.transition!==d&&u.transition("is supported")?(m.debug("Hiding results with css animations"),x.transition(n.transition+" out",n.duration)):(m.debug("Hiding results with javascript"),x.stop().fadeIn(n.duration,n.easing)),a.proxy(n.onResultsClose,x)())},select:function(c){m.debug("Search result selected");{var d=a(this),e=d.find(".title");e.html()}if("default"==n.onSelect||"default"==a.proxy(n.onSelect,this)(c)){var f=d.find("a[href]").eq(0),g=f.attr("href")||!1,h=f.attr("target")||!1;m.results.hide(),g&&("_blank"==h||c.ctrlKey?b.open(g):b.location.href=g)}}},message:function(a,b){return b=b||"standard",m.results.add(n.templates.message(a,b)),n.templates.message(a,b)},setting:function(b,c){if(a.isPlainObject(b))a.extend(!0,n,b);else{if(c===d)return n[b];n[b]=c}},internal:function(b,c){if(a.isPlainObject(b))a.extend(!0,m,b);else{if(c===d)return m[b];m[b]=c}},debug:function(){n.debug&&(n.performance?m.performance.log(arguments):(m.debug=Function.prototype.bind.call(console.info,console,n.name+":"),m.debug.apply(console,arguments)))},verbose:function(){n.verbose&&n.debug&&(n.performance?m.performance.log(arguments):(m.verbose=Function.prototype.bind.call(console.info,console,n.name+":"),m.verbose.apply(console,arguments)))},error:function(){m.error=Function.prototype.bind.call(console.error,console,n.name+":"),m.error.apply(console,arguments)},performance:{log:function(a){var b,c,d;n.performance&&(b=(new Date).getTime(),d=h||b,c=b-d,h=b,i.push({Name:a[0],Arguments:[].slice.call(a,1)||"",Element:y,"Execution Time":c})),clearTimeout(m.performance.timer),m.performance.timer=setTimeout(m.performance.display,100)},display:function(){var b=n.name+":",c=0;h=!1,clearTimeout(m.performance.timer),a.each(i,function(a,b){c+=b["Execution Time"]}),b+=" "+c+"ms",g&&(b+=" '"+g+"'"),f.size()>1&&(b+=" ("+f.size()+")"),(console.group!==d||console.table!==d)&&i.length>0&&(console.groupCollapsed(b),console.table?console.table(i):a.each(i,function(a,b){console.log(b.Name+": "+b["Execution Time"]+"ms")}),console.groupEnd()),i=[]}},invoke:function(b,c,f){var g,h,i,j=z;return c=c||l,f=y||f,"string"==typeof b&&j!==d&&(b=b.split(/[\. ]/),g=b.length-1,a.each(b,function(c,e){var f=c!=g?e+b[c+1].charAt(0).toUpperCase()+b[c+1].slice(1):b;if(a.isPlainObject(j[f])&&c!=g)j=j[f];else{if(j[f]!==d)return h=j[f],!1;if(!a.isPlainObject(j[e])||c==g)return j[e]!==d?(h=j[e],!1):!1;j=j[e]}})),a.isFunction(h)?i=h.apply(f,c):h!==d&&(i=h),a.isArray(e)?e.push(i):e!==d?e=[e,i]:i!==d&&(e=i),h}},k?(z===d&&m.initialize(),m.invoke(j)):(z!==d&&m.destroy(),m.initialize())}),e!==d?e:this},a.fn.search.settings={name:"Search Module",namespace:"search",debug:!1,verbose:!0,performance:!0,onSelect:"default",onResultsAdd:"default",onSearchQuery:function(){},onResults:function(){},onResultsOpen:function(){},onResultsClose:function(){},source:!1,automatic:"true",type:"simple",hideDelay:300,minCharacters:3,searchThrottle:300,maxResults:7,cache:!0,searchFields:["title","description"],transition:"scale",duration:300,easing:"easeOutExpo",apiSettings:!1,className:{active:"active",down:"down",focus:"focus",empty:"empty",loading:"loading"},error:{source:"No source or api action specified",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:{escape:function(a){var b=/[&<>"'`]/g,c=/[&<>"'`]/,d={"&":"&","<":"<",">":">",'"':""","'":"'","`":"`"},e=function(a){return d[a]};return c.test(a)?a.replace(b,e):a},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="",e=a.fn.search.settings.templates.escape;return b.results!==d?(a.each(b.results,function(b,f){f.results!==d&&f.results.length>0&&(c+='<div class="category"><div class="name">'+f.name+"</div>",a.each(f.results,function(a,b){c+='<div class="result">',c+='<a href="'+b.url+'"></a>',b.image!==d&&(b.image=e(b.image),c+='<div class="image"> <img src="'+b.image+'" alt=""></div>'),c+='<div class="content">',b.price!==d&&(b.price=e(b.price),c+='<div class="price">'+b.price+"</div>"),b.title!==d&&(b.title=e(b.title),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="content">',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);
|