|
|
/* * # Semantic UI * https://github.com/Semantic-Org/Semantic-UI
* http://www.semantic-ui.com/
* * Copyright 2014 Contributors * Released under the MIT license * http://opensource.org/licenses/MIT
* */ !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 f,p=e.extend(!0,{},e.fn.search.settings,s),m=p.className,h=p.selector,g=p.error,v=p.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);f={initialize:function(){f.verbose("Initializing module");var e=x[0],t=e!==r&&e.oninput!==r?"input":e!==r&&e.onpropertychange!==r?"propertychange":"keyup";p.automatic&&x.on(t+y,f.search.throttle),x.on("focus"+y,f.event.focus).on("blur"+y,f.event.blur).on("keydown"+y,f.handleKeyboard),C.on("click"+y,f.search.query),A.on("mousedown"+y,f.event.mousedown).on("mouseup"+y,f.event.mouseup).on("click"+y,h.result,f.results.select),f.instantiate()},instantiate:function(){f.verbose("Storing instance of module",f),T=f,w.data(b,f)},destroy:function(){f.verbose("Destroying instance"),w.removeData(b),x.off(y),C.off(y),A.off(y)},event:{focus:function(){w.addClass(m.focus),clearTimeout(f.timer),f.search.throttle(),f.has.minimum()&&f.results.show()},mousedown:function(){f.resultsClicked=!0},mouseup:function(){f.resultsClicked=!1},blur:function(){f.search.cancel(),w.removeClass(m.focus),f.resultsClicked||(f.timer=setTimeout(f.results.hide,p.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&&(f.verbose("Escape key pressed, blurring search field"),x.trigger("blur")),A.filter(":visible").size()>0)if(i==a.enter){if(f.verbose("Enter key pressed, selecting active result"),r.filter("."+o).size()>0)return e.proxy(f.results.select,r.filter("."+o))(t),t.preventDefault(),!1}else i==a.upArrow?(f.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&&(f.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&&(f.verbose("Enter key pressed, executing query"),f.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>=p.minCharacters}},search:{cancel:function(){var e=w.data("xhr")||!1;e&&"resolved"!=e.state()&&(f.debug("Cancelling last search"),e.abort())},throttle:function(){clearTimeout(f.timer),f.has.minimum()?f.timer=setTimeout(f.search.query,p.searchDelay):f.results.hide()},query:function(){var t=x.val(),s=f.search.cache.read(t);s?(f.debug("Reading result for '"+t+"' from cache"),f.results.add(s)):(f.debug("Querying for '"+t+"'"),e.isPlainObject(p.source)||e.isArray(p.source)?f.search.local(t):p.apiSettings?f.search.remote(t):e.fn.api!==r&&e.api.settings.api.search!==r?(f.debug("Searching with default search API endpoint"),p.apiSettings={action:"search"},f.search.remote(t)):f.error(g.source),e.proxy(p.onSearchQuery,w)(t))},local:function(t){var s,r=[],n=[],i=e.isArray(p.searchFields)?p.searchFields:[p.searchFields],a=new RegExp("(?:s|^)"+t,"i"),o=new RegExp(t,"i");w.addClass(m.loading),e.each(i,function(t,s){e.each(p.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):p.searchFullText&&o.test(i[s])&&n.push(i))})}),s=f.results.generate({results:e.merge(r,n)}),w.removeClass(m.loading),f.search.cache.write(t,s),f.results.add(s)},remote:function(t){var s,r={stateContext:w,urlData:{query:t},onSuccess:function(e){s=f.results.generate(e),f.search.cache.write(t,s),f.results.add(s)},onFailure:f.error};f.search.cancel(),f.debug("Executing search"),e.extend(!0,r,p.apiSettings),e.api(r)},cache:{read:function(e){var t=w.data("cache");return p.cache&&"object"==typeof t&&t[e]!==r?t[e]:!1},write:function(e,t){var s=w.data("cache")!==r?w.data("cache"):{};
|