preline
Version:
Preline UI is an open-source set of prebuilt UI components based on the utility-first Tailwind CSS framework.
9 lines • 9.1 kB
JavaScript
var t={615:(t,e,i)=>{i.d(e,{A:()=>a});class a{constructor(t,e,i){this.el=t,this.options=e,this.events=i,this.el=t,this.options=e,this.events={}}createCollection(t,e){var i;t.push({id:(null===(i=null==e?void 0:e.el)||void 0===i?void 0:i.id)||t.length+1,element:e})}fireEvent(t,e=null){if(this.events.hasOwnProperty(t))return this.events[t](e)}on(t,e){this.events[t]=e}}},926:(t,e,i)=>{i.d(e,{en:()=>s,fc:()=>n,sg:()=>a});const a=(t,e=200)=>{let i;return(...a)=>{clearTimeout(i),i=setTimeout((()=>{t.apply(void 0,a)}),e)}},n=t=>{const e=document.createElement("template");return t=t.trim(),e.innerHTML=t,e.content.firstChild},s=(t,e,i=" ",a="add")=>{t.split(i).forEach((t=>"add"===a?e.classList.add(t):e.classList.remove(t)))}}},e={};function i(a){var n=e[a];if(void 0!==n)return n.exports;var s=e[a]={exports:{}};return t[a](s,s.exports,i),s.exports}i.d=(t,e)=>{for(var a in e)i.o(e,a)&&!i.o(t,a)&&Object.defineProperty(t,a,{enumerable:!0,get:e[a]})},i.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e);var a={};i.d(a,{A:()=>o});var n=i(926),s=i(615);
/*
* HSDataTable
* @version: 2.5.0
* @author: Preline Labs Ltd.
* @license: Licensed under MIT and Preline UI Fair Use License (https://preline.co/docs/license.html)
* Copyright 2024 Preline Labs Ltd.
*/
class l extends s.A{constructor(t,e,i){var a,n,s,l,o,h,d,r,g,c,p,u,v,b,f,S,w,P,T,y,m,x,C,A,E;super(t,e,i),this.el="string"==typeof t?document.querySelector(t):t;const I=[];Array.from(this.el.querySelectorAll("thead th, thead td")).forEach(((t,e)=>{t.classList.contains("--exclude-from-ordering")&&I.push({targets:e,orderable:!1})}));const L=this.el.getAttribute("data-hs-datatable"),O=L?JSON.parse(L):{};this.concatOptions=Object.assign(Object.assign({searching:!0,lengthChange:!1,order:[],columnDefs:[...I]},O),e),this.table=this.el.querySelector("table"),this.search=null!==(a=this.el.querySelector("[data-hs-datatable-search]"))&&void 0!==a?a:null,this.pageEntities=null!==(n=this.el.querySelector("[data-hs-datatable-page-entities]"))&&void 0!==n?n:null,this.paging=null!==(s=this.el.querySelector("[data-hs-datatable-paging]"))&&void 0!==s?s:null,this.pagingPrev=null!==(l=this.el.querySelector("[data-hs-datatable-paging-prev]"))&&void 0!==l?l:null,this.pagingNext=null!==(o=this.el.querySelector("[data-hs-datatable-paging-next]"))&&void 0!==o?o:null,this.pagingPages=null!==(h=this.el.querySelector("[data-hs-datatable-paging-pages]"))&&void 0!==h?h:null,this.info=null!==(d=this.el.querySelector("[data-hs-datatable-info]"))&&void 0!==d?d:null,this.infoFrom=null!==(r=this.el.querySelector("[data-hs-datatable-info-from]"))&&void 0!==r?r:null,this.infoTo=null!==(g=this.el.querySelector("[data-hs-datatable-info-to]"))&&void 0!==g?g:null,this.infoLength=null!==(c=this.el.querySelector("[data-hs-datatable-info-length]"))&&void 0!==c?c:null,(null===(p=this.concatOptions)||void 0===p?void 0:p.rowSelectingOptions)&&(this.rowSelectingAll=null!==(S=(null===(v=null===(u=this.concatOptions)||void 0===u?void 0:u.rowSelectingOptions)||void 0===v?void 0:v.selectAllSelector)?document.querySelector(null===(f=null===(b=this.concatOptions)||void 0===b?void 0:b.rowSelectingOptions)||void 0===f?void 0:f.selectAllSelector):document.querySelector("[data-hs-datatable-row-selecting-all]"))&&void 0!==S?S:null),(null===(w=this.concatOptions)||void 0===w?void 0:w.rowSelectingOptions)&&(this.rowSelectingIndividual=null!==(m=null!==(y=null===(T=null===(P=this.concatOptions)||void 0===P?void 0:P.rowSelectingOptions)||void 0===T?void 0:T.individualSelector)&&void 0!==y?y:"[data-hs-datatable-row-selecting-individual]")&&void 0!==m?m:null),this.pageEntities&&(this.concatOptions.pageLength=parseInt(this.pageEntities.value)),this.maxPagesToShow=3,this.isRowSelecting=!!(null===(x=this.concatOptions)||void 0===x?void 0:x.rowSelectingOptions),this.pageBtnClasses=null!==(E=null===(A=null===(C=this.concatOptions)||void 0===C?void 0:C.pagingOptions)||void 0===A?void 0:A.pageBtnClasses)&&void 0!==E?E:null,this.init()}init(){this.createCollection(window.$hsDataTableCollection,this),this.initTable(),this.search&&this.initSearch(),this.pageEntities&&this.initPageEntities(),this.paging&&this.initPaging(),this.pagingPrev&&this.initPagingPrev(),this.pagingNext&&this.initPagingNext(),this.pagingPages&&this.buildPagingPages(),this.info&&this.initInfo(),this.isRowSelecting&&this.initRowSelecting()}initTable(){this.dataTable=new DataTable(this.table,this.concatOptions),this.isRowSelecting&&this.triggerChangeEventToRow(),this.dataTable.on("draw",(()=>{this.isRowSelecting&&this.updateSelectAllCheckbox(),this.isRowSelecting&&this.triggerChangeEventToRow(),this.updateInfo(),this.updatePaging()}))}initSearch(){this.search.addEventListener("input",(0,n.sg)((t=>this.onSearchInput(t.target.value))))}onSearchInput(t){this.dataTable.search(t).draw()}initPageEntities(){this.pageEntities.addEventListener("change",(t=>this.onEntitiesChange(parseInt(t.target.value))))}onEntitiesChange(t){this.dataTable.page.len(t).draw()}initInfo(){this.infoFrom&&this.initInfoFrom(),this.infoTo&&this.initInfoTo(),this.infoLength&&this.initInfoLength()}initInfoFrom(){const{start:t}=this.dataTable.page.info();this.infoFrom.innerText=`${t+1}`}initInfoTo(){const{end:t}=this.dataTable.page.info();this.infoTo.innerText=`${t}`}initInfoLength(){const{recordsTotal:t}=this.dataTable.page.info();this.infoLength.innerText=`${t}`}updateInfo(){this.initInfo()}initPaging(){this.hidePagingIfSinglePage()}hidePagingIfSinglePage(){const{pages:t}=this.dataTable.page.info();t<2?(this.paging.classList.add("hidden"),this.paging.style.display="none"):(this.paging.classList.remove("hidden"),this.paging.style.display="")}initPagingPrev(){this.pagingPrev.addEventListener("click",(()=>{this.onPrevClick()}))}onPrevClick(){this.dataTable.page("previous").draw("page")}disablePagingArrow(t,e){e?(t.classList.add("disabled"),t.setAttribute("disabled","disabled")):(t.classList.remove("disabled"),t.removeAttribute("disabled"))}initPagingNext(){this.pagingNext.addEventListener("click",(()=>{this.onNextClick()}))}onNextClick(){this.dataTable.page("next").draw("page")}buildPagingPages(){this.updatePaging()}updatePaging(){const{page:t,pages:e,length:i}=this.dataTable.page.info(),a=this.dataTable.rows({search:"applied"}).count(),s=Math.ceil(a/i),l=t+1;let o=Math.max(1,l-Math.floor(this.maxPagesToShow/2)),h=Math.min(s,o+(this.maxPagesToShow-1));h-o+1<this.maxPagesToShow&&(o=Math.max(1,h-this.maxPagesToShow+1)),this.pagingPages.innerHTML="",o>1&&(this.buildPagingPage(1),o>2&&this.pagingPages.appendChild((0,n.fc)('<span class="ellipsis">...</span>')));for(let t=o;t<=h;t++)this.buildPagingPage(t);h<s&&(h<s-1&&this.pagingPages.appendChild((0,n.fc)('<span class="ellipsis">...</span>')),this.buildPagingPage(s)),this.disablePagingArrow(this.pagingPrev,0===t),this.disablePagingArrow(this.pagingNext,t===e-1),this.hidePagingIfSinglePage()}buildPagingPage(t){const{page:e}=this.dataTable.page.info(),i=(0,n.fc)('<button type="button"></button>');i.innerText=`${t}`,i.setAttribute("data-page",`${t}`),this.pageBtnClasses&&(0,n.en)(this.pageBtnClasses,i),e===t-1&&i.classList.add("active"),i.addEventListener("click",(()=>this.onPageClick(t))),this.pagingPages.append(i)}onPageClick(t){this.dataTable.page(t-1).draw("page")}initRowSelecting(){this.rowSelectingAll.addEventListener("change",(()=>this.onSelectAllChange()))}triggerChangeEventToRow(){this.table.querySelectorAll(`tbody ${this.rowSelectingIndividual}`).forEach((t=>{t.addEventListener("change",(()=>{this.updateSelectAllCheckbox()}))}))}onSelectAllChange(){let t=this.rowSelectingAll.checked;Array.from(this.dataTable.rows({page:"current",search:"applied"}).nodes()).forEach((e=>{const i=e.querySelector(this.rowSelectingIndividual);i&&(i.checked=t)})),this.updateSelectAllCheckbox()}updateSelectAllCheckbox(){if(!this.dataTable.rows({search:"applied"}).count())return this.rowSelectingAll.checked=!1,!1;let t=!0;Array.from(this.dataTable.rows({page:"current",search:"applied"}).nodes()).forEach((e=>{const i=e.querySelector(this.rowSelectingIndividual);if(i&&!i.checked)return t=!1,!1})),this.rowSelectingAll.checked=t}static getInstance(t,e){const i=window.$hsDataTableCollection.find((e=>e.element.el===("string"==typeof t?document.querySelector(t):t)));return i?e?i:i.element.el:null}static autoInit(){window.$hsDataTableCollection||(window.$hsDataTableCollection=[]),document.querySelectorAll("[data-hs-datatable]:not(.--prevent-on-load-init)").forEach((t=>{window.$hsDataTableCollection.find((e=>{var i;return(null===(i=null==e?void 0:e.element)||void 0===i?void 0:i.el)===t}))||new l(t)}))}}window.addEventListener("load",(()=>{document.querySelectorAll("[data-hs-datatable]:not(.--prevent-on-load-init)").length&&("undefined"==typeof jQuery&&console.error("HSDataTable: jQuery is not available, please add it to the page."),"undefined"==typeof DataTable&&console.error("HSDataTable: DataTable is not available, please add it to the page.")),"undefined"!=typeof DataTable&&"undefined"!=typeof jQuery&&l.autoInit()})),"undefined"!=typeof window&&(window.HSDataTable=l);const o=l;var h=a.A;export{h as default};