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

10 years ago
10 years ago
10 years ago
9 years ago
  1. /*
  2. * # Semantic UI - 1.7.0
  3. * https://github.com/Semantic-Org/Semantic-UI
  4. * http://www.semantic-ui.com/
  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(s){var r,a=e(this),i=a.selector||"",o=(new Date).getTime(),l=[],c=arguments[0],u="string"==typeof c,d=[].slice.call(arguments,1);return e(this).each(function(){var f,m=e.extend(!0,{},e.fn.search.settings,s),p=m.className,h=m.selector,g=m.error,v=m.namespace,y="."+v,b=v+"-module",w=e(this),R=w.find(h.prompt),C=w.find(h.searchButton),x=w.find(h.results),k=(w.find(h.result),w.find(h.category),this),A=w.data(b);f={initialize:function(){f.verbose("Initializing module");var e=R[0],t=e!==n&&e.oninput!==n?"input":e!==n&&e.onpropertychange!==n?"propertychange":"keyup";m.automatic&&R.on(t+y,f.throttle),R.on("focus"+y,f.event.focus).on("blur"+y,f.event.blur).on("keydown"+y,f.handleKeyboard),C.on("click"+y,f.query),x.on("mousedown"+y,f.event.result.mousedown).on("mouseup"+y,f.event.result.mouseup).on("click"+y,h.result,f.event.result.click),f.instantiate()},instantiate:function(){f.verbose("Storing instance of module",f),A=f,w.data(b,f)},destroy:function(){f.verbose("Destroying instance"),w.removeData(b),R.off(y),C.off(y),x.off(y)},event:{focus:function(){w.addClass(p.focus),clearTimeout(f.timer),f.throttle(),f.has.minimumCharacters()&&f.showResults()},blur:function(){f.cancel(),w.removeClass(p.focus),f.resultsClicked||(f.timer=setTimeout(f.hideResults,m.hideDelay))},result:{mousedown:function(){f.resultsClicked=!0},mouseup:function(){f.resultsClicked=!1},click:function(s){f.debug("Search result selected");{var n=e(this),r=n.find(".title");r.html()}if("default"==m.onSelect||"default"==m.onSelect.call(this,s)){var a=n.find("a[href]").eq(0),r=n.find(h.title).eq(0),i=a.attr("href")||!1,o=a.attr("target")||!1,l=r.length>0?r.text():!1;f.hideResults(),l&&R.val(l),i&&("_blank"==o||s.ctrlKey?t.open(i):t.location.href=i)}}}},handleKeyboard:function(e){var t,s=w.find(h.result),n=w.find(h.category),r=e.which,a={backspace:8,enter:13,escape:27,upArrow:38,downArrow:40},i=p.active,o=s.index(s.filter("."+i)),l=s.length;if(r==a.escape&&(f.verbose("Escape key pressed, blurring search field"),R.trigger("blur")),x.filter(":visible").length>0)if(r==a.enter){if(f.verbose("Enter key pressed, selecting active result"),s.filter("."+i).length>0)return f.event.result.click.call(s.filter("."+i),e),e.preventDefault(),!1}else r==a.upArrow?(f.verbose("Up key pressed, changing active result"),t=0>o-1?o:o-1,n.removeClass(i),s.removeClass(i).eq(t).addClass(i).closest(n).addClass(i),e.preventDefault()):r==a.downArrow&&(f.verbose("Down key pressed, changing active result"),t=o+1>=l?o:o+1,n.removeClass(i),s.removeClass(i).eq(t).addClass(i).closest(n).addClass(i),e.preventDefault());else r==a.enter&&(f.verbose("Enter key pressed, executing query"),f.query(),C.addClass(p.down),R.one("keyup",function(){C.removeClass(p.down)}))},query:function(){var t=R.val(),s=f.read.cache(t);s?(f.debug("Reading result for "+t+" from cache"),f.addResults(s)):(f.debug("Querying for "+t),e.isPlainObject(m.source)||e.isArray(m.source)?f.search.local(t):m.apiSettings?f.search.remote(t):e.fn.api!==n&&e.api.settings.api.search!==n?(f.debug("Searching with default search API endpoint"),m.apiSettings={action:"search"},f.search.remote(t)):f.error(g.source),m.onSearchQuery.call(k,t))},search:{local:function(t){var s,n=[],r=[],a=e.isArray(m.searchFields)?m.searchFields:[m.searchFields],i=new RegExp("(?:s|^)"+t,"i"),o=new RegExp(t,"i");w.addClass(p.loading),e.each(a,function(t,s){e.each(m.source,function(t,a){var l="string"==typeof a[s],c=-1==e.inArray(a,n)&&-1==e.inArray(a,r);l&&c&&(a[s].match(i)?n.push(a):m.searchFullText&&a[s].match(o)&&r.push(a))})}),s=f.generateResults({results:e.merge(n,r)}),w.removeClass(p.loading),f.write.cache(t,s),f.addResults(s)},remote:function(t){var s,n={stateContext:w,urlData:{query:t},onSuccess:function(e){s=f.generateResults(e),f.write.cache(t,s),f.addResults(s)},onFailure:f.error};f.cancel(),f.debug("Executing search"),e.extend(!0,n,m.apiSettings),e.api(n)}},throttle:function(){clearTimeout(f.timer),f.has.minimumCharacters()?f.timer=setTimeout(f.query,m.searchDelay):f.hideResults()},cancel:function(){var e=w.data