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.
Plugins/features/fuzzySearch/dataTables.fuzzySearch.min.mjs

10 lines
5.4 KiB
JavaScript

/*!
* Fuzzy Search for DataTables
* 2021 SpryMedia Ltd - datatables.net/license MIT license
*
* Damerau-Levenshtein function courtesy of https://github.com/tad-lispy/node-damerau-levenshtein
* BSD 2-Clause License
* Copyright (c) 2018, Tadeusz Łazurski
* All rights reserved.
*/
import $ from"jquery";import DataTable from"datatables.net";function levenshtein(e,t,r){var a=e.length,o=t.length,n=[];r=(r||(a<o?o:a))+1;for(var s,i,c,l,u,h,f=0;f<r;f++)n[f]=[f],n[f].length=r;for(f=0;f<r;f++)n[0][f]=f;if(Math.abs(a-o)>(r||100))return d(r||100);if(0===a)return d(o);if(0===o)return d(a);for(f=1;f<=a;++f)for(i=e[f-1],s=1;s<=o;++s){if(f===s&&4<n[f][s])return d(a);c=t[s-1],u=n[f-1][s]+1,(h=n[f][s-1]+1)<u&&(u=h),(h=n[f-1][s-1]+(l=i===c?0:1))<u&&(u=h),n[f][s]=1<f&&1<s&&i===t[s-2]&&e[f-2]===c&&(h=n[f-2][s-2]+l)<u?h:u}return d(n[a][o]);function d(e){var t=Math.max(a,o),t=0===t?0:e/t;return{steps:e,relative:t,similarity:1-t}}}function fuzzySearch(e,t,r){if(void 0===e||0===e.length)return{pass:!0,score:""};for(var a=void 0!==r.threshold?r.threshold:.5,o=e.split(/ /g),n=[],s=0;s<o.length;s++)0===o[s].length||" "===o[s]?(o.splice(s,1),s--):n.length<o.length&&n.push({pass:!1,score:0});for(var i=0;i<t.length;i++){t[i]=t[i].toLowerCase();for(var c=t[i].split(/ /g),l=0;l<c.length;l++)0!==c[l].length&&" "!==c[l]||(c.splice(l,1),s--);for(s=0;s<o.length;s++){for(var u={pass:void 0,score:0},l=0;l<c.length;l++){0===c[l].indexOf(o[s])&&(h=o[s].length/c[l].length,u={pass:!0,score:u.score<h?h:u.score});var h=levenshtein(o[s],c[l]).similarity;h>u.score&&(u.score=h)}(n[s].score<u.score||u.pass)&&(n[s]={pass:!(!u.pass&&!n[s].pass)||u.score>a,score:u.score})}}for(i=0;i<n.length;i++)if(!n[i].pass)return{pass:!1,score:Math.round(n.reduce((e,t)=>e+t.score,0)/n.length*100)+"%"};return{pass:!0,score:Math.round(n.reduce((e,t)=>e+t.score,0)/n.length*100)+"%"}}DataTable.ext.search.push(function(e,t,r){var a,o=e.oInit.fuzzySearch;if(o){if(void 0!==e.aoData[r]._fuzzySearch)return a=e.aoData[r]._fuzzySearch.score,void 0!==o.rankColumn&&(e.aoData[r].anCells[o.rankColumn].innerHTML=a,e.aoData[r]._aSortData[o.rankColumn]=+a.substring(0,a.length-1)),e.aoData[r]._fuzzySearch.pass;void 0!==o.rankColumn&&(e.aoData[r].anCells[o.rankColumn].innerHTML="",e.aoData[r]._aSortData[o.rankColumn]="")}return!0}),$(document).on("init.dt",function(e,t){var r,a,o,n,s,i,c,l,u,h,f,d,p=new DataTable.Api(t),g=p.init(),v=g.fuzzySearch;function b(e){(s.attr("blurred")?(s.css({filter:"blur(0px)"}).removeAttr("blurred"),l.removeAttr("highlighted").css(n),c):(s.css({filter:"blur(1px)"}).attr("blurred",!0),c.removeAttr("highlighted").css(n),l)).attr("highlighted",!0).css(o),f(e)}function z(e,t){e.attr("highlighted")||b(t)}function m(){i.remove()}v&&(r=!1,a=$("div.dataTables_filter input",p.table().container()),o={"font-weight":"600","background-color":"rgba(255,255,255,0.1)"},n={"font-weight":"500","background-color":"transparent"},d={border:"none",background:"none","font-size":"100%",width:"50%",display:"inline-block",color:"white",cursor:"pointer",padding:"0.5em"},"object"==typeof v&&v.toggleSmart&&(s=$('<button class="toggleSearch">Abc</button>').insertAfter(a).css({border:"none",background:"none",position:"relative",right:"33px",top:"0px",cursor:"pointer",color:"#3b5e99","margin-top":"1px"}),c=$('<button class="toggleSearch">Exact</button>').insertAfter(a).css(d).css(o).attr("highlighted","true"),l=$('<button class="toggleSearch">Fuzzy</button>').insertAfter(a).css(d),a.css({"padding-right":"30px"}),$(a.parent()).css("right","-33px").css("position","relative"),d=$("<div>Search Type<div>").css({"padding-bottom":"0.5em","font-size":"0.8em"}),i=$('<div class="fuzzyToolTip"></div>').css({position:"absolute",top:"2em",background:"white","border-radius":"4px","text-align":"center",padding:"0.5em","background-color":"#16232a","box-shadow":"4px 4px 4px rgba(0, 0, 0, 0.5)",color:"white",transition:"opacity 0.25s","z-index":"30001",width:a.outerWidth()-3}).append(d).append(c).append(l)),$(t.nTable).off("search.dt.DT"),f=function(e){("input"!==e.type||void 0!==g.search&&g.search.return)&&"Enter"!==e.key&&"click"!==e.type||(s&&!s.attr("blurred")?(p.rows().iterator("row",function(e,t){e.aoData[t]._fuzzySearch=void 0},!1),h=a.val(),u=h,r=!0,p.search(h),r=!1,h=""):(u=a.val(),h="",void 0!==u&&0!==u.length&&(u=u.toLowerCase()),p.rows().iterator("row",function(e,t){e.aoData[t]._fuzzyS