wishthis/semantic/dist/components/search.min.js
2022-02-23 22:46:34 +01:00

11 lines
No EOL
19 KiB
JavaScript

/*
* # Fomantic UI - 2.8.8
* https://github.com/fomantic/Fomantic-UI
* http://fomantic-ui.com/
*
* Copyright 2022 Contributors
* Released under the MIT license
* http://opensource.org/licenses/MIT
*
*/
!function(q,k,D,E){"use strict";q.isFunction=q.isFunction||function(e){return"function"==typeof e&&"number"!=typeof e.nodeType},k=void 0!==k&&k.Math==Math?k:"undefined"!=typeof self&&self.Math==Math?self:Function("return this")(),q.fn.search=function(R){var x,C=q(this),w=C.selector||"",S=(new Date).getTime(),T=[],F=R,j="string"==typeof F,A=[].slice.call(arguments,1);return q(this).each(function(){var l=q.isPlainObject(R)?q.extend(!0,{},q.fn.search.settings,R):q.extend({},q.fn.search.settings),d=l.className,c=l.metadata,m=l.regExp,i=l.fields,f=l.selector,g=l.error,e=l.namespace,n="."+e,t=e+"-module",h=q(this),v=h.find(f.prompt),s=h.find(f.searchButton),r=h.find(f.results),a=h.find(f.result),u=(h.find(f.category),this),o=h.data(t),y=!1,b=!1,p={initialize:function(){p.verbose("Initializing module"),p.get.settings(),p.determine.searchFields(),p.bind.events(),p.set.type(),p.create.results(),p.instantiate()},instantiate:function(){p.verbose("Storing instance of module",p),o=p,h.data(t,p)},destroy:function(){p.verbose("Destroying instance"),h.off(n).removeData(t)},refresh:function(){p.debug("Refreshing selector cache"),v=h.find(f.prompt),s=h.find(f.searchButton),h.find(f.category),r=h.find(f.results),a=h.find(f.result)},refreshResults:function(){r=h.find(f.results),a=h.find(f.result)},bind:{events:function(){p.verbose("Binding events to search"),l.automatic&&(h.on(p.get.inputEvent()+n,f.prompt,p.event.input),v.attr("autocomplete",p.is.chrome()?"fomantic-search":"off")),h.on("focus"+n,f.prompt,p.event.focus).on("blur"+n,f.prompt,p.event.blur).on("keydown"+n,f.prompt,p.handleKeyboard).on("click"+n,f.searchButton,p.query).on("mousedown"+n,f.results,p.event.result.mousedown).on("mouseup"+n,f.results,p.event.result.mouseup).on("click"+n,f.result,p.event.result.click)}},determine:{searchFields:function(){R&&R.searchFields!==E&&(l.searchFields=R.searchFields)}},event:{input:function(){l.searchDelay?(clearTimeout(p.timer),p.timer=setTimeout(function(){p.is.focused()&&p.query()},l.searchDelay)):p.query()},focus:function(){p.set.focus(),l.searchOnFocus&&p.has.minimumCharacters()&&p.query(function(){p.can.show()&&p.showResults()})},blur:function(e){function t(){p.cancel.query(),p.remove.focus(),p.timer=setTimeout(p.hideResults,l.hideDelay)}var s=D.activeElement===this;s||(b=!1,p.resultsClicked?(p.debug("Determining if user action caused search to close"),h.one("click.close"+n,f.results,function(e){p.is.inMessage(e)||y?v.focus():(y=!1,p.is.animating()||p.is.hidden()||t())})):(p.debug("Input blurred without user action, closing results"),t()))},result:{mousedown:function(){p.resultsClicked=!0},mouseup:function(){p.resultsClicked=!1},click:function(e){p.debug("Search result selected");var t=q(this),s=t.find(f.title).eq(0),n=t.is("a[href]")?t:t.find("a[href]").eq(0),r=n.attr("href")||!1,i=n.attr("target")||!1,s=0<s.length&&s.text(),a=p.get.results(),t=t.data(c.result)||p.get.result(s,a),o=p.get.value();if(q.isFunction(l.onSelect)&&!1===l.onSelect.call(u,t,a))return p.debug("Custom onSelect callback cancelled default select action"),void(y=!0);p.hideResults(),s&&p.get.value()===o&&p.set.value(s),r&&(e.preventDefault(),p.verbose("Opening search link found in result",n),"_blank"==i||e.ctrlKey?k.open(r):k.location.href=r)}}},ensureVisible:function(e){var t,s,n;0!==e.length&&(e=(t=e.position().top)+e.outerHeight(!0),s=r.scrollTop(),n=r.height(),t<0?r.scrollTop(s+t):n<e&&r.scrollTop(s+(e-n)))},handleKeyboard:function(e){var t,s=h.find(f.result),n=h.find(f.category),r=s.filter("."+d.active),i=s.index(r),a=s.length,r=0<r.length,o=e.which,c=13,u=38,l=40;if(o==27&&(p.verbose("Escape key pressed, blurring search field"),p.hideResults(),b=!0),p.is.visible())if(o==c){if(p.verbose("Enter key pressed, selecting active result"),0<s.filter("."+d.active).length)return p.event.result.click.call(s.filter("."+d.active),e),e.preventDefault(),!1}else o==u&&r?(p.verbose("Up key pressed, changing active result"),t=i-1<0?i:i-1,n.removeClass(d.active),s.removeClass(d.active).eq(t).addClass(d.active).closest(n).addClass(d.active),p.ensureVisible(s.eq(t)),e.preventDefault()):o==l&&(p.verbose("Down key pressed, changing active result"),t=a<=i+1?i:i+1,n.removeClass(d.active),s.removeClass(d.active).eq(t).addClass(d.active).closest(n).addClass(d.active),p.ensureVisible(s.eq(t)),e.preventDefault());else o==c&&(p.verbose("Enter key pressed, executing query"),p.query(),p.set.buttonPressed(),v.one("keyup",p.remove.buttonFocus))},setup:{api:function(t,s){var e={debug:l.debug,on:!1,cache:l.cache,action:"search",urlData:{query:t},onSuccess:function(e){p.parse.response.call(u,e,t),s()},onFailure:function(){p.displayMessage(g.serverError),s()},onAbort:function(e){},onError:p.error};q.extend(!0,e,l.apiSettings),p.verbose("Setting up API request",e),h.api(e)}},can:{useAPI:function(){return q.fn.api!==E},show:function(){return p.is.focused()&&!p.is.visible()&&!p.is.empty()},transition:function(){return l.transition&&q.fn.transition!==E&&h.transition("is supported")}},is:{animating:function(){return r.hasClass(d.animating)},chrome:function(){return!(!k.chrome||!k.chrome.webstore&&!k.chrome.runtime)},hidden:function(){return r.hasClass(d.hidden)},inMessage:function(e){var t;if(e.target)return t=q(e.target),q.contains(D.documentElement,e.target)&&0<t.closest(f.message).length},empty:function(){return""===r.html()},visible:function(){return 0<r.filter(":visible").length},focused:function(){return 0<v.filter(":focus").length}},get:{settings:function(){q.isPlainObject(R)&&R.searchFullText&&(l.fullTextSearch=R.searchFullText,p.error(l.error.oldSearchSyntax,u)),l.ignoreDiacritics&&!String.prototype.normalize&&(l.ignoreDiacritics=!1,p.error(g.noNormalize,u))},inputEvent:function(){var e=v[0];return e!==E&&e.oninput!==E?"input":e!==E&&e.onpropertychange!==E?"propertychange":"keyup"},value:function(){return v.val()},results:function(){return h.data(c.results)},result:function(s,e){var n=!1;return s=s!==E?s:p.get.value(),e=e!==E?e:p.get.results(),"category"===l.type?(p.debug("Finding result that matches",s),q.each(e,function(e,t){if(Array.isArray(t.results)&&(n=p.search.object(s,t.results)[0]))return!1})):(p.debug("Finding result in results object",s),n=p.search.object(s,e)[0]),n||!1}},select:{firstResult:function(){p.verbose("Selecting first result"),a.first().addClass(d.active)}},set:{focus:function(){h.addClass(d.focus)},loading:function(){h.addClass(d.loading)},value:function(e){p.verbose("Setting search input value",e),v.val(e)},type:function(e){e=e||l.type,"category"==l.type&&h.addClass(l.type)},buttonPressed:function(){s.addClass(d.pressed)}},remove:{loading:function(){h.removeClass(d.loading)},focus:function(){h.removeClass(d.focus)},buttonPressed:function(){s.removeClass(d.pressed)},diacritics:function(e){return l.ignoreDiacritics?e.normalize("NFD").replace(/[\u0300-\u036f]/g,""):e}},query:function(e){e=q.isFunction(e)?e:function(){};var t=p.get.value(),s=p.read.cache(t);e=e||function(){},p.has.minimumCharacters()?(s?(p.debug("Reading result from cache",t),p.save.results(s.results),p.addResults(s.html),p.inject.id(s.results),e()):(p.debug("Querying for",t),q.isPlainObject(l.source)||Array.isArray(l.source)?(p.search.local(t),e()):p.can.useAPI()?p.search.remote(t,e):(p.error(g.source),e())),l.onSearchQuery.call(u,t)):p.hideResults()},search:{local:function(e){var t,s=p.search.object(e,l.source);p.set.loading(),p.save.results(s),p.debug("Returned full local search results",s),0<l.maxResults&&(p.debug("Using specified max results",s),s=s.slice(0,l.maxResults)),"category"==l.type&&(s=p.create.categoryResults(s)),t=p.generateResults({results:s}),p.remove.loading(),p.addResults(t),p.inject.id(s),p.write.cache(e,{html:t,results:s})},remote:function(e,t){t=q.isFunction(t)?t:function(){},h.api("is loading")&&h.api("abort"),p.setup.api(e,t),h.api("query")},object:function(r,t,e){r=p.remove.diacritics(String(r));function i(e,t){var s=-1==q.inArray(t,a),n=-1==q.inArray(t,c),r=-1==q.inArray(t,o);s&&n&&r&&e.push(t)}var a=[],o=[],c=[],s=r.replace(m.escape,"\\$&"),u=new RegExp(m.beginsWith+s,"i");return t=t||l.source,e=e!==E?e:l.searchFields,Array.isArray(e)||(e=[e]),t===E||!1===t?(p.error(g.source),[]):(q.each(e,function(e,n){q.each(t,function(e,t){var s;"string"!=typeof t[n]&&"number"!=typeof t[n]||(-1!==(s="string"==typeof t[n]?p.remove.diacritics(t[n]):t[n].toString()).search(u)?i(a,t):"exact"===l.fullTextSearch&&p.exactSearch(r,s)?i(o,t):1==l.fullTextSearch&&p.fuzzySearch(r,s)&&i(c,t))})}),q.merge(o,c),q.merge(a,o),a)}},exactSearch:function(e,t){return e=e.toLowerCase(),-1<(t=t.toLowerCase()).indexOf(e)},fuzzySearch:function(e,t){var s=t.length,n=e.length;if("string"!=typeof e)return!1;if(e=e.toLowerCase(),t=t.toLowerCase(),s<n)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){Array.isArray(e)&&((s={})[i.results]=e,e=s);var s=p.generateResults(e);p.verbose("Parsing server response",e),e!==E&&t!==E&&e[i.results]!==E&&(p.addResults(s),p.inject.id(e[i.results]),p.write.cache(t,{html:s,results:e[i.results]}),p.save.results(e[i.results]))}},cancel:{query:function(){p.can.useAPI()&&h.api("abort")}},has:{minimumCharacters:function(){return p.get.value().length>=l.minCharacters},results:function(){return 0!==r.length&&""!=r.html()}},clear:{cache:function(e){var t=h.data(c.cache);e?t&&t[e]&&(p.debug("Removing value from cache",e),delete t[e],h.data(c.cache,t)):(p.debug("Clearing cache",e),h.removeData(c.cache))}},read:{cache:function(e){var t=h.data(c.cache);return!!l.cache&&(p.verbose("Checking cache for generated html for query",e),"object"==typeof t&&t[e]!==E&&t[e])}},create:{categoryResults:function(e){var s={};return q.each(e,function(e,t){t.category&&(s[t.category]===E?(p.verbose("Creating new category of results",t.category),s[t.category]={name:t.category,results:[t]}):s[t.category].results.push(t))}),s},id:function(e,t){var s,e=e+1;return t!==E?(t=String.fromCharCode(97+t),p.verbose("Creating category result id",s=t+e)):p.verbose("Creating result id",s=e),s},results:function(){0===r.length&&(r=q("<div />").addClass(d.results).appendTo(h))}},inject:{result:function(e,t,s){p.verbose("Injecting result into results");s=(s!==E?r.children().eq(s).children(f.results).first():r).children(f.result).eq(t);p.verbose("Injecting results metadata",s),s.data(c.result,e)},id:function(e){p.debug("Injecting unique ids into results");var s=0,n=0;return"category"===l.type?q.each(e,function(e,t){0<t.results.length&&(n=0,q.each(t.results,function(e,t){t.id===E&&(t.id=p.create.id(n,s)),p.inject.result(t,n,s),n++}),s++)}):q.each(e,function(e,t){t.id===E&&(t.id=p.create.id(n)),p.inject.result(t,n),n++}),e}},save:{results:function(e){p.verbose("Saving current search results to metadata",e),h.data(c.results,e)}},write:{cache:function(e,t){var s=h.data(c.cache)!==E?h.data(c.cache):{};l.cache&&(p.verbose("Writing generated html to cache",e,t),s[e]=t,h.data(c.cache,s))}},addResults:function(e){if(q.isFunction(l.onResultsAdd)&&!1===l.onResultsAdd.call(r,e))return p.debug("onResultsAdd callback cancelled default action"),!1;e?(r.html(e),p.refreshResults(),l.selectFirstResult&&p.select.firstResult(),p.showResults()):p.hideResults(function(){r.empty()})},showResults:function(e){e=q.isFunction(e)?e:function(){},b||!p.is.visible()&&p.has.results()&&(p.can.transition()?(p.debug("Showing results with css animations"),r.transition({animation:l.transition+" in",debug:l.debug,verbose:l.verbose,duration:l.duration,onShow:function(){var e=h.find(f.result).eq(0);p.ensureVisible(e)},onComplete:function(){e()},queue:!0})):(p.debug("Showing results with javascript"),r.stop().fadeIn(l.duration,l.easing)),l.onResultsOpen.call(r))},hideResults:function(e){e=q.isFunction(e)?e:function(){},p.is.visible()&&(p.can.transition()?(p.debug("Hiding results with css animations"),r.transition({animation:l.transition+" out",debug:l.debug,verbose:l.verbose,duration:l.duration,onComplete:function(){e()},queue:!0})):(p.debug("Hiding results with javascript"),r.stop().fadeOut(l.duration,l.easing)),l.onResultsClose.call(r))},generateResults:function(e){p.debug("Generating html from response",e);var t=l.templates[l.type],s=q.isPlainObject(e[i.results])&&!q.isEmptyObject(e[i.results]),n=Array.isArray(e[i.results])&&0<e[i.results].length,r="";return s||n?(0<l.maxResults&&(s?"standard"==l.type&&p.error(g.maxResults):e[i.results]=e[i.results].slice(0,l.maxResults)),q.isFunction(t)?r=t(e,i,l.preserveHTML):p.error(g.noTemplate,!1)):l.showNoResults&&(r=p.displayMessage(g.noResults,"empty",g.noResultsHeader)),l.onResults.call(u,e),r},displayMessage:function(e,t,s){return p.debug("Displaying message",e,t=t||"standard",s),p.addResults(l.templates.message(e,t,s)),l.templates.message(e,t,s)},setting:function(e,t){if(q.isPlainObject(e))q.extend(!0,l,e);else{if(t===E)return l[e];l[e]=t}},internal:function(e,t){if(q.isPlainObject(e))q.extend(!0,p,e);else{if(t===E)return p[e];p[e]=t}},debug:function(){!l.silent&&l.debug&&(l.performance?p.performance.log(arguments):(p.debug=Function.prototype.bind.call(console.info,console,l.name+":"),p.debug.apply(console,arguments)))},verbose:function(){!l.silent&&l.verbose&&l.debug&&(l.performance?p.performance.log(arguments):(p.verbose=Function.prototype.bind.call(console.info,console,l.name+":"),p.verbose.apply(console,arguments)))},error:function(){l.silent||(p.error=Function.prototype.bind.call(console.error,console,l.name+":"),p.error.apply(console,arguments))},performance:{log:function(e){var t,s;l.performance&&(s=(t=(new Date).getTime())-(S||t),S=t,T.push({Name:e[0],Arguments:[].slice.call(e,1)||"",Element:u,"Execution Time":s})),clearTimeout(p.performance.timer),p.performance.timer=setTimeout(p.performance.display,500)},display:function(){var e=l.name+":",s=0;S=!1,clearTimeout(p.performance.timer),q.each(T,function(e,t){s+=t["Execution Time"]}),e+=" "+s+"ms",w&&(e+=" '"+w+"'"),1<C.length&&(e+=" ("+C.length+")"),(console.group!==E||console.table!==E)&&0<T.length&&(console.groupCollapsed(e),console.table?console.table(T):q.each(T,function(e,t){console.log(t.Name+": "+t["Execution Time"]+"ms")}),console.groupEnd()),T=[]}},invoke:function(n,e,t){var r,i,s,a=o;return e=e||A,t=u||t,"string"==typeof n&&a!==E&&(n=n.split(/[\. ]/),r=n.length-1,q.each(n,function(e,t){var s=e!=r?t+n[e+1].charAt(0).toUpperCase()+n[e+1].slice(1):n;if(q.isPlainObject(a[s])&&e!=r)a=a[s];else{if(a[s]!==E)return i=a[s],!1;{if(!q.isPlainObject(a[t])||e==r)return a[t]!==E&&(i=a[t]),!1;a=a[t]}}})),q.isFunction(i)?s=i.apply(t,e):i!==E&&(s=i),Array.isArray(x)?x.push(s):x!==E?x=[x,s]:s!==E&&(x=s),i}};j?(o===E&&p.initialize(),p.invoke(F)):(o!==E&&o.invoke("destroy"),p.initialize())}),x!==E?x:this},q.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:["id","title","description"],displayField:"",fullTextSearch:"exact",ignoreDiacritics:!1,automatic:!0,hideDelay:0,searchDelay:200,maxResults:7,cache:!0,showNoResults:!0,preserveHTML:!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",noResultsHeader:"No Results",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.",oldSearchSyntax:"searchFullText setting has been renamed fullTextSearch for consistency, please adjust your settings.",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.",noNormalize:'"ignoreDiacritics" setting will be ignored. Browser does not support String().normalize(). You may consider including <https://cdn.jsdelivr.net/npm/unorm@1.4.1/lib/unorm.min.js> as a polyfill.'},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,t){if(t)return e;var s={"<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#x27;","`":"&#x60;"};return/[&<>"'`]/.test(e)?(e=e.replace(/&(?![a-z0-9#]{1,6};)/,"&amp;")).replace(/[<>"'`]/g,function(e){return s[e]}):e},message:function(e,t,s){var n="";return e!==E&&t!==E&&(n+='<div class="message '+t+'">',s&&(n+='<div class="header">'+s+"</div>"),n=n+' <div class="description">'+e+"</div></div>"),n},category:function(e,s,n){var r="",i=q.fn.search.settings.templates.escape;return e[s.categoryResults]!==E&&(q.each(e[s.categoryResults],function(e,t){t[s.results]!==E&&0<t.results.length&&(r+='<div class="category">',t[s.categoryName]!==E&&(r+='<div class="name">'+i(t[s.categoryName],n)+"</div>"),r+='<div class="results">',q.each(t.results,function(e,t){t[s.url]?r+='<a class="result" href="'+t[s.url].replace(/"/g,"")+'">':r+='<a class="result">',t[s.image]!==E&&(r+='<div class="image"> <img src="'+t[s.image].replace(/"/g,"")+'"></div>'),r+='<div class="content">',t[s.price]!==E&&(r+='<div class="price">'+i(t[s.price],n)+"</div>"),t[s.title]!==E&&(r+='<div class="title">'+i(t[s.title],n)+"</div>"),t[s.description]!==E&&(r+='<div class="description">'+i(t[s.description],n)+"</div>"),r+="</div></a>"}),r+="</div></div>")}),e[s.action]&&(!1===s.actionURL?r+='<div class="action">'+i(e[s.action][s.actionText],n)+"</div>":r+='<a href="'+e[s.action][s.actionURL].replace(/"/g,"")+'" class="action">'+i(e[s.action][s.actionText],n)+"</a>"),r)},standard:function(e,s,n){var r="",i=q.fn.search.settings.templates.escape;return e[s.results]!==E&&(q.each(e[s.results],function(e,t){t[s.url]?r+='<a class="result" href="'+t[s.url].replace(/"/g,"")+'">':r+='<a class="result">',t[s.image]!==E&&(r+='<div class="image"> <img src="'+t[s.image].replace(/"/g,"")+'"></div>'),r+='<div class="content">',t[s.price]!==E&&(r+='<div class="price">'+i(t[s.price],n)+"</div>"),t[s.title]!==E&&(r+='<div class="title">'+i(t[s.title],n)+"</div>"),t[s.description]!==E&&(r+='<div class="description">'+i(t[s.description],n)+"</div>"),r+="</div></a>"}),e[s.action]&&(!1===s.actionURL?r+='<div class="action">'+i(e[s.action][s.actionText],n)+"</div>":r+='<a href="'+e[s.action][s.actionURL].replace(/"/g,"")+'" class="action">'+i(e[s.action][s.actionText],n)+"</a>"),r)}}}}(jQuery,window,document);