/** * Scripts to display simple data tables. * 2/1/2020 Mack Pexton (mack@mackpexton.com) * * This is a collection of scripts from the web that are intended to enhance usage of html data tables. * There are three main scripts bundled below. They provide: * 1) sorting the table on a column * 2) freezing the table header at the top of the screen when it scrolls out of view * 3) resizing table columns * * @credits * Table sorting is from sortable.js by Adam Schwartz at https://github.com/HubSpot/sortable * The table-fixed-header.js script came from: http://github.com/oma/table-fixed-header * Column resizing is from colResizable.js by Alvaro Prieto Lauroba * The double-tap event handler for column resizing is by Sergey Margaritov (sergey@margaritov.net) */ "use strict";!function(t){t.fn.setupSimpleDataTable=function(){if(1==this.data("simpleDataTable"))return this;if(!t("thead",this).length){var e=t("tr",this).first(),i=t(""),a=t(""),r=!t("th",e).length;t("td,th",e).each(function(e,i){var n=r&&"TD"==i.nodeName?t(i.outerHTML.replace(/^$/i,"")):t(i).clone(!0);a.append(n)}),this.prepend(i.append(a)),e.remove()}return t("colgroup",this).length&&t("colgroup",this).remove(),this.hasClass("table-fixed-header")&&"ontouchstart"in document.documentElement&&(this.parent().hasClass("table-wrapper")||this.wrap(t("
").addClass("table-wrapper"))),this.data("simpleDataTable",!0),this}}(jQuery),jQuery(function(t){var e,i,a,r,n,o=16;e="table.table-sortable,table[data-sortable]",a=/^-?[£$¤]?[\d,.]+%?$/,n=/^\s+|\s+$/g,i=["click"],r={init:function(t){var i,a,n,o,s;for(null==t&&(t={}),null==t.selector&&(t.selector=e),a=document.querySelectorAll(t.selector),s=[],n=0,o=a.length;o>n;n++)i=a[n],s.push(r.initTable(i));return s},initTable:function(e){var i,a,n,o,s,d;if(t(e).setupSimpleDataTable(),1===(null!=(d=e.tHead)?d.rows.length:void 0)&&"true"!==e.getAttribute("data-sortable-initialized")){for(e.setAttribute("data-sortable-initialized","true"),n=e.querySelectorAll("thead th"),i=o=0,s=n.length;s>o;i=++o)a=n[i],"false"!==a.getAttribute("data-sortable")&&r.setupClickableTH(e,a,i);return e}},setupClickableTH:function(e,a,n){var s,d,l,c,u,h;for(l=r.getColumnType(e,n),d=function(i){var o,s,d,c,u,h,f,p,g,w,b,m,v,C,x,A,y,R,T,L,z,D,H,S;if(i.handled===!0)return!1;for(i.handled=!0,f="true"===this.getAttribute("data-sorted"),p=this.getAttribute("data-sorted-direction"),d=f?"ascending"===p?"descending":"ascending":l.defaultSortDirection,w=this.parentNode.querySelectorAll("th"),v=0,y=w.length;y>v;v++)a=w[v],a.setAttribute("data-sorted","false"),a.removeAttribute("data-sorted-direction");if(this.setAttribute("data-sorted","true"),this.setAttribute("data-sorted-direction",d),g=e.tBodies[0],h=[],f){for(S=g.rows,A=0,L=S.length;L>A;A++)s=S[A],h.push(s);for(h.reverse(),D=0,z=h.length;z>D;D++)u=h[D],g.appendChild(u)}else{for(m=null!=l.compare?l.compare:function(t,e){return e-t},o=function(t,e){return t[0]===e[0]?t[2]-e[2]:l.reverse?m(e[0],t[0]):m(t[0],e[0])},H=g.rows,c=C=0,R=H.length;R>C;c=++C)u=H[c],b=r.getNodeValue(u.cells[n]),null!=l.comparator&&(b=l.comparator(b)),h.push([b,u,c]);for(h.sort(o),x=0,T=h.length;T>x;x++)u=h[x],g.appendChild(u[1])}return t(e).trigger("tableRefresh"),"function"==typeof window.CustomEvent&&"function"==typeof e.dispatchEvent?e.dispatchEvent(new CustomEvent("Sortable.sorted",{bubbles:!0})):void 0},parseInt(t(a).css("padding-right"))c;c++)s=i[c],h.push(t(a).on(s,d));return h},getColumnType:function(t,e){var i,a,n,o,s,d,l,c,u,h,f;if(a=null!=(u=t.querySelectorAll("th")[e])?u.getAttribute("data-sortable-type"):void 0,null!=a)return r.typesObject[a];for(h=t.tBodies[0].rows,s=0,l=h.length;l>s;s++)for(i=h[s],n=r.getNodeValue(i.cells[e]),f=r.types,d=0,c=f.length;c>d;d++)if(o=f[d],o.match(n))return o;return r.typesObject.alpha},getNodeValue:function(t){var e;return t?(e=t.getAttribute("data-value"),null!==e?e:"undefined"!=typeof t.innerText?t.innerText.replace(n,""):t.textContent.replace(n,"")):""},setupTypes:function(t){var e,i,a,n;for(r.types=t,r.typesObject={},n=[],i=0,a=t.length;a>i;i++)e=t[i],n.push(r.typesObject[e.name]=e);return n}},r.setupTypes([{name:"numeric",defaultSortDirection:"descending",match:function(t){return t.match(a)},comparator:function(t){return parseFloat(t.replace(/[^0-9.-]/g,""),10)||0}},{name:"date",defaultSortDirection:"ascending",reverse:!0,match:function(t){return!isNaN(Date.parse(t))},comparator:function(t){return Date.parse(t)||0}},{name:"alpha",defaultSortDirection:"ascending",match:function(){return!0},compare:function(t,e){return t.localeCompare(e)}}]),t("head").append('\n"),r.init(),t(document).on("tableSetup",function(){r.init()}),t(document).on("tableUnsetup",function(){t("table.table-sortable,table[data-sortable]").each(function(){t("thead tr th,thead tr td",this).each(function(){t(this).removeAttr("data-sorted")})}).removeAttr("data-sortable-initialized")}),"function"==typeof define&&define.amd?define(function(){return r}):"undefined"!=typeof exports?module.exports=r:window.Sortable=r}),function(t){var e={topOffset:0,topHeader:null,topHeaderClass:".top-header",className:"table-header-fixed"};t.fn.fixedHeader=function(i){var a;return a="object"==typeof i?t.extend({},e,i):t.isNumeric(i)?t.extend({},e,{topOffset:i}):t.extend({},e),this.each(function(){function e(){if(w.is(":visible")){var t=b.scrollTop()+u;!v&&t>h&&h+p>t?v=!0:v&&(h>=t||t>=h+p)&&(v=!1),v?(c.is(":visible")||r(),c.offset({top:t>g?g:t,left:m.offset().left})):c.is(":visible")&&n()}}function i(){v&&(s(),o(),d(),e())}function r(){o(),c.show(),d()}function n(){s(),d(),c.hide()}function o(){c.css("top",u).width(m.width()),t("tr>th",m).each(function(e,i){var a,r=t(i),n=t("tr>th:nth-child("+(e+1)+")",c);n.attr("class",r.attr("class")),a=r.width()+"px",n.css({width:a,"min-width":a,"max-width":a}),n.attr("data-sorted",r.attr("data-sorted")),n.attr("data-sortable-type",r.attr("data-sortable-type")),r.attr("data-sorted-direction")?n.attr("data-sorted-direction",r.attr("data-sorted-direction")):n.removeAttr("data-sorted-direction")})}function s(){t("tr>th",c).each(function(e,i){var a=t(i),r=t("tr>th:nth-child("+(e+1)+")",m);r.attr("class",a.attr("class")),r.attr("data-sorted",a.attr("data-sorted")),r.attr("data-sortable-type",a.attr("data-sortable-type")),a.attr("data-sorted-direction")?r.attr("data-sorted-direction",a.attr("data-sorted-direction")):r.removeAttr("data-sorted-direction")})}function d(){h=m.offset().top,f=m.height(),p=w.height(),g=h+p-f,u=l()}function l(){if(null!=window.TopOffset)return window.TopOffset;var e=t(w.data("topheader"));return e.length&&e.css("position").match(/(fixed|stickey)/)?e.scrollTop()+e.height():w.data("topoffset")||0}var c,u,h,f,p,g,w=t(this),b=t(window),m=t("thead",w).first(),v=!1;m.length&&(t("."+a.className,w).length||(null==w.data("topheader")&&(null!=a.topHeader?w.data("topheader",a.topHeader):t(a.topHeaderClass).length>0&&w.data("topheader",a.topHeaderClass)),null==w.data("topoffset")&&w.data("topoffset",a.topOffset),c&&w.data("fixedHeaderSetup")||(c=m.clone(!0).addClass(a.className).css({position:"fixed","border-spacing":"0",display:"none"}).appendTo(w),w.data("fixedHeaderSetup",!0)),d(),b.on("scroll.fixedHeader",e),b.on("resize.fixedHeader",i),t(document).on("tableRefresh",function(){d(),i()}),t(document).on("tableSetup",function(){t(".table-fixed-header").setupSimpleDataTable().fixedHeader()}),t(document).on("tableUnsetup",function(){t(".table-fixed-header").removeFixedHeader()}),"ontouchstart"in document.documentElement&&t(".table-wrapper").on("scroll.fixedHeader",e),setTimeout(function(){b.trigger("scroll.fixedHeader")},10)))})},t.fn.removeFixedHeader=function(){return this.each(function(){var e=t(this);t("thead",e).each(function(e){e>0&&t(this).remove()}),e.data("fixedHeaderSetup",!1)})},t(function(){t(".table-fixed-header").setupSimpleDataTable().fixedHeader()})}(jQuery),function(t){t.event.special.doubletap={bindType:"touchend",delegateType:"touchend",handle:function(t){var e=t.handleObj,i=jQuery.data(t.target),a=(new Date).getTime(),r=i.lastTouch?a-i.lastTouch:0,n=null==n?300:n;n>r&&r>30?(i.lastTouch=null,t.type=e.origType,["clientX","clientY","pageX","pageY"].forEach(function(e){t[e]=t.originalEvent.changedTouches[0][e]}),e.handler.apply(this,arguments)):i.lastTouch=a}}}(jQuery),function(t){var e,i=t(document),a=t("head"),r=null,n={},o=0,s="id",d="px",l="JColResizer",c="JCLRFlex",u=parseInt,h=Math,f=navigator.userAgent.indexOf("Trident/4.0")>0;try{e=sessionStorage}catch(p){}a.append('\n');var g,w=function(e,i){var r=t(e);if(r.opt=i,r.mode=i.resizeMode,r.dc=r.opt.disabledColumns,r.opt.disable)return b(r);var d=r.id=r.attr(s)||l+o++;o++,r.p=r.opt.postbackSafe,r.tm=null,r.uc=!1,!r.is("table")||n[d]&&!r.opt.partialRefresh||("col-resize"!==r.opt.hoverCursor&&a.append(""),r.addClass(l).attr(s,d).before('
'),r.g=[],r.c=[],r.w=r.width(),r.gc=r.prev(),r.f=r.opt.fixed,r.cs=u(f?e.cellSpacing||e.currentStyle.borderSpacing:r.css("border-spacing"))||2,r.b=u(f?e.border||e.currentStyle.borderLeftWidth:r.css("borderLeftWidth"))||1,n[d]=r,C(r),m(r))},b=function(t){var e=t.attr(s),t=n[e];t&&t.is("table")&&(t.removeClass(l+" "+c).gc.remove(),delete n[e])},m=function(i){var a=i.find(">thead>tr:first-child>th,>thead>tr:first-child>td");a.length||(a=i.find(">tbody>tr:first-child>th,>tr:first-child>th,>tbody>tr:first-child>td, >tr:first-child>td")),a=a.filter(":visible"),i.cg=i.find("col"),i.ln=a.length,i.owt=i.w,i.ow=[],a.each(function(e){i.ow[e]=t(this).width()}),i.p&&e&&e[i.id]&&v(i,a),a.each(function(e){var a=t(this),r=-1!=i.dc.indexOf(e),n=t(i.gc.append('
')[0].lastChild);n.append(r?"":i.opt.gripInnerHtml).append('
'),e==i.ln-1&&(n.addClass("JCLRLastGrip"),i.f&&n.html("")),r?n.addClass("JCLRdisabledGrip"):n.removeClass("JCLRdisabledGrip").on("mousedown",J).on("touchstart",S).on("dblclick doubletap",j),n.t=i,n.i=e,n.c=a,a.w=a.width(),i.g.push(n),i.c.push(a),r||a.width(a.w).removeAttr("width"),a.pl=u(a.css("paddingLeft")),a.pr=u(a.css("paddingRight")),a.bl=u(a.css("borderLeftWidth")),a.br=u(a.css("borderRightWidth")),a.wp=100*(a.w+a.pl+a.pr+a.bl+a.br)/i.w,n.data(l,{i:e,t:i.attr(s),last:e==i.ln-1})}),i.cg.removeAttr("width"),i.find("td, th").not(a).not("table th, table td").each(function(){t(this).removeAttr("width")}),i.f||i.removeAttr("width").addClass(c),x(i)},v=function(t,i){var a,r,n=0,o=0,s=[];if(i){if(t.cg.removeAttr("width"),t.opt.flush)return void(e[t.id]="");if(a=e[t.id].split(";"),a.length!=t.ln+2)return void(e[t.id]="");if(r=a[t.ln+1],!t.f&&r&&(t.width(r*=1),t.opt.overflow&&t.css("min-width",r+d)),t.opt.overflow)for(;o*{cursor:"+s.opt.dragCursor+"!important}"),d.addClass(s.opt.draggingClass),r=d,s.c[o.i].l)for(var u,h=0;h7&&(o.opt.overflow&&(D(o,f),o.w=o.width()),s.width(c),r=t(this),r.l=r.position().left,r.x=s.position().left+c,u?(s.width(r.w),s.w=r.w):A(o,a,!0),r=null,u?(H(o),e[o.id]=""):(v(o),C(o),x(o))),!1},k=function(){for(var t in n)if(n.hasOwnProperty(t)){if(t=n[t],!t.is(":visible"))continue;var e,i=0;if(t.removeClass(l),t.f){for(t.w=t.width(),e=0;etr>th",n).each(function(e,r){a=t(">tr>th",i).eq(e).width()+d,t(r).css({width:a,"min-width":a,"max-width":a})}))};t(".table-col-resizable").each(function(){var i=t(this);if(!i.data("colresizable-initialized")){i.setupSimpleDataTable();var a=[];t("thead:first-child th",i).each(function(e){t.hasData(this)&&0==t(this).data("resizable")&&a.push(e)}),i.colResizable({resizeMode:"overflow",liveDrag:!0,onDrag:e,headerOnly:!1,postbackSafe:!0,flush:!!/[?&;]reset\b/.test(location.search),disabledColumns:a,touchBeforeDrag:!0}),i.data("colresizable-initialized",!0),setTimeout(function(){i.css("table-layout","fixed")},10)}})},a=function(){t(".JCLRgrips").remove(),t(".table-col-resizable").data("colresizable-initialized",!1).removeClass("JColResizer").removeClass("JCLRFlex")};e(),i.on("tableSetup",function(){e()}),i.on("tableUnsetup",function(){a()}),i.on("tableRefresh",function(){N()})})}(jQuery);