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

10 years ago
10 years ago
  1. /*
  2. * # Semantic UI
  3. * git://github.com/Semantic-Org/Semantic-UI.git#1.0
  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,r){"use strict";e.fn.search=function(s){var i,n=e(this),a=n.selector||"",o=(new Date).getTime(),c=[],l=arguments[0],u="string"==typeof l,d=[].slice.call(arguments,1);return e(this).each(function(){var p,f=e.extend(!0,{},e.fn.search.settings,s),g=f.className,h=f.selector,m=f.error,v=f.namespace,y="."+v,b=v+"-module",w=e(this),x=w.find(h.prompt),C=w.find(h.searchButton),A=w.find(h.results),R=(w.find(h.result),w.find(h.category),this),T=w.data(b);p={initialize:function(){p.verbose("Initializing module");var e=x[0],t=e!==r&&e.oninput!==r?"input":e!==r&&e.onpropertychange!==r?"propertychange":"keyup";x.on("focus"+y,p.event.focus).on("blur"+y,p.event.blur).on("keydown"+y,p.handleKeyboard),f.automatic&&x.on(t+y,p.search.throttle),C.on("click"+y,p.search.query),A.on("click"+y,h.result,p.results.select),p.instantiate()},instantiate:function(){p.verbose("Storing instance of module",p),T=p,w.data(b,p)},destroy:function(){p.verbose("Destroying instance"),w.removeData(b),x.off(y),C.off(y),A.off(y)},event:{focus:function(){w.addClass(g.focus),clearTimeout(p.timer),p.search.throttle(),p.results.show()},blur:function(){p.search.cancel(),w.removeClass(g.focus),p.timer=setTimeout(p.results.hide,f.hideDelay)}},handleKeyboard:function(t){var s,r=w.find(h.result),i=w.find(h.category),n=t.which,a={backspace:8,enter:13,escape:27,upArrow:38,downArrow:40},o=g.active,c=r.index(r.filter("."+o)),l=r.size();if(n==a.escape&&(p.verbose("Escape key pressed, blurring search field"),x.trigger("blur")),A.filter(":visible").size()>0)if(n==a.enter){if(p.verbose("Enter key pressed, selecting active result"),r.filter("."+o).size()>0)return e.proxy(p.results.select,r.filter("."+o))(t),t.preventDefault(),!1}else n==a.upArrow?(p.verbose("Up key pressed, changing active result"),s=0>c-1?c:c-1,i.removeClass(o),r.removeClass(o).eq(s).addClass(o).closest(i).addClass(o),t.preventDefault()):n==a.downArrow&&(p.verbose("Down key pressed, changing active result"),s=c+1>=l?c:c+1,i.removeClass(o),r.removeClass(o).eq(s).addClass(o).closest(i).addClass(o),t.preventDefault());else n==a.enter&&(p.verbose("Enter key pressed, executing query"),p.search.query(),C.addClass(g.down),x.one("keyup",function(){C.removeClass(g.down)}))},search:{cancel:function(){var e=w.data("xhr")||!1;e&&"resolved"!=e.state()&&(p.debug("Cancelling last search"),e.abort())},throttle:function(){var e=x.val(),t=e.length;clearTimeout(p.timer),t>=f.minCharacters?p.timer=setTimeout(p.search.query,f.searchThrottle):p.results.hide()},query:function(){var t=x.val(),s=p.search.cache.read(t);s?(p.debug("Reading result for '"+t+"' from cache"),p.results.add(s)):(p.debug("Querying for '"+t+"'"),e.isPlainObject(f.source)||e.isArray(f.source)?p.search.local(t):f.apiSettings?p.search.remote(t):e.api!==r&&e.api.settings.api.search!==r?(p.debug("Searching with default search API endpoint"),f.apiSettings={action:"search"},p.search.remote(t)):p.error(m.source),e.proxy(f.onSearchQuery,w)(t))},local:function(t){var s,r=[],i=[],n=e.isArray(f.searchFields)?f.searchFields:[f.searchFields],a=new RegExp("(?:s|^)"+t,"i"),o=new RegExp(t,"i");w.addClass(g.loading),e.each(n,function(t,s){e.each(f.source,function(t,n){var c="string"==typeof n[s],l=-1==e.inArray(n,r)&&-1==e.inArray(n,i);c&&l&&(a.test(n[s])?r.push(n):o.test(n[s])&&i.push(n))})}),s=p.results.generate({results:e.merge(r,i)}),w.removeClass(g.loading),p.search.cache.write(t,s),p.results.add(s)},remote:function(t){var s,r={stateContext:w,urlData:{query:t},onSuccess:function(e){s=p.results.generate(e),p.search.cache.write(t,s),p.results.add(s)},failure:p.error};p.search.cancel(),p.debug("Executing search"),e.extend(!0,r,f.apiSettings),e.api(r)},cache:{read:function(e){var t=w.data("cache");return f.cache&&"object"==typeof t&&t[e]!==r?t[e]:!1},write:function(e,t){var s=w.data("cache")!==r?w.data("cache"):{};s[e]=t,w.data("cache",s)}}},results:{generate:function(t){p.debug("Generating html from response",t);var s=f.templates[f.type],r="";return e.isPlainObject(t.results)&&!e.isEmptyObject(t.results)||e.isArray(t.results)&&t.results.length>0?(f.maxResults>0&&(t