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
11 KiB
11 lines
11 KiB
/*
|
|
* # Semantic UI - 1.7.0
|
|
* 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(s){var r,a=e(this),i=a.selector||"",o=(new Date).getTime(),l=[],c=arguments[0],u="string"==typeof c,d=[].slice.call(arguments,1);return e(this).each(function(){var f,m=e.extend(!0,{},e.fn.search.settings,s),p=m.className,h=m.selector,g=m.error,v=m.namespace,y="."+v,b=v+"-module",w=e(this),R=w.find(h.prompt),C=w.find(h.searchButton),x=w.find(h.results),k=(w.find(h.result),w.find(h.category),this),A=w.data(b);f={initialize:function(){f.verbose("Initializing module");var e=R[0],t=e!==n&&e.oninput!==n?"input":e!==n&&e.onpropertychange!==n?"propertychange":"keyup";m.automatic&&R.on(t+y,f.throttle),R.on("focus"+y,f.event.focus).on("blur"+y,f.event.blur).on("keydown"+y,f.handleKeyboard),C.on("click"+y,f.query),x.on("mousedown"+y,f.event.result.mousedown).on("mouseup"+y,f.event.result.mouseup).on("click"+y,h.result,f.event.result.click),f.instantiate()},instantiate:function(){f.verbose("Storing instance of module",f),A=f,w.data(b,f)},destroy:function(){f.verbose("Destroying instance"),w.removeData(b),R.off(y),C.off(y),x.off(y)},event:{focus:function(){w.addClass(p.focus),clearTimeout(f.timer),f.throttle(),f.has.minimumCharacters()&&f.showResults()},blur:function(){f.cancel(),w.removeClass(p.focus),f.resultsClicked||(f.timer=setTimeout(f.hideResults,m.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(".title");r.html()}if("default"==m.onSelect||"default"==m.onSelect.call(this,s)){var a=n.find("a[href]").eq(0),r=n.find(h.title).eq(0),i=a.attr("href")||!1,o=a.attr("target")||!1,l=r.length>0?r.text():!1;f.hideResults(),l&&R.val(l),i&&("_blank"==o||s.ctrlKey?t.open(i):t.location.href=i)}}}},handleKeyboard:function(e){var t,s=w.find(h.result),n=w.find(h.category),r=e.which,a={backspace:8,enter:13,escape:27,upArrow:38,downArrow:40},i=p.active,o=s.index(s.filter("."+i)),l=s.length;if(r==a.escape&&(f.verbose("Escape key pressed, blurring search field"),R.trigger("blur")),x.filter(":visible").length>0)if(r==a.enter){if(f.verbose("Enter key pressed, selecting active result"),s.filter("."+i).length>0)return f.event.result.click.call(s.filter("."+i),e),e.preventDefault(),!1}else r==a.upArrow?(f.verbose("Up key pressed, changing active result"),t=0>o-1?o:o-1,n.removeClass(i),s.removeClass(i).eq(t).addClass(i).closest(n).addClass(i),e.preventDefault()):r==a.downArrow&&(f.verbose("Down key pressed, changing active result"),t=o+1>=l?o:o+1,n.removeClass(i),s.removeClass(i).eq(t).addClass(i).closest(n).addClass(i),e.preventDefault());else r==a.enter&&(f.verbose("Enter key pressed, executing query"),f.query(),C.addClass(p.down),R.one("keyup",function(){C.removeClass(p.down)}))},query:function(){var t=R.val(),s=f.read.cache(t);s?(f.debug("Reading result for "+t+" from cache"),f.addResults(s)):(f.debug("Querying for "+t),e.isPlainObject(m.source)||e.isArray(m.source)?f.search.local(t):m.apiSettings?f.search.remote(t):e.fn.api!==n&&e.api.settings.api.search!==n?(f.debug("Searching with default search API endpoint"),m.apiSettings={action:"search"},f.search.remote(t)):f.error(g.source),m.onSearchQuery.call(k,t))},search:{local:function(t){var s,n=[],r=[],a=e.isArray(m.searchFields)?m.searchFields:[m.searchFields],i=new RegExp("(?:s|^)"+t,"i"),o=new RegExp(t,"i");w.addClass(p.loading),e.each(a,function(t,s){e.each(m.source,function(t,a){var l="string"==typeof a[s],c=-1==e.inArray(a,n)&&-1==e.inArray(a,r);l&&c&&(a[s].match(i)?n.push(a):m.searchFullText&&a[s].match(o)&&r.push(a))})}),s=f.generateResults({results:e.merge(n,r)}),w.removeClass(p.loading),f.write.cache(t,s),f.addResults(s)},remote:function(t){var s,n={stateContext:w,urlData:{query:t},onSuccess:function(e){s=f.generateResults(e),f.write.cache(t,s),f.addResults(s)},onFailure:f.error};f.cancel(),f.debug("Executing search"),e.extend(!0,n,m.apiSettings),e.api(n)}},throttle:function(){clearTimeout(f.timer),f.has.minimumCharacters()?f.timer=setTimeout(f.query,m.searchDelay):f.hideResults()},cancel:function(){var e=w.data("xhr")||!1;e&&"resolved"!=e.state()&&(f.debug("Cancelling last search"),e.abort())},has:{minimumCharacters:function(){var e=R.val(),t=e.length;return t>=m.minCharacters}},read:{cache:function(e){var t=w.data("cache");return m.cache&&"object"==typeof t&&t[e]!==n?t[e]:!1}},write:{cache:function(e,t){var s=w.data("cache")!==n?w.data("cache"):{};s[e]=t,w.data("cache",s)}},addResults:function(e){("default"==m.onResultsAdd||"default"==m.onResultsAdd.call(x,e))&&x.html(e),f.showResults()},showResults:function(){0===x.filter(":visible").length&&R.filter(":focus").length>0&&""!==x.html()&&(m.transition&&e.fn.transition!==n&&w.transition("is supported")&&!x.transition("is inward")?(f.debug("Showing results with css animations"),x.transition({animation:m.transition+" in",duration:m.duration,queue:!0})):(f.debug("Showing results with javascript"),x.stop().fadeIn(m.duration,m.easing)),m.onResultsOpen.call(x))},hideResults:function(){x.filter(":visible").length>0&&(m.transition&&e.fn.transition!==n&&w.transition("is supported")&&!x.transition("is outward")?(f.debug("Hiding results with css animations"),x.transition({animation:m.transition+" out",duration:m.duration,queue:!0})):(f.debug("Hiding results with javascript"),x.stop().fadeIn(m.duration,m.easing)),m.onResultsClose.call(x))},generateResults:function(t){f.debug("Generating html from response",t);var s=m.templates[m.type],n="";return e.isPlainObject(t.results)&&!e.isEmptyObject(t.results)||e.isArray(t.results)&&t.results.length>0?(m.maxResults>0&&(t.results=e.isArray(t.results)?t.results.slice(0,m.maxResults):t.results),e.isFunction(s)?n=s(t):f.error(g.noTemplate,!1)):n=f.displayMessage(g.noResults,"empty"),m.onResults.call(k,t),n},displayMessage:function(e,t){return t=t||"standard",f.debug("Displaying message",e,t),f.addResults(m.templates.message(e,t)),m.templates.message(e,t)},setting:function(t,s){if(e.isPlainObject(t))e.extend(!0,m,t);else{if(s===n)return m[t];m[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(){m.debug&&(m.performance?f.performance.log(arguments):(f.debug=Function.prototype.bind.call(console.info,console,m.name+":"),f.debug.apply(console,arguments)))},verbose:function(){m.verbose&&m.debug&&(m.performance?f.performance.log(arguments):(f.verbose=Function.prototype.bind.call(console.info,console,m.name+":"),f.verbose.apply(console,arguments)))},error:function(){f.error=Function.prototype.bind.call(console.error,console,m.name+":"),f.error.apply(console,arguments)},performance:{log:function(e){var t,s,n;m.performance&&(t=(new Date).getTime(),n=o||t,s=t-n,o=t,l.push({Name:e[0],Arguments:[].slice.call(e,1)||"",Element:k,"Execution Time":s})),clearTimeout(f.performance.timer),f.performance.timer=setTimeout(f.performance.display,100)},display:function(){var t=m.name+":",s=0;o=!1,clearTimeout(f.performance.timer),e.each(l,function(e,t){s+=t["Execution Time"]}),t+=" "+s+"ms",i&&(t+=" '"+i+"'"),a.length>1&&(t+=" ("+a.length+")"),(console.group!==n||console.table!==n)&&l.length>0&&(console.groupCollapsed(t),console.table?console.table(l):e.each(l,function(e,t){console.log(t.Name+": "+t["Execution Time"]+"ms")}),console.groupEnd()),l=[]}},invoke:function(t,s,a){var i,o,l,c=A;return s=s||d,a=k||a,"string"==typeof t&&c!==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(c[a])&&s!=i)c=c[a];else{if(c[a]!==n)return o=c[a],!1;if(!e.isPlainObject(c[r])||s==i)return c[r]!==n?(o=c[r],!1):!1;c=c[r]}})),e.isFunction(o)?l=o.apply(a,s):o!==n&&(l=o),e.isArray(r)?r.push(l):r!==n?r=[r,l]:l!==n&&(r=l),o}},u?(A===n&&f.initialize(),f.invoke(c)):(A!==n&&f.destroy(),f.initialize())}),r!==n?r:this},e.fn.search.settings={name:"Search Module",namespace:"search",debug:!1,verbose:!0,performance:!0,apiSettings:!1,type:"standard",minCharacters:1,source:!1,searchFields:["title","description"],searchFullText:!0,automatic:"true",hideDelay:0,searchDelay:300,maxResults:7,cache:!0,transition:"scale",duration:300,easing:"easeOutExpo",onSelect:"default",onResultsAdd:"default",onSearchQuery:function(){},onResults:function(){},onResultsOpen:function(){},onResultsClose:function(){},className:{active:"active",down:"down",focus:"focus",empty:"empty",loading:"loading"},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.",method:"The method you called is not defined."},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);
|