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

9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
  1. /*!
  2. * # Semantic UI 2.1.6 - 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,r){"use strict";e.fn.search=function(n){var a,i=e(this),c=i.selector||"",o=(new Date).getTime(),u=[],l=arguments[0],d="string"==typeof l,g=[].slice.call(arguments,1);return e(this).each(function(){var f,p=e.isPlainObject(n)?e.extend(!0,{},e.fn.search.settings,n):e.extend({},e.fn.search.settings),v=p.className,h=p.metadata,m=p.regExp,b=p.fields,y=p.selector,R=p.error,C=p.namespace,w="."+C,x=C+"-module",j=e(this),q=j.find(y.prompt),E=j.find(y.searchButton),k=j.find(y.results),A=(j.find(y.result),j.find(y.category),this),T=j.data(x);f={initialize:function(){f.verbose("Initializing module"),f.determine.searchFields(),f.bind.events(),f.set.type(),f.create.results(),f.instantiate()},instantiate:function(){f.verbose("Storing instance of module",f),T=f,j.data(x,f)},destroy:function(){f.verbose("Destroying instance"),j.off(w).removeData(x)},bind:{events:function(){f.verbose("Binding events to search"),p.automatic&&(j.on(f.get.inputEvent()+w,y.prompt,f.event.input),q.attr("autocomplete","off")),j.on("focus"+w,y.prompt,f.event.focus).on("blur"+w,y.prompt,f.event.blur).on("keydown"+w,y.prompt,f.handleKeyboard).on("click"+w,y.searchButton,f.query).on("mousedown"+w,y.results,f.event.result.mousedown).on("mouseup"+w,y.results,f.event.result.mouseup).on("click"+w,y.result,f.event.result.click)}},determine:{searchFields:function(){n&&n.searchFields!==r&&(p.searchFields=n.searchFields)}},event:{input:function(){clearTimeout(f.timer),f.timer=setTimeout(f.query,p.searchDelay)},focus:function(){f.set.focus(),f.has.minimumCharacters()&&(f.query(),f.can.show()&&f.showResults())},blur:function(e){var t=s.activeElement===this;t||f.resultsClicked||(f.cancel.query(),f.remove.focus(),f.timer=setTimeout(f.hideResults,p.hideDelay))},result:{mousedown:function(){f.resultsClicked=!0},mouseup:function(){f.resultsClicked=!1},click:function(s){f.debug("Search result selected");var r=e(this),n=r.find(y.title).eq(0),a=r.find("a[href]").eq(0),i=a.attr("href")||!1,c=a.attr("target")||!1,o=(n.html(),n.length>0?n.text():!1),u=f.get.results(),l=r.data(h.result)||f.get.result(o,u);return e.isFunction(p.onSelect)&&p.onSelect.call(A,l,u)===!1?void f.debug("Custom onSelect callback cancelled default select action"):(f.hideResults(),o&&f.set.value(o),void(i&&(f.verbose("Opening search link found in result",a),"_blank"==c||s.ctrlKey?t.open(i):t.location.href=i)))}}},handleKeyboard:function(e){var t,s=j.find(y.result),r=j.find(y.category),n=s.index(s.filter("."+v.active)),a=s.length,i=e.which,c={backspace:8,enter:13,escape:27,upArrow:38,downArrow:40};if(i==c.escape&&(f.verbose("Escape key pressed, blurring search field"),f.trigger.blur()),f.is.visible())if(i==c.enter){if(f.verbose("Enter key pressed, selecting active result"),s.filter("."+v.active).length>0)return f.event.result.click.call(s.filter("."+v.active),e),e.preventDefault(),!1}else i==c.upArrow?(f.verbose("Up key pressed, changing active result"),t=0>n-1?n:n-1,r.removeClass(v.active),s.removeClass(v.active).eq(t).addClass(v.active).closest(r).addClass(v.active),e.preventDefault()):i==c.downArrow&&(f.verbose("Down key pressed, changing active result"),t=n+1>=a?n:n+1,r.removeClass(v.active),s.removeClass(v.active).eq(t).addClass(v.active).closest(r).addClass(v.active),e.preventDefault());else i==c.enter&&(f.verbose("Enter key pressed, executing query"),f.query(),f.set.buttonPressed(),q.one("keyup",f.remove.buttonFocus))},setup:{api:function(){var e={debug:p.debug,on:!1,cache:"local",action:"search",onError:f.error};f.verbose("First request, initializing API"),j.api(e)}},can:{useAPI:function(){return e.fn.api!==r},show:function(){return f.is.focused()&&!f.is.visible()&&!f.is.empty()},transition:function(){return p.transition&&e.fn.transition!==r&&j.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}},trigger:{blur:function(){var e=s.createEvent("HTMLEvents"),t=q[0];t&&(f.verbose("Triggering native blur event"),e.initEvent("blur",!1,!1),t.dispatchEvent(e))}},get:{inpu