/*! * # 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,n){"use strict";e.fn.search=function(r){var i,a=e(this),o=a.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,v=e.isPlainObject(r)?e.extend(!0,{},e.fn.search.settings,r):e.extend({},e.fn.search.settings),p=v.className,h=v.metadata,m=v.regExp,b=v.selector,y=v.error,R=v.namespace,w="."+R,C=R+"-module",x=e(this),q=x.find(b.prompt),j=x.find(b.searchButton),k=x.find(b.results),A=(x.find(b.result),x.find(b.category),this),F=x.data(C);g={initialize:function(){g.verbose("Initializing module"),g.determine.searchFields(),g.bind.events(),g.instantiate()},instantiate:function(){g.verbose("Storing instance of module",g),F=g,x.data(C,g)},destroy:function(){g.verbose("Destroying instance"),x.off(w).removeData(C)},bind:{events:function(){g.verbose("Binding events to search"),v.automatic&&(x.on(g.get.inputEvent()+w,b.prompt,g.throttle),q.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)}},determine:{searchFields:function(){r&&r.searchFields!==n&&(v.searchFields=r.searchFields)}},event:{focus:function(){g.set.focus(),g.has.minimumCharacters()&&(g.query(),g.showResults())},blur:function(){var e=s.activeElement===this;e||g.resultsClicked||(g.cancel.query(),g.remove.focus(),g.timer=setTimeout(g.hideResults,v.hideDelay))},result:{mousedown:function(){g.resultsClicked=!0},mouseup:function(){g.resultsClicked=!1},click:function(s){g.debug("Search result selected");var n=e(this),r=n.find(b.title).eq(0),i=n.find("a[href]").eq(0),a=i.attr("href")||!1,o=i.attr("target")||!1,c=(r.html(),r.length>0?r.text():!1),u=g.get.results(),l=n.data(h.result)||g.get.result(c,u);return e.isFunction(v.onSelect)&&v.onSelect.call(A,l,u)===!1?void g.debug("Custom onSelect callback cancelled default select action"):(g.hideResults(),c&&g.set.value(c),void(a&&(g.verbose("Opening search link found in result",i),"_blank"==o||s.ctrlKey?t.open(a):t.location.href=a)))}}},handleKeyboard:function(e){var t,s=x.find(b.result),n=x.find(b.category),r=s.index(s.filter("."+p.active)),i=s.length,a=e.which,o={backspace:8,enter:13,escape:27,upArrow:38,downArrow:40};if(a==o.escape&&(g.verbose("Escape key pressed, blurring search field"),q.trigger("blur")),g.is.visible())if(a==o.enter){if(g.verbose("Enter key pressed, selecting active result"),s.filter("."+p.active).length>0)return g.event.result.click.call(s.filter("."+p.active),e),e.preventDefault(),!1}else a==o.upArrow?(g.verbose("Up key pressed, changing active result"),t=0>r-1?r:r-1,n.removeClass(p.active),s.removeClass(p.active).eq(t).addClass(p.active).closest(n).addClass(p.active),e.preventDefault()):a==o.downArrow&&(g.verbose("Down key pressed, changing active result"),t=r+1>=i?r:r+1,n.removeClass(p.active),s.removeClass(p.active).eq(t).addClass(p.active).closest(n).addClass(p.active),e.preventDefault());else a==o.enter&&(g.verbose("Enter key pressed, executing query"),g.query(),g.set.buttonPressed(),q.one("keyup",g.remove.buttonFocus))},setup:{api:function(){var e={debug:v.debug,on:!1,action:"search",onFailure:g.error};g.verbose("First request, initializing API"),x.api(e)}},can:{useAPI:function(){return e.fn.api!==n},transition:function(){return v.transition&&e.fn.transition!==n&&x.transition("is supported")}},is:{empty:function(){return""===k.html()},visible:function(){return k.filter(":visible").length>0},focused:function(){return q.filter(":focus").length>0}},get:{inputEvent:function(){var e=q[0],t=e!==n&&e.oninput!==n?"input":e!==n&&e.onpropertychange!==n?"propertychange":"keyup";return t},value:function(){return q.val()},results:function(){var e=x.data(h.results);return e},result:function(t,s){var r=["title","id"],i=!1;return t=t!==n?t:g.get.value(),s=s!==n?s:g.get.results(),"category"===v.type?(g.debug("Finding result that matches",t),e.each(s,function(s,n){return e.isArray(n.results)&&(i=g.search.object(t,n.results,r)[0])?!1:void 0})):(g.debug("Finding result in results object",t),i=g.search.object(t,s,r)[0]),i||!1}},set:{focus:function(){x.addClass(p.focus)},loading:function(){x.addClass(p.loading)},value:function(e){g.verbose("Setting search input value",e),q.val(e)},buttonPressed:function(){j.addClass(p.pressed)}},remove:{loading:function(){x.removeClass(p.loading)},focus:function(){x.removeClass(p.focus)},buttonPressed:function(){j.removeClass(p.pressed)}},query:function(){var t=g.get.value(),s=g.read.cache(t);s?(g.debug("Reading result from cache",t),g.save.results(s.results),g.addResults(s.html),g.inject.id(s.results)):(g.debug("Querying for",t),e.isPlainObject(v.source)||e.isArray(v.source)?g.search.local(t):g.can.useAPI()?g.search.remote(t):g.error(y.source),v.onSearchQuery.call(A,t))},search:{local:function(e){var t,s=g.search.object(e,v.content);g.set.loading(),g.save.results(s),g.debug("Returned local search results",s),t=g.generateResults({results:s}),g.remove.loading(),g.addResults(t),g.inject.id(s),g.write.cache(e,{html:t,results:s})},remote:function(t){var s={onSuccess:function(e){g.parse.response.call(A,e,t)},urlData:{query:t}};x.api("get request")||g.setup.api(),e.extend(!0,s,v.apiSettings),g.debug("Executing search",s),g.cancel.query(),x.api("setting",s).api("query")},object:function(t,s,r){var i=[],a=[],o=t.toString().replace(m.escape,"\\$&"),c=new RegExp(m.beginsWith+o,"i"),r=r!==n?r:v.searchFields,u=function(t,s){var n=-1==e.inArray(s,i),r=-1==e.inArray(s,a);n&&r&&t.push(s)};return s=s||v.source,s===n?(g.error(y.source),[]):(e.isArray(r)||(r=[r]),e.each(r,function(n,r){e.each(s,function(e,s){var n="string"==typeof s[r];n&&(s[r].match(c)?u(i,s):v.searchFullText&&g.fuzzySearch(t,s[r])&&u(a,s))})}),e.merge(i,a))}},fuzzySearch:function(e,t){var s=t.length,n=e.length;if("string"!=typeof e)return!1;if(e=e.toLowerCase(),t=t.toLowerCase(),n>s)return!1;if(n===s)return e===t;e:for(var r=0,i=0;n>r;r++){for(var a=e.charCodeAt(r);s>i;)if(t.charCodeAt(i++)===a)continue e;return!1}return!0},parse:{response:function(e,t){var s=g.generateResults(e);g.verbose("Parsing server response",e),e!==n&&t!==n&&e.results!==n&&(g.addResults(s),g.inject.id(e.results),g.write.cache(t,{html:s,results:e.results}),g.save.results(e.results))}},throttle:function(){clearTimeout(g.timer),g.has.minimumCharacters()?g.timer=setTimeout(g.query,v.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>=v.minCharacters}},clear:{cache:function(e){var t=x.data(h.cache);e?e&&t&&t[e]&&(g.debug("Removing value from cache",e),delete t[e],x.data(h.cache,t)):(g.debug("Clearing cache",e),x.removeData(h.cache))}},read:{cache:function(e){var t=x.data(h.cache);return v.cache?(g.verbose("Checking cache for generated html for query",e),"object"==typeof t&&t[e]!==n?t[e]:!1):!1}},create:{id:function(e,t){var s=97,r=t!==n?String.fromCharCode(s+t):"",i=e,a=r+i;return g.verbose("Creating unique id",a),a}},inject:{result:function(e,t,s){g.verbose("Injecting result into results");var r=t-1,i=s-1,a=s!==n?k.children().eq(i).children().eq(r):k.children().eq(r);g.verbose("Injecting results metadata",a),a.data(h.result,e)},id:function(t){g.debug("Injecting unique ids into results");var s=1,r=1;return"category"===v.type?(r=1,e.each(t,function(t,i){e.each(i.results,function(e){var t=i.results[e];t.id===n&&(t.id=g.create.id(r,s)),g.inject.result(t,r,s),r++}),s++})):e.each(t,function(e){var s=t[e];s.id===n&&(s.id=g.create.id(r)),g.inject.result(s,r),r++}),t}},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)!==n?x.data(h.cache):{};v.cache&&(g.verbose("Writing generated html to cache",e,t),s[e]=t,x.data(h.cache,s))}},addResults:function(t){return e.isFunction(v.onResultsAdd)&&v.onResultsAdd.call(k,t)===!1?(g.debug("onResultsAdd callback cancelled default action"),!1):(k.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"),k.transition({animation:v.transition+" in",debug:v.debug,verbose:v.verbose,duration:v.duration,queue:!0})):(g.debug("Showing results with javascript"),k.stop().fadeIn(v.duration,v.easing)),v.onResultsOpen.call(k))},hideResults:function(){g.is.visible()&&(g.can.transition()?(g.debug("Hiding results with css animations"),k.transition({animation:v.transition+" out",debug:v.debug,verbose:v.verbose,duration:v.duration,queue:!0})):(g.debug("Hiding results with javascript"),k.stop().fadeOut(v.duration,v.easing)),v.onResultsClose.call(k))},generateResults:function(t){g.debug("Generating html from response",t);var s=v.templates[v.type],n=e.isPlainObject(t.results)&&!e.isEmptyObject(t.results),r=e.isArray(t.results)&&t.results.length>0,i="";return n||r?(v.maxResults>0&&(n?"standard"==v.type&&g.error(y.maxResults):t.results=t.results.slice(0,v.maxResults)),e.isFunction(s)?i=s(t):g.error(y.noTemplate,!1)):i=g.displayMessage(y.noResults,"empty"),v.onResults.call(A,t),i},displayMessage:function(e,t){return t=t||"standard",g.debug("Displaying message",e,t),g.addResults(v.templates.message(e,t)),v.templates.message(e,t)},setting:function(t,s){if(e.isPlainObject(t))e.extend(!0,v,t);else{if(s===n)return v[t];v[t]=s}},internal:function(t,s){if(e.isPlainObject(t))e.extend(!0,g,t);else{if(s===n)return g[t];g[t]=s}},debug:function(){v.debug&&(v.performance?g.performance.log(arguments):(g.debug=Function.prototype.bind.call(console.info,console,v.name+":"),g.debug.apply(console,arguments)))},verbose:function(){v.verbose&&v.debug&&(v.performance?g.performance.log(arguments):(g.verbose=Function.prototype.bind.call(console.info,console,v.name+":"),g.verbose.apply(console,arguments)))},error:function(){g.error=Function.prototype.bind.call(console.error,console,v.name+":"),g.error.apply(console,arguments)},performance:{log:function(e){var t,s,n;v.performance&&(t=(new Date).getTime(),n=c||t,s=t-n,c=t,u.push({Name:e[0],Arguments:[].slice.call(e,1)||"",Element:A,"Execution Time":s})),clearTimeout(g.performance.timer),g.performance.timer=setTimeout(g.performance.display,500)},display:function(){var t=v.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+"'"),a.length>1&&(t+=" ("+a.length+")"),(console.group!==n||console.table!==n)&&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,r){var a,o,c,u=F;return s=s||f,r=A||r,"string"==typeof t&&u!==n&&(t=t.split(/[\. ]/),a=t.length-1,e.each(t,function(s,r){var i=s!=a?r+t[s+1].charAt(0).toUpperCase()+t[s+1].slice(1):t;if(e.isPlainObject(u[i])&&s!=a)u=u[i];else{if(u[i]!==n)return o=u[i],!1;if(!e.isPlainObject(u[r])||s==a)return u[r]!==n?(o=u[r],!1):!1;u=u[r]}})),e.isFunction(o)?c=o.apply(r,s):o!==n&&(c=o),e.isArray(i)?i.push(c):i!==n?i=[i,c]:c!==n&&(i=c),o}},d?(F===n&&g.initialize(),g.invoke(l)):(F!==n&&F.invoke("destroy"),g.initialize())}),i!==n?i: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",result:"result"},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=/[&<>"'`]/,n={"&":"&","<":"<",">":">",'"':""","'":"'","`":"`"},r=function(e){return n[e]};return s.test(e)?e.replace(t,r):e},message:function(e,t){var s="";return e!==n&&t!==n&&(s+='
',s+="empty"==t?'
No Results
'+e+'
':'
'+e+"
",s+="
"),s},category:function(t){var s="",r=e.fn.search.settings.templates.escape;return t.results!==n?(e.each(t.results,function(t,i){i.results!==n&&i.results.length>0&&(s+='
'+i.name+"
",e.each(i.results,function(e,t){s+='
',t.url&&(s+=''),t.image!==n&&(t.image=r(t.image),s+='
'),s+='
',t.price!==n&&(t.price=r(t.price),s+='
'+t.price+"
"),t.title!==n&&(t.title=r(t.title),s+='
'+t.title+"
"),t.description!==n&&(s+='
'+t.description+"
"),s+="
"}),s+="
")}),t.action&&(s+=''+t.action.text+""),s):!1},standard:function(t){var s="";return t.results!==n?(e.each(t.results,function(e,t){s+=t.url?'':'',t.image!==n&&(s+='
'),s+='
',t.price!==n&&(s+='
'+t.price+"
"),t.title!==n&&(s+='
'+t.title+"
"),t.description!==n&&(s+='
'+t.description+"
"),s+="
",s+="
"}),t.action&&(s+=''+t.action.text+""),s):!1}}}}(jQuery,window,document);