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.
 
 
 

11 lines
13 KiB

/*!
* # Semantic UI 1.11.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 a,i=e(this),o=i.selector||"",c=(new Date).getTime(),u=[],l=arguments[0],d="string"==typeof l,p=[].slice.call(arguments,1);return e(this).each(function(){var f,g=e.extend(!0,{},e.fn.search.settings,r),m=g.className,h=g.metadata,v=g.regExp,b=g.selector,y=g.error,R=g.namespace,w="."+R,x=R+"-module",C=e(this),k=C.find(b.prompt),A=C.find(b.searchButton),q=C.find(b.results),S=(C.find(b.result),C.find(b.category),this),E=C.data(x);f={initialize:function(){f.verbose("Initializing module");var e=k[0],t=e!==n&&e.oninput!==n?"input":e!==n&&e.onpropertychange!==n?"propertychange":"keyup";g.automatic&&k.on(t+w,f.throttle).attr("autocomplete","off"),k.on("focus"+w,f.event.focus).on("blur"+w,f.event.blur).on("keydown"+w,f.handleKeyboard),A.on("click"+w,f.query),q.on("mousedown"+w,f.event.result.mousedown).on("mouseup"+w,f.event.result.mouseup).on("click"+w,b.result,f.event.result.click),f.instantiate()},instantiate:function(){f.verbose("Storing instance of module",f),E=f,C.data(x,f)},destroy:function(){f.verbose("Destroying instance"),C.removeData(x),k.off(w),A.off(w),q.off(w)},event:{focus:function(){f.set.focus(),clearTimeout(f.timer),f.throttle(),f.has.minimumCharacters()&&f.showResults()},blur:function(){var e=s.activeElement===this;e||f.resultsClicked||(f.cancel.query(),f.remove.focus(),f.timer=setTimeout(f.hideResults,g.hideDelay))},result:{mousedown:function(){f.resultsClicked=!0},mouseup:function(){f.resultsClicked=!1},click:function(s){f.debug("Search result selected");var n=e(this),r=n.find(b.title).eq(0),a=n.find("a[href]").eq(0),i=a.attr("href")||!1,o=a.attr("target")||!1,c=(r.html(),r.length>0?r.text():!1),u=f.get.results(),l=f.get.result(c,u);return e.isFunction(g.onSelect)&&g.onSelect.call(S,l,u)===!1?void f.debug("Custom onSelect callback cancelled default select action"):(f.hideResults(),c&&f.set.value(c),void(i&&(f.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=C.find(b.result),n=C.find(b.category),r=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&&(f.verbose("Escape key pressed, blurring search field"),k.trigger("blur")),f.is.visible())if(i==o.enter){if(f.verbose("Enter key pressed, selecting active result"),s.filter("."+m.active).length>0)return f.event.result.click.call(s.filter("."+m.active),e),e.preventDefault(),!1}else i==o.upArrow?(f.verbose("Up key pressed, changing active result"),t=0>r-1?r:r-1,n.removeClass(m.active),s.removeClass(m.active).eq(t).addClass(m.active).closest(n).addClass(m.active),e.preventDefault()):i==o.downArrow&&(f.verbose("Down key pressed, changing active result"),t=r+1>=a?r:r+1,n.removeClass(m.active),s.removeClass(m.active).eq(t).addClass(m.active).closest(n).addClass(m.active),e.preventDefault());else i==o.enter&&(f.verbose("Enter key pressed, executing query"),f.query(),f.set.buttonPressed(),k.one("keyup",f.remove.buttonFocus))},setup:{api:function(){var e={on:!1,action:"search",onFailure:f.error};f.verbose("First request, initializing API"),C.api(e)}},can:{useAPI:function(){return e.fn.api!==n},transition:function(){return g.transition&&e.fn.transition!==n&&C.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=C.data(h.results);return e},result:function(t,s){var n=!1;return t=t||f.get.value(),s=s||f.get.results(),"category"===g.type?(f.debug("Finding result that matches",t),e.each(s,function(s,r){return e.isArray(r.results)&&(n=f.search.object(t,r.results)[0],n&&n.length>0)?!0:void 0})):(f.debug("Finding result in results object",t),n=f.search.object(t,s)[0]),n}},set:{focus:function(){C.addClass(m.focus)},loading:function(){C.addClass(m.loading)},value:function(e){f.verbose("Setting search input value",e),k.val(e),f.query()},buttonPressed:function(){A.addClass(m.pressed)}},remove:{loading:function(){C.removeClass(m.loading)},focus:function(){C.removeClass(m.focus)},buttonPressed:function(){A.removeClass(m.pressed)}},query:function(){var t=f.get.value(),s=f.read.cache(t);s?(f.debug("Reading result for "+t+" from cache"),f.save.results(s.results),f.addResults(s.html)):(f.debug("Querying for "+t),e.isPlainObject(g.source)||e.isArray(g.source)?f.search.local(t):f.can.useAPI()?g.apiSettings?(f.debug("Searching with specified API settings",g.apiSettings),f.search.remote(t)):e.api.settings.api.search!==n?(f.debug("Searching with default search API endpoint"),f.search.remote(t)):f.error(y.noEndpoint):f.error(y.source),g.onSearchQuery.call(S,t))},search:{local:function(e){var t,s=f.search.object(e,g.content);f.set.loading(),f.save.results(s),f.debug("Returned local search results",s),t=f.generateResults({results:s}),f.remove.loading(),f.write.cache(e,{html:t,results:s}),f.addResults(t)},remote:function(t){var s={onSuccess:function(e){f.parse.response.call(S,e,t)},urlData:{query:t}};C.api("get request")||f.setup.api(),e.extend(!0,s,g.apiSettings),f.debug("Executing search",s),f.cancel.query(),C.api("setting",s).api("query")},object:function(t,s){var r=[],a=[],i=e.isArray(g.searchFields)?g.searchFields:[g.searchFields],o=new RegExp(v.exact+t,"i"),c=new RegExp(t,"i");return s=s||g.source,s===n?(f.error(y.source),[]):(e.each(i,function(t,n){e.each(s,function(t,s){var i="string"==typeof s[n],u=-1==e.inArray(s,r)&&-1==e.inArray(s,a);i&&u&&(s[n].match(o)?r.push(s):g.searchFullText&&s[n].match(c)&&a.push(s))})}),e.merge(r,a))}},parse:{response:function(e,t){var s=f.generateResults(e);f.verbose("Parsing server response",e),e!==n&&t!==n&&e.results!==n&&(f.write.cache(t,{html:s,results:e.results}),f.save.results(e.results),f.addResults(s))}},throttle:function(){clearTimeout(f.timer),f.has.minimumCharacters()?f.timer=setTimeout(f.query,g.searchDelay):f.hideResults()},cancel:{query:function(){f.can.useAPI()&&C.api("abort")}},has:{minimumCharacters:function(){var e=f.get.value(),t=e.length;return t>=g.minCharacters}},read:{cache:function(e){var t=C.data(h.cache);return g.cache?(f.verbose("Checking cache for generated html for query",e),"object"==typeof t&&t[e]!==n?t[e]:!1):!1}},save:{results:function(e){f.verbose("Saving current search results to metadata",e),C.data(h.results,e)}},write:{cache:function(e,t){var s=C.data(h.cache)!==n?C.data(h.cache):{};g.cache&&(f.verbose("Writing generated html to cache",e,t),s[e]=t,C.data(h.cache,s))}},addResults:function(t){return e.isFunction(g.onResultsAdd)&&g.onResultsAdd.call(q,t)===!1?(f.debug("onResultsAdd callback cancelled default action"),!1):(q.html(t),void f.showResults())},showResults:function(){f.is.visible()||!f.is.focused()||f.is.empty()||(f.can.transition()?(f.debug("Showing results with css animations"),q.transition({animation:g.transition+" in",duration:g.duration,queue:!0})):(f.debug("Showing results with javascript"),q.stop().fadeIn(g.duration,g.easing)),g.onResultsOpen.call(q))},hideResults:function(){f.is.visible()&&(f.can.transition()?(f.debug("Hiding results with css animations"),q.transition({animation:g.transition+" out",duration:g.duration,queue:!0})):(f.debug("Hiding results with javascript"),q.stop().fadeOut(g.duration,g.easing)),g.onResultsClose.call(q))},generateResults:function(t){f.debug("Generating html from response",t);var s=g.templates[g.type],n=e.isPlainObject(t.results)&&!e.isEmptyObject(t.results),r=e.isArray(t.results)&&t.results.length>0,a="";return n||r?(g.maxResults>0&&(n?"standard"==g.type&&f.error(y.maxResults):t.results=t.results.slice(0,g.maxResults)),e.isFunction(s)?a=s(t):f.error(y.noTemplate,!1)):a=f.displayMessage(y.noResults,"empty"),g.onResults.call(S,t),a},displayMessage:function(e,t){return t=t||"standard",f.debug("Displaying message",e,t),f.addResults(g.templates.message(e,t)),g.templates.message(e,t)},setting:function(t,s){if(e.isPlainObject(t))e.extend(!0,g,t);else{if(s===n)return g[t];g[t]=s}},internal:function(t,s){if(e.isPlainObject(t))e.extend(!0,f,t);else{if(s===n)return f[t];f[t]=s}},debug:function(){g.debug&&(g.performance?f.performance.log(arguments):(f.debug=Function.prototype.bind.call(console.info,console,g.name+":"),f.debug.apply(console,arguments)))},verbose:function(){g.verbose&&g.debug&&(g.performance?f.performance.log(arguments):(f.verbose=Function.prototype.bind.call(console.info,console,g.name+":"),f.verbose.apply(console,arguments)))},error:function(){f.error=Function.prototype.bind.call(console.error,console,g.name+":"),f.error.apply(console,arguments)},performance:{log:function(e){var t,s,n;g.performance&&(t=(new Date).getTime(),n=c||t,s=t-n,c=t,u.push({Name:e[0],Arguments:[].slice.call(e,1)||"",Element:S,"Execution Time":s})),clearTimeout(f.performance.timer),f.performance.timer=setTimeout(f.performance.display,100)},display:function(){var t=g.name+":",s=0;c=!1,clearTimeout(f.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!==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 i,o,c,u=E;return s=s||p,r=S||r,"string"==typeof t&&u!==n&&(t=t.split(/[\. ]/),i=t.length-1,e.each(t,function(s,r){var a=s!=i?r+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]!==n)return o=u[a],!1;if(!e.isPlainObject(u[r])||s==i)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(a)?a.push(c):a!==n?a=[a,c]:c!==n&&(a=c),o}},d?(E===n&&f.initialize(),f.invoke(l)):(E!==n&&E.invoke("destroy"),f.initialize())}),a!==n?a:this},e.fn.search.settings={name:"Search Module",namespace:"search",debug:!1,verbose:!0,performance:!0,type:"standard",minCharacters:1,apiSettings:!1,source:!1,searchFields:["title","description"],searchFullText:!0,automatic:"true",hideDelay:0,searchDelay:100,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:{exact:"(?: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={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#x27;","`":"&#x60;"},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+='<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){var s="",r=e.fn.search.settings.templates.escape;return t.results!==n?(e.each(t.results,function(t,a){a.results!==n&&a.results.length>0&&(s+='<div class="category"><div class="name">'+a.name+"</div>",e.each(a.results,function(e,t){s+='<div class="result">',t.url&&(s+='<a href="'+t.url+'"></a>'),t.image!==n&&(t.image=r(t.image),s+='<div class="image"> <img src="'+t.image+'" alt=""></div>'),s+='<div class="content">',t.price!==n&&(t.price=r(t.price),s+='<div class="price">'+t.price+"</div>"),t.title!==n&&(t.title=r(t.title),s+='<div class="title">'+t.title+"</div>"),t.description!==n&&(s+='<div class="description">'+t.description+"</div>"),s+="</div></div>"}),s+="</div>")}),t.action&&(s+='<a href="'+t.action.url+'" class="action">'+t.action.text+"</a>"),s):!1},standard:function(t){var s="";return t.results!==n?(e.each(t.results,function(e,t){s+=t.url?'<a class="result" href="'+t.url+'">':'<a class="result">',t.image!==n&&(s+='<div class="image"> <img src="'+t.image+'"></div>'),s+='<div class="content">',t.price!==n&&(s+='<div class="price">'+t.price+"</div>"),t.title!==n&&(s+='<div class="title">'+t.title+"</div>"),t.description!==n&&(s+='<div class="description">'+t.description+"</div>"),s+="</div>",s+="</a>"}),t.action&&(s+='<a href="'+t.action.url+'" class="action">'+t.action.text+"</a>"),s):!1}}}}(jQuery,window,document);