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