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

  1. (function(e,t,n,o){e.fn.searchPrompt=function(n,i){var s,a=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(a.selector.searchPrompt),d=l.find(a.selector.searchButton),f=l.find(a.selector.searchResults),p=(l.find(a.selector.result),l.find(a.selector.category),l.find(a.selector.emptyResult),l.find(a.selector.resultPage),this),m=(l.selector||"",l.data("module-"+a.namespace)),g=m!==o&&"string"==typeof r,h=a.className,v=a.namespace,b=a.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),a.automatic&&u.on(t+"."+v,i.search.throttle),d.on("click."+v,i.search.query),f.on("click."+v,a.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(a.selector.result),s=l.find(a.selector.category),r=t.which,c={backspace:8,enter:13,escape:27,upArrow:38,downArrow:40},p=h.active,m=o.index(o.filter("."+p)),g=o.size();if(r==c.escape&&u.trigger("blur"),f.filter(":visible").size()>0)if(r==c.enter){if(o.filter("."+p).exists())return e.proxy(i.results.select,o.filter("."+p))(),t.preventDefault(),!1}else r==c.upArrow?(n=0>m-1?m:m-1,s.removeClass(p),o.removeClass(p).eq(n).addClass(p).closest(s).addClass(p),t.preventDefault()):r==c.downArrow&&(n=m+1>=g?m:m+1,s.removeClass(p),o.removeClass(p).eq(n).addClass(p).closest(s).addClass(p),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>=a.minCharacters?(e=setTimeout(i.search.query,a.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(a.onSearchQuery,l)(t))},local:function(t){var o,s=[],r=[],c=e.isArray(a.searchFields)?a.searchFields:[a.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,s)&&-1==e.inArray(n,r)&&(u.test(n[o])?s.push(n):d.test(n[o])&&r.push(n))})}),o=i.results.generate({results:e.merge(s,r)}),l.removeClass(h.loading),i.search.cache.write(t,o),i.results.add(o)},remote:function(t){var s,r=l.data("xhr")!==o?l.data("xhr"):!1,c={stateContext:l,url:n,urlData:{query:t},success:function(e){s=i.results.generate(e),i.search.cache.write(t,s),i.results.add(s)},failure:i.error};r&&"resolved"!=r.state()&&r.abort(),e.extend(!0,c,a.apiSettings),e.api(c)},cache:{read:function(e){var t=l.data("cache");return a.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=a.templates[a.type],o="";return e.isPlainObject(t.results)&&!e.isEmptyObject(t.results)||e.isArray(t.results)&&t.results.length>0?(a.maxResults>0&&(t.results=e.makeArray(t.results).slice(0,a.maxResults)),t.results.length>0&&(e.isFunction(n)?o=n(t):i.error(b.noTemplate,!1))):o=i.message(b.noResults,"empty"),e.proxy(a.onSearchResults,l)(t),o},add:function(t){("default"==a.onResultsAdd||"default"==e.proxy(a.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(a.onResultsOpen,f)())},hide:function(){f.filter(":visible").size()>0&&(f.stop().fadeOut(200),e.proxy(a.onResultsClose,f)())},followLink:function(){},select:function(n){i.debug("Search result selected");var o=e(this),s=o.find(".title")