11 lines
No EOL
19 KiB
JavaScript
11 lines
No EOL
19 KiB
JavaScript
/*
|
|
* # Fomantic UI - 2.9.0
|
|
* 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,E,D){"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 S,x=q(this),C=x.selector||"",w=(new Date).getTime(),F=[],T=R,A="string"==typeof T,j=[].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,n=l.regExp,i=l.fields,f=l.selector,g=l.error,e=l.namespace,r="."+e,t=e+"-module",p=q(this),h=p.find(f.prompt),s=p.find(f.searchButton),a=p.find(f.results),o=p.find(f.result),u=(p.find(f.category),this),m=p.data(t),v=!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),m=b,p.data(t,b)},destroy:function(){b.verbose("Destroying instance"),p.off(r).removeData(t)},refresh:function(){b.debug("Refreshing selector cache"),h=p.find(f.prompt),s=p.find(f.searchButton),p.find(f.category),a=p.find(f.results),o=p.find(f.result)},refreshResults:function(){a=p.find(f.results),o=p.find(f.result)},bind:{events:function(){b.verbose("Binding events to search"),l.automatic&&(p.on(b.get.inputEvent()+r,f.prompt,b.event.input),h.attr("autocomplete",b.is.chrome()?"fomantic-search":"off")),p.on("focus"+r,f.prompt,b.event.focus).on("blur"+r,f.prompt,b.event.blur).on("keydown"+r,f.prompt,b.handleKeyboard).on("click"+r,f.searchButton,b.query).on("mousedown"+r,f.results,b.event.result.mousedown).on("mouseup"+r,f.results,b.event.result.mouseup).on("click"+r,f.result,b.event.result.click)}},determine:{searchFields:function(){R&&R.searchFields!==D&&(l.searchFields=R.searchFields)}},event:{input:function(){l.searchDelay?(clearTimeout(b.timer),b.timer=setTimeout(function(){b.is.focused()&&b.query()},l.searchDelay)):b.query()},focus:function(){b.set.focus(),l.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,l.hideDelay)}var s=E.activeElement===this;s||(y=!1,b.resultsClicked?(b.debug("Determining if user action caused search to close"),p.one("click.close"+r,f.results,function(e){b.is.inMessage(e)||v?h.focus():(v=!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(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=b.get.results(),t=t.data(c.result)||b.get.result(s,a),o=b.get.value();if(q.isFunction(l.onSelect)&&!1===l.onSelect.call(u,t,a))return b.debug("Custom onSelect callback cancelled default select action"),void(v=!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?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=a.scrollTop(),n=a.height(),t<0?a.scrollTop(s+t):n<e&&a.scrollTop(s+(e-n)))},handleKeyboard:function(e){var t,s=p.find(f.result),n=p.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&&(b.is.visible()?b.hideResults():(b.verbose("Escape key pressed, blurring search field"),h.blur()),e.stopPropagation(),y=!0),b.is.visible())if(o==c){if(b.verbose("Enter key pressed, selecting active result"),0<s.filter("."+d.active).length)return b.event.result.click.call(s.filter("."+d.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(d.active),s.removeClass(d.active).eq(t).addClass(d.active).closest(n).addClass(d.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(d.active),s.removeClass(d.active).eq(t).addClass(d.active).closest(n).addClass(d.active),b.ensureVisible(s.eq(t)),e.preventDefault());else o==c&&(b.verbose("Enter key pressed, executing query"),b.query(),b.set.buttonPressed(),h.one("keyup",b.remove.buttonFocus))},setup:{api:function(n,r){var e={debug:l.debug,on:!1,cache:l.cache,action:"search",urlData:{query:n}};q.extend(!0,e,l.apiSettings,{onSuccess:function(e,t,s){b.parse.response.call(u,e,n),r(),l.apiSettings&&"function"==typeof l.apiSettings.onSuccess&&l.apiSettings.onSuccess.call(this,e,t,s)},onFailure:function(e,t,s){b.displayMessage(g.serverError),r(),l.apiSettings&&"function"==typeof l.apiSettings.onFailure&&l.apiSettings.onFailure.call(this,e,t,s)},onAbort:function(e,t,s){l.apiSettings&&"function"==typeof l.apiSettings.onAbort&&l.apiSettings.onAbort.call(this,e,t,s)},onError:function(e,t,s){b.error(),l.apiSettings&&"function"==typeof l.apiSettings.onError&&l.apiSettings.onError.call(this,e,t,s)}}),b.verbose("Setting up API request",e),p.api(e)}},can:{useAPI:function(){return q.fn.api!==D},show:function(){return b.is.focused()&&!b.is.visible()&&!b.is.empty()},transition:function(){return l.transition&&q.fn.transition!==D&&p.transition("is supported")}},is:{animating:function(){return a.hasClass(d.animating)},chrome:function(){return!!k.chrome&&!k.StyleMedia},hidden:function(){return a.hasClass(d.hidden)},inMessage:function(e){var t;if(e.target)return t=q(e.target),q.contains(E.documentElement,e.target)&&0<t.closest(f.message).length},empty:function(){return""===a.html()},visible:function(){return 0<a.filter(":visible").length},focused:function(){return 0<h.filter(":focus").length}},get:{settings:function(){q.isPlainObject(R)&&R.searchFullText&&(l.fullTextSearch=R.searchFullText,b.error(l.error.oldSearchSyntax,u)),l.ignoreDiacritics&&!String.prototype.normalize&&(l.ignoreDiacritics=!1,b.error(g.noNormalize,u))},inputEvent:function(){var e=h[0];return e!==D&&e.oninput!==D?"input":e!==D&&e.onpropertychange!==D?"propertychange":"keyup"},value:function(){return h.val()},results:function(){return p.data(c.results)},result:function(s,e){var n=!1;return s=s!==D?s:b.get.value(),e=e!==D?e:b.get.results(),"category"===l.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(d.active)}},set:{focus:function(){p.addClass(d.focus)},loading:function(){p.addClass(d.loading)},value:function(e){b.verbose("Setting search input value",e),h.val(e)},type:function(e){e=e||l.type,d[e]&&p.addClass(d[e])},buttonPressed:function(){s.addClass(d.pressed)}},remove:{loading:function(){p.removeClass(d.loading)},focus:function(){p.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=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),q.isPlainObject(l.source)||Array.isArray(l.source)?(b.search.local(t),e()):b.can.useAPI()?b.search.remote(t,e):(b.error(g.source),e())),l.onSearchQuery.call(u,t)):b.hideResults()},search:{local:function(e){var t,s=b.search.object(e,l.source);b.set.loading(),b.save.results(s),b.debug("Returned full local search results",s),0<l.maxResults&&(b.debug("Using specified max results",s),s=s.slice(0,l.maxResults)),"category"==l.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=q.isFunction(t)?t:function(){},p.api("is loading")&&p.api("abort"),b.setup.api(e,t),p.api("query")},object:function(r,t,e){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");return t=t||l.source,e=e!==D?e:l.searchFields,Array.isArray(e)||(e=[e]),t===D||!1===t?(b.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]?b.remove.diacritics(t[n]):t[n].toString()).search(u)?i(a,t):"exact"===l.fullTextSearch&&b.exactSearch(r,s)?i(o,t):!0===l.fullTextSearch&&b.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=b.generateResults(e);b.verbose("Parsing server response",e),e!==D&&t!==D&&e[i.results]!==D&&(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()&&p.api("abort")}},has:{minimumCharacters:function(){return b.get.value().length>=l.minCharacters},results:function(){return 0!==a.length&&""!=a.html()}},clear:{cache:function(e){var t=p.data(c.cache);e?t&&t[e]&&(b.debug("Removing value from cache",e),delete t[e],p.data(c.cache,t)):(b.debug("Clearing cache",e),p.removeData(c.cache))}},read:{cache:function(e){var t=p.data(c.cache);return!!l.cache&&(b.verbose("Checking cache for generated html for query",e),"object"==typeof t&&t[e]!==D&&t[e])}},create:{categoryResults:function(e){var s={};return q.each(e,function(e,t){t.category&&(s[t.category]===D?(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 t!==D?(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(d.results).appendTo(p))}},inject:{result:function(e,t,s){b.verbose("Injecting result into results");s=(s!==D?a.children().eq(s).children(f.results).first():a).children(f.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"===l.type?q.each(e,function(e,t){0<t.results.length&&(n=0,q.each(t.results,function(e,t){t.id===D&&(t.id=b.create.id(n,s)),b.inject.result(t,n,s),n++}),s++)}):q.each(e,function(e,t){t.id===D&&(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),p.data(c.results,e)}},write:{cache:function(e,t){var s=p.data(c.cache)!==D?p.data(c.cache):{};l.cache&&(b.verbose("Writing generated html to cache",e,t),s[e]=t,p.data(c.cache,s))}},addResults:function(e){if(q.isFunction(l.onResultsAdd)&&!1===l.onResultsAdd.call(a,e))return b.debug("onResultsAdd callback cancelled default action"),!1;e?(a.html(e),b.refreshResults(),l.selectFirstResult&&b.select.firstResult(),b.showResults()):b.hideResults(function(){a.empty()})},showResults:function(e){e=q.isFunction(e)?e:function(){},y||!b.is.visible()&&b.has.results()&&(b.can.transition()?(b.debug("Showing results with css animations"),a.transition({animation:l.transition+" in",debug:l.debug,verbose:l.verbose,silent:l.silent,duration:l.duration,onShow:function(){var e=p.find(f.result).eq(0);b.ensureVisible(e)},onComplete:function(){e()},queue:!0})):(b.debug("Showing results with javascript"),a.stop().fadeIn(l.duration,l.easing)),l.onResultsOpen.call(a))},hideResults:function(e){e=q.isFunction(e)?e:function(){},b.is.visible()&&(b.can.transition()?(b.debug("Hiding results with css animations"),a.transition({animation:l.transition+" out",debug:l.debug,verbose:l.verbose,silent:l.silent,duration:l.duration,onComplete:function(){e()},queue:!0})):(b.debug("Hiding results with javascript"),a.stop().fadeOut(l.duration,l.easing)),l.onResultsClose.call(a))},generateResults:function(e){b.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&&b.error(g.maxResults):e[i.results]=e[i.results].slice(0,l.maxResults)),q.isFunction(t)?r=t(e,i,l.preserveHTML):b.error(g.noTemplate,!1)):l.showNoResults&&(r=b.displayMessage(g.noResults,"empty",g.noResultsHeader)),l.onResults.call(u,e),r},displayMessage:function(e,t,s){return b.debug("Displaying message",e,t=t||"standard",s),b.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===D)return l[e];l[e]=t}},internal:function(e,t){if(q.isPlainObject(e))q.extend(!0,b,e);else{if(t===D)return b[e];b[e]=t}},debug:function(){!l.silent&&l.debug&&(l.performance?b.performance.log(arguments):(b.debug=Function.prototype.bind.call(console.info,console,l.name+":"),b.debug.apply(console,arguments)))},verbose:function(){!l.silent&&l.verbose&&l.debug&&(l.performance?b.performance.log(arguments):(b.verbose=Function.prototype.bind.call(console.info,console,l.name+":"),b.verbose.apply(console,arguments)))},error:function(){l.silent||(b.error=Function.prototype.bind.call(console.error,console,l.name+":"),b.error.apply(console,arguments))},performance:{log:function(e){var t,s;l.performance&&(s=(t=(new Date).getTime())-(w||t),w=t,F.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=l.name+":",s=0;w=!1,clearTimeout(b.performance.timer),q.each(F,function(e,t){s+=t["Execution Time"]}),e+=" "+s+"ms",C&&(e+=" '"+C+"'"),1<x.length&&(e+=" ("+x.length+")"),(console.group!==D||console.table!==D)&&0<F.length&&(console.groupCollapsed(e),console.table?console.table(F):q.each(F,function(e,t){console.log(t.Name+": "+t["Execution Time"]+"ms")}),console.groupEnd()),F=[]}},invoke:function(n,e,t){var r,i,s,a=m;return e=e||j,t=t||u,"string"==typeof n&&a!==D&&(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]!==D)return i=a[s],!1;{if(!q.isPlainObject(a[t])||e==r)return a[t]!==D&&(i=a[t]),!1;a=a[t]}}})),q.isFunction(i)?s=i.apply(t,e):i!==D&&(s=i),Array.isArray(S)?S.push(s):S!==D?S=[S,s]:s!==D&&(S=s),i}};A?(m===D&&b.initialize(),b.invoke(T)):(m!==D&&m.invoke("destroy"),b.initialize())}),S!==D?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){},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 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={"<":"<",">":">",'"':""","'":"'","`":"`"};return/[&<>"'`]/.test(e)?(e=e.replace(/&(?![a-z0-9#]{1,12};)/gi,"&")).replace(/[<>"'`]/g,function(e){return s[e]}):e},message:function(e,t,s){var n="";return e!==D&&t!==D&&(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]!==D&&(q.each(e[s.categoryResults],function(e,t){t[s.results]!==D&&0<t.results.length&&(r+='<div class="category">',t[s.categoryName]!==D&&(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]!==D&&(r+='<div class="image"> <img src="'+t[s.image].replace(/"/g,"")+'"></div>'),r+='<div class="content">',t[s.price]!==D&&(r+='<div class="price">'+i(t[s.price],n)+"</div>"),t[s.title]!==D&&(r+='<div class="title">'+i(t[s.title],n)+"</div>"),t[s.description]!==D&&(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]!==D&&(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]!==D&&(r+='<div class="image"> <img src="'+t[s.image].replace(/"/g,"")+'"></div>'),r+='<div class="content">',t[s.price]!==D&&(r+='<div class="price">'+i(t[s.price],n)+"</div>"),t[s.title]!==D&&(r+='<div class="title">'+i(t[s.title],n)+"</div>"),t[s.description]!==D&&(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)}}},q.extend(q.easing,{easeOutExpo:function(e){return 1===e?1:1-Math.pow(2,-10*e)}})}(jQuery,window,document); |