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.
 
 
 

1 lines
16 KiB

!function(e,t,s,n){"use strict";t=void 0!==t&&t.Math==Math?t:"undefined"!=typeof self&&self.Math==Math?self:Function("return this")(),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,h=e.isPlainObject(r)?e.extend(!0,{},e.fn.search.settings,r):e.extend({},e.fn.search.settings),p=h.className,m=h.metadata,v=h.regExp,b=h.fields,y=h.selector,R=h.error,C=h.namespace,w="."+C,x=C+"-module",F=e(this),j=F.find(y.prompt),k=F.find(y.searchButton),q=F.find(y.results),S=F.find(y.result),A=(F.find(y.category),this),E=F.data(x),T=!1,D=!1;g={initialize:function(){g.verbose("Initializing module"),g.determine.searchFields(),g.bind.events(),g.set.type(),g.create.results(),g.instantiate()},instantiate:function(){g.verbose("Storing instance of module",g),E=g,F.data(x,g)},destroy:function(){g.verbose("Destroying instance"),F.off(w).removeData(x)},refresh:function(){g.debug("Refreshing selector cache"),j=F.find(y.prompt),k=F.find(y.searchButton),F.find(y.category),q=F.find(y.results),S=F.find(y.result)},refreshResults:function(){q=F.find(y.results),S=F.find(y.result)},bind:{events:function(){g.verbose("Binding events to search"),h.automatic&&(F.on(g.get.inputEvent()+w,y.prompt,g.event.input),j.attr("autocomplete","off")),F.on("focus"+w,y.prompt,g.event.focus).on("blur"+w,y.prompt,g.event.blur).on("keydown"+w,y.prompt,g.handleKeyboard).on("click"+w,y.searchButton,g.query).on("mousedown"+w,y.results,g.event.result.mousedown).on("mouseup"+w,y.results,g.event.result.mouseup).on("click"+w,y.result,g.event.result.click)}},determine:{searchFields:function(){r&&r.searchFields!==n&&(h.searchFields=r.searchFields)}},event:{input:function(){h.searchDelay?(clearTimeout(g.timer),g.timer=setTimeout(function(){g.is.focused()&&g.query()},h.searchDelay)):g.query()},focus:function(){g.set.focus(),h.searchOnFocus&&g.has.minimumCharacters()&&g.query(function(){g.can.show()&&g.showResults()})},blur:function(e){var t=function(){g.cancel.query(),g.remove.focus(),g.timer=setTimeout(g.hideResults,h.hideDelay)};s.activeElement===this||(D=!1,g.resultsClicked?(g.debug("Determining if user action caused search to close"),F.one("click.close"+w,y.results,function(e){g.is.inMessage(e)||T?j.focus():(T=!1,g.is.animating()||g.is.hidden()||t())})):(g.debug("Input blurred without user action, closing results"),t()))},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(y.title).eq(0),i=n.is("a[href]")?n:n.find("a[href]").eq(0),a=i.attr("href")||!1,o=i.attr("target")||!1,c=(r.html(),r.length>0&&r.text()),u=g.get.results(),l=n.data(m.result)||g.get.result(c,u);if(e.isFunction(h.onSelect)&&!1===h.onSelect.call(A,l,u))return g.debug("Custom onSelect callback cancelled default select action"),void(T=!0);g.hideResults(),c&&g.set.value(c),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=F.find(y.result),n=F.find(y.category),r=s.filter("."+p.active),i=s.index(r),a=s.length,o=r.length>0,c=e.which,u=13,l=38,d=40;if(c==27&&(g.verbose("Escape key pressed, blurring search field"),g.hideResults(),D=!0),g.is.visible())if(c==u){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 c==l&&o?(g.verbose("Up key pressed, changing active result"),t=i-1<0?i:i-1,n.removeClass(p.active),s.removeClass(p.active).eq(t).addClass(p.active).closest(n).addClass(p.active),e.preventDefault()):c==d&&(g.verbose("Down key pressed, changing active result"),t=i+1>=a?i:i+1,n.removeClass(p.active),s.removeClass(p.active).eq(t).addClass(p.active).closest(n).addClass(p.active),e.preventDefault());else c==u&&(g.verbose("Enter key pressed, executing query"),g.query(),g.set.buttonPressed(),j.one("keyup",g.remove.buttonFocus))},setup:{api:function(t,s){var n={debug:h.debug,on:!1,cache:!0,action:"search",urlData:{query:t},onSuccess:function(e){g.parse.response.call(A,e,t),s()},onFailure:function(){g.displayMessage(R.serverError),s()},onAbort:function(e){},onError:g.error};e.extend(!0,n,h.apiSettings),g.verbose("Setting up API request",n),F.api(n)}},can:{useAPI:function(){return e.fn.api!==n},show:function(){return g.is.focused()&&!g.is.visible()&&!g.is.empty()},transition:function(){return h.transition&&e.fn.transition!==n&&F.transition("is supported")}},is:{animating:function(){return q.hasClass(p.animating)},hidden:function(){return q.hasClass(p.hidden)},inMessage:function(t){if(t.target){var n=e(t.target);return e.contains(s.documentElement,t.target)&&n.closest(y.message).length>0}},empty:function(){return""===q.html()},visible:function(){return q.filter(":visible").length>0},focused:function(){return j.filter(":focus").length>0}},get:{inputEvent:function(){var e=j[0];return e!==n&&e.oninput!==n?"input":e!==n&&e.onpropertychange!==n?"propertychange":"keyup"},value:function(){return j.val()},results:function(){return F.data(m.results)},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"===h.type?(g.debug("Finding result that matches",t),e.each(s,function(s,n){if(e.isArray(n.results)&&(i=g.search.object(t,n.results,r)[0]))return!1})):(g.debug("Finding result in results object",t),i=g.search.object(t,s,r)[0]),i||!1}},select:{firstResult:function(){g.verbose("Selecting first result"),S.first().addClass(p.active)}},set:{focus:function(){F.addClass(p.focus)},loading:function(){F.addClass(p.loading)},value:function(e){g.verbose("Setting search input value",e),j.val(e)},type:function(e){e=e||h.type,"category"==h.type&&F.addClass(h.type)},buttonPressed:function(){k.addClass(p.pressed)}},remove:{loading:function(){F.removeClass(p.loading)},focus:function(){F.removeClass(p.focus)},buttonPressed:function(){k.removeClass(p.pressed)}},query:function(t){t=e.isFunction(t)?t:function(){};var s=g.get.value(),n=g.read.cache(s);t=t||function(){},g.has.minimumCharacters()?(n?(g.debug("Reading result from cache",s),g.save.results(n.results),g.addResults(n.html),g.inject.id(n.results),t()):(g.debug("Querying for",s),e.isPlainObject(h.source)||e.isArray(h.source)?(g.search.local(s),t()):g.can.useAPI()?g.search.remote(s,t):(g.error(R.source),t())),h.onSearchQuery.call(A,s)):g.hideResults()},search:{local:function(e){var t,s=g.search.object(e,h.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,s){s=e.isFunction(s)?s:function(){},F.api("is loading")&&F.api("abort"),g.setup.api(t,s),F.api("query")},object:function(t,s,r){var i=[],a=[],o=t.toString().replace(v.escape,"\\$&"),c=new RegExp(v.beginsWith+o,"i"),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||h.source,r=r!==n?r:h.searchFields,e.isArray(r)||(r=[r]),s===n||!1===s?(g.error(R.source),[]):(e.each(r,function(n,r){e.each(s,function(e,s){"string"==typeof s[r]&&(-1!==s[r].search(c)?u(i,s):h.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;r<n;r++){for(var a=e.charCodeAt(r);i<s;)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[b.results]!==n&&(g.addResults(s),g.inject.id(e[b.results]),g.write.cache(t,{html:s,results:e[b.results]}),g.save.results(e[b.results]))}},cancel:{query:function(){g.can.useAPI()&&F.api("abort")}},has:{minimumCharacters:function(){return g.get.value().length>=h.minCharacters},results:function(){return 0!==q.length&&""!=q.html()}},clear:{cache:function(e){var t=F.data(m.cache);e?e&&t&&t[e]&&(g.debug("Removing value from cache",e),delete t[e],F.data(m.cache,t)):(g.debug("Clearing cache",e),F.removeData(m.cache))}},read:{cache:function(e){var t=F.data(m.cache);return!!h.cache&&(g.verbose("Checking cache for generated html for query",e),"object"==typeof t&&t[e]!==n&&t[e])}},create:{id:function(e,t){var s,r=e+1;return t!==n?(s=String.fromCharCode(97+t)+r,g.verbose("Creating category result id",s)):(s=r,g.verbose("Creating result id",s)),s},results:function(){0===q.length&&(q=e("<div />").addClass(p.results).appendTo(F))}},inject:{result:function(e,t,s){g.verbose("Injecting result into results");var r=s!==n?q.children().eq(s).children(y.result).eq(t):q.children(y.result).eq(t);g.verbose("Injecting results metadata",r),r.data(m.result,e)},id:function(t){g.debug("Injecting unique ids into results");var s=0,r=0;return"category"===h.type?e.each(t,function(t,i){r=0,e.each(i.results,function(e,t){var a=i.results[e];a.id===n&&(a.id=g.create.id(r,s)),g.inject.result(a,r,s),r++}),s++}):e.each(t,function(e,s){var i=t[e];i.id===n&&(i.id=g.create.id(r)),g.inject.result(i,r),r++}),t}},save:{results:function(e){g.verbose("Saving current search results to metadata",e),F.data(m.results,e)}},write:{cache:function(e,t){var s=F.data(m.cache)!==n?F.data(m.cache):{};h.cache&&(g.verbose("Writing generated html to cache",e,t),s[e]=t,F.data(m.cache,s))}},addResults:function(t){if(e.isFunction(h.onResultsAdd)&&!1===h.onResultsAdd.call(q,t))return g.debug("onResultsAdd callback cancelled default action"),!1;t?(q.html(t),g.refreshResults(),h.selectFirstResult&&g.select.firstResult(),g.showResults()):g.hideResults(function(){q.empty()})},showResults:function(t){t=e.isFunction(t)?t:function(){},D||!g.is.visible()&&g.has.results()&&(g.can.transition()?(g.debug("Showing results with css animations"),q.transition({animation:h.transition+" in",debug:h.debug,verbose:h.verbose,duration:h.duration,onComplete:function(){t()},queue:!0})):(g.debug("Showing results with javascript"),q.stop().fadeIn(h.duration,h.easing)),h.onResultsOpen.call(q))},hideResults:function(t){t=e.isFunction(t)?t:function(){},g.is.visible()&&(g.can.transition()?(g.debug("Hiding results with css animations"),q.transition({animation:h.transition+" out",debug:h.debug,verbose:h.verbose,duration:h.duration,onComplete:function(){t()},queue:!0})):(g.debug("Hiding results with javascript"),q.stop().fadeOut(h.duration,h.easing)),h.onResultsClose.call(q))},generateResults:function(t){g.debug("Generating html from response",t);var s=h.templates[h.type],n=e.isPlainObject(t[b.results])&&!e.isEmptyObject(t[b.results]),r=e.isArray(t[b.results])&&t[b.results].length>0,i="";return n||r?(h.maxResults>0&&(n?"standard"==h.type&&g.error(R.maxResults):t[b.results]=t[b.results].slice(0,h.maxResults)),e.isFunction(s)?i=s(t,b):g.error(R.noTemplate,!1)):h.showNoResults&&(i=g.displayMessage(R.noResults,"empty")),h.onResults.call(A,t),i},displayMessage:function(e,t){return t=t||"standard",g.debug("Displaying message",e,t),g.addResults(h.templates.message(e,t)),h.templates.message(e,t)},setting:function(t,s){if(e.isPlainObject(t))e.extend(!0,h,t);else{if(s===n)return h[t];h[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(){!h.silent&&h.debug&&(h.performance?g.performance.log(arguments):(g.debug=Function.prototype.bind.call(console.info,console,h.name+":"),g.debug.apply(console,arguments)))},verbose:function(){!h.silent&&h.verbose&&h.debug&&(h.performance?g.performance.log(arguments):(g.verbose=Function.prototype.bind.call(console.info,console,h.name+":"),g.verbose.apply(console,arguments)))},error:function(){h.silent||(g.error=Function.prototype.bind.call(console.error,console,h.name+":"),g.error.apply(console,arguments))},performance:{log:function(e){var t,s;h.performance&&(s=(t=(new Date).getTime())-(c||t),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=h.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=E;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);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?(E===n&&g.initialize(),g.invoke(l)):(E!==n&&E.invoke("destroy"),g.initialize())}),i!==n?i:this},e.fn.search.settings={name:"Search",namespace:"search",silent:!1,debug:!1,verbose:!1,performance:!0,type:"standard",minCharacters:1,selectFirstResult:!1,apiSettings:!1,source:!1,searchOnFocus:!0,searchFields:["title","description"],displayField:"",searchFullText:!0,automatic:!0,hideDelay:0,searchDelay:200,maxResults:7,cache:!0,showNoResults:!0,transition:"scale",duration:200,easing:"easeOutExpo",onSelect:!1,onResultsAdd:!1,onSearchQuery:function(e){},onResults:function(e){},onResultsOpen:function(){},onResultsClose:function(){},className:{animating:"animating",active:"active",empty:"empty",focus:"focus",hidden:"hidden",loading:"loading",results:"results",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 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|^)"},fields:{categories:"results",categoryName:"name",categoryResults:"results",description:"description",image:"image",price:"price",results:"results",title:"title",url:"url",action:"action",actionText:"text",actionURL:"url"},selector:{prompt:".prompt",searchButton:".search.button",results:".results",message:".results > .message",category:".category",result:".result",title:".title, .name"},templates:{escape:function(e){var t={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#x27;","`":"&#x60;"};return/[&<>"'`]/.test(e)?e.replace(/[&<>"'`]/g,function(e){return t[e]}):e},message:function(e,t){var s="";return e!==n&&t!==n&&(s+='<div class="message '+t+'">',s+="empty"==t?'<div class="header">No Results</div class="header"><div class="description">'+e+'</div class="description">':' <div class="description">'+e+"</div>",s+="</div>"),s},category:function(t,s){var r="";e.fn.search.settings.templates.escape;return t[s.categoryResults]!==n&&(e.each(t[s.categoryResults],function(t,i){i[s.results]!==n&&i.results.length>0&&(r+='<div class="category">',i[s.categoryName]!==n&&(r+='<div class="name">'+i[s.categoryName]+"</div>"),e.each(i.results,function(e,t){t[s.url]?r+='<a class="result" href="'+t[s.url]+'">':r+='<a class="result">',t[s.image]!==n&&(r+='<div class="image"> <img src="'+t[s.image]+'"></div>'),r+='<div class="content">',t[s.price]!==n&&(r+='<div class="price">'+t[s.price]+"</div>"),t[s.title]!==n&&(r+='<div class="title">'+t[s.title]+"</div>"),t[s.description]!==n&&(r+='<div class="description">'+t[s.description]+"</div>"),r+="</div>",r+="</a>"}),r+="</div>")}),t[s.action]&&(r+='<a href="'+t[s.action][s.actionURL]+'" class="action">'+t[s.action][s.actionText]+"</a>"),r)},standard:function(t,s){var r="";return t[s.results]!==n&&(e.each(t[s.results],function(e,t){t[s.url]?r+='<a class="result" href="'+t[s.url]+'">':r+='<a class="result">',t[s.image]!==n&&(r+='<div class="image"> <img src="'+t[s.image]+'"></div>'),r+='<div class="content">',t[s.price]!==n&&(r+='<div class="price">'+t[s.price]+"</div>"),t[s.title]!==n&&(r+='<div class="title">'+t[s.title]+"</div>"),t[s.description]!==n&&(r+='<div class="description">'+t[s.description]+"</div>"),r+="</div>",r+="</a>"}),t[s.action]&&(r+='<a href="'+t[s.action][s.actionURL]+'" class="action">'+t[s.action][s.actionText]+"</a>"),r)}}}}(jQuery,window,document);