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
7.4 KiB
1 lines
7.4 KiB
!function(a,b,c,d){a.fn.searchPrompt=function(c,e){var f,g=a.extend(!0,{},a.fn.searchPrompt.settings,e),h=arguments[0],i=[].slice.call(arguments,1);return a(this).each(function(){var e,j=a(this),k=j.find(g.selector.searchPrompt),l=j.find(g.selector.searchButton),m=j.find(g.selector.searchResults),n=(j.find(g.selector.result),j.find(g.selector.category),j.find(g.selector.emptyResult),j.find(g.selector.resultPage),this),o=(j.selector||"",j.data("module-"+g.namespace)),p=o!==d&&"string"==typeof h,q=g.className,r=g.namespace,s=g.errors;e={initialize:function(){var a=k[0],b=a.oninput!==d?"input":a.onpropertychange!==d?"propertychange":"keyup";k.on("focus."+r,e.event.focus).on("blur."+r,e.event.blur).on("keydown."+r,e.handleKeyboard),g.automatic&&k.on(b+"."+r,e.search.throttle),l.on("click."+r,e.search.query),m.on("click."+r,g.selector.result,e.results.select),j.data("module-"+r,e)},event:{focus:function(){j.addClass(q.focus),e.results.show()},blur:function(){e.search.cancel(),j.removeClass(q.focus),e.results.hide()}},handleKeyboard:function(b){var c,d=j.find(g.selector.result),f=j.find(g.selector.category),h=b.which,i={backspace:8,enter:13,escape:27,upArrow:38,downArrow:40},n=q.active,o=d.index(d.filter("."+n)),p=d.size();if(h==i.escape&&k.trigger("blur"),m.filter(":visible").size()>0)if(h==i.enter){if(d.filter("."+n).exists())return a.proxy(e.results.select,d.filter("."+n))(),b.preventDefault(),!1}else h==i.upArrow?(c=0>o-1?o:o-1,f.removeClass(n),d.removeClass(n).eq(c).addClass(n).closest(f).addClass(n),b.preventDefault()):h==i.downArrow&&(c=o+1>=p?o:o+1,f.removeClass(n),d.removeClass(n).eq(c).addClass(n).closest(f).addClass(n),b.preventDefault());else h==i.enter&&(e.search.query(),l.addClass(q.down),k.one("keyup",function(){l.removeClass(q.down)}))},search:{cancel:function(){var a=j.data("xhr")||!1;a&&"resolved"!=a.state()&&a.abort()},throttle:function(){var a,b=k.val(),c=b.length;clearTimeout(j.data("timer")),c>=g.minCharacters?(a=setTimeout(e.search.query,g.searchThrottle),j.data("timer",a)):e.results.hide()},query:function(){var b=k.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(g.onSearchQuery,j)(b))},local:function(b){var d,f=[],h=[],i=a.isArray(g.searchFields)?g.searchFields:[g.searchFields],k=new RegExp("(?:s|^)"+b,"i"),l=new RegExp(b,"i");j.addClass(q.loading),a.each(i,function(b,d){a.each(c,function(b,c){"string"==typeof c[d]&&-1==a.inArray(c,f)&&-1==a.inArray(c,h)&&(k.test(c[d])?f.push(c):l.test(c[d])&&h.push(c))})}),d=e.results.generate({results:a.merge(f,h)}),j.removeClass(q.loading),e.search.cache.write(b,d),e.results.add(d)},remote:function(b){var f,h=j.data("xhr")!==d?j.data("xhr"):!1,i={stateContext:j,url:c,urlData:{query:b},success:function(a){f=e.results.generate(a),e.search.cache.write(b,f),e.results.add(f)},failure:e.error};h&&"resolved"!=h.state()&&h.abort(),a.extend(!0,i,g.apiSettings),a.api(i)},cache:{read:function(a){var b=j.data("cache");return g.cache&&"object"==typeof b&&b[a]!==d?b[a]:!1},write:function(a,b){var c=j.data("cache")!==d?j.data("cache"):{};c[a]=b,j.data("cache",c)}}},results:{generate:function(b){e.debug("Generating html from response",b);var c=g.templates[g.type],d="";return a.isPlainObject(b.results)&&!a.isEmptyObject(b.results)||a.isArray(b.results)&&b.results.length>0?(g.maxResults>0&&(b.results=a.makeArray(b.results).slice(0,g.maxResults)),b.results.length>0&&(a.isFunction(c)?d=c(b):e.error(s.noTemplate,!1))):d=e.message(s.noResults,"empty"),a.proxy(g.onSearchResults,j)(b),d},add:function(b){("default"==g.onResultsAdd||"default"==a.proxy(g.onResultsAdd,m)(b))&&m.html(b),e.results.show()},show:function(){0===m.filter(":visible").size()&&k.filter(":focus").size()>0&&""!==m.html()&&(m.stop().fadeIn(200),a.proxy(g.onResultsOpen,m)())},hide:function(){m.filter(":visible").size()>0&&(m.stop().fadeOut(200),a.proxy(g.onResultsClose,m)())},followLink:function(){},select:function(c){e.debug("Search result selected");var d=a(this),f=d.find(".title"),h=f.html();if("default"==g.onSelect||"default"==a.proxy(g.onSelect,this)(c)){var i=d.find("a[href]").eq(0),j=i.attr("href"),l=i.attr("target");try{e.results.hide(),k.val(h),"_blank"==l||c.ctrlKey?b.open(j):b.location.href=j}catch(m){}}}},setting:function(a,b){return b===d?g[a]:(g[a]=b,void 0)},debug:function(){var a=[],b=g.moduleName+": "+arguments[0],c=[].slice.call(arguments,1),d=console.info||console.log||function(){};d=Function.prototype.bind.call(d,console),g.debug&&(a.push(b),d.apply(console,a.concat(c)))},message:function(a,b){return b=b||"standard",e.results.add(g.templates.message(a,b)),g.templates.message(a,b)},error:function(a,b){b=b!==d?b:!0,console.warn(g.moduleName+": "+a),b&&a!==d&&e.message(a,"error")},invoke:function(b,c,f){var g,h;return f=f||[].slice.call(arguments,2),"string"==typeof b&&o!==d&&(b=b.split("."),g=b.length-1,a.each(b,function(b,c){return a.isPlainObject(o[c])&&b!=g?(o=o[c],!0):o[c]!==d?(h=o[c],!0):(e.error(s.method),!1)})),a.isFunction(h)?h.apply(c,f):h}},p?f=e.invoke(h,n,i):e.initialize()}),f!==d?f:this},a.fn.searchPrompt.settings={moduleName:"Search Module",debug:!0,namespace:"search",onSelect:"default",onResultsAdd:"default",onSearchQuery:function(){},onSearchResults: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:{searchPrompt:".prompt",searchButton:".search.button",searchResults:".results",category:".category",result:".result",emptyResult:".results .message",resultPage:".results .page"},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);
|