UNPKG

@public-ui/components

Version:

Contains all web components that belong to KoliBri - The accessible HTML-Standard.

4 lines 12.9 kB
/*! * KoliBri - The accessible HTML-Standard */ import{r as e,h as t,F as i,H as s,g as n}from"./index-f0de5c73.js";import{g as o,a,c as l}from"./component-names-6ca7e863.js";import{t as r}from"./i18n-5fe08f1c.js";import{n as d}from"./dev.utils-0b748391.js";import{t as c}from"./events-29e84d75.js";import{q as h,o as f,p as u,w as v,i as p}from"./prop.validators-64749272.js";import{v as b,a as y,d as m,b as k,E as g}from"./events-5b899bd2.js";import{v as w}from"./label-e8ec0e3a.js";import"./i18n-7ea9f137.js";import"./reuse-32931294.js";import"./dev.utils-fab45cbc.js";import"./index-da3e0c00.js";import"./common-0f1f2276.js";const _=(e,t)=>{h(t,(()=>{f(t,(()=>{try{t=u(t)}catch(e){}v(e,"_headerCells",(e=>"object"==typeof e&&null!==e&&(void 0===e.horizontal||Array.isArray(e.horizontal)&&void 0===e.horizontal.find((e=>!Array.isArray(e))))&&(void 0===e.vertical||Array.isArray(e.vertical)&&void 0===e.vertical.find((e=>!Array.isArray(e))))&&!0),new Set(["TableHeaderCellsPropType"]),t)}))}))},j=class{constructor(a){e(this,a),this.horizontal=!0,this.cellsToRenderTimeouts=new Map,this.dataToKeyMap=new Map,this.checkboxRefs=[],this.renderTableRow=(e,a,i)=>{var l,o;let s=String(a);return this.horizontal&&(null===(l=e[0])||void 0===l?void 0:l.data)&&(s=null!==(o=this.getDataKey(e[0].data))&&void 0!==o?o:s),t("tr",{key:`row-${s}`},this.renderSelectionCell(e,a),e.map(((e,t)=>this.renderTableCell(e,a,t,i))))},this.renderTableCell=(e,a,i,l)=>{let o=`${a}-${i}-${e.label}`;if(e.data){const t=this.getDataKey(e.data);o=t?`${t}-${this.horizontal?i:a}`:o}return!1===e.asTd?this.renderHeadingCell(e,a,i,l):t("td",{key:`cell-${o}`,class:{[e.textAlign]:"string"==typeof e.textAlign&&e.textAlign.length>0},colSpan:e.colSpan,rowSpan:e.rowSpan,style:{textAlign:e.textAlign,width:e.width},ref:"function"==typeof e.render?t=>{this.cellRender(e,t)}:void 0},"function"!=typeof e.render?e.label:"")},this.state={_data:[],_label:"",_headerCells:{horizontal:[],vertical:[]}},this.tableDivElementHasScrollbar=!1,this._data=void 0,this._dataFoot=void 0,this._headerCells=void 0,this._label=void 0,this._minWidth=void 0,this._on=void 0,this._selection=void 0}validateData(e){b(this,e,{beforePatch:e=>{this.updateDataToKeyMap(e)}})}validateDataFoot(e){y(this,e)}validateHeaderCells(e){_(this,e)}validateLabel(e){w(this,e,{required:!0})}validateMinWidth(e){p(this,"_deprecatedMinWidth",e,{defaultValue:void 0})}validateOn(e){m(this,e)}validateSelection(e){k(this,e)}handleKeyDown(e){var t;if("ArrowUp"===e.key||"ArrowDown"===e.key){const a=null===(t=this.tableDivElement)||void 0===t?void 0:t.querySelector(":focus");let i=this.checkboxRefs.indexOf(a);i>-1&&(e.preventDefault(),"ArrowDown"===e.key?(i=(i+1)%this.checkboxRefs.length,this.checkboxRefs[i].focus()):"ArrowUp"===e.key&&(e.preventDefault(),i=(i+this.checkboxRefs.length-1)%this.checkboxRefs.length,this.checkboxRefs[i].focus()))}}componentDidRender(){this.checkDivElementScrollbar()}componentDidLoad(){this.tableDivElement&&ResizeObserver&&(this.tableDivElementResizeObserver=new ResizeObserver(this.checkDivElementScrollbar.bind(this)),this.tableDivElementResizeObserver.observe(this.tableDivElement))}disconnectedCallback(){var e;null===(e=this.tableDivElementResizeObserver)||void 0===e||e.disconnect()}checkDivElementScrollbar(){this.tableDivElement&&(this.tableDivElementHasScrollbar=this.tableDivElement.scrollWidth>this.tableDivElement.clientWidth)}updateDataToKeyMap(e){e.forEach((e=>{this.dataToKeyMap.has(e)||this.dataToKeyMap.set(e,d())})),this.dataToKeyMap.forEach(((t,a)=>{e.includes(a)||this.dataToKeyMap.delete(a)}))}getDataKey(e){return this.dataToKeyMap.get(e)}cellRender(e,t){t&&(clearTimeout(this.cellsToRenderTimeouts.get(t)),this.cellsToRenderTimeouts.set(t,setTimeout((()=>{if("function"==typeof e.render){const a=e.render(t,e,e.data,this.state._data);"string"==typeof a&&(t.textContent=a)}}))))}getNumberOfCols(e,t){let a=0;return e.forEach((e=>{let t=0;Array.isArray(e)&&e.forEach((e=>{var a;return t+=null!==(a=e.colSpan)&&void 0!==a?a:1})),a<t&&(a=t)})),0===a&&(a=t.length),a}getNumberOfRows(e,t){var a;let i=0;return e.forEach((e=>{let t=0;Array.isArray(e)&&e.forEach((e=>{var a;return t+=null!==(a=e.rowSpan)&&void 0!==a?a:1})),i<t&&(i=t)})),0===i?i=t.length:i-=(null===(a=this.state._dataFoot)||void 0===a?void 0:a.length)||0,i}getThePrimaryHeadersWithKeysIfExists(e){const t=[];return e.forEach((e=>{e.forEach((e=>{"string"==typeof e.key&&t.push(e)}))})),t}getPrimaryHeaders(e){var t,a;let i=this.getThePrimaryHeadersWithKeysIfExists(null!==(t=e.horizontal)&&void 0!==t?t:[]);return this.horizontal=!0,0===i.length&&(i=this.getThePrimaryHeadersWithKeysIfExists(null!==(a=e.vertical)&&void 0!==a?a:[]),i.length>0&&(this.horizontal=!1)),i}createDataField(e,t,a){var i;t.horizontal=Array.isArray(null==t?void 0:t.horizontal)?t.horizontal:[],t.vertical=Array.isArray(null==t?void 0:t.vertical)?t.vertical:[];const l=this.getPrimaryHeaders(t),o=this.getNumberOfCols(t.horizontal,e);let s=this.getNumberOfRows(t.vertical,e),n=0;a&&(n=s,s+=(null===(i=this.state._dataFoot)||void 0===i?void 0:i.length)||0);const c=[],d=[],h=[];t.vertical.forEach(((e,t)=>{d[t]=0,h[t]=[]}));for(let i=n;i<s;i++){const s=[];t.vertical.forEach(((e,t)=>{let a=0;if(h[t].forEach((e=>a+=e)),a<=i){const l=e[i-a+d[t]];if("object"==typeof l){s.push(Object.assign(Object.assign({},l),{asTd:!1,data:{}}));let e=1;if("number"==typeof l.rowSpan&&l.rowSpan>1&&(e=l.rowSpan),h[t].push(e),"number"==typeof l.colSpan&&l.colSpan>1)for(let a=1;a<l.colSpan;a++)h[t+a].push(e);d[t]++}}}));for(let t=0;t<o;t++)if(!0===this.horizontal){const o=a&&this.state._dataFoot?this.state._dataFoot[i-n]:e[i];"object"==typeof l[t]&&null!==l[t]&&"string"==typeof l[t].key&&"object"==typeof o&&null!==o&&s.push(Object.assign(Object.assign({},l[t]),{colSpan:void 0,data:o,label:o[l[t].key],rowSpan:void 0}))}else"object"==typeof l[i]&&null!==l[i]&&"string"==typeof l[i].key&&"object"==typeof e[t]&&null!==e[t]&&s.push(Object.assign(Object.assign({},l[i]),{colSpan:void 0,data:e[t],label:e[t][l[i].key],rowSpan:void 0}));c.push(s)}if(0===e.length){let e=0,a=0;Array.isArray(t.horizontal)&&t.horizontal.length>0&&t.horizontal[0].forEach((t=>{e+=t.colSpan||1})),Array.isArray(t.vertical)&&t.vertical.length>0&&(e-=t.vertical.length,t.vertical[0].forEach((e=>{a+=e.rowSpan||1})));const i={colSpan:e,label:r("kol-no-entries"),render:void 0,rowSpan:Math.max(a,1)};0===c.length?c.push([i]):c[0].push(i)}return c}componentWillLoad(){this.validateData(this._data),this.validateDataFoot(this._dataFoot),this.validateHeaderCells(this._headerCells),this.validateLabel(this._label),this.validateMinWidth(this._minWidth),this.validateOn(this._on),this.validateSelection(this._selection)}renderSelectionCell(e,i){var l,s,n;const r=this.state._selection;if(!r)return"";const d=null!==(l=r.keyPropertyName)&&void 0!==l?l:"id",h=null===(s=e[0])||void 0===s?void 0:s.data;if(!h)return"";const v=h[d],p=r.multiple||void 0===r.multiple,b=null===(n=null==r?void 0:r.selectedKeys)||void 0===n?void 0:n.includes(v),f=r.label(h),u={name:"selection",checked:b,id:v,"aria-label":f};return t("td",{key:`tbody-${i}-selection`,class:"selection-cell"},t("div",{class:"input "+(b?"checked":"")},p?t("label",{class:"checkbox-container"},t(o,{class:"icon",_icons:"codicon "+(b?"codicon-check":""),_label:""}),t("input",Object.assign({ref:e=>e&&this.checkboxRefs.push(e)},u,{type:"checkbox",onInput:e=>{var t,a,i;const l=b?null===(a=null==r?void 0:r.selectedKeys)||void 0===a?void 0:a.filter((e=>e!==v)):[...null!==(t=null==r?void 0:r.selectedKeys)&&void 0!==t?t:[],v];c("selection-change",this.host,l),"function"==typeof(null===(i=this.state._on)||void 0===i?void 0:i[g.onSelectionChange])&&this.state._on[g.onSelectionChange](e,null!=l?l:[])}}))):t("label",{class:"radio-container"},t("input",Object.assign({},u,{type:"radio",onInput:e=>{var t;c("selection-change",this.host,v),"function"==typeof(null===(t=this.state._on)||void 0===t?void 0:t[g.onSelectionChange])&&this.state._on[g.onSelectionChange](e,v)}}))),t(a,{"aria-hidden":"true",class:"input-tooltip",_align:"right",_id:`${v}-label`,_label:f})))}renderHeadingSelectionCell(){var e,i;const l=this.state._selection;if(!l||!l.multiple&&void 0!==l.multiple)return t("th",{key:"thead-0"});const s=null!==(e=l.keyPropertyName)&&void 0!==e?e:"id",n=null===(i=l.selectedKeys)||void 0===i?void 0:i.length,d=n===this.state._data.length,h=0!==n&&!d;let v="kol-table-selection-indeterminate";d&&!h&&(v="kol-table-selection-none"),0===n&&(v="kol-table-selection-all");const p=r(v);return t("th",{key:"thead-0-selection",class:"selection-cell selection-control"},t("div",{class:"input "+(h?"indeterminate":d?"checked":"")},t("label",{class:"checkbox-container"},t(o,{class:"icon",_icons:"codicon "+(h?"codicon-remove":d?"codicon-check":""),_label:""}),t("input",{ref:e=>e&&this.checkboxRefs.push(e),name:"selection",checked:d&&!h,"aria-label":p,type:"checkbox",onInput:e=>{var t;const a=d?[]:this.state._data.map((e=>null==e?void 0:e[s]));c("selection-change",this.host,a),"function"==typeof(null===(t=this.state._on)||void 0===t?void 0:t[g.onSelectionChange])&&this.state._on[g.onSelectionChange](e,a)}})),t(a,{"aria-hidden":"true",class:"input-tooltip",_align:"right",_id:`${v}-label`,_label:p})))}renderHeaderTdCell(){return t(i,null,Array.isArray(this.state._headerCells.horizontal)&&this.state._headerCells.horizontal.length>0&&Array.isArray(this.state._headerCells.vertical)&&this.state._headerCells.vertical.length>0&&Array.isArray(this.state._headerCells.horizontal)&&t("td",{"aria-hidden":"true",colSpan:this.state._headerCells.vertical.length,rowSpan:this.state._headerCells.horizontal.length}))}renderHeadingCell(e,a,i,o){let s,n="codicon codicon-fold";if(e.sortDirection)switch(e.sortDirection){case"ASC":n="codicon codicon-chevron-up",s="ascending";break;case"DESC":n="codicon codicon-chevron-down",s="descending"}const r=o?"row":"number"==typeof e.colSpan&&e.colSpan>1?"colgroup":"col";return t("th",{key:`${a}-${i}-${e.label}`,class:e.textAlign?`align-${e.textAlign}`:void 0,scope:r,colSpan:e.colSpan,rowSpan:e.rowSpan,style:{width:e.width},"aria-sort":s,"data-sort":`sort-${e.sortDirection}`},e.sortDirection?t(l,{class:"table-sort-button",exportparts:"icon",_icons:{right:n},_label:e.label,_on:{onClick:t=>{var a;"function"==typeof(null===(a=this.state._on)||void 0===a?void 0:a.onSort)&&e.key&&e.sortDirection&&this.state._on.onSort(t,{key:e.key,currentSortDirection:e.sortDirection})}}}):e.label)}renderSpacer(e,a){var i,l;const o=(null===(i=this.state._headerCells.vertical)||void 0===i?void 0:i.length)||0,s=null===(l=null==a?void 0:a[0])||void 0===l?void 0:l.reduce(((e,t)=>e+(t.colSpan||1)),0),n=this.state._selection?1:0;return t("tr",{"aria-hidden":"true",class:`${e}-spacer`},t("td",{colSpan:o+s+n}))}renderFoot(){if(!this.state._dataFoot||0===this.state._dataFoot.length)return null;const e=this.createDataField(this.state._dataFoot,this.state._headerCells,!0);return t("tfoot",null,[this.renderSpacer("foot",e),e.map(((e,t)=>this.renderTableRow(e,t,!0)))])}render(){const e=this.createDataField(this.state._data,this.state._headerCells);return this.checkboxRefs=[],t(s,{key:"d052c595574b48e3ccc828fb78c9f7f5c9e38fde",class:"kol-table-stateless-wc"},t("div",{key:"9da74abb2c80036ea0a475efe8b3f589ee804b8b",ref:e=>this.tableDivElement=e,class:"table",tabindex:this.tableDivElementHasScrollbar?"-1":void 0},t("table",{key:"3e0b47f4ed6834ff9367fb45f39a6bbfc1fe0fe0",style:{minWidth:this.state._deprecatedMinWidth||this.state._minWidth}},t("div",{key:"7a1f44793c3a77450920e6b77542119a0f0fa6ed",class:"focus-element",tabindex:this.tableDivElementHasScrollbar?"0":void 0,"aria-describedby":"caption"}," "),t("caption",{key:"7708de5a275959669f6c1d083b0afa1e34bbbf39",id:"caption"},this.state._label),Array.isArray(this.state._headerCells.horizontal)&&t("thead",{key:"99d7da8bc6f894fd83b7017d79f2765c99799700"},[this.state._headerCells.horizontal.map(((e,a)=>t("tr",{key:`thead-${a}`},this.state._selection&&this.renderHeadingSelectionCell(),0===a&&this.renderHeaderTdCell(),Array.isArray(e)&&e.map(((e,i)=>!0===e.asTd?t("td",{key:`thead-${a}-${i}-${e.label}`,class:{[e.textAlign]:"string"==typeof e.textAlign&&e.textAlign.length>0},colSpan:e.colSpan,rowSpan:e.rowSpan,style:{textAlign:e.textAlign,width:e.width},ref:"function"==typeof e.render?t=>{this.cellRender(e,t)}:void 0},"function"!=typeof e.render?e.label:""):this.renderHeadingCell(e,a,i,!1)))))),this.renderSpacer("head",this.state._headerCells.horizontal)]),t("tbody",{key:"9c045d95a6a4faa9072c2bd9f767c9801f62a849"},e.map(((e,t)=>this.renderTableRow(e,t,!0)))),this.renderFoot())))}get host(){return n(this)}static get watchers(){return{_data:["validateData"],_dataFoot:["validateDataFoot"],_headerCells:["validateHeaderCells"],_label:["validateLabel"],_minWidth:["validateMinWidth"],_on:["validateOn"],_selection:["validateSelection"]}}};export{j as kol_table_stateless_wc};