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.

0 lines
7.4 KiB

  1. !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("