wishthis/node_modules/fomantic-ui/dist/components/search.min.js

11 lines
No EOL
20 KiB
JavaScript

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