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.5 KiB

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