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
  1. /*
  2. * # Semantic UI
  3. * https://github.com/Semantic-Org/Semantic-UI
  4. * http://beta.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,r){"use strict";e.fn.search=function(s){var n,i=e(this),a=i.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),m=f.className,h=f.selector,g=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";f.automatic&&x.on(t+y,p.search.throttle),x.on("focus"+y,p.event.focus).on("blur"+y,p.event.blur).on("keydown"+y,p.handleKeyboard),C.on("click"+y,p.search.query),A.on("mousedown"+y,p.event.mousedown).on("mouseup"+y,p.event.mouseup).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(m.focus),clearTimeout(p.timer),p.search.throttle(),p.has.minimum()&&p.results.show()},mousedown:function(){p.resultsClicked=!0},mouseup:function(){p.resultsClicked=!1},blur:function(){p.search.cancel(),w.removeClass(m.focus),p.resultsClicked||(p.timer=setTimeout(p.results.hide,f.hideDelay))}},handleKeyboard:function(t){var s,r=w.find(h.result),n=w.find(h.category),i=t.which,a={backspace:8,enter:13,escape:27,upArrow:38,downArrow:40},o=m.active,c=r.index(r.filter("."+o)),l=r.size();if(i==a.escape&&(p.verbose("Escape key pressed, blurring search field"),x.trigger("blur")),A.filter(":visible").size()>0)if(i==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 i==a.upArrow?(p.verbose("Up key pressed, changing active result"),s=0>c-1?c:c-1,n.removeClass(o),r.removeClass(o).eq(s).addClass(o).closest(n).addClass(o),t.preventDefault()):i==a.downArrow&&(p.verbose("Down key pressed, changing active result"),s=c+1>=l?c:c+1,n.removeClass(o),r.removeClass(o).eq(s).addClass(o).closest(n).addClass(o),t.preventDefault());else i==a.enter&&(p.verbose("Enter key pressed, executing query"),p.search.query(),C.addClass(m.down),x.one("keyup",function(){C.removeClass(m.down)}))},has:{minimum:function(){var e=x.val(),t=e.length;return t>=f.minCharacters}},search:{cancel:function(){var e=w.data("xhr")||!1;e&&"resolved"!=e.state()&&(p.debug("Cancelling last search"),e.abort())},throttle:function(){clearTimeout(p.timer),p.has.minimum()?p.timer=setTimeout(p.search.query,f.searchDelay):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(g.source),e.proxy(f.onSearchQuery,w)(t))},local:function(t){var s,r=[],n=[],i=e.isArray(f.searchFields)?f.searchFields:[f.searchFields],a=new RegExp("(?:s|^)"+t,"i"),o=new RegExp(t,"i");w.addClass(m.loading),e.each(i,function(t,s){e.each(f.source,function(t,i){var c="string"==typeof i[s],l=-1==e.inArray(i,r)&&-1==e.inArray(i,n);c&&l&&(a.test(i[s])?r.push(i):f.searchFullText&&o.test(i[s])&&n.push(i))})}),s=p.results.generate({results:e.merge(r,n)}),w.removeClass(m.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)},onFailure: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