|
|
!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(
|