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.

10 lines
13 KiB

9 years ago
9 years ago
9 years ago
10 years ago
9 years ago
  1. /*!
  2. * # Semantic UI 1.12.3 - Search
  3. * http://github.com/semantic-org/semantic-ui/
  4. *
  5. *
  6. * Copyright 2014 Contributors
  7. * Released under the MIT license
  8. * http://opensource.org/licenses/MIT
  9. *
  10. */
  11. !function(e,t,s,n){"use strict";e.fn.search=function(r){var a,i=e(this),o=i.selector||"",c=(new Date).getTime(),u=[],l=arguments[0],d="string"==typeof l,f=[].slice.call(arguments,1);return e(this).each(function(){var p,g=e.extend(!0,{},e.fn.search.settings,r),h=g.className,m=g.metadata,v=g.regExp,b=g.selector,y=g.error,w=g.namespace,R="."+w,C=w+"-module",x=e(this),A=x.find(b.prompt),k=x.find(b.searchButton),S=x.find(b.results),q=(x.find(b.result),x.find(b.category),this),E=x.data(C);p={initialize:function(){p.verbose("Initializing module");var e=A[0],t=e!==n&&e.oninput!==n?"input":e!==n&&e.onpropertychange!==n?"propertychange":"keyup";g.automatic&&A.on(t+R,p.throttle).attr("autocomplete","off"),A.on("focus"+R,p.event.focus).on("blur"+R,p.event.blur).on("keydown"+R,p.handleKeyboard),k.on("click"+R,p.query),S.on("mousedown"+R,p.event.result.mousedown).on("mouseup"+R,p.event.result.mouseup).on("click"+R,b.result,p.event.result.click),p.instantiate()},instantiate:function(){p.verbose("Storing instance of module",p),E=p,x.data(C,p)},destroy:function(){p.verbose("Destroying instance"),x.removeData(C),A.off(R),k.off(R),S.off(R)},event:{focus:function(){p.set.focus(),clearTimeout(p.timer),p.throttle(),p.has.minimumCharacters()&&p.showResults()},blur:function(e){var t=s.activeElement===this;t||p.resultsClicked||(p.cancel.query(),p.remove.focus(),p.timer=setTimeout(p.hideResults,g.hideDelay))},result:{mousedown:function(){p.resultsClicked=!0},mouseup:function(){p.resultsClicked=!1},click:function(s){p.debug("Search result selected");var n=e(this),r=n.find(b.title).eq(0),a=n.find("a[href]").eq(0),i=a.attr("href")||!1,o=a.attr("target")||!1,c=(r.html(),r.length>0?r.text():!1),u=p.get.results(),l=p.get.result(c,u);return e.isFunction(g.onSelect)&&g.onSelect.call(q,l,u)===!1?void p.debug("Custom onSelect callback cancelled default select action"):(p.hideResults(),c&&p.set.value(c),void(i&&(p.verbose("Opening search link found in result",a),"_blank"==o||s.ctrlKey?t.open(i):t.location.href=i)))}}},handleKeyboard:function(e){var t,s=x.find(b.result),n=x.find(b.category),r=s.index(s.filter("."+h.active)),a=s.length,i=e.which,o={backspace:8,enter:13,escape:27,upArrow:38,downArrow:40};if(i==o.escape&&(p.verbose("Escape key pressed, blurring search field"),A.trigger("blur")),p.is.visible())if(i==o.enter){if(p.verbose("Enter key pressed, selecting active result"),s.filter("."+h.active).length>0)return p.event.result.click.call(s.filter("."+h.active),e),e.preventDefault(),!1}else i==o.upArrow?(p.verbose("Up key pressed, changing active result"),t=0>r-1?r:r-1,n.removeClass(h.active),s.removeClass(h.active).eq(t).addClass(h.active).closest(n).addClass(h.active),e.preventDefault()):i==o.downArrow&&(p.verbose("Down key pressed, changing active result"),t=r+1>=a?r:r+1,n.removeClass(h.active),s.removeClass(h.active).eq(t).addClass(h.active).closest(n).addClass(h.active),e.preventDefault());else i==o.enter&&(p.verbose("Enter key pressed, executing query"),p.query(),p.set.buttonPressed(),A.one("keyup",p.remove.buttonFocus))},setup:{api:function(){var e={on:!1,action:"search",onFailure:p.error};p.verbose("First request, initializing API"),x.api(e)}},can:{useAPI:function(){return e.fn.api!==n},transition:function(){return g.transition&&e.fn.transition!==n&&x.transition("is supported")}},is:{empty:function(){return""===S.html()},visible:function(){return S.filter(":visible").length>0},focused:function(){return A.filter(":focus").length>0}},get:{value:function(){return A.val()},results:function(){var e=x.data(m.results);return e},result:function(t,s){var n=!1;return t=t||p.get.value(),s=s||p.get.results(),"category"===g.type?(p.debug("Finding result that matches",t),e.each(s,function(s,r){return e.isArray(r.results)&&(n=p.search.object(t,r.results)[0],n&&n.length>0)?!0:void 0})):(p.debug("Finding result in results object",t),n=p.search.object(t,s)[0]),n}},set:{focus:function(){x.addClass(h.focus)},loading:function(){x.addClass(h.loading)},value:function(e){p.verbose("Setting search input value",e),A.val(e),p.query()},buttonPressed:function(){k.addClass(h