11 lines
No EOL
20 KiB
JavaScript
11 lines
No EOL
20 KiB
JavaScript
/*
|
|
* # Fomantic UI - 2.9.2
|
|
* https://github.com/fomantic/Fomantic-UI
|
|
* https://fomantic-ui.com/
|
|
*
|
|
* Copyright 2023 Contributors
|
|
* Released under the MIT license
|
|
* https://opensource.org/licenses/MIT
|
|
*
|
|
*/
|
|
!function(F,k,D){"use strict";function E(e){return"function"==typeof e&&"number"!=typeof e.nodeType}k=void 0!==k&&k.Math===Math?k:globalThis,F.fn.search=function(R){var S,x=F(this),C=x.selector||"",w=Date.now(),T=[],A=R,j="string"==typeof A,q=[].slice.call(arguments,1);return x.each(function(){var d=F.isPlainObject(R)?F.extend(!0,{},F.fn.search.settings,R):F.extend({},F.fn.search.settings),f=d.className,c=d.metadata,r=d.regExp,i=d.fields,g=d.selector,h=d.error,e=d.namespace,n="."+e,t=e+"-module",v=F(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(n).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()+n,g.prompt,b.event.input),p.attr("autocomplete",b.is.chrome()?"fomantic-search":"off")),v.on("focus"+n,g.prompt,b.event.focus).on("blur"+n,g.prompt,b.event.blur).on("keydown"+n,g.prompt,b.handleKeyboard).on("click"+n,g.searchButton,b.query).on("mousedown"+n,g.results,b.event.result.mousedown).on("mouseup"+n,g.results,b.event.result.mouseup).on("click"+n,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(b.hideResults,d.hideDelay)}var s=D.activeElement===this;s||(y=!1,b.resultsClicked?(b.debug("Determining if user action caused search to close"),v.one("click.close"+n,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=F(this),s=t.find(g.title).eq(0),r=t.is("a[href]")?t:t.find("a[href]").eq(0),n=r.attr("href")||!1,i=r.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(E(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),n&&(e.preventDefault(),b.verbose("Opening search link found in result",r),"_blank"===i||e.ctrlKey?k.open(n):k.location.href=n)}}},ensureVisible:function(e){var t,s,r;0!==e.length&&(e=(t=e.position().top)+e.outerHeight(!0),s=a.scrollTop(),r=a.height(),t<0?a.scrollTop(s+t):r<e&&a.scrollTop(s+(e-r)))},handleKeyboard:function(e){var t,s=v.find(g.result),r=v.find(g.category),n=s.filter("."+f.active),i=s.index(n),a=s.length,n=0<n.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&&n?(b.verbose("Up key pressed, changing active result"),t=i-1<0?i:i-1,r.removeClass(f.active),s.removeClass(f.active).eq(t).addClass(f.active).closest(r).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,r.removeClass(f.active),s.removeClass(f.active).eq(t).addClass(f.active).closest(r).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(r,n){var e={debug:d.debug,on:!1,cache:d.cache,action:"search",urlData:{query:r}};F.extend(!0,e,d.apiSettings,{onSuccess:function(e,t,s){b.parse.response.call(u,e,r),n(),d.apiSettings&&"function"==typeof d.apiSettings.onSuccess&&d.apiSettings.onSuccess.call(this,e,t,s)},onFailure:function(e,t,s){b.displayMessage(h.serverError),n(),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!==F.fn.api},show:function(){return b.is.focused()&&!b.is.visible()&&!b.is.empty()},transition:function(){return d.transition&&void 0!==F.fn.transition}},is:{animating:function(){return a.hasClass(f.animating)},chrome:function(){return!!k.chrome&&!k.StyleMedia},hidden:function(){return a.hasClass(f.hidden)},inMessage:function(e){var t;if(e.target)return t=F(e.target),F.contains(D.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(){F.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 r=!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),F.each(e,function(e,t){if(Array.isArray(t.results)&&(r=b.search.object(s,t.results)[0]))return!1})):(b.debug("Finding result in results object",s),r=b.search.object(s,e)[0]),r||!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=E(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),b.addResults(s.html),b.inject.id(s.results),e()):(b.debug("Querying for",t),F.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=E(t)?t:function(){},v.api("is loading")&&v.api("abort"),b.setup.api(e,t),v.api("query")},object:function(n,e,t){n=b.remove.diacritics(String(n));function i(e,t){var s=-1===F.inArray(t,a),r=-1===F.inArray(t,c),n=-1===F.inArray(t,o);s&&r&&n&&e.push(t)}var a=[],o=[],c=[],s=n.replace(r.escape,"\\$&"),u=new RegExp(r.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 F.each(e,function(e,s){var r=[];F.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(r.push(t),e<l)return!0;t=r.join(" ")}"all"!==d.fullTextSearch&&-1!==t.search(u)?i(a,s):"exact"===d.fullTextSearch&&b.exactSearch(n,t)||"some"===d.fullTextSearch&&b.wordSearch(n,t)||"all"===d.fullTextSearch&&b.wordSearch(n,t,!0)?i(o,s):!0===d.fullTextSearch&&b.fuzzySearch(n,t)&&i(c,s)}})}),F.merge(o,c),F.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 r=e.split(/\s+/),n=r.length,i=!1,a=0;a<n&&!(!(i=b.exactSearch(r[a],t))&&s||i&&!s);a++);return i},fuzzySearch:function(e,t){var s=t.length,r=e.length;if("string"!=typeof e)return!1;if(e=e.toLowerCase(),t=t.toLowerCase(),s<r)return!1;if(r===s)return e===t;for(var n=0,i=0;n<r;n++){for(var a=!1,o=e.charCodeAt(n);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 F.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=F("<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,r=0;return"category"===d.type?F.each(e,function(e,t){0<t.results.length&&(r=0,F.each(t.results,function(e,t){void 0===t.id&&(t.id=b.create.id(r,s)),b.inject.result(t,r,s),r++}),s++)}):F.each(e,function(e,t){void 0===t.id&&(t.id=b.create.id(r)),b.inject.result(t,r),r++}),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(E(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=E(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=E(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=F.isPlainObject(e[i.results])&&!F.isEmptyObject(e[i.results]),r=Array.isArray(e[i.results])&&0<e[i.results].length,n="";return s||r?(0<d.maxResults&&(s?"standard"===d.type&&b.error(h.maxResults):e[i.results]=e[i.results].slice(0,d.maxResults)),E(t)?n=t(e,i,d.preserveHTML):b.error(h.noTemplate,!1)):d.showNoResults&&(n=b.displayMessage(h.noResults,"empty",h.noResultsHeader)),d.onResults.call(u,e),n},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(F.isPlainObject(e))F.extend(!0,d,e);else{if(void 0===t)return d[e];d[e]=t}},internal:function(e,t){if(F.isPlainObject(e))F.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())-(w||t),w=t,T.push({Name:e[0],Arguments:[].slice.call(e,1)||"",Element:u,"Execution Time":s})),clearTimeout(b.performance.timer),b.performance.timer=setTimeout(b.performance.display,500)},display:function(){var e=d.name+":",s=0;w=!1,clearTimeout(b.performance.timer),F.each(T,function(e,t){s+=t["Execution Time"]}),e+=" "+s+"ms",C&&(e+=" '"+C+"'"),1<x.length&&(e+=" ("+x.length+")"),0<T.length&&(console.groupCollapsed(e),console.table?console.table(T):F.each(T,function(e,t){console.log(t.Name+": "+t["Execution Time"]+"ms")}),console.groupEnd()),T=[]}},invoke:function(r,e,t){var n,i,s,a=l;return e=e||q,t=t||u,"string"==typeof r&&void 0!==a&&(r=r.split(/[ .]/),n=r.length-1,F.each(r,function(e,t){var s=e!==n?t+r[e+1].charAt(0).toUpperCase()+r[e+1].slice(1):r;if(F.isPlainObject(a[s])&&e!==n)a=a[s];else{if(void 0!==a[s])return i=a[s],!1;{if(!F.isPlainObject(a[t])||e===n)return void 0!==a[t]?i=a[t]:b.error(h.method,r),!1;a=a[t]}}})),E(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}};j?(void 0===l&&b.initialize(),b.invoke(A)):(void 0!==l&&l.invoke("destroy"),b.initialize())}),void 0!==S?S:this},F.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",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={"<":"<",">":">",'"':""","'":"'","`":"`"};return/["&'<>`]/.test(e)?(e=e.replace(/&(?![\d#a-z]{1,12};)/gi,"&")).replace(/["'<>`]/g,function(e){return s[e]}):e},message:function(e,t,s){var r="";return void 0!==e&&void 0!==t&&(r+='<div class="message '+t+'">',s&&(r+='<div class="header">'+s+"</div>"),r=r+' <div class="description">'+e+"</div></div>"),r},category:function(e,s,r){var n="",i=F.fn.search.settings.templates.escape;return void 0!==e[s.categoryResults]&&(F.each(e[s.categoryResults],function(e,t){void 0!==t[s.results]&&0<t.results.length&&(n+='<div class="category">',void 0!==t[s.categoryName]&&(n+='<div class="name">'+i(t[s.categoryName],r)+"</div>"),n+='<div class="results">',F.each(t.results,function(e,t){n+=t[s.url]?'<a class="result" href="'+t[s.url].replace(/"/g,"")+'">':'<a class="result">',void 0!==t[s.image]&&(n+='<div class="image"> <img src="'+t[s.image].replace(/"/g,"")+'"></div>'),n+='<div class="content">',void 0!==t[s.price]&&(n+='<div class="price">'+i(t[s.price],r)+"</div>"),void 0!==t[s.title]&&(n+='<div class="title">'+i(t[s.title],r)+"</div>"),void 0!==t[s.description]&&(n+='<div class="description">'+i(t[s.description],r)+"</div>"),n+="</div></a>"}),n+="</div></div>")}),e[s.action]&&(n+=!1===s.actionURL?'<div class="action">'+i(e[s.action][s.actionText],r)+"</div>":'<a href="'+e[s.action][s.actionURL].replace(/"/g,"")+'" class="action">'+i(e[s.action][s.actionText],r)+"</a>"),n)},standard:function(e,s,r){var n="",i=F.fn.search.settings.templates.escape;return void 0!==e[s.results]&&(F.each(e[s.results],function(e,t){n+=t[s.url]?'<a class="result" href="'+t[s.url].replace(/"/g,"")+'">':'<a class="result">',void 0!==t[s.image]&&(n+='<div class="image"> <img src="'+t[s.image].replace(/"/g,"")+'"></div>'),n+='<div class="content">',void 0!==t[s.price]&&(n+='<div class="price">'+i(t[s.price],r)+"</div>"),void 0!==t[s.title]&&(n+='<div class="title">'+i(t[s.title],r)+"</div>"),void 0!==t[s.description]&&(n+='<div class="description">'+i(t[s.description],r)+"</div>"),n+="</div></a>"}),e[s.action]&&(n+=!1===s.actionURL?'<div class="action">'+i(e[s.action][s.actionText],r)+"</div>":'<a href="'+e[s.action][s.actionURL].replace(/"/g,"")+'" class="action">'+i(e[s.action][s.actionText],r)+"</a>"),n)}}},F.extend(F.easing,{easeOutExpo:function(e){return 1===e?1:1-Math.pow(2,-10*e)}})}(jQuery,window,document); |