kempo-ui 0.0.77 → 0.0.78
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/Table.js +88 -78
- package/docs/src/components/Table.js +88 -78
- package/package.json +1 -1
- package/src/components/Table.js +136 -186
package/dist/components/Table.js
CHANGED
|
@@ -1,65 +1,64 @@
|
|
|
1
|
-
import{html,css,unsafeStatic,literal}from"../lit-all.min.js";import ShadowComponent from"./ShadowComponent.js";import{toTitleCase}from"../utils/string.js";import{boolExists}from"../utils/propConverters.js";const selected=Symbol("selected"),hidden=Symbol("hidden"),index=Symbol("index"),editing=Symbol("editing");export default class Table extends ShadowComponent{static properties={enablePages:{type:Boolean,reflect:!0,converter:boolExists,attribute:"enable-pages"},pageSize:{type:Number,reflect:!0,attribute:"page-size"},currentPage:{type:Number,reflect:!0,attribute:"current-page"},pageSizeOptions:{type:Array,attribute:"page-size-options"},enableSelection:{type:Boolean,reflect:!0,converter:boolExists,attribute:"enable-selection"},enableSorting:{type:Boolean,reflect:!0,converter:boolExists,attribute:"enable-sorting"},caseSensitiveFilters:{type:Boolean,reflect:!0,converter:boolExists,attribute:"case-sensitive-filters"},fields:{type:Array},records:{type:Array},filters:{type:Array},sort:{type:Array},columnSizes:{type:Object},fetchPending:{type:Boolean}};constructor(e={}){super(),void 0===this.pageSize&&(this.pageSize=50),void 0===this.currentPage&&(this.currentPage=1),void 0===this.pageSizeOptions&&(this.pageSizeOptions=[10,25,50,100,500]),void 0===this.records&&(this.records=e.records||[]),void 0===this.fields&&(this.fields=e.fields||[]),void 0===this.filters&&(this.filters=e.filters||[]),void 0===this.sort&&(this.sort=[]),void 0===this.columnSizes&&(this.columnSizes={}),void 0===this.fetchPending&&(this.fetchPending=!1)}handleSelectAllChange=e=>{e.target.checked?this.selectAllOnPage():this.deselectAllOnPage()};handleFieldClick=e=>{const t=this.sort.find(t=>t.name===e),s=!t||!t.asc;this.sortBy(e,s)};handleRecordSelectionChange=(e,t)=>{e[selected]=!!t.target.checked,this.requestUpdate(),this.dispatchEvent(new CustomEvent("selectionChange",{bubbles:!0}))};firstUpdated(){this.setData({records:this.records,fields:this.fields,filters:this.filters})}childrenUpdated(){this.requestUpdate()}updated(e){if(super.updated(e),this.enableSelection){const e=this.shadowRoot.getElementById("select-all");e&&(e.checked=this.allOnPageSelected())}
|
|
2
|
-
<
|
|
3
|
-
<input
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
`),this.hasBeforeControls()&&e.push(html`
|
|
10
|
-
<div class="field cell field-before-controls" style="width: ${this.columnSizes.beforeControls}px"></div>
|
|
11
|
-
`),this.fields.forEach(({name:t,label:s,hidden:i})=>{if(i)return;const r=this.sort.find(e=>e.name===t),o=this.sort.length>0&&this.sort[this.sort.length-1].name===t,l=r?r.asc?"sort-asc":"sort-desc":"";e.push(html`
|
|
12
|
-
<div
|
|
13
|
-
class="field cell ${l}"
|
|
14
|
-
style="width: ${this.columnSizes[t]}px; ${this.enableSorting?"cursor: pointer;":""}"
|
|
1
|
+
import{html,css,unsafeStatic,literal}from"../lit-all.min.js";import ShadowComponent from"./ShadowComponent.js";import{toTitleCase}from"../utils/string.js";import{boolExists}from"../utils/propConverters.js";const selected=Symbol("selected"),hidden=Symbol("hidden"),index=Symbol("index"),editing=Symbol("editing");export default class Table extends ShadowComponent{static properties={enablePages:{type:Boolean,reflect:!0,converter:boolExists,attribute:"enable-pages"},pageSize:{type:Number,reflect:!0,attribute:"page-size"},currentPage:{type:Number,reflect:!0,attribute:"current-page"},pageSizeOptions:{type:Array,attribute:"page-size-options"},enableSelection:{type:Boolean,reflect:!0,converter:boolExists,attribute:"enable-selection"},enableSorting:{type:Boolean,reflect:!0,converter:boolExists,attribute:"enable-sorting"},caseSensitiveFilters:{type:Boolean,reflect:!0,converter:boolExists,attribute:"case-sensitive-filters"},fields:{type:Array},records:{type:Array},filters:{type:Array},sort:{type:Array},columnSizes:{type:Object},fetchPending:{type:Boolean}};constructor(e={}){super(),void 0===this.pageSize&&(this.pageSize=50),void 0===this.currentPage&&(this.currentPage=1),void 0===this.pageSizeOptions&&(this.pageSizeOptions=[10,25,50,100,500]),void 0===this.records&&(this.records=e.records||[]),void 0===this.fields&&(this.fields=e.fields||[]),void 0===this.filters&&(this.filters=e.filters||[]),void 0===this.sort&&(this.sort=[]),void 0===this.columnSizes&&(this.columnSizes={}),void 0===this.fetchPending&&(this.fetchPending=!1)}handleSelectAllChange=e=>{e.target.checked?this.selectAllOnPage():this.deselectAllOnPage()};handleFieldClick=e=>{const t=this.sort.find(t=>t.name===e),s=!t||!t.asc;this.sortBy(e,s)};handleRecordSelectionChange=(e,t)=>{e[selected]=!!t.target.checked,this.requestUpdate(),this.dispatchEvent(new CustomEvent("selectionChange",{bubbles:!0}))};firstUpdated(){this.setData({records:this.records,fields:this.fields,filters:this.filters})}childrenUpdated(){this.requestUpdate()}updated(e){if(super.updated(e),this.enableSelection){const e=this.shadowRoot.getElementById("select-all");e&&(e.checked=this.allOnPageSelected())}}renderColgroupTemplate(){const e=[];return this.enableSelection&&e.push(html`<col style="width: 40px" />`),this.hasBeforeControls()&&e.push(html`<col style="width: ${this.columnSizes.beforeControls}px" />`),this.fields.forEach(({size:t,hidden:s})=>{s||e.push(t?html`<col style="width: ${t}px" />`:html`<col />`)}),this.hasAfterControls()&&e.push(html`<col style="width: ${this.columnSizes.afterControls}px" />`),e}getColumnCount(){let e=0;return this.enableSelection&&e++,this.hasBeforeControls()&&e++,this.fields.forEach(({hidden:t})=>{t||e++}),this.hasAfterControls()&&e++,e}renderFieldsTemplate(){const e=[];return this.enableSelection&&e.push(html`
|
|
2
|
+
<th class="controls field-select">
|
|
3
|
+
<input type="checkbox" id="select-all" @change=${this.handleSelectAllChange} />
|
|
4
|
+
</th>
|
|
5
|
+
`),this.hasBeforeControls()&&e.push(html`<th class="controls field-before-controls"></th>`),this.fields.forEach(({name:t,label:s,hidden:i})=>{if(i)return;const r=this.sort.find(e=>e.name===t),o=this.sort.length>0&&this.sort[this.sort.length-1].name===t,n=r?r.asc?"sort-asc":"sort-desc":"";e.push(html`
|
|
6
|
+
<th
|
|
7
|
+
class="${n}"
|
|
8
|
+
style="${this.enableSorting?"cursor: pointer;":""}"
|
|
15
9
|
@click=${this.enableSorting?()=>this.handleFieldClick(t):null}
|
|
16
10
|
>
|
|
17
11
|
${s}
|
|
18
12
|
${o?html`<k-icon name="arrow" direction="${r.asc?"down":"up"}" class="icon-sort"></k-icon>`:""}
|
|
19
|
-
</
|
|
20
|
-
`)}),this.hasAfterControls()&&e.push(html`
|
|
21
|
-
<
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
<input
|
|
25
|
-
type="checkbox"
|
|
13
|
+
</th>
|
|
14
|
+
`)}),this.hasAfterControls()&&e.push(html`<th class="controls field-after-controls"></th>`),e}renderRecordsTemplate(){let e=this.getDisplayedRecords(),t=0,s=this.pageSize;this.enablePages&&(t=(this.currentPage-1)*this.pageSize,s=t+this.pageSize,e=e.slice(t,s));let i=null,r=0;const o=e.map((e,s)=>null!==e?this.renderRecordTemplate(e):(null===i&&(i=t+s),r++,html`<tr class="record fetching"><td class="cell" colspan="${this.getColumnCount()}">Loading...</td></tr>`));return null===i||this.fetchPending||setTimeout(()=>{this.fetchPending||this.dispatchEvent(new CustomEvent("fetchRecords",{detail:{start:i,count:r},bubbles:!0}))},0),o}renderRecordTemplate(e){const t=[];return this.enableSelection&&t.push(html`
|
|
15
|
+
<td class="cell selection controls">
|
|
16
|
+
<input
|
|
17
|
+
type="checkbox"
|
|
26
18
|
.checked=${e[selected]}
|
|
27
19
|
@change=${t=>this.handleRecordSelectionChange(e,t)}
|
|
28
20
|
/>
|
|
29
|
-
</
|
|
30
|
-
`),this.hasBeforeControls()&&t.push(this.renderBeforeControlsTemplate()),this.fields.forEach(({name:s,formatter:i,calculator:r,type:o,editor:
|
|
31
|
-
<
|
|
32
|
-
${e[editing]?this.renderEditingCell(e,s,a,r,
|
|
33
|
-
</
|
|
21
|
+
</td>
|
|
22
|
+
`),this.hasBeforeControls()&&t.push(this.renderBeforeControlsTemplate()),this.fields.forEach(({name:s,formatter:i,calculator:r,type:o,editor:n,hidden:l})=>{if(l)return;let a=e[s]||"";t.push(html`
|
|
23
|
+
<td class="cell" data-field=${s}>
|
|
24
|
+
${e[editing]?this.renderEditingCell(e,s,a,r,n,o):this.renderDisplayCell(e,s,a,r,i)}
|
|
25
|
+
</td>
|
|
34
26
|
`)}),this.hasAfterControls()&&t.push(this.renderAfterControlsTemplate()),html`
|
|
35
|
-
<
|
|
27
|
+
<tr class="record ${e[editing]?"editing":""}" data-index=${e[index]}>
|
|
36
28
|
${t}
|
|
37
|
-
</
|
|
29
|
+
</tr>
|
|
38
30
|
`}renderEditingCell(e,t,s,i,r,o){if(i)return html`<input disabled .value=${i(e,this)} />`;if(r){const e=r(s);return html`${e}`}switch(o||typeof s){case"number":return html`<input type="number" .value=${s} />`;case"date":return html`<input type="date" .value=${s} />`;case"boolean":return html`
|
|
39
31
|
<select .value=${s}>
|
|
40
32
|
<option value="true" ?selected=${s}>True</option>
|
|
41
33
|
<option value="false" ?selected=${!s}>False</option>
|
|
42
34
|
</select>
|
|
43
35
|
`;default:return html`<input type="text" .value=${s} />`}}renderDisplayCell(e,t,s,i,r){return i?i(e,this):r?r(s):s}renderBeforeControlsTemplate(){const e=[];return this.querySelectorAll('[slot="before"]').forEach(t=>{const s=t.tagName.toLowerCase(),i=document.createElement(s);Array.from(t.attributes).forEach(e=>{"slot"!==e.name&&i.setAttribute(e.name,e.value)}),t.innerHTML&&(i.innerHTML=t.innerHTML),e.push(i)}),html`
|
|
44
|
-
<
|
|
36
|
+
<td class="cell controls controls-before">
|
|
45
37
|
${e}
|
|
46
|
-
</
|
|
38
|
+
</td>
|
|
47
39
|
`}renderAfterControlsTemplate(){const e=[];return this.querySelectorAll('[slot="after"]').forEach(t=>{const s=t.tagName.toLowerCase(),i=document.createElement(s);Array.from(t.attributes).forEach(e=>{"slot"!==e.name&&i.setAttribute(e.name,e.value)}),t.innerHTML&&(i.innerHTML=t.innerHTML),e.push(i)}),html`
|
|
48
|
-
<
|
|
40
|
+
<td class="cell controls controls-after">
|
|
49
41
|
${e}
|
|
50
|
-
</
|
|
51
|
-
`}hasBeforeControls(){return!!this.querySelector('[slot="before"]')}hasAfterControls(){return!!this.querySelector('[slot="after"]')}hasTopControls(){return!!this.querySelector('[slot="top"]')}hasBottomControls(){return!!this.querySelector(":scope > :not([slot])")}editRecord(e){e[editing]=!0;const t=this.shadowRoot.querySelector(`.record[data-index="${e[index]}"]`);t&&(t.classList.add("editing"),t.setAttribute("editing","true"),t.querySelectorAll(".cell[data-field]").forEach(t=>{const s=t.dataset.field,i=this.fields.find(e=>e.name===s);if(i){const r=e[s]||"";if(t.innerHTML="",i.calculator){const s=document.createElement("input");s.disabled=!0,s.value=i.calculator(e,this),t.appendChild(s)}else if(i.editor)t.appendChild(i.editor(r));else{const e=i.type||typeof r,s=Table.editors[e]||Table.editors.string;t.appendChild(s(r))}}})),this.dispatchEvent(new CustomEvent("editingChange",{detail:{record:e,editing:!0},bubbles:!0}))}saveEditedRecord(e){const t=this.shadowRoot.querySelector(`.record[data-index="${e[index]}"]`);t&&(t.querySelectorAll(".cell[data-field]").forEach(t=>{const s=t.dataset.field,i=this.fields.find(e=>e.name===s);if(i&&!i.calculator){const i=t.querySelector("input, select");i&&(e[s]=i.value)}}),e[editing]=!1,t.classList.remove("editing"),t.removeAttribute("editing"),t.querySelectorAll(".cell[data-field]").forEach(t=>{const s=t.dataset.field,i=this.fields.find(e=>e.name===s);if(i){const r=e[s]||"";i.calculator?t.textContent=i.calculator(e,this):i.formatter?t.innerHTML=i.formatter(r):t.textContent=r}})),this.dispatchEvent(new CustomEvent("editingChange",{detail:{record:e,editing:!1},bubbles:!0}))}cancelEditedRecord(e){e[editing]=!1;const t=this.shadowRoot.querySelector(`.record[data-index="${e[index]}"]`);t&&(t.classList.remove("editing"),t.removeAttribute("editing"),t.querySelectorAll(".cell[data-field]").forEach(t=>{const s=t.dataset.field,i=this.fields.find(e=>e.name===s);if(i){const r=e[s]||"";i.calculator?t.textContent=i.calculator(e,this):i.formatter?t.innerHTML=i.formatter(r):t.textContent=r}})),this.dispatchEvent(new CustomEvent("editingChange",{detail:{record:e,editing:!1},bubbles:!0}))}recordIsEditing(e){return e[editing]}getCurrentPage(){return this.currentPage}getTotalPages(){return Math.ceil(this.getDisplayedRecords().length/this.pageSize)}setPage(e){e<1||e>this.getTotalPages()||(this.currentPage=e,this.requestUpdate(),this.dispatchEvent(new CustomEvent("pageChange",{bubbles:!0})))}firstPage(){1!==this.currentPage&&this.setPage(1)}nextPage(){this.currentPage<this.getTotalPages()&&this.setPage(this.currentPage+1)}prevPage(){this.currentPage>1&&this.setPage(this.currentPage-1)}lastPage(){this.currentPage!==this.getTotalPages()&&this.setPage(this.getTotalPages())}setPageSize(e){this.pageSize=e,this.currentPage=1,this.requestUpdate(),this.dispatchEvent(new CustomEvent("pageSizeChange",{bubbles:!0}))}getPageSize(){return this.pageSize}getPageSizeOptions(){return this.pageSizeOptions}getFieldLabel(e){const t=this.fields.find(t=>t.name===e);return t?t.label:toTitleCase(e)}setPageSizeOptions(e){this.pageSizeOptions=e,this.requestUpdate()}setData({records:e=!1,fields:t=!1,pageSize:s=!1,pageSizeOptions:i=!1,currentPage:r=!1,enableSelection:o}={}){let l=!1,n=this.getTotalPages(),a=this.currentPage;e&&(this.records=e.map(e=>({...e})),this.records.forEach((e,t)=>{e[index]=t,e[selected]=!1,e[hidden]=!1,e[editing]=!1}),this.fields=t||Table.extractFieldsFromRecords(this.records),l=!0),s&&(this.pageSize=s,l=!0),i&&(this.pageSizeOptions=i),r&&(this.currentPage=r,l=!0),void 0!==o&&(this.enableSelection=o,l=!0),l&&this.requestUpdate();const d=this.getTotalPages();d!==n&&this.dispatchEvent(new CustomEvent("pageCountChanged",{detail:{totalPages:this.getTotalPages()},bubbles:!0})),a>d&&this.setPage(d)}setRecords(e,t){let s=this.getTotalPages(),i=this.currentPage;this.records=e.map(e=>({...e})),this.records.forEach((e,t)=>{e[index]=t,e[selected]=!1,e[hidden]=!1,e[editing]=!1}),this.fields=t||Table.extractFieldsFromRecords(this.records),this.requestUpdate(),this.dispatchEvent(new CustomEvent("recordsSet",{detail:{records:e},bubbles:!0}));const r=this.getTotalPages();r!==s&&this.dispatchEvent(new CustomEvent("pageCountChanged",{detail:{totalPages:this.getTotalPages()},bubbles:!0})),i>r&&this.setPage(r)}setupFetchRecords(e,t){const s=this.records.length,i=this.getTotalPages();if(s<e){this.records.length=e,this.records.fill(null,s),this.requestUpdate();const t=this.getTotalPages();t!==i&&setTimeout(()=>{this.dispatchEvent(new CustomEvent("pageCountChanged",{detail:{totalPages:t},bubbles:!0}))},0)}this.addEventListener("fetchRecords",async e=>{if(this.fetchPending)return;this.fetchPending=!0;const{start:s,count:i}=e.detail,r=await t(s,i);r.forEach((e,t)=>{e[index]=s+t,void 0===e[selected]&&(e[selected]=!1),void 0===e[hidden]&&(e[hidden]=!1),void 0===e[editing]&&(e[editing]=!1)}),this.records.splice(s,r.length,...r),this.fetchPending=!1,this.requestUpdate()})}addRecord(e){e[selected]=!1,e[hidden]=!1,e[index]=this.records.length,this.records.push(e),this.requestUpdate(),this.dispatchEvent(new CustomEvent("recordAdded",{detail:{record:e},bubbles:!0}))}updateRecord(e,t){let s=!1,i=this.records.find(t=>t===e);if(i||void 0===e[index]||(i=this.records[e[index]]),Object.keys(t).forEach(e=>{i.hasOwnProperty(e)&&(i[e]=t[e],s=!0)}),s){const e=(this.currentPage-1)*this.pageSize,t=e+this.pageSize;(!this.enablePages||i[index]>=e&&i[index]<t)&&this.requestUpdate()}}deleteRecord(e){let t=this.records.find(t=>t===e),s=this.getTotalPages();if(t||void 0===e[index]||(t=this.records[e[index]]),t){const e=this.records.indexOf(t);this.records.splice(e,1),this.records.forEach((e,t)=>{e[index]=t}),this.requestUpdate(),this.dispatchEvent(new CustomEvent("selectionChange",{bubbles:!0})),this.dispatchEvent(new CustomEvent("recordDeleted",{detail:{index:e},bubbles:!0}));const i=this.getTotalPages();this.currentPage>i&&this.setPage(i),i!==s&&this.dispatchEvent(new CustomEvent("pageCountChanged",{detail:{totalPages:i},bubbles:!0}))}}deleteSelected(){let e=this.getTotalPages();this.getSelectedRecords().forEach(e=>{let t=this.records.find(t=>t===e);if(t||void 0===e[index]||(t=this.records[e[index]]),t){const e=this.records.indexOf(t);this.records.splice(e,1)}}),this.records.forEach((e,t)=>{e[index]=t}),this.requestUpdate();const t=this.getTotalPages();this.currentPage>t&&this.setPage(t),t!==e&&this.dispatchEvent(new CustomEvent("pageCountChanged",{detail:{totalPages:t},bubbles:!0})),this.dispatchEvent(new CustomEvent("selectionChange",{bubbles:!0}))}getSelectedRecords(){return this.records.filter(e=>e[selected])}selectAllOnPage(){const e=(this.currentPage-1)*this.pageSize,t=Math.min(e+this.pageSize,this.records.length);for(let s=e;s<t;s++)this.records[s][selected]=!0;this.requestUpdate(),setTimeout(()=>{this.dispatchEvent(new CustomEvent("selectionChange",{bubbles:!0}))},0)}deselectAllOnPage(){const e=(this.currentPage-1)*this.pageSize,t=Math.min(e+this.pageSize,this.records.length);for(let s=e;s<t;s++)this.records[s][selected]=!1;this.requestUpdate(),setTimeout(()=>{this.dispatchEvent(new CustomEvent("selectionChange",{bubbles:!0}))},0)}allOnPageSelected(){const e=(this.currentPage-1)*this.pageSize,t=Math.min(e+this.pageSize,this.records.length);for(let s=e;s<t;s++)if(!this.records[s][selected])return!1;return!0}sortBy(e,t=!0){this.sort=this.sort.filter(t=>t.name!==e),this.sort.push({name:e,asc:t}),this.requestUpdate()}hideRecord(e){let t=this.records.find(t=>t===e);t||void 0===e[index]||(t=this.records[e[index]]),t&&(t[hidden]=!0,this.requestUpdate(),this.dispatchEvent(new CustomEvent("recordHidden",{bubbles:!0})))}showRecord(e){let t=this.records.find(t=>t===e);t||void 0===e[index]||(t=this.records[e[index]]),t&&(t[hidden]=!1,this.requestUpdate(),this.dispatchEvent(new CustomEvent("recordShown",{bubbles:!0})))}showAllRecords(){this.records.forEach(e=>{e[hidden]=!1}),this.filters.length&&(this.filters=[],this.dispatchEvent(new CustomEvent("filterRemoved",{bubbles:!0})),this.dispatchEvent(new CustomEvent("filterChange",{bubbles:!0}))),this.requestUpdate(),this.dispatchEvent(new CustomEvent("recordShown",{bubbles:!0})),this.dispatchEvent(new CustomEvent("allRecordsShown",{bubbles:!0}))}addFilter(e,t,s){this.filters.push({field:e,condition:t,value:s}),this.dispatchEvent(new CustomEvent("filterAdded",{bubbles:!0})),this.dispatchEvent(new CustomEvent("filterChange",{bubbles:!0})),this.requestUpdate()}removeFilter(e,t,s,i=!0){const r=this.filters.findIndex(i=>i.field===e&&i.condition===t&&i.value===s);-1!==r&&(this.records.forEach(i=>{this.testFilter(i,e,t,s)||(i[hidden]=!1)}),this.filters.splice(r,1),this.dispatchEvent(new CustomEvent("filterRemoved",{bubbles:!0})),this.dispatchEvent(new CustomEvent("filterChange",{bubbles:!0})),i&&this.requestUpdate())}testFilter(e,t,s,i){let r=e[t],o=i;switch(this.caseSensitiveFilters||"string"!=typeof r||"string"!=typeof i||(r=r.toLowerCase(),o=i.toLowerCase()),s){case"equals":return r===o;case"not-equals":return r!==o;case"contains":return r.includes(o);case"not-contains":return!r.includes(o);case"greater-than":return r>o;case"less-than":return r<o;case"greater-than-or-equal":return r>=o;case"less-than-or-equal":return r<=o;default:return!0}}removeAllFilters(){this.filters.length&&([...this.filters].forEach(({field:e,condition:t,value:s})=>{this.removeFilter(e,t,s,!1)}),this.requestUpdate())}search(e){const t=e.trim().toLowerCase();let s=!1;this.records.forEach(e=>{if(e[hidden])return;let i=!1;this.fields.forEach(({name:s})=>{(e[s]?.toString().toLowerCase()||"").includes(t)&&(i=!0)}),e[hidden]!==!i&&(e[hidden]=!i,s=!0)}),s&&(this.dispatchEvent(new CustomEvent("recordHidden",{bubbles:!0})),this.requestUpdate()),this.dispatchEvent(new CustomEvent("search",{detail:{term:e},bubbles:!0}))}getDisplayedRecords(){this.filters.forEach(({field:e,condition:t,value:s})=>{this.records.forEach(i=>{null!==i&&(this.testFilter(i,e,t,s)||(i[hidden]=!0))})});let e=this.records.filter(e=>null===e||!e[hidden]);return this.sort.forEach(({name:t,asc:s})=>{e.sort((e,i)=>null===e||null===i?0:e[t]<i[t]?s?-1:1:e[t]>i[t]?s?1:-1:0)}),e}getHiddenRecords(){return this.records.filter(e=>e[hidden])}calculateColumnSizes(){const e={total:0};this.enableSelection&&(e.total+=40);const t=Array.from(this.querySelectorAll('[slot="before"]')),s=Array.from(this.querySelectorAll('[slot="after"]'));e.beforeControls=t.reduce((e,t)=>e+(t.maxWidth||40),0),e.afterControls=s.reduce((e,t)=>e+(t.maxWidth||40),0),this.hasBeforeControls()&&(e.total+=e.beforeControls),this.hasAfterControls()&&(e.total+=e.afterControls),this.fields.forEach(t=>{if(t.size)e[t.name]=t.size,e.total+=t.size;else{let s=0;this.records.slice(0,100).forEach(e=>{if(null===e)return;let i=e[t.name];t.calculator&&(i=t.calculator(e,this)),t.formatter&&(i=t.formatter(i)),i&&i.toString().length>s&&(s=i.toString().length)}),e[t.name]=Math.max(10*s+32,128),t.hidden||(e.total+=e[t.name])}});const i=[...t,...s].some(e=>void 0===e.maxWidth);return JSON.stringify(this.columnSizes)!==JSON.stringify(e)&&(this.columnSizes=e),i&&setTimeout(()=>this.calculateColumnSizes(),0),this.columnSizes}setFieldHiddenState(e,t){const s=this.fields.find(t=>t.name===e);s&&(s.hidden=t,this.calculateColumnSizes(),this.requestUpdate(),this.dispatchEvent(new CustomEvent("fieldVisibilityChanged",{detail:{field:s},bubbles:!0})),this.dispatchEvent(new CustomEvent(t?"fieldHidden":"fieldShown",{detail:{field:s},bubbles:!0})))}hideField(e){this.setFieldHiddenState(e,!0)}showField(e){this.setFieldHiddenState(e,!1)}reorderFields(e){const t=[];e.forEach(e=>{const s=this.fields.find(t=>t.name===e);s&&t.push(s)}),this.fields=t,this.requestUpdate()}render(){return this.records&&this.fields?(this.calculateColumnSizes(),this.hasTopControls()?this.setAttribute("top-controls","true"):this.removeAttribute("top-controls"),this.hasBottomControls()?this.setAttribute("bottom-controls","true"):this.removeAttribute("bottom-controls"),html`
|
|
42
|
+
</td>
|
|
43
|
+
`}hasBeforeControls(){return!!this.querySelector('[slot="before"]')}hasAfterControls(){return!!this.querySelector('[slot="after"]')}hasTopControls(){return!!this.querySelector('[slot="top"]')}hasBottomControls(){return!!this.querySelector(":scope > :not([slot])")}editRecord(e){e[editing]=!0;const t=this.shadowRoot.querySelector(`.record[data-index="${e[index]}"]`);t&&(t.classList.add("editing"),t.setAttribute("editing","true"),t.querySelectorAll(".cell[data-field]").forEach(t=>{const s=t.dataset.field,i=this.fields.find(e=>e.name===s);if(i){const r=e[s]||"";if(t.innerHTML="",i.calculator){const s=document.createElement("input");s.disabled=!0,s.value=i.calculator(e,this),t.appendChild(s)}else if(i.editor)t.appendChild(i.editor(r));else{const e=i.type||typeof r,s=Table.editors[e]||Table.editors.string;t.appendChild(s(r))}}})),this.dispatchEvent(new CustomEvent("editingChange",{detail:{record:e,editing:!0},bubbles:!0}))}saveEditedRecord(e){const t=this.shadowRoot.querySelector(`.record[data-index="${e[index]}"]`);t&&(t.querySelectorAll(".cell[data-field]").forEach(t=>{const s=t.dataset.field,i=this.fields.find(e=>e.name===s);if(i&&!i.calculator){const i=t.querySelector("input, select");i&&(e[s]=i.value)}}),e[editing]=!1,t.classList.remove("editing"),t.removeAttribute("editing"),t.querySelectorAll(".cell[data-field]").forEach(t=>{const s=t.dataset.field,i=this.fields.find(e=>e.name===s);if(i){const r=e[s]||"";i.calculator?t.textContent=i.calculator(e,this):i.formatter?t.innerHTML=i.formatter(r):t.textContent=r}})),this.dispatchEvent(new CustomEvent("editingChange",{detail:{record:e,editing:!1},bubbles:!0}))}cancelEditedRecord(e){e[editing]=!1;const t=this.shadowRoot.querySelector(`.record[data-index="${e[index]}"]`);t&&(t.classList.remove("editing"),t.removeAttribute("editing"),t.querySelectorAll(".cell[data-field]").forEach(t=>{const s=t.dataset.field,i=this.fields.find(e=>e.name===s);if(i){const r=e[s]||"";i.calculator?t.textContent=i.calculator(e,this):i.formatter?t.innerHTML=i.formatter(r):t.textContent=r}})),this.dispatchEvent(new CustomEvent("editingChange",{detail:{record:e,editing:!1},bubbles:!0}))}recordIsEditing(e){return e[editing]}getCurrentPage(){return this.currentPage}getTotalPages(){return Math.ceil(this.getDisplayedRecords().length/this.pageSize)}setPage(e){e<1||e>this.getTotalPages()||(this.currentPage=e,this.requestUpdate(),this.dispatchEvent(new CustomEvent("pageChange",{bubbles:!0})))}firstPage(){1!==this.currentPage&&this.setPage(1)}nextPage(){this.currentPage<this.getTotalPages()&&this.setPage(this.currentPage+1)}prevPage(){this.currentPage>1&&this.setPage(this.currentPage-1)}lastPage(){this.currentPage!==this.getTotalPages()&&this.setPage(this.getTotalPages())}setPageSize(e){this.pageSize=e,this.currentPage=1,this.requestUpdate(),this.dispatchEvent(new CustomEvent("pageSizeChange",{bubbles:!0}))}getPageSize(){return this.pageSize}getPageSizeOptions(){return this.pageSizeOptions}getFieldLabel(e){const t=this.fields.find(t=>t.name===e);return t?t.label:toTitleCase(e)}setPageSizeOptions(e){this.pageSizeOptions=e,this.requestUpdate()}setData({records:e=!1,fields:t=!1,pageSize:s=!1,pageSizeOptions:i=!1,currentPage:r=!1,enableSelection:o}={}){let n=!1,l=this.getTotalPages(),a=this.currentPage;e&&(this.records=e.map(e=>({...e})),this.records.forEach((e,t)=>{e[index]=t,e[selected]=!1,e[hidden]=!1,e[editing]=!1}),this.fields=t||Table.extractFieldsFromRecords(this.records),n=!0),s&&(this.pageSize=s,n=!0),i&&(this.pageSizeOptions=i),r&&(this.currentPage=r,n=!0),void 0!==o&&(this.enableSelection=o,n=!0),n&&this.requestUpdate();const d=this.getTotalPages();d!==l&&this.dispatchEvent(new CustomEvent("pageCountChanged",{detail:{totalPages:this.getTotalPages()},bubbles:!0})),a>d&&this.setPage(d)}setRecords(e,t){let s=this.getTotalPages(),i=this.currentPage;this.records=e.map(e=>({...e})),this.records.forEach((e,t)=>{e[index]=t,e[selected]=!1,e[hidden]=!1,e[editing]=!1}),this.fields=t||Table.extractFieldsFromRecords(this.records),this.requestUpdate(),this.dispatchEvent(new CustomEvent("recordsSet",{detail:{records:e},bubbles:!0}));const r=this.getTotalPages();r!==s&&this.dispatchEvent(new CustomEvent("pageCountChanged",{detail:{totalPages:this.getTotalPages()},bubbles:!0})),i>r&&this.setPage(r)}setupFetchRecords(e,t){const s=this.records.length,i=this.getTotalPages();if(s<e){this.records.length=e,this.records.fill(null,s),this.requestUpdate();const t=this.getTotalPages();t!==i&&setTimeout(()=>{this.dispatchEvent(new CustomEvent("pageCountChanged",{detail:{totalPages:t},bubbles:!0}))},0)}this.addEventListener("fetchRecords",async e=>{if(this.fetchPending)return;this.fetchPending=!0;const{start:s,count:i}=e.detail,r=await t(s,i);r.forEach((e,t)=>{e[index]=s+t,void 0===e[selected]&&(e[selected]=!1),void 0===e[hidden]&&(e[hidden]=!1),void 0===e[editing]&&(e[editing]=!1)}),this.records.splice(s,r.length,...r),this.fetchPending=!1,this.requestUpdate()})}addRecord(e){e[selected]=!1,e[hidden]=!1,e[index]=this.records.length,this.records.push(e),this.requestUpdate(),this.dispatchEvent(new CustomEvent("recordAdded",{detail:{record:e},bubbles:!0}))}updateRecord(e,t){let s=!1,i=this.records.find(t=>t===e);if(i||void 0===e[index]||(i=this.records[e[index]]),Object.keys(t).forEach(e=>{i.hasOwnProperty(e)&&(i[e]=t[e],s=!0)}),s){const e=(this.currentPage-1)*this.pageSize,t=e+this.pageSize;(!this.enablePages||i[index]>=e&&i[index]<t)&&this.requestUpdate()}}deleteRecord(e){let t=this.records.find(t=>t===e),s=this.getTotalPages();if(t||void 0===e[index]||(t=this.records[e[index]]),t){const e=this.records.indexOf(t);this.records.splice(e,1),this.records.forEach((e,t)=>{e[index]=t}),this.requestUpdate(),this.dispatchEvent(new CustomEvent("selectionChange",{bubbles:!0})),this.dispatchEvent(new CustomEvent("recordDeleted",{detail:{index:e},bubbles:!0}));const i=this.getTotalPages();this.currentPage>i&&this.setPage(i),i!==s&&this.dispatchEvent(new CustomEvent("pageCountChanged",{detail:{totalPages:i},bubbles:!0}))}}deleteSelected(){let e=this.getTotalPages();this.getSelectedRecords().forEach(e=>{let t=this.records.find(t=>t===e);if(t||void 0===e[index]||(t=this.records[e[index]]),t){const e=this.records.indexOf(t);this.records.splice(e,1)}}),this.records.forEach((e,t)=>{e[index]=t}),this.requestUpdate();const t=this.getTotalPages();this.currentPage>t&&this.setPage(t),t!==e&&this.dispatchEvent(new CustomEvent("pageCountChanged",{detail:{totalPages:t},bubbles:!0})),this.dispatchEvent(new CustomEvent("selectionChange",{bubbles:!0}))}getSelectedRecords(){return this.records.filter(e=>e[selected])}selectAllOnPage(){const e=(this.currentPage-1)*this.pageSize,t=Math.min(e+this.pageSize,this.records.length);for(let s=e;s<t;s++)this.records[s][selected]=!0;this.requestUpdate(),setTimeout(()=>{this.dispatchEvent(new CustomEvent("selectionChange",{bubbles:!0}))},0)}deselectAllOnPage(){const e=(this.currentPage-1)*this.pageSize,t=Math.min(e+this.pageSize,this.records.length);for(let s=e;s<t;s++)this.records[s][selected]=!1;this.requestUpdate(),setTimeout(()=>{this.dispatchEvent(new CustomEvent("selectionChange",{bubbles:!0}))},0)}allOnPageSelected(){const e=(this.currentPage-1)*this.pageSize,t=Math.min(e+this.pageSize,this.records.length);for(let s=e;s<t;s++)if(!this.records[s][selected])return!1;return!0}sortBy(e,t=!0){this.sort=this.sort.filter(t=>t.name!==e),this.sort.push({name:e,asc:t}),this.requestUpdate()}hideRecord(e){let t=this.records.find(t=>t===e);t||void 0===e[index]||(t=this.records[e[index]]),t&&(t[hidden]=!0,this.requestUpdate(),this.dispatchEvent(new CustomEvent("recordHidden",{bubbles:!0})))}showRecord(e){let t=this.records.find(t=>t===e);t||void 0===e[index]||(t=this.records[e[index]]),t&&(t[hidden]=!1,this.requestUpdate(),this.dispatchEvent(new CustomEvent("recordShown",{bubbles:!0})))}showAllRecords(){this.records.forEach(e=>{e[hidden]=!1}),this.filters.length&&(this.filters=[],this.dispatchEvent(new CustomEvent("filterRemoved",{bubbles:!0})),this.dispatchEvent(new CustomEvent("filterChange",{bubbles:!0}))),this.requestUpdate(),this.dispatchEvent(new CustomEvent("recordShown",{bubbles:!0})),this.dispatchEvent(new CustomEvent("allRecordsShown",{bubbles:!0}))}addFilter(e,t,s){this.filters.push({field:e,condition:t,value:s}),this.dispatchEvent(new CustomEvent("filterAdded",{bubbles:!0})),this.dispatchEvent(new CustomEvent("filterChange",{bubbles:!0})),this.requestUpdate()}removeFilter(e,t,s,i=!0){const r=this.filters.findIndex(i=>i.field===e&&i.condition===t&&i.value===s);-1!==r&&(this.records.forEach(i=>{this.testFilter(i,e,t,s)||(i[hidden]=!1)}),this.filters.splice(r,1),this.dispatchEvent(new CustomEvent("filterRemoved",{bubbles:!0})),this.dispatchEvent(new CustomEvent("filterChange",{bubbles:!0})),i&&this.requestUpdate())}testFilter(e,t,s,i){let r=e[t],o=i;switch(this.caseSensitiveFilters||"string"!=typeof r||"string"!=typeof i||(r=r.toLowerCase(),o=i.toLowerCase()),s){case"equals":return r===o;case"not-equals":return r!==o;case"contains":return r.includes(o);case"not-contains":return!r.includes(o);case"greater-than":return r>o;case"less-than":return r<o;case"greater-than-or-equal":return r>=o;case"less-than-or-equal":return r<=o;default:return!0}}removeAllFilters(){this.filters.length&&([...this.filters].forEach(({field:e,condition:t,value:s})=>{this.removeFilter(e,t,s,!1)}),this.requestUpdate())}search(e){const t=e.trim().toLowerCase();let s=!1;this.records.forEach(e=>{if(e[hidden])return;let i=!1;this.fields.forEach(({name:s})=>{(e[s]?.toString().toLowerCase()||"").includes(t)&&(i=!0)}),e[hidden]!==!i&&(e[hidden]=!i,s=!0)}),s&&(this.dispatchEvent(new CustomEvent("recordHidden",{bubbles:!0})),this.requestUpdate()),this.dispatchEvent(new CustomEvent("search",{detail:{term:e},bubbles:!0}))}getDisplayedRecords(){this.filters.forEach(({field:e,condition:t,value:s})=>{this.records.forEach(i=>{null!==i&&(this.testFilter(i,e,t,s)||(i[hidden]=!0))})});let e=this.records.filter(e=>null===e||!e[hidden]);return this.sort.forEach(({name:t,asc:s})=>{e.sort((e,i)=>null===e||null===i?0:e[t]<i[t]?s?-1:1:e[t]>i[t]?s?1:-1:0)}),e}getHiddenRecords(){return this.records.filter(e=>e[hidden])}calculateColumnSizes(){const e=Array.from(this.querySelectorAll('[slot="before"]')),t=Array.from(this.querySelectorAll('[slot="after"]')),s={beforeControls:e.reduce((e,t)=>e+(t.maxWidth||40),0),afterControls:t.reduce((e,t)=>e+(t.maxWidth||40),0)},i=[...e,...t].some(e=>void 0===e.maxWidth);return JSON.stringify(this.columnSizes)!==JSON.stringify(s)&&(this.columnSizes=s),i&&setTimeout(()=>this.calculateColumnSizes(),0),this.columnSizes}setFieldHiddenState(e,t){const s=this.fields.find(t=>t.name===e);s&&(s.hidden=t,this.calculateColumnSizes(),this.requestUpdate(),this.dispatchEvent(new CustomEvent("fieldVisibilityChanged",{detail:{field:s},bubbles:!0})),this.dispatchEvent(new CustomEvent(t?"fieldHidden":"fieldShown",{detail:{field:s},bubbles:!0})))}hideField(e){this.setFieldHiddenState(e,!0)}showField(e){this.setFieldHiddenState(e,!1)}reorderFields(e){const t=[];e.forEach(e=>{const s=this.fields.find(t=>t.name===e);s&&t.push(s)}),this.fields=t,this.requestUpdate()}render(){return this.records&&this.fields?(this.calculateColumnSizes(),this.hasTopControls()?this.setAttribute("top-controls","true"):this.removeAttribute("top-controls"),this.hasBottomControls()?this.setAttribute("bottom-controls","true"):this.removeAttribute("bottom-controls"),html`
|
|
52
44
|
<div id="wrapper">
|
|
53
|
-
<div id="top"
|
|
54
|
-
<div id="table">
|
|
55
|
-
<
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
45
|
+
<div id="top"><slot name="top"></slot></div>
|
|
46
|
+
<div id="table-container">
|
|
47
|
+
<table>
|
|
48
|
+
<colgroup>
|
|
49
|
+
${this.renderColgroupTemplate()}
|
|
50
|
+
</colgroup>
|
|
51
|
+
<thead>
|
|
52
|
+
<tr>
|
|
53
|
+
${this.renderFieldsTemplate()}
|
|
54
|
+
</tr>
|
|
55
|
+
</thead>
|
|
56
|
+
<tbody>
|
|
57
|
+
${this.renderRecordsTemplate()}
|
|
58
|
+
</tbody>
|
|
59
|
+
</table>
|
|
61
60
|
</div>
|
|
62
|
-
<div id="bottom"
|
|
61
|
+
<div id="bottom"><slot></slot></div>
|
|
63
62
|
</div>
|
|
64
63
|
<div style="display: none">
|
|
65
64
|
<slot name="before"></slot>
|
|
@@ -68,9 +67,8 @@ import{html,css,unsafeStatic,literal}from"../lit-all.min.js";import ShadowCompon
|
|
|
68
67
|
`):html`
|
|
69
68
|
<div id="wrapper">
|
|
70
69
|
<div id="top"><slot name="top"></slot></div>
|
|
71
|
-
<div id="table">
|
|
72
|
-
<
|
|
73
|
-
<div id="records"></div>
|
|
70
|
+
<div id="table-container">
|
|
71
|
+
<table><thead><tr></tr></thead><tbody></tbody></table>
|
|
74
72
|
</div>
|
|
75
73
|
<div id="bottom"><slot></slot></div>
|
|
76
74
|
</div>
|
|
@@ -81,40 +79,67 @@ import{html,css,unsafeStatic,literal}from"../lit-all.min.js";import ShadowCompon
|
|
|
81
79
|
`}static styles=css`
|
|
82
80
|
:host {
|
|
83
81
|
display: block;
|
|
84
|
-
width: 100%;
|
|
85
|
-
overflow: auto;
|
|
86
82
|
margin-bottom: var(--spacer);
|
|
87
83
|
}
|
|
88
84
|
#wrapper {
|
|
89
|
-
width: min-content;
|
|
90
85
|
border: 1px solid var(--c_border);
|
|
91
86
|
border-radius: var(--radius);
|
|
87
|
+
overflow: hidden;
|
|
92
88
|
}
|
|
93
|
-
#table {
|
|
94
|
-
|
|
89
|
+
#table-container {
|
|
90
|
+
overflow-x: auto;
|
|
95
91
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
92
|
+
table {
|
|
93
|
+
width: 100%;
|
|
94
|
+
border-collapse: collapse;
|
|
99
95
|
}
|
|
100
|
-
|
|
96
|
+
thead tr {
|
|
101
97
|
background-color: var(--c_bg__alt);
|
|
102
|
-
border-bottom: 1px solid var(--c_border);
|
|
103
98
|
}
|
|
104
|
-
|
|
105
|
-
#fields .cell:not(.controls) {
|
|
99
|
+
th, td {
|
|
106
100
|
padding: calc(0.5 * var(--spacer)) var(--spacer);
|
|
101
|
+
vertical-align: middle;
|
|
107
102
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
103
|
+
th:not(:last-child),
|
|
104
|
+
td:not(:last-child) {
|
|
105
|
+
border-right: 1px solid var(--c_border);
|
|
111
106
|
}
|
|
112
|
-
|
|
113
|
-
|
|
107
|
+
th:first-child,
|
|
108
|
+
td:first-child {
|
|
109
|
+
border-left: none;
|
|
110
|
+
}
|
|
111
|
+
th:last-child,
|
|
112
|
+
td:last-child {
|
|
113
|
+
border-right: none;
|
|
114
|
+
}
|
|
115
|
+
thead tr th {
|
|
116
|
+
border-top: none;
|
|
117
|
+
border-bottom: 1px solid var(--c_border);
|
|
114
118
|
}
|
|
115
|
-
|
|
119
|
+
tbody tr:not(:last-child) td {
|
|
116
120
|
border-bottom: 1px solid var(--c_border);
|
|
117
121
|
}
|
|
122
|
+
tbody tr:last-child td {
|
|
123
|
+
border-bottom: none;
|
|
124
|
+
}
|
|
125
|
+
th.controls,
|
|
126
|
+
td.controls {
|
|
127
|
+
padding: 0;
|
|
128
|
+
}
|
|
129
|
+
.field-select,
|
|
130
|
+
.selection {
|
|
131
|
+
width: 40px;
|
|
132
|
+
text-align: center;
|
|
133
|
+
}
|
|
134
|
+
.field-select input,
|
|
135
|
+
.selection input {
|
|
136
|
+
width: 1.25rem;
|
|
137
|
+
height: 1.25rem;
|
|
138
|
+
}
|
|
139
|
+
.icon-sort {
|
|
140
|
+
float: right;
|
|
141
|
+
opacity: 0.5;
|
|
142
|
+
}
|
|
118
143
|
#top, #bottom {
|
|
119
144
|
display: flex;
|
|
120
145
|
width: 100%;
|
|
@@ -133,19 +158,4 @@ import{html,css,unsafeStatic,literal}from"../lit-all.min.js";import ShadowCompon
|
|
|
133
158
|
:host(:not([bottom-controls])) #bottom {
|
|
134
159
|
display: none;
|
|
135
160
|
}
|
|
136
|
-
.field-select,
|
|
137
|
-
.selection {
|
|
138
|
-
display: flex;
|
|
139
|
-
justify-content: center;
|
|
140
|
-
align-items: center;
|
|
141
|
-
}
|
|
142
|
-
.field-select input,
|
|
143
|
-
.selection input {
|
|
144
|
-
width: 1.25rem;
|
|
145
|
-
height: 1.25rem;
|
|
146
|
-
}
|
|
147
|
-
.icon-sort {
|
|
148
|
-
float: right;
|
|
149
|
-
opacity: 0.5;
|
|
150
|
-
}
|
|
151
161
|
`;static extractFieldsFromRecords(e,t=100){const s=new Set;return e.slice(0,t).forEach(e=>{Object.keys(e).forEach(e=>s.add(e))}),[...s].map(e=>({name:e,label:toTitleCase(e)}))}static format(e){return(Array.isArray(e)?Table.formatters.array:Table.formatters[typeof e])(e)}static formatters={string:e=>e,number:e=>`${e}`,date:e=>e.toLocaleDateString(),boolean:e=>e?"True":"False",array:e=>e.map(e=>Table.format(e)).join(", "),undefined:e=>"",null:e=>"<code>null</code>"};static editors={string:e=>{const t=document.createElement("input");return t.value=e,t},number:e=>{const t=document.createElement("input");return t.type="number",t.value=e,t},date:e=>{const t=document.createElement("input");return t.type="date",t.value=e,t},boolean:e=>{const t=document.createElement("select");return t.innerHTML=`\n <option value="true" ${e?"selected":""}>True</option>\n <option value="false" ${e?"":"selected"}>False</option>\n `,t.value=e,t},calculated:e=>{const t=document.createElement("input");return t.disabled=!0,t.value=e,t}}}window.customElements.define("k-table",Table);
|
|
@@ -1,65 +1,64 @@
|
|
|
1
|
-
import{html,css,unsafeStatic,literal}from"../lit-all.min.js";import ShadowComponent from"./ShadowComponent.js";import{toTitleCase}from"../utils/string.js";import{boolExists}from"../utils/propConverters.js";const selected=Symbol("selected"),hidden=Symbol("hidden"),index=Symbol("index"),editing=Symbol("editing");export default class Table extends ShadowComponent{static properties={enablePages:{type:Boolean,reflect:!0,converter:boolExists,attribute:"enable-pages"},pageSize:{type:Number,reflect:!0,attribute:"page-size"},currentPage:{type:Number,reflect:!0,attribute:"current-page"},pageSizeOptions:{type:Array,attribute:"page-size-options"},enableSelection:{type:Boolean,reflect:!0,converter:boolExists,attribute:"enable-selection"},enableSorting:{type:Boolean,reflect:!0,converter:boolExists,attribute:"enable-sorting"},caseSensitiveFilters:{type:Boolean,reflect:!0,converter:boolExists,attribute:"case-sensitive-filters"},fields:{type:Array},records:{type:Array},filters:{type:Array},sort:{type:Array},columnSizes:{type:Object},fetchPending:{type:Boolean}};constructor(e={}){super(),void 0===this.pageSize&&(this.pageSize=50),void 0===this.currentPage&&(this.currentPage=1),void 0===this.pageSizeOptions&&(this.pageSizeOptions=[10,25,50,100,500]),void 0===this.records&&(this.records=e.records||[]),void 0===this.fields&&(this.fields=e.fields||[]),void 0===this.filters&&(this.filters=e.filters||[]),void 0===this.sort&&(this.sort=[]),void 0===this.columnSizes&&(this.columnSizes={}),void 0===this.fetchPending&&(this.fetchPending=!1)}handleSelectAllChange=e=>{e.target.checked?this.selectAllOnPage():this.deselectAllOnPage()};handleFieldClick=e=>{const t=this.sort.find(t=>t.name===e),s=!t||!t.asc;this.sortBy(e,s)};handleRecordSelectionChange=(e,t)=>{e[selected]=!!t.target.checked,this.requestUpdate(),this.dispatchEvent(new CustomEvent("selectionChange",{bubbles:!0}))};firstUpdated(){this.setData({records:this.records,fields:this.fields,filters:this.filters})}childrenUpdated(){this.requestUpdate()}updated(e){if(super.updated(e),this.enableSelection){const e=this.shadowRoot.getElementById("select-all");e&&(e.checked=this.allOnPageSelected())}
|
|
2
|
-
<
|
|
3
|
-
<input
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
`),this.hasBeforeControls()&&e.push(html`
|
|
10
|
-
<div class="field cell field-before-controls" style="width: ${this.columnSizes.beforeControls}px"></div>
|
|
11
|
-
`),this.fields.forEach(({name:t,label:s,hidden:i})=>{if(i)return;const r=this.sort.find(e=>e.name===t),o=this.sort.length>0&&this.sort[this.sort.length-1].name===t,l=r?r.asc?"sort-asc":"sort-desc":"";e.push(html`
|
|
12
|
-
<div
|
|
13
|
-
class="field cell ${l}"
|
|
14
|
-
style="width: ${this.columnSizes[t]}px; ${this.enableSorting?"cursor: pointer;":""}"
|
|
1
|
+
import{html,css,unsafeStatic,literal}from"../lit-all.min.js";import ShadowComponent from"./ShadowComponent.js";import{toTitleCase}from"../utils/string.js";import{boolExists}from"../utils/propConverters.js";const selected=Symbol("selected"),hidden=Symbol("hidden"),index=Symbol("index"),editing=Symbol("editing");export default class Table extends ShadowComponent{static properties={enablePages:{type:Boolean,reflect:!0,converter:boolExists,attribute:"enable-pages"},pageSize:{type:Number,reflect:!0,attribute:"page-size"},currentPage:{type:Number,reflect:!0,attribute:"current-page"},pageSizeOptions:{type:Array,attribute:"page-size-options"},enableSelection:{type:Boolean,reflect:!0,converter:boolExists,attribute:"enable-selection"},enableSorting:{type:Boolean,reflect:!0,converter:boolExists,attribute:"enable-sorting"},caseSensitiveFilters:{type:Boolean,reflect:!0,converter:boolExists,attribute:"case-sensitive-filters"},fields:{type:Array},records:{type:Array},filters:{type:Array},sort:{type:Array},columnSizes:{type:Object},fetchPending:{type:Boolean}};constructor(e={}){super(),void 0===this.pageSize&&(this.pageSize=50),void 0===this.currentPage&&(this.currentPage=1),void 0===this.pageSizeOptions&&(this.pageSizeOptions=[10,25,50,100,500]),void 0===this.records&&(this.records=e.records||[]),void 0===this.fields&&(this.fields=e.fields||[]),void 0===this.filters&&(this.filters=e.filters||[]),void 0===this.sort&&(this.sort=[]),void 0===this.columnSizes&&(this.columnSizes={}),void 0===this.fetchPending&&(this.fetchPending=!1)}handleSelectAllChange=e=>{e.target.checked?this.selectAllOnPage():this.deselectAllOnPage()};handleFieldClick=e=>{const t=this.sort.find(t=>t.name===e),s=!t||!t.asc;this.sortBy(e,s)};handleRecordSelectionChange=(e,t)=>{e[selected]=!!t.target.checked,this.requestUpdate(),this.dispatchEvent(new CustomEvent("selectionChange",{bubbles:!0}))};firstUpdated(){this.setData({records:this.records,fields:this.fields,filters:this.filters})}childrenUpdated(){this.requestUpdate()}updated(e){if(super.updated(e),this.enableSelection){const e=this.shadowRoot.getElementById("select-all");e&&(e.checked=this.allOnPageSelected())}}renderColgroupTemplate(){const e=[];return this.enableSelection&&e.push(html`<col style="width: 40px" />`),this.hasBeforeControls()&&e.push(html`<col style="width: ${this.columnSizes.beforeControls}px" />`),this.fields.forEach(({size:t,hidden:s})=>{s||e.push(t?html`<col style="width: ${t}px" />`:html`<col />`)}),this.hasAfterControls()&&e.push(html`<col style="width: ${this.columnSizes.afterControls}px" />`),e}getColumnCount(){let e=0;return this.enableSelection&&e++,this.hasBeforeControls()&&e++,this.fields.forEach(({hidden:t})=>{t||e++}),this.hasAfterControls()&&e++,e}renderFieldsTemplate(){const e=[];return this.enableSelection&&e.push(html`
|
|
2
|
+
<th class="controls field-select">
|
|
3
|
+
<input type="checkbox" id="select-all" @change=${this.handleSelectAllChange} />
|
|
4
|
+
</th>
|
|
5
|
+
`),this.hasBeforeControls()&&e.push(html`<th class="controls field-before-controls"></th>`),this.fields.forEach(({name:t,label:s,hidden:i})=>{if(i)return;const r=this.sort.find(e=>e.name===t),o=this.sort.length>0&&this.sort[this.sort.length-1].name===t,n=r?r.asc?"sort-asc":"sort-desc":"";e.push(html`
|
|
6
|
+
<th
|
|
7
|
+
class="${n}"
|
|
8
|
+
style="${this.enableSorting?"cursor: pointer;":""}"
|
|
15
9
|
@click=${this.enableSorting?()=>this.handleFieldClick(t):null}
|
|
16
10
|
>
|
|
17
11
|
${s}
|
|
18
12
|
${o?html`<k-icon name="arrow" direction="${r.asc?"down":"up"}" class="icon-sort"></k-icon>`:""}
|
|
19
|
-
</
|
|
20
|
-
`)}),this.hasAfterControls()&&e.push(html`
|
|
21
|
-
<
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
<input
|
|
25
|
-
type="checkbox"
|
|
13
|
+
</th>
|
|
14
|
+
`)}),this.hasAfterControls()&&e.push(html`<th class="controls field-after-controls"></th>`),e}renderRecordsTemplate(){let e=this.getDisplayedRecords(),t=0,s=this.pageSize;this.enablePages&&(t=(this.currentPage-1)*this.pageSize,s=t+this.pageSize,e=e.slice(t,s));let i=null,r=0;const o=e.map((e,s)=>null!==e?this.renderRecordTemplate(e):(null===i&&(i=t+s),r++,html`<tr class="record fetching"><td class="cell" colspan="${this.getColumnCount()}">Loading...</td></tr>`));return null===i||this.fetchPending||setTimeout(()=>{this.fetchPending||this.dispatchEvent(new CustomEvent("fetchRecords",{detail:{start:i,count:r},bubbles:!0}))},0),o}renderRecordTemplate(e){const t=[];return this.enableSelection&&t.push(html`
|
|
15
|
+
<td class="cell selection controls">
|
|
16
|
+
<input
|
|
17
|
+
type="checkbox"
|
|
26
18
|
.checked=${e[selected]}
|
|
27
19
|
@change=${t=>this.handleRecordSelectionChange(e,t)}
|
|
28
20
|
/>
|
|
29
|
-
</
|
|
30
|
-
`),this.hasBeforeControls()&&t.push(this.renderBeforeControlsTemplate()),this.fields.forEach(({name:s,formatter:i,calculator:r,type:o,editor:
|
|
31
|
-
<
|
|
32
|
-
${e[editing]?this.renderEditingCell(e,s,a,r,
|
|
33
|
-
</
|
|
21
|
+
</td>
|
|
22
|
+
`),this.hasBeforeControls()&&t.push(this.renderBeforeControlsTemplate()),this.fields.forEach(({name:s,formatter:i,calculator:r,type:o,editor:n,hidden:l})=>{if(l)return;let a=e[s]||"";t.push(html`
|
|
23
|
+
<td class="cell" data-field=${s}>
|
|
24
|
+
${e[editing]?this.renderEditingCell(e,s,a,r,n,o):this.renderDisplayCell(e,s,a,r,i)}
|
|
25
|
+
</td>
|
|
34
26
|
`)}),this.hasAfterControls()&&t.push(this.renderAfterControlsTemplate()),html`
|
|
35
|
-
<
|
|
27
|
+
<tr class="record ${e[editing]?"editing":""}" data-index=${e[index]}>
|
|
36
28
|
${t}
|
|
37
|
-
</
|
|
29
|
+
</tr>
|
|
38
30
|
`}renderEditingCell(e,t,s,i,r,o){if(i)return html`<input disabled .value=${i(e,this)} />`;if(r){const e=r(s);return html`${e}`}switch(o||typeof s){case"number":return html`<input type="number" .value=${s} />`;case"date":return html`<input type="date" .value=${s} />`;case"boolean":return html`
|
|
39
31
|
<select .value=${s}>
|
|
40
32
|
<option value="true" ?selected=${s}>True</option>
|
|
41
33
|
<option value="false" ?selected=${!s}>False</option>
|
|
42
34
|
</select>
|
|
43
35
|
`;default:return html`<input type="text" .value=${s} />`}}renderDisplayCell(e,t,s,i,r){return i?i(e,this):r?r(s):s}renderBeforeControlsTemplate(){const e=[];return this.querySelectorAll('[slot="before"]').forEach(t=>{const s=t.tagName.toLowerCase(),i=document.createElement(s);Array.from(t.attributes).forEach(e=>{"slot"!==e.name&&i.setAttribute(e.name,e.value)}),t.innerHTML&&(i.innerHTML=t.innerHTML),e.push(i)}),html`
|
|
44
|
-
<
|
|
36
|
+
<td class="cell controls controls-before">
|
|
45
37
|
${e}
|
|
46
|
-
</
|
|
38
|
+
</td>
|
|
47
39
|
`}renderAfterControlsTemplate(){const e=[];return this.querySelectorAll('[slot="after"]').forEach(t=>{const s=t.tagName.toLowerCase(),i=document.createElement(s);Array.from(t.attributes).forEach(e=>{"slot"!==e.name&&i.setAttribute(e.name,e.value)}),t.innerHTML&&(i.innerHTML=t.innerHTML),e.push(i)}),html`
|
|
48
|
-
<
|
|
40
|
+
<td class="cell controls controls-after">
|
|
49
41
|
${e}
|
|
50
|
-
</
|
|
51
|
-
`}hasBeforeControls(){return!!this.querySelector('[slot="before"]')}hasAfterControls(){return!!this.querySelector('[slot="after"]')}hasTopControls(){return!!this.querySelector('[slot="top"]')}hasBottomControls(){return!!this.querySelector(":scope > :not([slot])")}editRecord(e){e[editing]=!0;const t=this.shadowRoot.querySelector(`.record[data-index="${e[index]}"]`);t&&(t.classList.add("editing"),t.setAttribute("editing","true"),t.querySelectorAll(".cell[data-field]").forEach(t=>{const s=t.dataset.field,i=this.fields.find(e=>e.name===s);if(i){const r=e[s]||"";if(t.innerHTML="",i.calculator){const s=document.createElement("input");s.disabled=!0,s.value=i.calculator(e,this),t.appendChild(s)}else if(i.editor)t.appendChild(i.editor(r));else{const e=i.type||typeof r,s=Table.editors[e]||Table.editors.string;t.appendChild(s(r))}}})),this.dispatchEvent(new CustomEvent("editingChange",{detail:{record:e,editing:!0},bubbles:!0}))}saveEditedRecord(e){const t=this.shadowRoot.querySelector(`.record[data-index="${e[index]}"]`);t&&(t.querySelectorAll(".cell[data-field]").forEach(t=>{const s=t.dataset.field,i=this.fields.find(e=>e.name===s);if(i&&!i.calculator){const i=t.querySelector("input, select");i&&(e[s]=i.value)}}),e[editing]=!1,t.classList.remove("editing"),t.removeAttribute("editing"),t.querySelectorAll(".cell[data-field]").forEach(t=>{const s=t.dataset.field,i=this.fields.find(e=>e.name===s);if(i){const r=e[s]||"";i.calculator?t.textContent=i.calculator(e,this):i.formatter?t.innerHTML=i.formatter(r):t.textContent=r}})),this.dispatchEvent(new CustomEvent("editingChange",{detail:{record:e,editing:!1},bubbles:!0}))}cancelEditedRecord(e){e[editing]=!1;const t=this.shadowRoot.querySelector(`.record[data-index="${e[index]}"]`);t&&(t.classList.remove("editing"),t.removeAttribute("editing"),t.querySelectorAll(".cell[data-field]").forEach(t=>{const s=t.dataset.field,i=this.fields.find(e=>e.name===s);if(i){const r=e[s]||"";i.calculator?t.textContent=i.calculator(e,this):i.formatter?t.innerHTML=i.formatter(r):t.textContent=r}})),this.dispatchEvent(new CustomEvent("editingChange",{detail:{record:e,editing:!1},bubbles:!0}))}recordIsEditing(e){return e[editing]}getCurrentPage(){return this.currentPage}getTotalPages(){return Math.ceil(this.getDisplayedRecords().length/this.pageSize)}setPage(e){e<1||e>this.getTotalPages()||(this.currentPage=e,this.requestUpdate(),this.dispatchEvent(new CustomEvent("pageChange",{bubbles:!0})))}firstPage(){1!==this.currentPage&&this.setPage(1)}nextPage(){this.currentPage<this.getTotalPages()&&this.setPage(this.currentPage+1)}prevPage(){this.currentPage>1&&this.setPage(this.currentPage-1)}lastPage(){this.currentPage!==this.getTotalPages()&&this.setPage(this.getTotalPages())}setPageSize(e){this.pageSize=e,this.currentPage=1,this.requestUpdate(),this.dispatchEvent(new CustomEvent("pageSizeChange",{bubbles:!0}))}getPageSize(){return this.pageSize}getPageSizeOptions(){return this.pageSizeOptions}getFieldLabel(e){const t=this.fields.find(t=>t.name===e);return t?t.label:toTitleCase(e)}setPageSizeOptions(e){this.pageSizeOptions=e,this.requestUpdate()}setData({records:e=!1,fields:t=!1,pageSize:s=!1,pageSizeOptions:i=!1,currentPage:r=!1,enableSelection:o}={}){let l=!1,n=this.getTotalPages(),a=this.currentPage;e&&(this.records=e.map(e=>({...e})),this.records.forEach((e,t)=>{e[index]=t,e[selected]=!1,e[hidden]=!1,e[editing]=!1}),this.fields=t||Table.extractFieldsFromRecords(this.records),l=!0),s&&(this.pageSize=s,l=!0),i&&(this.pageSizeOptions=i),r&&(this.currentPage=r,l=!0),void 0!==o&&(this.enableSelection=o,l=!0),l&&this.requestUpdate();const d=this.getTotalPages();d!==n&&this.dispatchEvent(new CustomEvent("pageCountChanged",{detail:{totalPages:this.getTotalPages()},bubbles:!0})),a>d&&this.setPage(d)}setRecords(e,t){let s=this.getTotalPages(),i=this.currentPage;this.records=e.map(e=>({...e})),this.records.forEach((e,t)=>{e[index]=t,e[selected]=!1,e[hidden]=!1,e[editing]=!1}),this.fields=t||Table.extractFieldsFromRecords(this.records),this.requestUpdate(),this.dispatchEvent(new CustomEvent("recordsSet",{detail:{records:e},bubbles:!0}));const r=this.getTotalPages();r!==s&&this.dispatchEvent(new CustomEvent("pageCountChanged",{detail:{totalPages:this.getTotalPages()},bubbles:!0})),i>r&&this.setPage(r)}setupFetchRecords(e,t){const s=this.records.length,i=this.getTotalPages();if(s<e){this.records.length=e,this.records.fill(null,s),this.requestUpdate();const t=this.getTotalPages();t!==i&&setTimeout(()=>{this.dispatchEvent(new CustomEvent("pageCountChanged",{detail:{totalPages:t},bubbles:!0}))},0)}this.addEventListener("fetchRecords",async e=>{if(this.fetchPending)return;this.fetchPending=!0;const{start:s,count:i}=e.detail,r=await t(s,i);r.forEach((e,t)=>{e[index]=s+t,void 0===e[selected]&&(e[selected]=!1),void 0===e[hidden]&&(e[hidden]=!1),void 0===e[editing]&&(e[editing]=!1)}),this.records.splice(s,r.length,...r),this.fetchPending=!1,this.requestUpdate()})}addRecord(e){e[selected]=!1,e[hidden]=!1,e[index]=this.records.length,this.records.push(e),this.requestUpdate(),this.dispatchEvent(new CustomEvent("recordAdded",{detail:{record:e},bubbles:!0}))}updateRecord(e,t){let s=!1,i=this.records.find(t=>t===e);if(i||void 0===e[index]||(i=this.records[e[index]]),Object.keys(t).forEach(e=>{i.hasOwnProperty(e)&&(i[e]=t[e],s=!0)}),s){const e=(this.currentPage-1)*this.pageSize,t=e+this.pageSize;(!this.enablePages||i[index]>=e&&i[index]<t)&&this.requestUpdate()}}deleteRecord(e){let t=this.records.find(t=>t===e),s=this.getTotalPages();if(t||void 0===e[index]||(t=this.records[e[index]]),t){const e=this.records.indexOf(t);this.records.splice(e,1),this.records.forEach((e,t)=>{e[index]=t}),this.requestUpdate(),this.dispatchEvent(new CustomEvent("selectionChange",{bubbles:!0})),this.dispatchEvent(new CustomEvent("recordDeleted",{detail:{index:e},bubbles:!0}));const i=this.getTotalPages();this.currentPage>i&&this.setPage(i),i!==s&&this.dispatchEvent(new CustomEvent("pageCountChanged",{detail:{totalPages:i},bubbles:!0}))}}deleteSelected(){let e=this.getTotalPages();this.getSelectedRecords().forEach(e=>{let t=this.records.find(t=>t===e);if(t||void 0===e[index]||(t=this.records[e[index]]),t){const e=this.records.indexOf(t);this.records.splice(e,1)}}),this.records.forEach((e,t)=>{e[index]=t}),this.requestUpdate();const t=this.getTotalPages();this.currentPage>t&&this.setPage(t),t!==e&&this.dispatchEvent(new CustomEvent("pageCountChanged",{detail:{totalPages:t},bubbles:!0})),this.dispatchEvent(new CustomEvent("selectionChange",{bubbles:!0}))}getSelectedRecords(){return this.records.filter(e=>e[selected])}selectAllOnPage(){const e=(this.currentPage-1)*this.pageSize,t=Math.min(e+this.pageSize,this.records.length);for(let s=e;s<t;s++)this.records[s][selected]=!0;this.requestUpdate(),setTimeout(()=>{this.dispatchEvent(new CustomEvent("selectionChange",{bubbles:!0}))},0)}deselectAllOnPage(){const e=(this.currentPage-1)*this.pageSize,t=Math.min(e+this.pageSize,this.records.length);for(let s=e;s<t;s++)this.records[s][selected]=!1;this.requestUpdate(),setTimeout(()=>{this.dispatchEvent(new CustomEvent("selectionChange",{bubbles:!0}))},0)}allOnPageSelected(){const e=(this.currentPage-1)*this.pageSize,t=Math.min(e+this.pageSize,this.records.length);for(let s=e;s<t;s++)if(!this.records[s][selected])return!1;return!0}sortBy(e,t=!0){this.sort=this.sort.filter(t=>t.name!==e),this.sort.push({name:e,asc:t}),this.requestUpdate()}hideRecord(e){let t=this.records.find(t=>t===e);t||void 0===e[index]||(t=this.records[e[index]]),t&&(t[hidden]=!0,this.requestUpdate(),this.dispatchEvent(new CustomEvent("recordHidden",{bubbles:!0})))}showRecord(e){let t=this.records.find(t=>t===e);t||void 0===e[index]||(t=this.records[e[index]]),t&&(t[hidden]=!1,this.requestUpdate(),this.dispatchEvent(new CustomEvent("recordShown",{bubbles:!0})))}showAllRecords(){this.records.forEach(e=>{e[hidden]=!1}),this.filters.length&&(this.filters=[],this.dispatchEvent(new CustomEvent("filterRemoved",{bubbles:!0})),this.dispatchEvent(new CustomEvent("filterChange",{bubbles:!0}))),this.requestUpdate(),this.dispatchEvent(new CustomEvent("recordShown",{bubbles:!0})),this.dispatchEvent(new CustomEvent("allRecordsShown",{bubbles:!0}))}addFilter(e,t,s){this.filters.push({field:e,condition:t,value:s}),this.dispatchEvent(new CustomEvent("filterAdded",{bubbles:!0})),this.dispatchEvent(new CustomEvent("filterChange",{bubbles:!0})),this.requestUpdate()}removeFilter(e,t,s,i=!0){const r=this.filters.findIndex(i=>i.field===e&&i.condition===t&&i.value===s);-1!==r&&(this.records.forEach(i=>{this.testFilter(i,e,t,s)||(i[hidden]=!1)}),this.filters.splice(r,1),this.dispatchEvent(new CustomEvent("filterRemoved",{bubbles:!0})),this.dispatchEvent(new CustomEvent("filterChange",{bubbles:!0})),i&&this.requestUpdate())}testFilter(e,t,s,i){let r=e[t],o=i;switch(this.caseSensitiveFilters||"string"!=typeof r||"string"!=typeof i||(r=r.toLowerCase(),o=i.toLowerCase()),s){case"equals":return r===o;case"not-equals":return r!==o;case"contains":return r.includes(o);case"not-contains":return!r.includes(o);case"greater-than":return r>o;case"less-than":return r<o;case"greater-than-or-equal":return r>=o;case"less-than-or-equal":return r<=o;default:return!0}}removeAllFilters(){this.filters.length&&([...this.filters].forEach(({field:e,condition:t,value:s})=>{this.removeFilter(e,t,s,!1)}),this.requestUpdate())}search(e){const t=e.trim().toLowerCase();let s=!1;this.records.forEach(e=>{if(e[hidden])return;let i=!1;this.fields.forEach(({name:s})=>{(e[s]?.toString().toLowerCase()||"").includes(t)&&(i=!0)}),e[hidden]!==!i&&(e[hidden]=!i,s=!0)}),s&&(this.dispatchEvent(new CustomEvent("recordHidden",{bubbles:!0})),this.requestUpdate()),this.dispatchEvent(new CustomEvent("search",{detail:{term:e},bubbles:!0}))}getDisplayedRecords(){this.filters.forEach(({field:e,condition:t,value:s})=>{this.records.forEach(i=>{null!==i&&(this.testFilter(i,e,t,s)||(i[hidden]=!0))})});let e=this.records.filter(e=>null===e||!e[hidden]);return this.sort.forEach(({name:t,asc:s})=>{e.sort((e,i)=>null===e||null===i?0:e[t]<i[t]?s?-1:1:e[t]>i[t]?s?1:-1:0)}),e}getHiddenRecords(){return this.records.filter(e=>e[hidden])}calculateColumnSizes(){const e={total:0};this.enableSelection&&(e.total+=40);const t=Array.from(this.querySelectorAll('[slot="before"]')),s=Array.from(this.querySelectorAll('[slot="after"]'));e.beforeControls=t.reduce((e,t)=>e+(t.maxWidth||40),0),e.afterControls=s.reduce((e,t)=>e+(t.maxWidth||40),0),this.hasBeforeControls()&&(e.total+=e.beforeControls),this.hasAfterControls()&&(e.total+=e.afterControls),this.fields.forEach(t=>{if(t.size)e[t.name]=t.size,e.total+=t.size;else{let s=0;this.records.slice(0,100).forEach(e=>{if(null===e)return;let i=e[t.name];t.calculator&&(i=t.calculator(e,this)),t.formatter&&(i=t.formatter(i)),i&&i.toString().length>s&&(s=i.toString().length)}),e[t.name]=Math.max(10*s+32,128),t.hidden||(e.total+=e[t.name])}});const i=[...t,...s].some(e=>void 0===e.maxWidth);return JSON.stringify(this.columnSizes)!==JSON.stringify(e)&&(this.columnSizes=e),i&&setTimeout(()=>this.calculateColumnSizes(),0),this.columnSizes}setFieldHiddenState(e,t){const s=this.fields.find(t=>t.name===e);s&&(s.hidden=t,this.calculateColumnSizes(),this.requestUpdate(),this.dispatchEvent(new CustomEvent("fieldVisibilityChanged",{detail:{field:s},bubbles:!0})),this.dispatchEvent(new CustomEvent(t?"fieldHidden":"fieldShown",{detail:{field:s},bubbles:!0})))}hideField(e){this.setFieldHiddenState(e,!0)}showField(e){this.setFieldHiddenState(e,!1)}reorderFields(e){const t=[];e.forEach(e=>{const s=this.fields.find(t=>t.name===e);s&&t.push(s)}),this.fields=t,this.requestUpdate()}render(){return this.records&&this.fields?(this.calculateColumnSizes(),this.hasTopControls()?this.setAttribute("top-controls","true"):this.removeAttribute("top-controls"),this.hasBottomControls()?this.setAttribute("bottom-controls","true"):this.removeAttribute("bottom-controls"),html`
|
|
42
|
+
</td>
|
|
43
|
+
`}hasBeforeControls(){return!!this.querySelector('[slot="before"]')}hasAfterControls(){return!!this.querySelector('[slot="after"]')}hasTopControls(){return!!this.querySelector('[slot="top"]')}hasBottomControls(){return!!this.querySelector(":scope > :not([slot])")}editRecord(e){e[editing]=!0;const t=this.shadowRoot.querySelector(`.record[data-index="${e[index]}"]`);t&&(t.classList.add("editing"),t.setAttribute("editing","true"),t.querySelectorAll(".cell[data-field]").forEach(t=>{const s=t.dataset.field,i=this.fields.find(e=>e.name===s);if(i){const r=e[s]||"";if(t.innerHTML="",i.calculator){const s=document.createElement("input");s.disabled=!0,s.value=i.calculator(e,this),t.appendChild(s)}else if(i.editor)t.appendChild(i.editor(r));else{const e=i.type||typeof r,s=Table.editors[e]||Table.editors.string;t.appendChild(s(r))}}})),this.dispatchEvent(new CustomEvent("editingChange",{detail:{record:e,editing:!0},bubbles:!0}))}saveEditedRecord(e){const t=this.shadowRoot.querySelector(`.record[data-index="${e[index]}"]`);t&&(t.querySelectorAll(".cell[data-field]").forEach(t=>{const s=t.dataset.field,i=this.fields.find(e=>e.name===s);if(i&&!i.calculator){const i=t.querySelector("input, select");i&&(e[s]=i.value)}}),e[editing]=!1,t.classList.remove("editing"),t.removeAttribute("editing"),t.querySelectorAll(".cell[data-field]").forEach(t=>{const s=t.dataset.field,i=this.fields.find(e=>e.name===s);if(i){const r=e[s]||"";i.calculator?t.textContent=i.calculator(e,this):i.formatter?t.innerHTML=i.formatter(r):t.textContent=r}})),this.dispatchEvent(new CustomEvent("editingChange",{detail:{record:e,editing:!1},bubbles:!0}))}cancelEditedRecord(e){e[editing]=!1;const t=this.shadowRoot.querySelector(`.record[data-index="${e[index]}"]`);t&&(t.classList.remove("editing"),t.removeAttribute("editing"),t.querySelectorAll(".cell[data-field]").forEach(t=>{const s=t.dataset.field,i=this.fields.find(e=>e.name===s);if(i){const r=e[s]||"";i.calculator?t.textContent=i.calculator(e,this):i.formatter?t.innerHTML=i.formatter(r):t.textContent=r}})),this.dispatchEvent(new CustomEvent("editingChange",{detail:{record:e,editing:!1},bubbles:!0}))}recordIsEditing(e){return e[editing]}getCurrentPage(){return this.currentPage}getTotalPages(){return Math.ceil(this.getDisplayedRecords().length/this.pageSize)}setPage(e){e<1||e>this.getTotalPages()||(this.currentPage=e,this.requestUpdate(),this.dispatchEvent(new CustomEvent("pageChange",{bubbles:!0})))}firstPage(){1!==this.currentPage&&this.setPage(1)}nextPage(){this.currentPage<this.getTotalPages()&&this.setPage(this.currentPage+1)}prevPage(){this.currentPage>1&&this.setPage(this.currentPage-1)}lastPage(){this.currentPage!==this.getTotalPages()&&this.setPage(this.getTotalPages())}setPageSize(e){this.pageSize=e,this.currentPage=1,this.requestUpdate(),this.dispatchEvent(new CustomEvent("pageSizeChange",{bubbles:!0}))}getPageSize(){return this.pageSize}getPageSizeOptions(){return this.pageSizeOptions}getFieldLabel(e){const t=this.fields.find(t=>t.name===e);return t?t.label:toTitleCase(e)}setPageSizeOptions(e){this.pageSizeOptions=e,this.requestUpdate()}setData({records:e=!1,fields:t=!1,pageSize:s=!1,pageSizeOptions:i=!1,currentPage:r=!1,enableSelection:o}={}){let n=!1,l=this.getTotalPages(),a=this.currentPage;e&&(this.records=e.map(e=>({...e})),this.records.forEach((e,t)=>{e[index]=t,e[selected]=!1,e[hidden]=!1,e[editing]=!1}),this.fields=t||Table.extractFieldsFromRecords(this.records),n=!0),s&&(this.pageSize=s,n=!0),i&&(this.pageSizeOptions=i),r&&(this.currentPage=r,n=!0),void 0!==o&&(this.enableSelection=o,n=!0),n&&this.requestUpdate();const d=this.getTotalPages();d!==l&&this.dispatchEvent(new CustomEvent("pageCountChanged",{detail:{totalPages:this.getTotalPages()},bubbles:!0})),a>d&&this.setPage(d)}setRecords(e,t){let s=this.getTotalPages(),i=this.currentPage;this.records=e.map(e=>({...e})),this.records.forEach((e,t)=>{e[index]=t,e[selected]=!1,e[hidden]=!1,e[editing]=!1}),this.fields=t||Table.extractFieldsFromRecords(this.records),this.requestUpdate(),this.dispatchEvent(new CustomEvent("recordsSet",{detail:{records:e},bubbles:!0}));const r=this.getTotalPages();r!==s&&this.dispatchEvent(new CustomEvent("pageCountChanged",{detail:{totalPages:this.getTotalPages()},bubbles:!0})),i>r&&this.setPage(r)}setupFetchRecords(e,t){const s=this.records.length,i=this.getTotalPages();if(s<e){this.records.length=e,this.records.fill(null,s),this.requestUpdate();const t=this.getTotalPages();t!==i&&setTimeout(()=>{this.dispatchEvent(new CustomEvent("pageCountChanged",{detail:{totalPages:t},bubbles:!0}))},0)}this.addEventListener("fetchRecords",async e=>{if(this.fetchPending)return;this.fetchPending=!0;const{start:s,count:i}=e.detail,r=await t(s,i);r.forEach((e,t)=>{e[index]=s+t,void 0===e[selected]&&(e[selected]=!1),void 0===e[hidden]&&(e[hidden]=!1),void 0===e[editing]&&(e[editing]=!1)}),this.records.splice(s,r.length,...r),this.fetchPending=!1,this.requestUpdate()})}addRecord(e){e[selected]=!1,e[hidden]=!1,e[index]=this.records.length,this.records.push(e),this.requestUpdate(),this.dispatchEvent(new CustomEvent("recordAdded",{detail:{record:e},bubbles:!0}))}updateRecord(e,t){let s=!1,i=this.records.find(t=>t===e);if(i||void 0===e[index]||(i=this.records[e[index]]),Object.keys(t).forEach(e=>{i.hasOwnProperty(e)&&(i[e]=t[e],s=!0)}),s){const e=(this.currentPage-1)*this.pageSize,t=e+this.pageSize;(!this.enablePages||i[index]>=e&&i[index]<t)&&this.requestUpdate()}}deleteRecord(e){let t=this.records.find(t=>t===e),s=this.getTotalPages();if(t||void 0===e[index]||(t=this.records[e[index]]),t){const e=this.records.indexOf(t);this.records.splice(e,1),this.records.forEach((e,t)=>{e[index]=t}),this.requestUpdate(),this.dispatchEvent(new CustomEvent("selectionChange",{bubbles:!0})),this.dispatchEvent(new CustomEvent("recordDeleted",{detail:{index:e},bubbles:!0}));const i=this.getTotalPages();this.currentPage>i&&this.setPage(i),i!==s&&this.dispatchEvent(new CustomEvent("pageCountChanged",{detail:{totalPages:i},bubbles:!0}))}}deleteSelected(){let e=this.getTotalPages();this.getSelectedRecords().forEach(e=>{let t=this.records.find(t=>t===e);if(t||void 0===e[index]||(t=this.records[e[index]]),t){const e=this.records.indexOf(t);this.records.splice(e,1)}}),this.records.forEach((e,t)=>{e[index]=t}),this.requestUpdate();const t=this.getTotalPages();this.currentPage>t&&this.setPage(t),t!==e&&this.dispatchEvent(new CustomEvent("pageCountChanged",{detail:{totalPages:t},bubbles:!0})),this.dispatchEvent(new CustomEvent("selectionChange",{bubbles:!0}))}getSelectedRecords(){return this.records.filter(e=>e[selected])}selectAllOnPage(){const e=(this.currentPage-1)*this.pageSize,t=Math.min(e+this.pageSize,this.records.length);for(let s=e;s<t;s++)this.records[s][selected]=!0;this.requestUpdate(),setTimeout(()=>{this.dispatchEvent(new CustomEvent("selectionChange",{bubbles:!0}))},0)}deselectAllOnPage(){const e=(this.currentPage-1)*this.pageSize,t=Math.min(e+this.pageSize,this.records.length);for(let s=e;s<t;s++)this.records[s][selected]=!1;this.requestUpdate(),setTimeout(()=>{this.dispatchEvent(new CustomEvent("selectionChange",{bubbles:!0}))},0)}allOnPageSelected(){const e=(this.currentPage-1)*this.pageSize,t=Math.min(e+this.pageSize,this.records.length);for(let s=e;s<t;s++)if(!this.records[s][selected])return!1;return!0}sortBy(e,t=!0){this.sort=this.sort.filter(t=>t.name!==e),this.sort.push({name:e,asc:t}),this.requestUpdate()}hideRecord(e){let t=this.records.find(t=>t===e);t||void 0===e[index]||(t=this.records[e[index]]),t&&(t[hidden]=!0,this.requestUpdate(),this.dispatchEvent(new CustomEvent("recordHidden",{bubbles:!0})))}showRecord(e){let t=this.records.find(t=>t===e);t||void 0===e[index]||(t=this.records[e[index]]),t&&(t[hidden]=!1,this.requestUpdate(),this.dispatchEvent(new CustomEvent("recordShown",{bubbles:!0})))}showAllRecords(){this.records.forEach(e=>{e[hidden]=!1}),this.filters.length&&(this.filters=[],this.dispatchEvent(new CustomEvent("filterRemoved",{bubbles:!0})),this.dispatchEvent(new CustomEvent("filterChange",{bubbles:!0}))),this.requestUpdate(),this.dispatchEvent(new CustomEvent("recordShown",{bubbles:!0})),this.dispatchEvent(new CustomEvent("allRecordsShown",{bubbles:!0}))}addFilter(e,t,s){this.filters.push({field:e,condition:t,value:s}),this.dispatchEvent(new CustomEvent("filterAdded",{bubbles:!0})),this.dispatchEvent(new CustomEvent("filterChange",{bubbles:!0})),this.requestUpdate()}removeFilter(e,t,s,i=!0){const r=this.filters.findIndex(i=>i.field===e&&i.condition===t&&i.value===s);-1!==r&&(this.records.forEach(i=>{this.testFilter(i,e,t,s)||(i[hidden]=!1)}),this.filters.splice(r,1),this.dispatchEvent(new CustomEvent("filterRemoved",{bubbles:!0})),this.dispatchEvent(new CustomEvent("filterChange",{bubbles:!0})),i&&this.requestUpdate())}testFilter(e,t,s,i){let r=e[t],o=i;switch(this.caseSensitiveFilters||"string"!=typeof r||"string"!=typeof i||(r=r.toLowerCase(),o=i.toLowerCase()),s){case"equals":return r===o;case"not-equals":return r!==o;case"contains":return r.includes(o);case"not-contains":return!r.includes(o);case"greater-than":return r>o;case"less-than":return r<o;case"greater-than-or-equal":return r>=o;case"less-than-or-equal":return r<=o;default:return!0}}removeAllFilters(){this.filters.length&&([...this.filters].forEach(({field:e,condition:t,value:s})=>{this.removeFilter(e,t,s,!1)}),this.requestUpdate())}search(e){const t=e.trim().toLowerCase();let s=!1;this.records.forEach(e=>{if(e[hidden])return;let i=!1;this.fields.forEach(({name:s})=>{(e[s]?.toString().toLowerCase()||"").includes(t)&&(i=!0)}),e[hidden]!==!i&&(e[hidden]=!i,s=!0)}),s&&(this.dispatchEvent(new CustomEvent("recordHidden",{bubbles:!0})),this.requestUpdate()),this.dispatchEvent(new CustomEvent("search",{detail:{term:e},bubbles:!0}))}getDisplayedRecords(){this.filters.forEach(({field:e,condition:t,value:s})=>{this.records.forEach(i=>{null!==i&&(this.testFilter(i,e,t,s)||(i[hidden]=!0))})});let e=this.records.filter(e=>null===e||!e[hidden]);return this.sort.forEach(({name:t,asc:s})=>{e.sort((e,i)=>null===e||null===i?0:e[t]<i[t]?s?-1:1:e[t]>i[t]?s?1:-1:0)}),e}getHiddenRecords(){return this.records.filter(e=>e[hidden])}calculateColumnSizes(){const e=Array.from(this.querySelectorAll('[slot="before"]')),t=Array.from(this.querySelectorAll('[slot="after"]')),s={beforeControls:e.reduce((e,t)=>e+(t.maxWidth||40),0),afterControls:t.reduce((e,t)=>e+(t.maxWidth||40),0)},i=[...e,...t].some(e=>void 0===e.maxWidth);return JSON.stringify(this.columnSizes)!==JSON.stringify(s)&&(this.columnSizes=s),i&&setTimeout(()=>this.calculateColumnSizes(),0),this.columnSizes}setFieldHiddenState(e,t){const s=this.fields.find(t=>t.name===e);s&&(s.hidden=t,this.calculateColumnSizes(),this.requestUpdate(),this.dispatchEvent(new CustomEvent("fieldVisibilityChanged",{detail:{field:s},bubbles:!0})),this.dispatchEvent(new CustomEvent(t?"fieldHidden":"fieldShown",{detail:{field:s},bubbles:!0})))}hideField(e){this.setFieldHiddenState(e,!0)}showField(e){this.setFieldHiddenState(e,!1)}reorderFields(e){const t=[];e.forEach(e=>{const s=this.fields.find(t=>t.name===e);s&&t.push(s)}),this.fields=t,this.requestUpdate()}render(){return this.records&&this.fields?(this.calculateColumnSizes(),this.hasTopControls()?this.setAttribute("top-controls","true"):this.removeAttribute("top-controls"),this.hasBottomControls()?this.setAttribute("bottom-controls","true"):this.removeAttribute("bottom-controls"),html`
|
|
52
44
|
<div id="wrapper">
|
|
53
|
-
<div id="top"
|
|
54
|
-
<div id="table">
|
|
55
|
-
<
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
45
|
+
<div id="top"><slot name="top"></slot></div>
|
|
46
|
+
<div id="table-container">
|
|
47
|
+
<table>
|
|
48
|
+
<colgroup>
|
|
49
|
+
${this.renderColgroupTemplate()}
|
|
50
|
+
</colgroup>
|
|
51
|
+
<thead>
|
|
52
|
+
<tr>
|
|
53
|
+
${this.renderFieldsTemplate()}
|
|
54
|
+
</tr>
|
|
55
|
+
</thead>
|
|
56
|
+
<tbody>
|
|
57
|
+
${this.renderRecordsTemplate()}
|
|
58
|
+
</tbody>
|
|
59
|
+
</table>
|
|
61
60
|
</div>
|
|
62
|
-
<div id="bottom"
|
|
61
|
+
<div id="bottom"><slot></slot></div>
|
|
63
62
|
</div>
|
|
64
63
|
<div style="display: none">
|
|
65
64
|
<slot name="before"></slot>
|
|
@@ -68,9 +67,8 @@ import{html,css,unsafeStatic,literal}from"../lit-all.min.js";import ShadowCompon
|
|
|
68
67
|
`):html`
|
|
69
68
|
<div id="wrapper">
|
|
70
69
|
<div id="top"><slot name="top"></slot></div>
|
|
71
|
-
<div id="table">
|
|
72
|
-
<
|
|
73
|
-
<div id="records"></div>
|
|
70
|
+
<div id="table-container">
|
|
71
|
+
<table><thead><tr></tr></thead><tbody></tbody></table>
|
|
74
72
|
</div>
|
|
75
73
|
<div id="bottom"><slot></slot></div>
|
|
76
74
|
</div>
|
|
@@ -81,40 +79,67 @@ import{html,css,unsafeStatic,literal}from"../lit-all.min.js";import ShadowCompon
|
|
|
81
79
|
`}static styles=css`
|
|
82
80
|
:host {
|
|
83
81
|
display: block;
|
|
84
|
-
width: 100%;
|
|
85
|
-
overflow: auto;
|
|
86
82
|
margin-bottom: var(--spacer);
|
|
87
83
|
}
|
|
88
84
|
#wrapper {
|
|
89
|
-
width: min-content;
|
|
90
85
|
border: 1px solid var(--c_border);
|
|
91
86
|
border-radius: var(--radius);
|
|
87
|
+
overflow: hidden;
|
|
92
88
|
}
|
|
93
|
-
#table {
|
|
94
|
-
|
|
89
|
+
#table-container {
|
|
90
|
+
overflow-x: auto;
|
|
95
91
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
92
|
+
table {
|
|
93
|
+
width: 100%;
|
|
94
|
+
border-collapse: collapse;
|
|
99
95
|
}
|
|
100
|
-
|
|
96
|
+
thead tr {
|
|
101
97
|
background-color: var(--c_bg__alt);
|
|
102
|
-
border-bottom: 1px solid var(--c_border);
|
|
103
98
|
}
|
|
104
|
-
|
|
105
|
-
#fields .cell:not(.controls) {
|
|
99
|
+
th, td {
|
|
106
100
|
padding: calc(0.5 * var(--spacer)) var(--spacer);
|
|
101
|
+
vertical-align: middle;
|
|
107
102
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
103
|
+
th:not(:last-child),
|
|
104
|
+
td:not(:last-child) {
|
|
105
|
+
border-right: 1px solid var(--c_border);
|
|
111
106
|
}
|
|
112
|
-
|
|
113
|
-
|
|
107
|
+
th:first-child,
|
|
108
|
+
td:first-child {
|
|
109
|
+
border-left: none;
|
|
110
|
+
}
|
|
111
|
+
th:last-child,
|
|
112
|
+
td:last-child {
|
|
113
|
+
border-right: none;
|
|
114
|
+
}
|
|
115
|
+
thead tr th {
|
|
116
|
+
border-top: none;
|
|
117
|
+
border-bottom: 1px solid var(--c_border);
|
|
114
118
|
}
|
|
115
|
-
|
|
119
|
+
tbody tr:not(:last-child) td {
|
|
116
120
|
border-bottom: 1px solid var(--c_border);
|
|
117
121
|
}
|
|
122
|
+
tbody tr:last-child td {
|
|
123
|
+
border-bottom: none;
|
|
124
|
+
}
|
|
125
|
+
th.controls,
|
|
126
|
+
td.controls {
|
|
127
|
+
padding: 0;
|
|
128
|
+
}
|
|
129
|
+
.field-select,
|
|
130
|
+
.selection {
|
|
131
|
+
width: 40px;
|
|
132
|
+
text-align: center;
|
|
133
|
+
}
|
|
134
|
+
.field-select input,
|
|
135
|
+
.selection input {
|
|
136
|
+
width: 1.25rem;
|
|
137
|
+
height: 1.25rem;
|
|
138
|
+
}
|
|
139
|
+
.icon-sort {
|
|
140
|
+
float: right;
|
|
141
|
+
opacity: 0.5;
|
|
142
|
+
}
|
|
118
143
|
#top, #bottom {
|
|
119
144
|
display: flex;
|
|
120
145
|
width: 100%;
|
|
@@ -133,19 +158,4 @@ import{html,css,unsafeStatic,literal}from"../lit-all.min.js";import ShadowCompon
|
|
|
133
158
|
:host(:not([bottom-controls])) #bottom {
|
|
134
159
|
display: none;
|
|
135
160
|
}
|
|
136
|
-
.field-select,
|
|
137
|
-
.selection {
|
|
138
|
-
display: flex;
|
|
139
|
-
justify-content: center;
|
|
140
|
-
align-items: center;
|
|
141
|
-
}
|
|
142
|
-
.field-select input,
|
|
143
|
-
.selection input {
|
|
144
|
-
width: 1.25rem;
|
|
145
|
-
height: 1.25rem;
|
|
146
|
-
}
|
|
147
|
-
.icon-sort {
|
|
148
|
-
float: right;
|
|
149
|
-
opacity: 0.5;
|
|
150
|
-
}
|
|
151
161
|
`;static extractFieldsFromRecords(e,t=100){const s=new Set;return e.slice(0,t).forEach(e=>{Object.keys(e).forEach(e=>s.add(e))}),[...s].map(e=>({name:e,label:toTitleCase(e)}))}static format(e){return(Array.isArray(e)?Table.formatters.array:Table.formatters[typeof e])(e)}static formatters={string:e=>e,number:e=>`${e}`,date:e=>e.toLocaleDateString(),boolean:e=>e?"True":"False",array:e=>e.map(e=>Table.format(e)).join(", "),undefined:e=>"",null:e=>"<code>null</code>"};static editors={string:e=>{const t=document.createElement("input");return t.value=e,t},number:e=>{const t=document.createElement("input");return t.type="number",t.value=e,t},date:e=>{const t=document.createElement("input");return t.type="date",t.value=e,t},boolean:e=>{const t=document.createElement("select");return t.innerHTML=`\n <option value="true" ${e?"selected":""}>True</option>\n <option value="false" ${e?"":"selected"}>False</option>\n `,t.value=e,t},calculated:e=>{const t=document.createElement("input");return t.disabled=!0,t.value=e,t}}}window.customElements.define("k-table",Table);
|
package/package.json
CHANGED
package/src/components/Table.js
CHANGED
|
@@ -80,86 +80,72 @@ export default class Table extends ShadowComponent {
|
|
|
80
80
|
|
|
81
81
|
updated(changedProperties) {
|
|
82
82
|
super.updated(changedProperties);
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
if (this.enableSelection) {
|
|
83
|
+
|
|
84
|
+
if(this.enableSelection) {
|
|
86
85
|
const selectAllCheckbox = this.shadowRoot.getElementById('select-all');
|
|
87
|
-
if
|
|
86
|
+
if(selectAllCheckbox) {
|
|
88
87
|
selectAllCheckbox.checked = this.allOnPageSelected();
|
|
89
88
|
}
|
|
90
89
|
}
|
|
91
|
-
|
|
92
|
-
// Update container widths
|
|
93
|
-
this.updateContainerWidths();
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
updateContainerWidths() {
|
|
97
|
-
const totalWidth = this.columnSizes.total + 'px';
|
|
98
|
-
const fieldsEl = this.shadowRoot.getElementById('fields');
|
|
99
|
-
const topEl = this.shadowRoot.getElementById('top');
|
|
100
|
-
const bottomEl = this.shadowRoot.getElementById('bottom');
|
|
101
|
-
const recordsEl = this.shadowRoot.getElementById('records');
|
|
102
|
-
|
|
103
|
-
if (fieldsEl) fieldsEl.style.width = totalWidth;
|
|
104
|
-
if (topEl) topEl.style.width = totalWidth;
|
|
105
|
-
if (bottomEl) bottomEl.style.width = totalWidth;
|
|
106
|
-
if (recordsEl) recordsEl.style.width = totalWidth;
|
|
107
90
|
}
|
|
108
91
|
|
|
109
92
|
/*
|
|
110
93
|
Rendering Functions
|
|
111
94
|
*/
|
|
112
95
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
this.
|
|
116
|
-
this.
|
|
96
|
+
renderColgroupTemplate() {
|
|
97
|
+
const cols = [];
|
|
98
|
+
if(this.enableSelection) cols.push(html`<col style="width: 40px" />`);
|
|
99
|
+
if(this.hasBeforeControls()) cols.push(html`<col style="width: ${this.columnSizes.beforeControls}px" />`);
|
|
100
|
+
this.fields.forEach(({ size, hidden }) => {
|
|
101
|
+
if(hidden) return;
|
|
102
|
+
cols.push(size ? html`<col style="width: ${size}px" />` : html`<col />`);
|
|
103
|
+
});
|
|
104
|
+
if(this.hasAfterControls()) cols.push(html`<col style="width: ${this.columnSizes.afterControls}px" />`);
|
|
105
|
+
return cols;
|
|
106
|
+
}
|
|
117
107
|
|
|
118
|
-
|
|
108
|
+
getColumnCount() {
|
|
109
|
+
let count = 0;
|
|
110
|
+
if(this.enableSelection) count++;
|
|
111
|
+
if(this.hasBeforeControls()) count++;
|
|
112
|
+
this.fields.forEach(({ hidden }) => { if(!hidden) count++; });
|
|
113
|
+
if(this.hasAfterControls()) count++;
|
|
114
|
+
return count;
|
|
115
|
+
}
|
|
119
116
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
/>
|
|
128
|
-
</div>
|
|
117
|
+
renderFieldsTemplate() {
|
|
118
|
+
const headers = [];
|
|
119
|
+
if(this.enableSelection) {
|
|
120
|
+
headers.push(html`
|
|
121
|
+
<th class="controls field-select">
|
|
122
|
+
<input type="checkbox" id="select-all" @change=${this.handleSelectAllChange} />
|
|
123
|
+
</th>
|
|
129
124
|
`);
|
|
130
125
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
fieldCells.push(html`
|
|
134
|
-
<div class="field cell field-before-controls" style="width: ${this.columnSizes.beforeControls}px"></div>
|
|
135
|
-
`);
|
|
126
|
+
if(this.hasBeforeControls()) {
|
|
127
|
+
headers.push(html`<th class="controls field-before-controls"></th>`);
|
|
136
128
|
}
|
|
137
|
-
|
|
138
129
|
this.fields.forEach(({ name, label, hidden }) => {
|
|
139
|
-
if
|
|
130
|
+
if(hidden) return;
|
|
140
131
|
const sortItem = this.sort.find(item => item.name === name);
|
|
141
132
|
const isCurrentSort = this.sort.length > 0 && this.sort[this.sort.length - 1].name === name;
|
|
142
133
|
const sortClass = sortItem ? (sortItem.asc ? 'sort-asc' : 'sort-desc') : '';
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
style="width: ${this.columnSizes[name]}px; ${this.enableSorting ? 'cursor: pointer;' : ''}"
|
|
134
|
+
headers.push(html`
|
|
135
|
+
<th
|
|
136
|
+
class="${sortClass}"
|
|
137
|
+
style="${this.enableSorting ? 'cursor: pointer;' : ''}"
|
|
148
138
|
@click=${this.enableSorting ? () => this.handleFieldClick(name) : null}
|
|
149
139
|
>
|
|
150
140
|
${label}
|
|
151
141
|
${isCurrentSort ? html`<k-icon name="arrow" direction="${sortItem.asc ? 'down' : 'up'}" class="icon-sort"></k-icon>` : ''}
|
|
152
|
-
</
|
|
142
|
+
</th>
|
|
153
143
|
`);
|
|
154
144
|
});
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
fieldCells.push(html`
|
|
158
|
-
<div class="field cell field-after-controls" style="width: ${this.columnSizes.afterControls}px"></div>
|
|
159
|
-
`);
|
|
145
|
+
if(this.hasAfterControls()) {
|
|
146
|
+
headers.push(html`<th class="controls field-after-controls"></th>`);
|
|
160
147
|
}
|
|
161
|
-
|
|
162
|
-
return fieldCells;
|
|
148
|
+
return headers;
|
|
163
149
|
}
|
|
164
150
|
|
|
165
151
|
renderRecordsTemplate() {
|
|
@@ -182,7 +168,7 @@ export default class Table extends ShadowComponent {
|
|
|
182
168
|
} else {
|
|
183
169
|
if (fetchStart === null) fetchStart = start + idx;
|
|
184
170
|
fetchCount++;
|
|
185
|
-
return html`<
|
|
171
|
+
return html`<tr class="record fetching"><td class="cell" colspan="${this.getColumnCount()}">Loading...</td></tr>`;
|
|
186
172
|
}
|
|
187
173
|
});
|
|
188
174
|
|
|
@@ -202,42 +188,41 @@ export default class Table extends ShadowComponent {
|
|
|
202
188
|
|
|
203
189
|
renderRecordTemplate(record) {
|
|
204
190
|
const recordCells = [];
|
|
205
|
-
|
|
206
|
-
if
|
|
191
|
+
|
|
192
|
+
if(this.enableSelection) {
|
|
207
193
|
recordCells.push(html`
|
|
208
|
-
<
|
|
209
|
-
<input
|
|
210
|
-
type="checkbox"
|
|
194
|
+
<td class="cell selection controls">
|
|
195
|
+
<input
|
|
196
|
+
type="checkbox"
|
|
211
197
|
.checked=${record[selected]}
|
|
212
198
|
@change=${(e) => this.handleRecordSelectionChange(record, e)}
|
|
213
199
|
/>
|
|
214
|
-
</
|
|
200
|
+
</td>
|
|
215
201
|
`);
|
|
216
202
|
}
|
|
217
|
-
|
|
218
|
-
if
|
|
203
|
+
|
|
204
|
+
if(this.hasBeforeControls()) {
|
|
219
205
|
recordCells.push(this.renderBeforeControlsTemplate());
|
|
220
206
|
}
|
|
221
|
-
|
|
207
|
+
|
|
222
208
|
this.fields.forEach(({ name, formatter, calculator, type, editor, hidden }) => {
|
|
223
|
-
if
|
|
209
|
+
if(hidden) return;
|
|
224
210
|
let value = record[name] || '';
|
|
225
|
-
|
|
226
211
|
recordCells.push(html`
|
|
227
|
-
<
|
|
212
|
+
<td class="cell" data-field=${name}>
|
|
228
213
|
${record[editing] ? this.renderEditingCell(record, name, value, calculator, editor, type) : this.renderDisplayCell(record, name, value, calculator, formatter)}
|
|
229
|
-
</
|
|
214
|
+
</td>
|
|
230
215
|
`);
|
|
231
216
|
});
|
|
232
|
-
|
|
233
|
-
if
|
|
217
|
+
|
|
218
|
+
if(this.hasAfterControls()) {
|
|
234
219
|
recordCells.push(this.renderAfterControlsTemplate());
|
|
235
220
|
}
|
|
236
|
-
|
|
221
|
+
|
|
237
222
|
return html`
|
|
238
|
-
<
|
|
223
|
+
<tr class="record ${record[editing] ? 'editing' : ''}" data-index=${record[index]}>
|
|
239
224
|
${recordCells}
|
|
240
|
-
</
|
|
225
|
+
</tr>
|
|
241
226
|
`;
|
|
242
227
|
}
|
|
243
228
|
|
|
@@ -280,55 +265,37 @@ export default class Table extends ShadowComponent {
|
|
|
280
265
|
|
|
281
266
|
renderBeforeControlsTemplate() {
|
|
282
267
|
const controls = [];
|
|
283
|
-
|
|
284
268
|
this.querySelectorAll('[slot="before"]').forEach(control => {
|
|
285
269
|
const tagName = control.tagName.toLowerCase();
|
|
286
270
|
const newControl = document.createElement(tagName);
|
|
287
|
-
|
|
288
271
|
Array.from(control.attributes).forEach(attr => {
|
|
289
|
-
if(attr.name !== 'slot')
|
|
290
|
-
newControl.setAttribute(attr.name, attr.value);
|
|
291
|
-
}
|
|
272
|
+
if(attr.name !== 'slot') newControl.setAttribute(attr.name, attr.value);
|
|
292
273
|
});
|
|
293
|
-
|
|
294
|
-
if(control.innerHTML){
|
|
295
|
-
newControl.innerHTML = control.innerHTML;
|
|
296
|
-
}
|
|
297
|
-
|
|
274
|
+
if(control.innerHTML) newControl.innerHTML = control.innerHTML;
|
|
298
275
|
controls.push(newControl);
|
|
299
276
|
});
|
|
300
|
-
|
|
301
277
|
return html`
|
|
302
|
-
<
|
|
278
|
+
<td class="cell controls controls-before">
|
|
303
279
|
${controls}
|
|
304
|
-
</
|
|
280
|
+
</td>
|
|
305
281
|
`;
|
|
306
282
|
}
|
|
307
283
|
|
|
308
284
|
renderAfterControlsTemplate() {
|
|
309
285
|
const controls = [];
|
|
310
|
-
|
|
311
286
|
this.querySelectorAll('[slot="after"]').forEach(control => {
|
|
312
287
|
const tagName = control.tagName.toLowerCase();
|
|
313
288
|
const newControl = document.createElement(tagName);
|
|
314
|
-
|
|
315
289
|
Array.from(control.attributes).forEach(attr => {
|
|
316
|
-
if(attr.name !== 'slot')
|
|
317
|
-
newControl.setAttribute(attr.name, attr.value);
|
|
318
|
-
}
|
|
290
|
+
if(attr.name !== 'slot') newControl.setAttribute(attr.name, attr.value);
|
|
319
291
|
});
|
|
320
|
-
|
|
321
|
-
if(control.innerHTML){
|
|
322
|
-
newControl.innerHTML = control.innerHTML;
|
|
323
|
-
}
|
|
324
|
-
|
|
292
|
+
if(control.innerHTML) newControl.innerHTML = control.innerHTML;
|
|
325
293
|
controls.push(newControl);
|
|
326
294
|
});
|
|
327
|
-
|
|
328
295
|
return html`
|
|
329
|
-
<
|
|
296
|
+
<td class="cell controls controls-after">
|
|
330
297
|
${controls}
|
|
331
|
-
</
|
|
298
|
+
</td>
|
|
332
299
|
`;
|
|
333
300
|
}
|
|
334
301
|
|
|
@@ -949,54 +916,19 @@ export default class Table extends ShadowComponent {
|
|
|
949
916
|
}
|
|
950
917
|
|
|
951
918
|
calculateColumnSizes() {
|
|
952
|
-
const newSizes = {};
|
|
953
|
-
newSizes.total = 0;
|
|
954
|
-
|
|
955
|
-
if (this.enableSelection) newSizes.total += 40;
|
|
956
|
-
|
|
957
919
|
const beforeEls = Array.from(this.querySelectorAll('[slot="before"]'));
|
|
958
920
|
const afterEls = Array.from(this.querySelectorAll('[slot="after"]'));
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
if (this.hasBeforeControls()) newSizes.total += newSizes.beforeControls;
|
|
964
|
-
if (this.hasAfterControls()) newSizes.total += newSizes.afterControls;
|
|
965
|
-
|
|
966
|
-
this.fields.forEach(field => {
|
|
967
|
-
if (field.size) {
|
|
968
|
-
newSizes[field.name] = field.size;
|
|
969
|
-
newSizes.total += field.size;
|
|
970
|
-
} else {
|
|
971
|
-
let maxLength = 0;
|
|
972
|
-
this.records.slice(0, 100).forEach(record => {
|
|
973
|
-
if(record === null) return;
|
|
974
|
-
let value = record[field.name];
|
|
975
|
-
if (field.calculator) {
|
|
976
|
-
value = field.calculator(record, this);
|
|
977
|
-
}
|
|
978
|
-
if (field.formatter) {
|
|
979
|
-
value = field.formatter(value);
|
|
980
|
-
}
|
|
981
|
-
if (value && value.toString().length > maxLength) {
|
|
982
|
-
maxLength = value.toString().length;
|
|
983
|
-
}
|
|
984
|
-
});
|
|
985
|
-
newSizes[field.name] = Math.max((maxLength * 10 + 32), 128);
|
|
986
|
-
if (!field.hidden) newSizes.total += newSizes[field.name];
|
|
987
|
-
}
|
|
988
|
-
});
|
|
989
|
-
|
|
921
|
+
const newSizes = {
|
|
922
|
+
beforeControls: beforeEls.reduce((total, el) => total + (el.maxWidth || 40), 0),
|
|
923
|
+
afterControls: afterEls.reduce((total, el) => total + (el.maxWidth || 40), 0)
|
|
924
|
+
};
|
|
990
925
|
const hasUndefinedMaxWidth = [...beforeEls, ...afterEls].some(el => el.maxWidth === undefined);
|
|
991
|
-
|
|
992
|
-
if (JSON.stringify(this.columnSizes) !== JSON.stringify(newSizes)) {
|
|
926
|
+
if(JSON.stringify(this.columnSizes) !== JSON.stringify(newSizes)) {
|
|
993
927
|
this.columnSizes = newSizes;
|
|
994
928
|
}
|
|
995
|
-
|
|
996
|
-
if (hasUndefinedMaxWidth) {
|
|
929
|
+
if(hasUndefinedMaxWidth) {
|
|
997
930
|
setTimeout(() => this.calculateColumnSizes(), 0);
|
|
998
931
|
}
|
|
999
|
-
|
|
1000
932
|
return this.columnSizes;
|
|
1001
933
|
}
|
|
1002
934
|
|
|
@@ -1040,13 +972,12 @@ export default class Table extends ShadowComponent {
|
|
|
1040
972
|
/* Rendering */
|
|
1041
973
|
|
|
1042
974
|
render() {
|
|
1043
|
-
if
|
|
975
|
+
if(!this.records || !this.fields) {
|
|
1044
976
|
return html`
|
|
1045
977
|
<div id="wrapper">
|
|
1046
978
|
<div id="top"><slot name="top"></slot></div>
|
|
1047
|
-
<div id="table">
|
|
1048
|
-
<
|
|
1049
|
-
<div id="records"></div>
|
|
979
|
+
<div id="table-container">
|
|
980
|
+
<table><thead><tr></tr></thead><tbody></tbody></table>
|
|
1050
981
|
</div>
|
|
1051
982
|
<div id="bottom"><slot></slot></div>
|
|
1052
983
|
</div>
|
|
@@ -1063,16 +994,23 @@ export default class Table extends ShadowComponent {
|
|
|
1063
994
|
|
|
1064
995
|
return html`
|
|
1065
996
|
<div id="wrapper">
|
|
1066
|
-
<div id="top"
|
|
1067
|
-
<div id="table">
|
|
1068
|
-
<
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
997
|
+
<div id="top"><slot name="top"></slot></div>
|
|
998
|
+
<div id="table-container">
|
|
999
|
+
<table>
|
|
1000
|
+
<colgroup>
|
|
1001
|
+
${this.renderColgroupTemplate()}
|
|
1002
|
+
</colgroup>
|
|
1003
|
+
<thead>
|
|
1004
|
+
<tr>
|
|
1005
|
+
${this.renderFieldsTemplate()}
|
|
1006
|
+
</tr>
|
|
1007
|
+
</thead>
|
|
1008
|
+
<tbody>
|
|
1009
|
+
${this.renderRecordsTemplate()}
|
|
1010
|
+
</tbody>
|
|
1011
|
+
</table>
|
|
1074
1012
|
</div>
|
|
1075
|
-
<div id="bottom"
|
|
1013
|
+
<div id="bottom"><slot></slot></div>
|
|
1076
1014
|
</div>
|
|
1077
1015
|
<div style="display: none">
|
|
1078
1016
|
<slot name="before"></slot>
|
|
@@ -1084,40 +1022,67 @@ export default class Table extends ShadowComponent {
|
|
|
1084
1022
|
static styles = css`
|
|
1085
1023
|
:host {
|
|
1086
1024
|
display: block;
|
|
1087
|
-
width: 100%;
|
|
1088
|
-
overflow: auto;
|
|
1089
1025
|
margin-bottom: var(--spacer);
|
|
1090
1026
|
}
|
|
1091
1027
|
#wrapper {
|
|
1092
|
-
width: min-content;
|
|
1093
1028
|
border: 1px solid var(--c_border);
|
|
1094
1029
|
border-radius: var(--radius);
|
|
1030
|
+
overflow: hidden;
|
|
1095
1031
|
}
|
|
1096
|
-
#table {
|
|
1097
|
-
|
|
1032
|
+
#table-container {
|
|
1033
|
+
overflow-x: auto;
|
|
1098
1034
|
}
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1035
|
+
table {
|
|
1036
|
+
width: 100%;
|
|
1037
|
+
border-collapse: collapse;
|
|
1102
1038
|
}
|
|
1103
|
-
|
|
1039
|
+
thead tr {
|
|
1104
1040
|
background-color: var(--c_bg__alt);
|
|
1105
|
-
border-bottom: 1px solid var(--c_border);
|
|
1106
1041
|
}
|
|
1107
|
-
|
|
1108
|
-
#fields .cell:not(.controls) {
|
|
1042
|
+
th, td {
|
|
1109
1043
|
padding: calc(0.5 * var(--spacer)) var(--spacer);
|
|
1044
|
+
vertical-align: middle;
|
|
1110
1045
|
}
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1046
|
+
th:not(:last-child),
|
|
1047
|
+
td:not(:last-child) {
|
|
1048
|
+
border-right: 1px solid var(--c_border);
|
|
1049
|
+
}
|
|
1050
|
+
th:first-child,
|
|
1051
|
+
td:first-child {
|
|
1052
|
+
border-left: none;
|
|
1114
1053
|
}
|
|
1115
|
-
|
|
1116
|
-
|
|
1054
|
+
th:last-child,
|
|
1055
|
+
td:last-child {
|
|
1056
|
+
border-right: none;
|
|
1117
1057
|
}
|
|
1118
|
-
|
|
1058
|
+
thead tr th {
|
|
1059
|
+
border-top: none;
|
|
1119
1060
|
border-bottom: 1px solid var(--c_border);
|
|
1120
1061
|
}
|
|
1062
|
+
tbody tr:not(:last-child) td {
|
|
1063
|
+
border-bottom: 1px solid var(--c_border);
|
|
1064
|
+
}
|
|
1065
|
+
tbody tr:last-child td {
|
|
1066
|
+
border-bottom: none;
|
|
1067
|
+
}
|
|
1068
|
+
th.controls,
|
|
1069
|
+
td.controls {
|
|
1070
|
+
padding: 0;
|
|
1071
|
+
}
|
|
1072
|
+
.field-select,
|
|
1073
|
+
.selection {
|
|
1074
|
+
width: 40px;
|
|
1075
|
+
text-align: center;
|
|
1076
|
+
}
|
|
1077
|
+
.field-select input,
|
|
1078
|
+
.selection input {
|
|
1079
|
+
width: 1.25rem;
|
|
1080
|
+
height: 1.25rem;
|
|
1081
|
+
}
|
|
1082
|
+
.icon-sort {
|
|
1083
|
+
float: right;
|
|
1084
|
+
opacity: 0.5;
|
|
1085
|
+
}
|
|
1121
1086
|
#top, #bottom {
|
|
1122
1087
|
display: flex;
|
|
1123
1088
|
width: 100%;
|
|
@@ -1136,21 +1101,6 @@ export default class Table extends ShadowComponent {
|
|
|
1136
1101
|
:host(:not([bottom-controls])) #bottom {
|
|
1137
1102
|
display: none;
|
|
1138
1103
|
}
|
|
1139
|
-
.field-select,
|
|
1140
|
-
.selection {
|
|
1141
|
-
display: flex;
|
|
1142
|
-
justify-content: center;
|
|
1143
|
-
align-items: center;
|
|
1144
|
-
}
|
|
1145
|
-
.field-select input,
|
|
1146
|
-
.selection input {
|
|
1147
|
-
width: 1.25rem;
|
|
1148
|
-
height: 1.25rem;
|
|
1149
|
-
}
|
|
1150
|
-
.icon-sort {
|
|
1151
|
-
float: right;
|
|
1152
|
-
opacity: 0.5;
|
|
1153
|
-
}
|
|
1154
1104
|
`;
|
|
1155
1105
|
|
|
1156
1106
|
/*
|