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

9 years ago
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
  1. /*!
  2. * # Semantic UI 2.0.0 - Search
  3. * http://github.com/semantic-org/semantic-ui/
  4. *
  5. *
  6. * Copyright 2015 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 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,v=e.isPlainObject(r)?e.extend(!0,{},e.fn.search.settings,r):e.extend({},e.fn.search.settings),p=v.className,h=v.metadata,m=v.regExp,b=v.selector,y=v.error,R=v.namespace,w="."+R,C=R+"-module",x=e(this),q=x.find(b.prompt),j=x.find(b.searchButton),k=x.find(b.results),A=(x.find(b.result),x.find(b.category),this),F=x.data(C);g={initialize:function(){g.verbose("Initializing module"),g.determine.searchFields(),g.bind.events(),g.instantiate()},instantiate:function(){g.verbose("Storing instance of module",g),F=g,x.data(C,g)},destroy:function(){g.verbose("Destroying instance"),x.off(w).removeData(C)},bind:{events:function(){g.verbose("Binding events to search"),v.automatic&&(x.on(g.get.inputEvent()+w,b.prompt,g.throttle),q.attr("autocomplete","off")),x.on("focus"+w,b.prompt,g.event.focus).on("blur"+w,b.prompt,g.event.blur).on("keydown"+w,b.prompt,g.handleKeyboard).on("click"+w,b.searchButton,g.query).on("mousedown"+w,b.results,g.event.result.mousedown).on("mouseup"+w,b.results,g.event.result.mouseup).on("click"+w,b.result,g.event.result.click)}},determine:{searchFields:function(){r&&r.searchFields!==n&&(v.searchFields=r.searchFields)}},event:{focus:function(){g.set.focus(),g.has.minimumCharacters()&&(g.query(),g.showResults())},blur:function(e){var t=s.activeElement===this;t||g.resultsClicked||(g.cancel.query(),g.remove.focus(),g.timer=setTimeout(g.hideResults,v.hideDelay))},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(b.title).eq(0),i=n.find("a[href]").eq(0),a=i.attr("href")||!1,o=i.attr("target")||!1,c=(r.html(),r.length>0?r.text():!1),u=g.get.results(),l=n.data(h.result)||g.get.result(c,u);return e.isFunction(v.onSelect)&&v.onSelect.call(A,l,u)===!1?void g.debug("Custom onSelect callback cancelled default select action"):(g.hideResults(),c&&g.set.value(c),void(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=x.find(b.result),n=x.find(b.category),r=s.index(s.filter("."+p.active)),i=s.length,a=e.which,o={backspace:8,enter:13,escape:27,upArrow:38,downArrow:40};if(a==o.escape&&(g.verbose("Escape key pressed, blurring search field"),q.trigger("blur")),g.is.visible())if(a==o.enter){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 a==o.upArrow?(g.verbose("Up key pressed, changing active result"),t=0>r-1?r:r-1,n.removeClass(p.active),s.removeClass(p.active).eq(t).addClass(p.active).closest(n).addClass(p.active),e.preventDefault()):a==o.downArrow&&(g.verbose("Down key pressed, changing active result"),t=r+1>=i?r:r+1,n.removeClass(p.active),s.removeClass(p.active).eq(t).addClass(p.active).closest(n).addClass(p.active),e.preventDefault());else a==o.enter&&(g.verbose("Enter key pressed, executing query"),g.query(),g.set.buttonPressed(),q.one("keyup",g.remove.buttonFocus))},setup:{api:function(){var e={debug:v.debug,on:!1,action:"search",onFailure:g.error};g.verbose("First request, initializing API"),x.api(e)}},can:{useAPI:function(){return e.fn.api!==n},transition:function(){return v.transition&&e.fn.transition!==n&&x.transition("is supported")}},is:{empty:function(){return""===k.html()},visible:function(){return k.filter(":visible").length>0},focused:function(){return q.filter(":focus").length>0}},get:{inputEvent:function(){var e=q[0],t=e!==n&&e.oninput!==n?"input":e!==n&&e.onpropertychange!==n?"propertychange":"keyup";return t},value:function(){return q.val()},results:function(){var e=x.data(h.results);return e},result:function(t,s){var r=["title","id"],i=!1;return t=t!==n?t:g.get.value(),s=s!==n?s:g.get.results(),"category"===v.type?(g.debug("Finding result that matches",t),e.each(