@public-ui/components
Version:
Contains all web components that belong to KoliBri - The accessible HTML-Standard.
4 lines • 14.2 kB
JavaScript
/*!
* KoliBri - The accessible HTML-Standard
*/
import{proxyCustomElement,HTMLElement,h,Fragment,Host}from"@stencil/core/internal/client";import{e as KolIconTag,a as KolTooltipWcTag,c as KolButtonWcTag}from"./component-names.js";import{t as translate}from"./i18n2.js";import{n as nonce}from"./dev.utils2.js";import{t as tryToDispatchKoliBriEvent}from"./events.js";import{z as emptyStringByArrayHandler,o as objectObjectHandler,p as parseJson,w as watchValidator,e as watchString}from"./prop.validators.js";import{v as validateTableData,a as validateTableDataFoot,d as validateTableCallbacks,b as validateTableSelection,E as Events}from"./events2.js";import{v as validateLabel}from"./label.js";const validateTableHeaderCells=(e,t)=>{emptyStringByArrayHandler(t,(()=>{objectObjectHandler(t,(()=>{try{t=parseJson(t)}catch(e){}watchValidator(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)}))}))},KolTableStateless=proxyCustomElement(class extends HTMLElement{constructor(){super(),this.__registerHost(),this.horizontal=!0,this.cellsToRenderTimeouts=new Map,this.dataToKeyMap=new Map,this.checkboxRefs=[],this.renderTableRow=(e,t,a)=>{var l,i;let o=String(t);return this.horizontal&&(null===(l=e[0])||void 0===l?void 0:l.data)&&(o=null!==(i=this.getDataKey(e[0].data))&&void 0!==i?i:o),h("tr",{key:`row-${o}`},this.renderSelectionCell(e,t),e.map(((e,l)=>this.renderTableCell(e,t,l,a))))},this.renderTableCell=(e,t,a,l)=>{let i=`${t}-${a}-${e.label}`;if(e.data){const l=this.getDataKey(e.data);i=l?`${l}-${this.horizontal?a:t}`:i}return!1===e.asTd?this.renderHeadingCell(e,t,a,l):h("td",{key:`cell-${i}`,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){validateTableData(this,e,{beforePatch:e=>{this.updateDataToKeyMap(e)}})}validateDataFoot(e){validateTableDataFoot(this,e)}validateHeaderCells(e){validateTableHeaderCells(this,e)}validateLabel(e){validateLabel(this,e,{required:!0})}validateMinWidth(e){watchString(this,"_deprecatedMinWidth",e,{defaultValue:void 0})}validateOn(e){validateTableCallbacks(this,e)}validateSelection(e){validateTableSelection(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 l=this.checkboxRefs.indexOf(a);l>-1&&(e.preventDefault(),"ArrowDown"===e.key?(l=(l+1)%this.checkboxRefs.length,this.checkboxRefs[l].focus()):"ArrowUp"===e.key&&(e.preventDefault(),l=(l+this.checkboxRefs.length-1)%this.checkboxRefs.length,this.checkboxRefs[l].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,nonce())})),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 l=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})),l<t&&(l=t)})),0===l?l=t.length:l-=(null===(a=this.state._dataFoot)||void 0===a?void 0:a.length)||0,l}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 l=this.getThePrimaryHeadersWithKeysIfExists(null!==(t=e.horizontal)&&void 0!==t?t:[]);return this.horizontal=!0,0===l.length&&(l=this.getThePrimaryHeadersWithKeysIfExists(null!==(a=e.vertical)&&void 0!==a?a:[]),l.length>0&&(this.horizontal=!1)),l}createDataField(e,t,a){var l;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 i=this.getPrimaryHeaders(t),o=this.getNumberOfCols(t.horizontal,e);let s=this.getNumberOfRows(t.vertical,e),n=0;a&&(n=s,s+=(null===(l=this.state._dataFoot)||void 0===l?void 0:l.length)||0);const r=[],c=[],d=[];t.vertical.forEach(((e,t)=>{c[t]=0,d[t]=[]}));for(let l=n;l<s;l++){const s=[];t.vertical.forEach(((e,t)=>{let a=0;if(d[t].forEach((e=>a+=e)),a<=l){const i=e[l-a+c[t]];if("object"==typeof i){s.push(Object.assign(Object.assign({},i),{asTd:!1,data:{}}));let e=1;if("number"==typeof i.rowSpan&&i.rowSpan>1&&(e=i.rowSpan),d[t].push(e),"number"==typeof i.colSpan&&i.colSpan>1)for(let a=1;a<i.colSpan;a++)d[t+a].push(e);c[t]++}}}));for(let t=0;t<o;t++)if(!0===this.horizontal){const o=a&&this.state._dataFoot?this.state._dataFoot[l-n]:e[l];"object"==typeof i[t]&&null!==i[t]&&"string"==typeof i[t].key&&"object"==typeof o&&null!==o&&s.push(Object.assign(Object.assign({},i[t]),{colSpan:void 0,data:o,label:o[i[t].key],rowSpan:void 0}))}else"object"==typeof i[l]&&null!==i[l]&&"string"==typeof i[l].key&&"object"==typeof e[t]&&null!==e[t]&&s.push(Object.assign(Object.assign({},i[l]),{colSpan:void 0,data:e[t],label:e[t][i[l].key],rowSpan:void 0}));r.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 l={colSpan:e,label:translate("kol-no-entries"),render:void 0,rowSpan:Math.max(a,1)};0===r.length?r.push([l]):r[0].push(l)}return r}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,t){var a,l,i;const o=this.state._selection;if(!o)return"";const s=null!==(a=o.keyPropertyName)&&void 0!==a?a:"id",n=null===(l=e[0])||void 0===l?void 0:l.data;if(!n)return"";const r=n[s],c=o.multiple||void 0===o.multiple,d=null===(i=null==o?void 0:o.selectedKeys)||void 0===i?void 0:i.includes(r),v=o.label(n),b={name:"selection",checked:d,id:r,"aria-label":v};return h("td",{key:`tbody-${t}-selection`,class:"selection-cell"},h("div",{class:"input "+(d?"checked":"")},c?h("label",{class:"checkbox-container"},h(KolIconTag,{class:"icon",_icons:"codicon "+(d?"codicon-check":""),_label:""}),h("input",Object.assign({ref:e=>e&&this.checkboxRefs.push(e)},b,{type:"checkbox",onInput:e=>{var t,a,l;const i=d?null===(a=null==o?void 0:o.selectedKeys)||void 0===a?void 0:a.filter((e=>e!==r)):[...null!==(t=null==o?void 0:o.selectedKeys)&&void 0!==t?t:[],r];tryToDispatchKoliBriEvent("selection-change",this.host,i),"function"==typeof(null===(l=this.state._on)||void 0===l?void 0:l[Events.onSelectionChange])&&this.state._on[Events.onSelectionChange](e,null!=i?i:[])}}))):h("label",{class:"radio-container"},h("input",Object.assign({},b,{type:"radio",onInput:e=>{var t;tryToDispatchKoliBriEvent("selection-change",this.host,r),"function"==typeof(null===(t=this.state._on)||void 0===t?void 0:t[Events.onSelectionChange])&&this.state._on[Events.onSelectionChange](e,r)}}))),h(KolTooltipWcTag,{"aria-hidden":"true",class:"input-tooltip",_align:"right",_id:`${r}-label`,_label:v})))}renderHeadingSelectionCell(){var e,t;const a=this.state._selection;if(!a||!a.multiple&&void 0!==a.multiple)return h("th",{key:"thead-0"});const l=null!==(e=a.keyPropertyName)&&void 0!==e?e:"id",i=null===(t=a.selectedKeys)||void 0===t?void 0:t.length,o=i===this.state._data.length,s=0!==i&&!o;let n="kol-table-selection-indeterminate";o&&!s&&(n="kol-table-selection-none"),0===i&&(n="kol-table-selection-all");const r=translate(n);return h("th",{key:"thead-0-selection",class:"selection-cell selection-control"},h("div",{class:"input "+(s?"indeterminate":o?"checked":"")},h("label",{class:"checkbox-container"},h(KolIconTag,{class:"icon",_icons:"codicon "+(s?"codicon-remove":o?"codicon-check":""),_label:""}),h("input",{ref:e=>e&&this.checkboxRefs.push(e),name:"selection",checked:o&&!s,"aria-label":r,type:"checkbox",onInput:e=>{var t;const a=o?[]:this.state._data.map((e=>null==e?void 0:e[l]));tryToDispatchKoliBriEvent("selection-change",this.host,a),"function"==typeof(null===(t=this.state._on)||void 0===t?void 0:t[Events.onSelectionChange])&&this.state._on[Events.onSelectionChange](e,a)}})),h(KolTooltipWcTag,{"aria-hidden":"true",class:"input-tooltip",_align:"right",_id:`${n}-label`,_label:r})))}renderHeaderTdCell(){return h(Fragment,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)&&h("td",{"aria-hidden":"true",colSpan:this.state._headerCells.vertical.length,rowSpan:this.state._headerCells.horizontal.length}))}renderHeadingCell(e,t,a,l){let i,o="codicon codicon-fold";if(e.sortDirection)switch(e.sortDirection){case"ASC":o="codicon codicon-chevron-up",i="ascending";break;case"DESC":o="codicon codicon-chevron-down",i="descending"}const s=l?"row":"number"==typeof e.colSpan&&e.colSpan>1?"colgroup":"col";return h("th",{key:`${t}-${a}-${e.label}`,class:e.textAlign?`align-${e.textAlign}`:void 0,scope:s,colSpan:e.colSpan,rowSpan:e.rowSpan,style:{width:e.width},"aria-sort":i,"data-sort":`sort-${e.sortDirection}`},e.sortDirection?h(KolButtonWcTag,{class:"table-sort-button",exportparts:"icon",_icons:{right:o},_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,t){var a,l;const i=(null===(a=this.state._headerCells.vertical)||void 0===a?void 0:a.length)||0,o=null===(l=null==t?void 0:t[0])||void 0===l?void 0:l.reduce(((e,t)=>e+(t.colSpan||1)),0),s=this.state._selection?1:0;return h("tr",{"aria-hidden":"true",class:`${e}-spacer`},h("td",{colSpan:i+o+s}))}renderFoot(){if(!this.state._dataFoot||0===this.state._dataFoot.length)return null;const e=this.createDataField(this.state._dataFoot,this.state._headerCells,!0);return h("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=[],h(Host,{key:"d052c595574b48e3ccc828fb78c9f7f5c9e38fde",class:"kol-table-stateless-wc"},h("div",{key:"9da74abb2c80036ea0a475efe8b3f589ee804b8b",ref:e=>this.tableDivElement=e,class:"table",tabindex:this.tableDivElementHasScrollbar?"-1":void 0},h("table",{key:"3e0b47f4ed6834ff9367fb45f39a6bbfc1fe0fe0",style:{minWidth:this.state._deprecatedMinWidth||this.state._minWidth}},h("div",{key:"7a1f44793c3a77450920e6b77542119a0f0fa6ed",class:"focus-element",tabindex:this.tableDivElementHasScrollbar?"0":void 0,"aria-describedby":"caption"}," "),h("caption",{key:"7708de5a275959669f6c1d083b0afa1e34bbbf39",id:"caption"},this.state._label),Array.isArray(this.state._headerCells.horizontal)&&h("thead",{key:"99d7da8bc6f894fd83b7017d79f2765c99799700"},[this.state._headerCells.horizontal.map(((e,t)=>h("tr",{key:`thead-${t}`},this.state._selection&&this.renderHeadingSelectionCell(),0===t&&this.renderHeaderTdCell(),Array.isArray(e)&&e.map(((e,a)=>!0===e.asTd?h("td",{key:`thead-${t}-${a}-${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,t,a,!1)))))),this.renderSpacer("head",this.state._headerCells.horizontal)]),h("tbody",{key:"9c045d95a6a4faa9072c2bd9f767c9801f62a849"},e.map(((e,t)=>this.renderTableRow(e,t,!0)))),this.renderFoot())))}get host(){return this}static get watchers(){return{_data:["validateData"],_dataFoot:["validateDataFoot"],_headerCells:["validateHeaderCells"],_label:["validateLabel"],_minWidth:["validateMinWidth"],_on:["validateOn"],_selection:["validateSelection"]}}},[0,"kol-table-stateless-wc",{_data:[1],_dataFoot:[1,"_data-foot"],_headerCells:[1,"_header-cells"],_label:[1],_minWidth:[1,"_min-width"],_on:[16],_selection:[1],state:[32],tableDivElementHasScrollbar:[32]},[[0,"keydown","handleKeyDown"]],{_data:["validateData"],_dataFoot:["validateDataFoot"],_headerCells:["validateHeaderCells"],_label:["validateLabel"],_minWidth:["validateMinWidth"],_on:["validateOn"],_selection:["validateSelection"]}]);function defineCustomElement$1(){if("undefined"==typeof customElements)return;["kol-table-stateless-wc"].forEach((e=>{if("kol-table-stateless-wc"===e)customElements.get(e)||customElements.define(e,KolTableStateless)}))}const KolTableStatelessWc=KolTableStateless,defineCustomElement=defineCustomElement$1;export{KolTableStatelessWc,defineCustomElement};