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
16 KiB

  1. !function(e,t,s,n){"use strict";t=void 0!==t&&t.Math==Math?t:"undefined"!=typeof self&&self.Math==Math?self:Function("return this")(),e.fn.search=function(r){var i,a=e(this),o=a.selector||"",c=(new Date).getTime(),u=[],l=arguments[0],d="string"==typeof l,f=[].slice.call(arguments,1);return e(this).each(function(){var g,h=e.isPlainObject(r)?e.extend(!0,{},e.fn.search.settings,r):e.extend({},e.fn.search.settings),p=h.className,m=h.metadata,v=h.regExp,b=h.fields,y=h.selector,R=h.error,C=h.namespace,w="."+C,x=C+"-module",F=e(this),j=F.find(y.prompt),k=F.find(y.searchButton),q=F.find(y.results),S=F.find(y.result),A=(F.find(y.category),this),E=F.data(x),T=!1,D=!1;g={initialize:function(){g.verbose("Initializing module"),g.determine.searchFields(),g.bind.events(),g.set.type(),g.create.results(),g.instantiate()},instantiate:function(){g.verbose("Storing instance of module",g),E=g,F.data(x,g)},destroy:function(){g.verbose("Destroying instance"),F.off(w).removeData(x)},refresh:function(){g.debug("Refreshing selector cache"),j=F.find(y.prompt),k=F.find(y.searchButton),F.find(y.category),q=F.find(y.results),S=F.find(y.result)},refreshResults:function(){q=F.find(y.results),S=F.find(y.result)},bind:{events:function(){g.verbose("Binding events to search"),h.automatic&&(F.on(g.get.inputEvent()+w,y.prompt,g.event.input),j.attr("autocomplete","off")),F.on("focus"+w,y.prompt,g.event.focus).on("blur"+w,y.prompt,g.event.blur).on("keydown"+w,y.prompt,g.handleKeyboard).on("click"+w,y.searchButton,g.query).on("mousedown"+w,y.results,g.event.result.mousedown).on("mouseup"+w,y.results,g.event.result.mouseup).on("click"+w,y.result,g.event.result.click)}},determine:{searchFields:function(){r&&r.searchFields!==n&&(h.searchFields=r.searchFields)}},event:{input:function(){h.searchDelay?(clearTimeout(g.timer),g.timer=setTimeout(function(){g.is.focused()&&g.query()},h.searchDelay)):g.query()},focus:function(){g.set.focus(),h.searchOnFocus&&g.has.minimumCharacters()&&g.query(function(){g.can.show()&&g.showResults()})},blur:function(e){var t=function(){g.cancel.query(),g.remove.focus(),g.timer=setTimeout(g.hideResults,h.hideDelay)};s.activeElement===this||(D=!1,g.resultsClicked?(g.debug("Determining if user action caused search to close"),F.one("click.close"+w,y.results,function(e){g.is.inMessage(e)||T?j.focus():(T=!1,g.is.animating()||g.is.hidden()||t())})):(g.debug("Input blurred without user action, closing results"),t()))},result:{mousedown:function(){g.resultsClicked=!0},mouseup:function(){g.resultsClicked=!1},click:function(s){g.debug("Search result selected");var n=e(this),r=n.find(y.title).eq(0),i=n.is("a[href]")?n:n.find("a[href]").eq(0),a=i.attr("href")||!1,o=i.attr("target")||!1,c=(r.html(),r.length>0&&r.text()),u=g.get.results(),l=n.data(m.result)||g.get.result(c,u);if(e.isFunction(h.onSelect)&&!1===h.onSelect.call(A,l,u))return g.debug("Custom onSelect callback cancelled default select action"),void(T=!0);g.hideResults(),c&&g.set.value(c),a&&(g.verbose("Opening search link found in result",i),"_blank"==o||s.ctrlKey?t.open(a):t.location.href=a)}}},handleKeyboard:function(e){var t,s=F.find(y.result),n=F.find(y.category),r=s.filter("."+p.active),i=s.index(r),a=s.length,o=r.length>0,c=e.which,u=13,l=38,d=40;if(c==27&&(g.verbose("Escape key pressed, blurring search field"),g.hideResults(),D=!0),g.is.visible())if(c==u){if(g.verbose("Enter key pressed, selecting active result"),s.filter("."+p.active).length>0)return g.event.result.click.call(s.filter("."+p.active),e),e.preventDefault(),!1}else c==l&&o?(g.verbose("Up key pressed, changing active result"),t=i-1<0?i:i-1,n.removeClass(p.active),s.removeClass(p.active).eq(t).addClass(p.active).closest(n).addClass(p.active),e.preventDefault()):c==d&&(g.verbose("Down key pressed, changing active result"),t=i+1>=a?i:i+1,n.removeClass(p.active),s.removeClass(p.active).eq(t).addClass(p.active).closest(n).addClass(p.active),e.preventDefault());else c==u&&(g.verbose("Enter key pressed, executing query"),g.query(),g.set.buttonPressed(),j.one("keyup",g.remove.buttonFocus))},setup:{api:function(t,s){var n={debug: