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.
10 lines
5.7 KiB
JavaScript
10 lines
5.7 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.
|
|
*/
|
|
!function(r){var n,o;"function"==typeof define&&define.amd?define(["jquery","datatables.net"],function(e){return r(e,window,document)}):"object"==typeof exports?(n=require("jquery"),o=function(e,t){t.fn.dataTable||require("datatables.net")(e,t)},"undefined"!=typeof window?module.exports=function(e,t){return e=e||window,t=t||n(e),o(e,t),r(t,0,e.document)}:(o(window,n),module.exports=r(n,window,window.document))):r(jQuery,window,document)}(function(z,e,t,y){"use strict";var k=z.fn.dataTable;function x(e,t,r){if(e===y||0===e.length)return{pass:!0,score:""};for(var n=r.threshold!==y?r.threshold:.5,o=e.split(/ /g),a=[],s=0;s<o.length;s++)0===o[s].length||" "===o[s]?(o.splice(s,1),s--):a.length<o.length&&a.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),u=0;u<c.length;u++)0!==c[u].length&&" "!==c[u]||(c.splice(u,1),s--);for(s=0;s<o.length;s++){for(var l={pass:y,score:0},u=0;u<c.length;u++){0===c[u].indexOf(o[s])&&(f=o[s].length/c[u].length,l={pass:!0,score:l.score<f?f:l.score});var f=function(e,t,r){var n=e.length,o=t.length,a=[];r=(r||(n<o?o:n))+1;for(var s,i,c,u,l,f,h=0;h<r;h++)a[h]=[h],a[h].length=r;for(h=0;h<r;h++)a[0][h]=h;if(Math.abs(n-o)>(r||100))return d(r||100);if(0===n)return d(o);if(0===o)return d(n);for(h=1;h<=n;++h)for(i=e[h-1],s=1;s<=o;++s){if(h===s&&4<a[h][s])return d(n);c=t[s-1],l=a[h-1][s]+1,(f=a[h][s-1]+1)<l&&(l=f),(f=a[h-1][s-1]+(u=i===c?0:1))<l&&(l=f),a[h][s]=1<h&&1<s&&i===t[s-2]&&e[h-2]===c&&(f=a[h-2][s-2]+u)<l?f:l}return d(a[n][o]);function d(e){var t=Math.max(n,o),t=0===t?0:e/t;return{steps:e,relative:t,similarity:1-t}}}(o[s],c[u]).similarity;f>l.score&&(l.score=f)}(a[s].score<l.score||l.pass)&&(a[s]={pass:!(!l.pass&&!a[s].pass)||l.score>n,score:l.score})}}for(i=0;i<a.length;i++)if(!a[i].pass)return{pass:!1,score:Math.round(a.reduce((e,t)=>e+t.score,0)/a.length*100)+"%"};return{pass:!0,score:Math.round(a.reduce((e,t)=>e+t.score,0)/a.length*100)+"%"}}return k.ext.search.push(function(e,t,r){var n,o=e.oInit.fuzzySearch;if(o){if(e.aoData[r]._fuzzySearch!==y)return n=e.aoData[r]._fuzzySearch.score,o.rankColumn!==y&&(e.aoData[r].anCells[o.rankColumn].innerHTML=n,e.aoData[r]._aSortData[o.rankColumn]=+n.substring(0,n.length-1)),e.aoData[r]._fuzzySearch.pass;o.rankColumn!==y&&(e.aoData[r].anCells[o.rankColumn].innerHTML="",e.aoData[r]._aSortData[o.rankColumn]="")}return!0}),z(t).on("init.dt",function(e,t){var r,n,o,a,s,i,c,u,l,f,h,d,p=new k.Api(t),g=p.init(),b=g.fuzzySearch;function v(e){(s.attr("blurred")?(s.css({filter:"blur(0px)"}).removeAttr("blurred"),u.removeAttr("highlighted").css(a),c):(s.css({filter:"blur(1px)"}).attr("blurred",!0),c.removeAttr("highlighted").css(a),u)).attr("highlighted",!0).css(o),h(e)}function m(e,t){e.attr("highlighted")||v(t)}function w(){i.remove()}b&&(r=!1,n=z("div.dataTables_filter input",p.table().container()),o={"font-weight":"600","background-color":"rgba(255,255,255,0.1)"},a={"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 b&&b.toggleSmart&&(s=z('<button class="toggleSearch">Abc</button>').insertAfter(n).css({border:"none",background:"none",position:"relative",right:"33px",top:"0px",cursor:"pointer",color:"#3b5e99","margin-top":"1px"}),c=z('<button class="toggleSearch">Exact</button>').insertAfter(n).css(d).css(o).attr("highlighted","true"),u=z('<button class="toggleSearch">Fuzzy</button>').insertAfter(n).css(d),n.css({"padding-right":"30px"}),z(n.parent()).css("right","-33px").css("position","relative"),d=z("<div>Search Type<div>").css({"padding-bottom":"0.5em","font-size":"0.8em"}),i=z('<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:n.outerWidth()-3}).append(d).append(c).append(u)),z(t.nTable).off("search.dt.DT"),h=function(e){("input"!==e.type||g.search!==y&&g.search.return)&&"Enter"!==e.key&&"click"!==e.type||(s&&!s.attr("blurred")?(p.rows().iterator("row",function(e,t){e.aoData[t]._fuzzySearch=y},!1),f=n.val(),l=f,r=!0,p.search(f),r=!1,f=""):(l=n.val(),f="",l!==y&&0!==l.length&&(l=l.toLowerCase()),p.rows().iterator("row",function(e,t){e.aoData[t]._fuzzySearch=x(l,e.aoData[t]._aFilterData,b)},!1),r=!0,p.search(""),n.val(l),r=!1),r=!0,p.draw(),r=!1)},(f=l="",k.Api.register)("search.fuzzy()",function(e){return e===y?l:(l=e.toLowerCase(),f=p.search(),n.val(l),p.rows().iterator("row",function(e,t){e.aoData[t]._fuzzySearch=x(l,e.aoData[t]._aFilterData,b)},!1),this)}),n.off(),s&&(s.on("click",v).on("mouseenter",function(){i.insertAfter(s).on("mouseleave",w),i.css("left",n.position().left+3),c.on("click",e=>m(c,e)),u.on("click",e=>m(u,e))}).on("mouseleave",w),n.on("mouseenter",function(){i.insertAfter(s).on("mouseleave",w),i.css("left",n.position().left+3),c.on("click",e=>m(c,e)),u.on("click",e=>m(u,e))}).on("mouseleave",function(){var e=!1;i.on("mouseenter",()=>e=!0),s.on("mouseenter",()=>e=!0),setTimeout(function(){e||w()},250)}),d=p.state.loaded(),p.on("stateSaveParams",function(e,t,r){r._fuzzySearch={active:s.attr("blurred"),val:n.val()}}),null!==d)&&d._fuzzySearch!==y&&(n.val(d._fuzzySearch.val),"true"===d._fuzzySearch.active)&&(s.click(),p.page(d.start/d.length).draw("page")),p.on("search",function(){r||n.val(p.search()!==f?p.search():l)}),n.on("input keydown",h))}),k}); |