/*! * # Semantic UI 2.0.0 - Search * http://github.com/semantic-org/semantic-ui/ * * * Copyright 2014 Contributors * Released under the MIT license * http://opensource.org/licenses/MIT * */ !function(e,t,s,r){"use strict";e.fn.search=function(n){var a,i=e(this),o=i.selector||"",c=(new Date).getTime(),u=[],l=arguments[0],d="string"==typeof l,f=[].slice.call(arguments,1);return e(this).each(function(){var g,p=e.extend(!0,{},e.fn.search.settings,n),m=p.className,h=p.metadata,v=p.regExp,b=p.selector,y=p.error,R=p.namespace,w="."+R,C=R+"-module",x=e(this),k=x.find(b.prompt),A=x.find(b.searchButton),q=x.find(b.results),S=(x.find(b.result),x.find(b.category),this),E=x.data(C);g={initialize:function(){g.verbose("Initializing module");var e=k[0],t=e!==r&&e.oninput!==r?"input":e!==r&&e.onpropertychange!==r?"propertychange":"keyup";p.automatic&&(x.on(t+w,b.prompt,g.throttle),k.attr("autocomplete","off")),x.on("focus"+w,b.prompt,g.event.focus).on("blur"+w,b.prompt,g.event.blur).on("keydown"+w,b.prompt,g.handleKeyboard).on("click"+w,b.searchButton,g.query).on("mousedown"+w,b.results,g.event.result.mousedown).on("mouseup"+w,b.results,g.event.result.mouseup).on("click"+w,b.result,g.event.result.click),g.instantiate()},instantiate:function(){g.verbose("Storing instance of module",g),E=g,x.data(C,g)},destroy:function(){g.verbose("Destroying instance"),x.off(w).removeData(C)},event:{focus:function(){g.set.focus(),g.has.minimumCharacters()&&g.showResults()},blur:function(){var e=s.activeElement===this;e||g.resultsClicked||(g.cancel.query(),g.remove.focus(),g.timer=setTimeout(g.hideResults,p.hideDelay))},result:{mousedown:function(){g.resultsClicked=!0},mouseup:function(){g.resultsClicked=!1},click:function(s){g.debug("Search result selected");var r=e(this),n=r.find(b.title).eq(0),a=r.find("a[href]").eq(0),i=a.attr("href")||!1,o=a.attr("target")||!1,c=(n.html(),n.length>0?n.text():!1),u=g.get.results(),l=g.get.result(c,u);return e.isFunction(p.onSelect)&&p.onSelect.call(S,l,u)===!1?void g.debug("Custom onSelect callback cancelled default select action"):(g.hideResults(),c&&g.set.value(c),void(i&&(g.verbose("Opening search link found in result",a),"_blank"==o||s.ctrlKey?t.open(i):t.location.href=i)))}}},handleKeyboard:function(e){var t,s=x.find(b.result),r=x.find(b.category),n=s.index(s.filter("."+m.active)),a=s.length,i=e.which,o={backspace:8,enter:13,escape:27,upArrow:38,downArrow:40};if(i==o.escape&&(g.verbose("Escape key pressed, blurring search field"),k.trigger("blur")),g.is.visible())if(i==o.enter){if(g.verbose("Enter key pressed, selecting active result"),s.filter("."+m.active).length>0)return g.event.result.click.call(s.filter("."+m.active),e),e.preventDefault(),!1}else i==o.upArrow?(g.verbose("Up key pressed, changing active result"),t=0>n-1?n:n-1,r.removeClass(m.active),s.removeClass(m.active).eq(t).addClass(m.active).closest(r).addClass(m.active),e.preventDefault()):i==o.downArrow&&(g.verbose("Down key pressed, changing active result"),t=n+1>=a?n:n+1,r.removeClass(m.active),s.removeClass(m.active).eq(t).addClass(m.active).closest(r).addClass(m.active),e.preventDefault());else i==o.enter&&(g.verbose("Enter key pressed, executing query"),g.query(),g.set.buttonPressed(),k.one("keyup",g.remove.buttonFocus))},setup:{api:function(){var e={debug:p.debug,on:!1,action:"search",onFailure:g.error};g.verbose("First request, initializing API"),x.api(e)}},can:{useAPI:function(){return e.fn.api!==r},transition:function(){return p.transition&&e.fn.transition!==r&&x.transition("is supported")}},is:{empty:function(){return""===q.html()},visible:function(){return q.filter(":visible").length>0},focused:function(){return k.filter(":focus").length>0}},get:{value:function(){return k.val()},results:function(){var e=x.data(h.results);return e},result:function(t,s){var r=!1;return t=t||g.get.value(),s=s||g.get.results(),"category"===p.type?(g.debug("Finding result that matches",t),e.each(s,function(s,n){return e.isArray(n.results)&&(r=g.search.object(t,n.results,!0)[0])?!1:void 0})):(g.debug("Finding result in results object",t),r=g.search.object(t,s,!0)[0]),r}},set:{focus:function(){x.addClass(m.focus)},loading:function(){x.addClass(m.loading)},value:function(e){g.verbose("Setting search input value",e),k.val(e),g.query()},buttonPressed:function(){A.addClass(m.pressed)}},remove:{loading:function(){x.removeClass(m.loading)},focus:function(){x.removeClass(m.focus)},buttonPressed:function(){A.removeClass(m.pressed)}},query:function(){var t=g.get.value(),s=g.read.cache(t);s?(g.debug("Reading result for "+t+" from cache"),g.save.results(s.results),g.addResults(s.html)):(g.debug("Querying for "+t),e.isPlainObject(p.source)||e.isArray(p.source)?g.search.local(t):g.can.useAPI()?g.search.remote(t):g.error(y.source),p.onSearchQuery.call(S,t))},search:{local:function(e){var t,s=g.search.object(e,p.content);g.set.loading(),g.save.results(s),g.debug("Returned local search results",s),t=g.generateResults({results:s}),g.remove.loading(),g.write.cache(e,{html:t,results:s}),g.addResults(t)},remote:function(t){var s={onSuccess:function(e){g.parse.response.call(S,e,t)},urlData:{query:t}};x.api("get request")||g.setup.api(),e.extend(!0,s,p.apiSettings),g.debug("Executing search",s),g.cancel.query(),x.api("setting",s).api("query")},object:function(t,s,n){var a=[],i=[],o=t.replace(v.escape,"\\$&"),c=new RegExp(v.beginsWith+o,"i"),u=e.isArray(p.searchFields)?p.searchFields:[p.searchFields],l=function(t,s){var r=-1==e.inArray(s,a),n=-1==e.inArray(s,i);r&&n&&t.push(s)};return s=s||p.source,s===r?(g.error(y.source),[]):(e.each(u,function(r,o){e.each(s,function(e,s){var r="string"==typeof s[o];r&&(n?s[o]==t&&l(a,s):s[o].match(c)?l(a,s):p.searchFullText&&g.fuzzySearch(t,s[o])&&l(i,s))})}),e.merge(a,i))}},fuzzySearch:function(e,t){var s=t.length,r=e.length;if(e=e.toLowerCase(),t=t.toLowerCase(),r>s)return!1;if(r===s)return e===t;e:for(var n=0,a=0;r>n;n++){for(var i=e.charCodeAt(n);s>a;)if(t.charCodeAt(a++)===i)continue e;return!1}return!0},parse:{response:function(e,t){var s=g.generateResults(e);g.verbose("Parsing server response",e),e!==r&&t!==r&&e.results!==r&&(g.write.cache(t,{html:s,results:e.results}),g.save.results(e.results),g.addResults(s))}},throttle:function(){clearTimeout(g.timer),g.has.minimumCharacters()?g.timer=setTimeout(g.query,p.searchDelay):g.hideResults()},cancel:{query:function(){g.can.useAPI()&&x.api("abort")}},has:{minimumCharacters:function(){var e=g.get.value(),t=e.length;return t>=p.minCharacters}},clear:{cache:function(e){e?e&&cache&&cache[e]&&(g.debug("Removing value from cache",e),delete cache[e],x.data(h.cache,cache)):(g.debug("Clearing cache",e),x.removeData(h.cache))}},read:{cache:function(e){var t=x.data(h.cache);return p.cache?(g.verbose("Checking cache for generated html for query",e),"object"==typeof t&&t[e]!==r?t[e]:!1):!1}},save:{results:function(e){g.verbose("Saving current search results to metadata",e),x.data(h.results,e)}},write:{cache:function(e,t){var s=x.data(h.cache)!==r?x.data(h.cache):{};p.cache&&(g.verbose("Writing generated html to cache",e,t),s[e]=t,x.data(h.cache,s))}},addResults:function(t){return e.isFunction(p.onResultsAdd)&&p.onResultsAdd.call(q,t)===!1?(g.debug("onResultsAdd callback cancelled default action"),!1):(q.html(t),void g.showResults())},showResults:function(){g.is.visible()||!g.is.focused()||g.is.empty()||(g.can.transition()?(g.debug("Showing results with css animations"),q.transition({animation:p.transition+" in",debug:p.debug,verbose:p.verbose,duration:p.duration,queue:!0})):(g.debug("Showing results with javascript"),q.stop().fadeIn(p.duration,p.easing)),p.onResultsOpen.call(q))},hideResults:function(){g.is.visible()&&(g.can.transition()?(g.debug("Hiding results with css animations"),q.transition({animation:p.transition+" out",debug:p.debug,verbose:p.verbose,duration:p.duration,queue:!0})):(g.debug("Hiding results with javascript"),q.stop().fadeOut(p.duration,p.easing)),p.onResultsClose.call(q))},generateResults:function(t){g.debug("Generating html from response",t);var s=p.templates[p.type],r=e.isPlainObject(t.results)&&!e.isEmptyObject(t.results),n=e.isArray(t.results)&&t.results.length>0,a="";return r||n?(p.maxResults>0&&(r?"standard"==p.type&&g.error(y.maxResults):t.results=t.results.slice(0,p.maxResults)),e.isFunction(s)?a=s(t):g.error(y.noTemplate,!1)):a=g.displayMessage(y.noResults,"empty"),p.onResults.call(S,t),a},displayMessage:function(e,t){return t=t||"standard",g.debug("Displaying message",e,t),g.addResults(p.templates.message(e,t)),p.templates.message(e,t)},setting:function(t,s){if(e.isPlainObject(t))e.extend(!0,p,t);else{if(s===r)return p[t];p[t]=s}},internal:function(t,s){if(e.isPlainObject(t))e.extend(!0,g,t);else{if(s===r)return g[t];g[t]=s}},debug:function(){p.debug&&(p.performance?g.performance.log(arguments):(g.debug=Function.prototype.bind.call(console.info,console,p.name+":"),g.debug.apply(console,arguments)))},verbose:function(){p.verbose&&p.debug&&(p.performance?g.performance.log(arguments):(g.verbose=Function.prototype.bind.call(console.info,console,p.name+":"),g.verbose.apply(console,arguments)))},error:function(){g.error=Function.prototype.bind.call(console.error,console,p.name+":"),g.error.apply(console,arguments)},performance:{log:function(e){var t,s,r;p.performance&&(t=(new Date).getTime(),r=c||t,s=t-r,c=t,u.push({Name:e[0],Arguments:[].slice.call(e,1)||"",Element:S,"Execution Time":s})),clearTimeout(g.performance.timer),g.performance.timer=setTimeout(g.performance.display,500)},display:function(){var t=p.name+":",s=0;c=!1,clearTimeout(g.performance.timer),e.each(u,function(e,t){s+=t["Execution Time"]}),t+=" "+s+"ms",o&&(t+=" '"+o+"'"),i.length>1&&(t+=" ("+i.length+")"),(console.group!==r||console.table!==r)&&u.length>0&&(console.groupCollapsed(t),console.table?console.table(u):e.each(u,function(e,t){console.log(t.Name+": "+t["Execution Time"]+"ms")}),console.groupEnd()),u=[]}},invoke:function(t,s,n){var i,o,c,u=E;return s=s||f,n=S||n,"string"==typeof t&&u!==r&&(t=t.split(/[\. ]/),i=t.length-1,e.each(t,function(s,n){var a=s!=i?n+t[s+1].charAt(0).toUpperCase()+t[s+1].slice(1):t;if(e.isPlainObject(u[a])&&s!=i)u=u[a];else{if(u[a]!==r)return o=u[a],!1;if(!e.isPlainObject(u[n])||s==i)return u[n]!==r?(o=u[n],!1):!1;u=u[n]}})),e.isFunction(o)?c=o.apply(n,s):o!==r&&(c=o),e.isArray(a)?a.push(c):a!==r?a=[a,c]:c!==r&&(a=c),o}},d?(E===r&&g.initialize(),g.invoke(l)):(E!==r&&E.invoke("destroy"),g.initialize())}),a!==r?a:this},e.fn.search.settings={name:"Search Module",namespace:"search",debug:!1,verbose:!1,performance:!0,type:"standard",minCharacters:1,apiSettings:!1,source:!1,searchFields:["title","description"],searchFullText:!0,automatic:!0,hideDelay:0,searchDelay:200,maxResults:7,cache:!0,transition:"scale",duration:300,easing:"easeOutExpo",onSelect:!1,onResultsAdd:!1,onSearchQuery:function(){},onResults:function(){},onResultsOpen:function(){},onResultsClose:function(){},className:{active:"active",empty:"empty",focus:"focus",loading:"loading",pressed:"down"},error:{source:"Cannot search. No source used, and Semantic API module was not included",noResults:"Your search returned no results",logging:"Error in debug logging, exiting.",noEndpoint:"No search endpoint was specified",noTemplate:"A valid template name was not specified.",serverError:"There was an issue with querying the server.",maxResults:"Results must be an array to use maxResults setting",method:"The method you called is not defined."},metadata:{cache:"cache",results:"results"},regExp:{escape:/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,beginsWith:"(?:s|^)"},selector:{prompt:".prompt",searchButton:".search.button",results:".results",category:".category",result:".result",title:".title, .name"},templates:{escape:function(e){var t=/[&<>"'`]/g,s=/[&<>"'`]/,r={"&":"&","<":"<",">":">",'"':""","'":"'","`":"`"},n=function(e){return r[e]};return s.test(e)?e.replace(t,n):e},message:function(e,t){var s="";return e!==r&&t!==r&&(s+='
',s+="empty"==t?'
No Results
'+e+'
':'
'+e+"
",s+="
"),s},category:function(t){var s="",n=e.fn.search.settings.templates.escape;return t.results!==r?(e.each(t.results,function(t,a){a.results!==r&&a.results.length>0&&(s+='
'+a.name+"
",e.each(a.results,function(e,t){s+='
',t.url&&(s+=''),t.image!==r&&(t.image=n(t.image),s+='
'),s+='
',t.price!==r&&(t.price=n(t.price),s+='
'+t.price+"
"),t.title!==r&&(t.title=n(t.title),s+='
'+t.title+"
"),t.description!==r&&(s+='
'+t.description+"
"),s+="
"}),s+="
")}),t.action&&(s+=''+t.action.text+""),s):!1},standard:function(t){var s="";return t.results!==r?(e.each(t.results,function(e,t){s+=t.url?'':'',t.image!==r&&(s+='
'),s+='
',t.price!==r&&(s+='
'+t.price+"
"),t.title!==r&&(s+='
'+t.title+"
"),t.description!==r&&(s+='
'+t.description+"
"),s+="
",s+="
"}),t.action&&(s+=''+t.action.text+""),s):!1}}}}(jQuery,window,document);