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
10 KiB
11 lines
10 KiB
/*
|
|
* # Semantic UI
|
|
* git://github.com/Semantic-Org/Semantic-UI.git#1.0
|
|
*
|
|
*
|
|
* 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 i,n=e(this),a=n.selector||"",o=(new Date).getTime(),c=[],l=arguments[0],u="string"==typeof l,d=[].slice.call(arguments,1);return e(this).each(function(){var p,f=e.extend(!0,{},e.fn.search.settings,s),g=f.className,h=f.selector,m=f.error,v=f.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);p={initialize:function(){p.verbose("Initializing module");var e=x[0],t=e!==r&&e.oninput!==r?"input":e!==r&&e.onpropertychange!==r?"propertychange":"keyup";x.on("focus"+y,p.event.focus).on("blur"+y,p.event.blur).on("keydown"+y,p.handleKeyboard),f.automatic&&x.on(t+y,p.search.throttle),C.on("click"+y,p.search.query),A.on("click"+y,h.result,p.results.select),p.instantiate()},instantiate:function(){p.verbose("Storing instance of module",p),T=p,w.data(b,p)},destroy:function(){p.verbose("Destroying instance"),w.removeData(b),x.off(y),C.off(y),A.off(y)},event:{focus:function(){w.addClass(g.focus),clearTimeout(p.timer),p.search.throttle(),p.results.show()},blur:function(){p.search.cancel(),w.removeClass(g.focus),p.timer=setTimeout(p.results.hide,f.hideDelay)}},handleKeyboard:function(t){var s,r=w.find(h.result),i=w.find(h.category),n=t.which,a={backspace:8,enter:13,escape:27,upArrow:38,downArrow:40},o=g.active,c=r.index(r.filter("."+o)),l=r.size();if(n==a.escape&&(p.verbose("Escape key pressed, blurring search field"),x.trigger("blur")),A.filter(":visible").size()>0)if(n==a.enter){if(p.verbose("Enter key pressed, selecting active result"),r.filter("."+o).size()>0)return e.proxy(p.results.select,r.filter("."+o))(t),t.preventDefault(),!1}else n==a.upArrow?(p.verbose("Up key pressed, changing active result"),s=0>c-1?c:c-1,i.removeClass(o),r.removeClass(o).eq(s).addClass(o).closest(i).addClass(o),t.preventDefault()):n==a.downArrow&&(p.verbose("Down key pressed, changing active result"),s=c+1>=l?c:c+1,i.removeClass(o),r.removeClass(o).eq(s).addClass(o).closest(i).addClass(o),t.preventDefault());else n==a.enter&&(p.verbose("Enter key pressed, executing query"),p.search.query(),C.addClass(g.down),x.one("keyup",function(){C.removeClass(g.down)}))},search:{cancel:function(){var e=w.data("xhr")||!1;e&&"resolved"!=e.state()&&(p.debug("Cancelling last search"),e.abort())},throttle:function(){var e=x.val(),t=e.length;clearTimeout(p.timer),t>=f.minCharacters?p.timer=setTimeout(p.search.query,f.searchThrottle):p.results.hide()},query:function(){var t=x.val(),s=p.search.cache.read(t);s?(p.debug("Reading result for '"+t+"' from cache"),p.results.add(s)):(p.debug("Querying for '"+t+"'"),e.isPlainObject(f.source)||e.isArray(f.source)?p.search.local(t):f.apiSettings?p.search.remote(t):e.api!==r&&e.api.settings.api.search!==r?(p.debug("Searching with default search API endpoint"),f.apiSettings={action:"search"},p.search.remote(t)):p.error(m.source),e.proxy(f.onSearchQuery,w)(t))},local:function(t){var s,r=[],i=[],n=e.isArray(f.searchFields)?f.searchFields:[f.searchFields],a=new RegExp("(?:s|^)"+t,"i"),o=new RegExp(t,"i");w.addClass(g.loading),e.each(n,function(t,s){e.each(f.source,function(t,n){var c="string"==typeof n[s],l=-1==e.inArray(n,r)&&-1==e.inArray(n,i);c&&l&&(a.test(n[s])?r.push(n):o.test(n[s])&&i.push(n))})}),s=p.results.generate({results:e.merge(r,i)}),w.removeClass(g.loading),p.search.cache.write(t,s),p.results.add(s)},remote:function(t){var s,r={stateContext:w,urlData:{query:t},onSuccess:function(e){s=p.results.generate(e),p.search.cache.write(t,s),p.results.add(s)},failure:p.error};p.search.cancel(),p.debug("Executing search"),e.extend(!0,r,f.apiSettings),e.api(r)},cache:{read:function(e){var t=w.data("cache");return f.cache&&"object"==typeof t&&t[e]!==r?t[e]:!1},write:function(e,t){var s=w.data("cache")!==r?w.data("cache"):{};s[e]=t,w.data("cache",s)}}},results:{generate:function(t){p.debug("Generating html from response",t);var s=f.templates[f.type],r="";return e.isPlainObject(t.results)&&!e.isEmptyObject(t.results)||e.isArray(t.results)&&t.results.length>0?(f.maxResults>0&&(t.results=e.makeArray(t.results).slice(0,f.maxResults)),e.isFunction(s)?r=s(t):p.error(m.noTemplate,!1)):r=p.message(m.noResults,"empty"),e.proxy(f.onResults,w)(t),r},add:function(t){("default"==f.onResultsAdd||"default"==e.proxy(f.onResultsAdd,A)(t))&&A.html(t),p.results.show()},show:function(){0===A.filter(":visible").size()&&x.filter(":focus").size()>0&&""!==A.html()&&(f.transition&&e.fn.transition!==r&&w.transition("is supported")?(p.debug("Showing results with css animations"),A.transition(f.transition+" in",f.duration)):(p.debug("Showing results with javascript"),A.stop().fadeIn(f.duration,f.easing)),e.proxy(f.onResultsOpen,A)())},hide:function(){A.filter(":visible").size()>0&&(f.transition&&e.fn.transition!==r&&w.transition("is supported")?(p.debug("Hiding results with css animations"),A.transition(f.transition+" out",f.duration)):(p.debug("Hiding results with javascript"),A.stop().fadeIn(f.duration,f.easing)),e.proxy(f.onResultsClose,A)())},select:function(s){p.debug("Search result selected");{var r=e(this),i=r.find(".title");i.html()}if("default"==f.onSelect||"default"==e.proxy(f.onSelect,this)(s)){var n=r.find("a[href]").eq(0),a=n.attr("href")||!1,o=n.attr("target")||!1;p.results.hide(),a&&("_blank"==o||s.ctrlKey?t.open(a):t.location.href=a)}}},message:function(e,t){return t=t||"standard",p.results.add(f.templates.message(e,t)),f.templates.message(e,t)},setting:function(t,s){if(e.isPlainObject(t))e.extend(!0,f,t);else{if(s===r)return f[t];f[t]=s}},internal:function(t,s){if(e.isPlainObject(t))e.extend(!0,p,t);else{if(s===r)return p[t];p[t]=s}},debug:function(){f.debug&&(f.performance?p.performance.log(arguments):(p.debug=Function.prototype.bind.call(console.info,console,f.name+":"),p.debug.apply(console,arguments)))},verbose:function(){f.verbose&&f.debug&&(f.performance?p.performance.log(arguments):(p.verbose=Function.prototype.bind.call(console.info,console,f.name+":"),p.verbose.apply(console,arguments)))},error:function(){p.error=Function.prototype.bind.call(console.error,console,f.name+":"),p.error.apply(console,arguments)},performance:{log:function(e){var t,s,r;f.performance&&(t=(new Date).getTime(),r=o||t,s=t-r,o=t,c.push({Name:e[0],Arguments:[].slice.call(e,1)||"",Element:R,"Execution Time":s})),clearTimeout(p.performance.timer),p.performance.timer=setTimeout(p.performance.display,100)},display:function(){var t=f.name+":",s=0;o=!1,clearTimeout(p.performance.timer),e.each(c,function(e,t){s+=t["Execution Time"]}),t+=" "+s+"ms",a&&(t+=" '"+a+"'"),n.size()>1&&(t+=" ("+n.size()+")"),(console.group!==r||console.table!==r)&&c.length>0&&(console.groupCollapsed(t),console.table?console.table(c):e.each(c,function(e,t){console.log(t.Name+": "+t["Execution Time"]+"ms")}),console.groupEnd()),c=[]}},invoke:function(t,s,n){var a,o,c,l=T;return s=s||d,n=R||n,"string"==typeof t&&l!==r&&(t=t.split(/[\. ]/),a=t.length-1,e.each(t,function(s,i){var n=s!=a?i+t[s+1].charAt(0).toUpperCase()+t[s+1].slice(1):t;if(e.isPlainObject(l[n])&&s!=a)l=l[n];else{if(l[n]!==r)return o=l[n],!1;if(!e.isPlainObject(l[i])||s==a)return l[i]!==r?(o=l[i],!1):!1;l=l[i]}})),e.isFunction(o)?c=o.apply(n,s):o!==r&&(c=o),e.isArray(i)?i.push(c):i!==r?i=[i,c]:c!==r&&(i=c),o}},u?(T===r&&p.initialize(),p.invoke(l)):(T!==r&&p.destroy(),p.initialize())}),i!==r?i:this},e.fn.search.settings={name:"Search Module",namespace:"search",debug:!1,verbose:!0,performance:!0,onSelect:"default",onResultsAdd:"default",onSearchQuery:function(){},onResults:function(){},onResultsOpen:function(){},onResultsClose:function(){},source:!1,automatic:"true",type:"simple",hideDelay:300,minCharacters:3,searchThrottle:300,maxResults:7,cache:!0,searchFields:["title","description"],transition:"scale",duration:300,easing:"easeOutExpo",apiSettings:!1,className:{active:"active",down:"down",focus:"focus",empty:"empty",loading:"loading"},error:{source:"No source or api action specified",noResults:"Your search returned no results",logging:"Error in debug logging, exiting.",noTemplate:"A valid template name was not specified.",serverError:"There was an issue with querying the server.",method:"The method you called is not defined."},selector:{prompt:".prompt",searchButton:".search.button",results:".results",category:".category",result:".result"},templates:{escape:function(e){var t=/[&<>"'`]/g,s=/[&<>"'`]/,r={"&":"&","<":"<",">":">",'"':""","'":"'","`":"`"},i=function(e){return r[e]};return s.test(e)?e.replace(t,i):e},message:function(e,t){var s="";return e!==r&&t!==r&&(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},categories:function(t){var s="",i=e.fn.search.settings.templates.escape;return t.results!==r?(e.each(t.results,function(t,n){n.results!==r&&n.results.length>0&&(s+='<div class="category"><div class="name">'+n.name+"</div>",e.each(n.results,function(e,t){s+='<div class="result">',s+='<a href="'+t.url+'"></a>',t.image!==r&&(t.image=i(t.image),s+='<div class="image"> <img src="'+t.image+'" alt=""></div>'),s+='<div class="content">',t.price!==r&&(t.price=i(t.price),s+='<div class="price">'+t.price+"</div>"),t.title!==r&&(t.title=i(t.title),s+='<div class="title">'+t.title+"</div>"),t.description!==r&&(s+='<div class="description">'+t.description+"</div>"),s+="</div></div>"}),s+="</div>")}),t.resultPage&&(s+='<a href="'+t.resultPage.url+'" class="all">'+t.resultPage.text+"</a>"),s):!1},simple:function(t){var s="";return t.results!==r?(e.each(t.results,function(e,t){s+='<a class="result" href="'+t.url+'">',t.image!==r&&(s+='<div class="image"> <img src="'+t.image+'"></div>'),s+='<div class="content">',t.price!==r&&(s+='<div class="price">'+t.price+"</div>"),t.title!==r&&(s+='<div class="title">'+t.title+"</div>"),t.description!==r&&(s+='<div class="description">'+t.description+"</div>"),s+="</div></a>"}),t.resultPage&&(s+='<a href="'+t.resultPage.url+'" class="all">'+t.resultPage.text+"</a>"),s):!1}}}}(jQuery,window,document);
|