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
11 lines
13 KiB
/*
|
|
* # Semantic UI - 1.8.1
|
|
* https://github.com/Semantic-Org/Semantic-UI
|
|
* http://www.semantic-ui.com/
|
|
*
|
|
* 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,g=[].slice.call(arguments,1);return e(this).each(function(){var f,p=e.extend(!0,{},e.fn.search.settings,r),m=p.className,h=p.metadata,v=p.regExp,b=p.selector,y=p.error,R=p.namespace,w="."+R,x=R+"-module",C=e(this),k=C.find(b.prompt),A=C.find(b.searchButton),T=C.find(b.results),q=(C.find(b.result),C.find(b.category),this),S=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";p.automatic&&k.on(t+w,f.throttle),k.on("focus"+w,f.event.focus).on("blur"+w,f.event.blur).on("keydown"+w,f.handleKeyboard),A.on("click"+w,f.query),T.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),S=f,C.data(x,f)},destroy:function(){f.verbose("Destroying instance"),C.removeData(x),k.off(w),A.off(w),T.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,p.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(p.onSelect)&&p.onSelect.call(q,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 p.transition&&e.fn.transition!==n&&C.transition("is supported")}},is:{empty:function(){return""===T.html()},visible:function(){return T.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 r=!1;return t=t||f.get.value(),s=s||f.get.results(),"category"===p.type?(f.debug("Finding result from category results",t),e.each(s,function(e,s){return s.results!==n&&(r=f.search.object(t,s.results)[0],r.length>0)?!0:void 0})):(f.debug("Finding result in results object",t),r=f.search.object(t,s)[0]),r}},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)},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.cachedHTML(t);s?(f.debug("Reading result for "+t+" from cache"),f.addResults(s)):(f.debug("Querying for "+t),e.isPlainObject(p.source)||e.isArray(p.source)?f.search.local(t):f.can.useAPI()?p.apiSettings?(f.debug("Searching with specified API settings",p.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.source),p.onSearchQuery.call(q,t))},search:{local:function(e){var t,s=f.search.object(e,p.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.cachedHTML(e,t),f.addResults(t)},remote:function(t){var s={onSuccess:function(e){f.parse.response.call(q,e,t)},urlData:{query:t}};C.api("get request")||f.setup.api(),e.extend(!0,s,p.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(p.searchFields)?p.searchFields:[p.searchFields],o=new RegExp(v.exact+t,"i"),c=new RegExp(t,"i");return s=s||p.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):p.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&&(f.write.cachedHTML(t,s),e.results!==n&&f.save.results(e.results)),f.addResults(s))}},throttle:function(){clearTimeout(f.timer),f.has.minimumCharacters()?f.timer=setTimeout(f.query,p.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>=p.minCharacters}},read:{cachedHTML:function(e){var t=C.data(h.cache);return p.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:{cachedHTML:function(e,t){var s=C.data(h.cache)!==n?C.data(h.cache):{};p.cache&&(f.verbose("Writing generated html to cache",e,t),s[e]=t,C.data(h.cache,s))}},addResults:function(t){return e.isFunction(p.onResultsAdd)&&p.onResultsAdd.call(T,t)===!1?(f.debug("onResultsAdd callback cancelled default action"),!1):(T.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"),T.transition({animation:p.transition+" in",duration:p.duration,queue:!0})):(f.debug("Showing results with javascript"),T.stop().fadeIn(p.duration,p.easing)),p.onResultsOpen.call(T))},hideResults:function(){f.is.visible()&&(f.can.transition()?(f.debug("Hiding results with css animations"),T.transition({animation:p.transition+" out",duration:p.duration,queue:!0})):(f.debug("Hiding results with javascript"),T.stop().fadeOut(p.duration,p.easing)),p.onResultsClose.call(T))},generateResults:function(t){f.debug("Generating html from response",t);var s=p.templates[p.type],n=e.isPlainObject(t.results)&&!e.isEmptyObject(t.results),r=e.isArray(t.results)&&t.results.length>0,a="";return n||r?(p.maxResults>0&&(n?f.error(y.maxResults):t.results=t.results.slice(0,p.maxResults)),e.isFunction(s)?a=s(t):f.error(y.noTemplate,!1)):a=f.displayMessage(y.noResults,"empty"),p.onResults.call(q,t),a},displayMessage:function(e,t){return t=t||"standard",f.debug("Displaying message",e,t),f.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===n)return p[t];p[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(){p.debug&&(p.performance?f.performance.log(arguments):(f.debug=Function.prototype.bind.call(console.info,console,p.name+":"),f.debug.apply(console,arguments)))},verbose:function(){p.verbose&&p.debug&&(p.performance?f.performance.log(arguments):(f.verbose=Function.prototype.bind.call(console.info,console,p.name+":"),f.verbose.apply(console,arguments)))},error:function(){f.error=Function.prototype.bind.call(console.error,console,p.name+":"),f.error.apply(console,arguments)},performance:{log:function(e){var t,s,n;p.performance&&(t=(new Date).getTime(),n=c||t,s=t-n,c=t,u.push({Name:e[0],Arguments:[].slice.call(e,1)||"",Element:q,"Execution Time":s})),clearTimeout(f.performance.timer),f.performance.timer=setTimeout(f.performance.display,100)},display:function(){var t=p.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=S;return s=s||g,r=q||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?(S===n&&f.initialize(),f.invoke(l)):(S!==n&&f.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.",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={"&":"&","<":"<",">":">",'"':""","'":"'","`":"`"},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);
|