dees-catalog/ts_web/elements/dees-table.ts

571 lines
17 KiB
TypeScript
Raw Normal View History

2021-10-07 16:01:05 +00:00
import {
customElement,
html,
DeesElement,
property,
2023-08-07 18:02:18 +00:00
type TemplateResult,
2021-10-07 16:01:05 +00:00
cssManager,
css,
unsafeCSS,
2023-08-07 18:02:18 +00:00
type CSSResult,
2022-12-07 01:28:31 +00:00
state,
2023-09-04 17:28:50 +00:00
resolveExec,
2023-08-07 17:13:29 +00:00
} from '@design.estate/dees-element';
2021-10-07 16:01:05 +00:00
2023-09-04 17:28:50 +00:00
import { DeesContextmenu } from './dees-contextmenu.js'
2023-08-07 17:13:29 +00:00
import * as domtools from '@design.estate/dees-domtools';
2023-09-04 17:29:39 +00:00
import { type TIconKey } from './dees-icon.js';
2021-10-07 16:01:05 +00:00
declare global {
interface HTMLElementTagNameMap {
'dees-table': DeesTable<any>;
}
}
2023-09-04 17:28:50 +00:00
// interfaces
2022-12-06 12:11:06 +00:00
export interface IDataAction<T = any> {
name: string;
2023-09-04 17:28:50 +00:00
iconName: TIconKey;
/**
* the table behaviour to use for this action
* e.g. upload: allows to upload files to the table
*/
2022-12-06 12:11:06 +00:00
useTableBehaviour?: 'upload' | 'cancelUpload' | 'none';
2023-09-04 17:28:50 +00:00
/**
* the type of the action
*/
type: 'inRow' | 'rightClick' | 'footer' | 'header' | 'preview' | 'keyCombination';
/**
* allows to check if the action is relevant for the given item
* @param itemArg
* @returns
*/
actionRelevancyCheckFunc?: (itemArg: T) => boolean;
/**
* the actual action function implementation
* @param itemArg
* @returns
*/
2022-12-06 12:11:06 +00:00
actionFunc: (itemArg: T) => Promise<any>;
}
2023-09-04 17:28:50 +00:00
export type TDisplayFunction<T = any> = (itemArg: T) => object;
// the table implementation
2021-10-07 16:01:05 +00:00
@customElement('dees-table')
export class DeesTable<T> extends DeesElement {
public static demo = () => html`
<style>
.demoWrapper {
box-sizing: border-box;
position: absolute;
width: 100%;
height: 100%;
padding: 60px;
background: #000000;
}
</style>
<div class="demoWrapper">
<dees-table
heading1="Current Account Statement"
heading2="Bunq - Payment Account 2 - April 2021"
.data=${[
{
date: '2021-04-01',
amount: '2464.65 €',
description: 'Printing Paper (Office Supplies) - STAPLES BREMEN',
},
{
date: '2021-04-02',
amount: '165.65 €',
description: 'Logitech Mouse (Hardware) - logi.com OnlineShop',
},
{
date: '2021-04-03',
amount: '2999,00 €',
description: 'Macbook Pro 16inch (Hardware) - Apple.de OnlineShop',
},
{
date: '2021-04-01',
amount: '2464.65 €',
description: 'Office-Supplies - STAPLES BREMEN',
},
{
date: '2021-04-01',
amount: '2464.65 €',
description: 'Office-Supplies - STAPLES BREMEN',
},
]}
2023-09-04 17:28:50 +00:00
dataName="transactions"
.dataActions="${[
{
name: 'upload',
iconName: 'bell',
useTableBehaviour: 'upload',
type: 'inRow',
actionFunc: async (itemArg: any) => {
alert(itemArg.amount);
},
2022-12-07 01:28:31 +00:00
},
2023-09-04 17:28:50 +00:00
{
name: 'visibility',
iconName: 'copy',
type: 'inRow',
useTableBehaviour: 'preview',
actionFunc: async (itemArg: any) => {},
},
{
name: 'create new',
iconName: 'instagram',
type: 'header',
useTableBehaviour: 'preview',
actionFunc: async (itemArg: any) => {},
2022-12-06 12:11:06 +00:00
},
2023-09-04 17:28:50 +00:00
{
name: 'to gallery',
iconName: 'message',
type: 'footer',
useTableBehaviour: 'preview',
actionFunc: async (itemArg: any) => {},
},
] as IDataAction[]}"
.displayFunction=${(itemArg) => {
return {
...itemArg,
onlyDisplayProp: 'onlyDisplay',
};
}}
>This is a slotted Text</dees-table
>
2021-10-07 16:01:05 +00:00
</div>
`;
2022-12-07 01:28:31 +00:00
// INSTANCE
2021-11-26 19:06:09 +00:00
@property({
type: String,
})
public heading1: string = 'heading 1';
2021-10-07 16:01:05 +00:00
2021-11-26 19:06:09 +00:00
@property({
type: String,
})
public heading2: string = 'heading 2';
2021-10-07 16:01:05 +00:00
@property({
2021-10-07 16:47:36 +00:00
type: Array,
2021-10-07 16:01:05 +00:00
})
public data: T[] = [];
2022-12-06 12:11:06 +00:00
@property({
2023-09-04 17:28:50 +00:00
type: String,
reflect: true,
2022-12-06 12:11:06 +00:00
})
2023-09-04 17:28:50 +00:00
public dataName: string;
2022-12-06 12:11:06 +00:00
2021-11-26 19:06:09 +00:00
@property({
2023-09-04 17:28:50 +00:00
type: Array,
2021-11-26 19:06:09 +00:00
})
2023-09-04 17:28:50 +00:00
public dataActions: IDataAction[] = [];
2021-10-07 16:01:05 +00:00
@property({
2023-09-04 17:28:50 +00:00
attribute: false,
2021-10-07 16:01:05 +00:00
})
2023-09-04 17:28:50 +00:00
public displayFunction: TDisplayFunction = (itemArg: T) => itemArg as any;
2021-10-07 16:01:05 +00:00
@property({
2023-09-04 17:28:50 +00:00
type: Object,
2021-10-07 16:01:05 +00:00
})
2023-09-04 17:28:50 +00:00
public selectedDataRow: T;
2021-10-07 16:01:05 +00:00
2022-12-07 01:28:31 +00:00
public files: File[] = [];
public fileWeakMap = new WeakMap();
2021-10-07 16:01:05 +00:00
constructor() {
super();
}
public static styles = [
cssManager.defaultStyles,
css`
.mainbox {
2022-05-26 16:07:02 +00:00
color: ${cssManager.bdTheme('#333', '#fff')};
2023-08-07 17:13:29 +00:00
font-family: 'Mona Sans', 'Inter', sans-serif;
2023-01-07 15:47:01 +00:00
font-weight: 400;
2023-09-07 00:57:30 +00:00
font-size: 14px;
2022-06-10 13:50:56 +00:00
padding: 16px;
2021-10-07 16:01:05 +00:00
display: block;
width: 100%;
min-height: 50px;
2023-01-13 10:48:00 +00:00
background: ${cssManager.bdTheme('#ffffff', '#333333')};
2021-10-07 16:01:05 +00:00
border-radius: 3px;
2022-06-26 13:04:04 +00:00
border-top: 1px solid ${cssManager.bdTheme('#fff', '#444')};
2023-01-13 10:48:00 +00:00
box-shadow: 0px 1px 4px rgba(0, 0, 0, 0.3);
2023-03-25 16:32:55 +00:00
overflow-x: auto;
2021-10-07 16:01:05 +00:00
}
2023-09-04 17:28:50 +00:00
.header {
display: flex;
justify-content: flex-end;
align-items: center;
}
.headingContainer {
}
.heading {
font-family: 'Hubot Sans', 'Inter', sans-serif;
}
.heading1 {
font-weight: 600;
}
.heading2 {
opacity: 0.6;
}
2021-10-07 16:01:05 +00:00
.headingSeparation {
margin-top: 7px;
2022-05-26 16:07:02 +00:00
border-bottom: 1px solid ${cssManager.bdTheme('#bcbcbc', '#bcbcbc')};
2021-10-07 16:01:05 +00:00
}
2023-09-04 17:28:50 +00:00
.headerActions {
margin-left: auto;
cursor: pointer;
}
.headerAction {
display: flex;
color: ${cssManager.bdTheme('#333', '#ccc')};
}
.headerAction:hover {
color: ${cssManager.bdTheme('#555', '#fff')};
}
.headerAction dees-icon {
margin-right: 8px;
}
2021-10-07 16:01:05 +00:00
table,
.noDataSet {
2022-06-10 13:50:56 +00:00
margin-top: 16px;
2022-05-26 16:07:02 +00:00
color: ${cssManager.bdTheme('#333', '#fff')};
2021-10-07 16:01:05 +00:00
border-collapse: collapse;
width: 100%;
}
.noDataSet {
text-align: center;
}
tr {
2022-05-26 16:07:02 +00:00
border-bottom: 1px dashed ${cssManager.bdTheme('#999', '#808080')};
2021-10-07 16:01:05 +00:00
text-align: left;
}
tr:last-child {
border-bottom: none;
text-align: left;
}
tr:hover {
cursor: pointer;
}
tr:hover .innerCellContainer {
2022-05-26 16:07:02 +00:00
background: ${cssManager.bdTheme('#22222210', '#ffffff20')};
2021-10-07 16:01:05 +00:00
}
tr:first-child:hover {
cursor: auto;
}
tr:first-child:hover .innerCellContainer {
background: none;
}
2021-10-07 16:47:36 +00:00
tr.selected .innerCellContainer {
2023-09-04 17:28:50 +00:00
background: ${cssManager.bdTheme('#22222220', '#ffffff40')};
2021-10-07 16:47:36 +00:00
}
2021-10-07 16:01:05 +00:00
th {
text-transform: uppercase;
}
th,
td {
padding: 3px 0px;
2022-05-26 16:07:02 +00:00
border-right: 1px dashed ${cssManager.bdTheme('#999', '#808080')};
2021-10-07 16:01:05 +00:00
}
.innerCellContainer {
2022-06-10 13:50:56 +00:00
padding: 6px 8px;
2021-10-07 16:01:05 +00:00
}
th:first-child .innerCellContainer,
td:first-child .innerCellContainer {
padding-left: 0px;
}
th:last-child .innerCellContainer,
td:last-child .innerCellContainer {
padding-right: 0px;
}
th:last-child,
td:last-child {
border-right: none;
}
2022-12-06 12:11:06 +00:00
.action {
margin: -8px 0px;
padding: 8px;
2022-12-06 12:32:00 +00:00
line-height: 16px;
2022-12-07 01:28:31 +00:00
display: inline-block;
2022-12-06 12:11:06 +00:00
}
.action:first-child {
margin-left: -8px;
width: min-content;
}
.action:hover {
2022-12-06 12:32:00 +00:00
background: ${cssManager.bdTheme('#CCC', '#111')};
2022-12-06 12:11:06 +00:00
}
2023-09-04 17:28:50 +00:00
.footer {
font-size: 14px;
2022-05-26 16:07:02 +00:00
color: ${cssManager.bdTheme('#111', '#ffffff90')};
2022-05-26 18:03:18 +00:00
background: ${cssManager.bdTheme('#eeeeeb', '#00000050')};
2022-06-10 13:50:56 +00:00
margin: 16px -16px -16px -16px;
2022-05-26 19:23:55 +00:00
border-bottom-left-radius: 3px;
border-bottom-right-radius: 3px;
2023-09-04 17:28:50 +00:00
display: flex;
}
.tableStatistics {
padding: 8px 16px;
}
.footerActions {
margin-left: auto;
}
.footerActions .footerAction {
cursor: pointer;
padding: 8px 16px;
display: flex;
}
.footerActions .footerAction:hover {
background: ${cssManager.bdTheme('#CCC', '#111')};
}
.footerActions dees-icon {
display: flex;
margin-right: 8px;
2021-10-07 16:01:05 +00:00
}
`,
];
public render(): TemplateResult {
return html`
<div class="mainbox">
<!-- the heading part -->
2023-09-04 17:28:50 +00:00
<div class="header">
<div class="headingContainer">
<div class="heading heading1">${this.heading1}</div>
<div class="heading heading2">${this.heading2}</div>
</div>
<div class="headerActions">
${resolveExec(async () => {
const resultArray: TemplateResult[] = [];
for (const action of this.dataActions) {
if (action.type !== 'header') continue;
resultArray.push(
html`<div
class="headerAction"
@click=${() => {
action.actionFunc(this.selectedDataRow);
}}
>
${action.iconName
? html`<dees-icon .iconSize=${14} .iconFA=${action.iconName}></dees-icon>
${action.name}`
: action.name}
</div>`
);
}
return resultArray;
})}
</div>
</div>
2021-10-07 16:01:05 +00:00
<div class="headingSeparation"></div>
<!-- the actual table -->
<style></style>
${this.data.length > 0
? (() => {
2023-09-04 17:28:50 +00:00
// Only pick up the keys from the first transformed data object
// as all data objects are assumed to have the same structure
const firstTransformedItem = this.displayFunction(this.data[0]);
const headings: string[] = Object.keys(firstTransformedItem);
2021-10-07 16:01:05 +00:00
return html`
<table>
<tr>
2021-10-07 16:47:36 +00:00
${headings.map(
(headingArg) => html`
<th>
<div class="innerCellContainer">${headingArg}</div>
</th>
`
)}
2022-12-06 12:11:06 +00:00
${(() => {
2022-12-11 16:24:12 +00:00
if (this.dataActions && this.dataActions.length > 0) {
2022-12-06 12:11:06 +00:00
return html`
<th>
<div class="innerCellContainer">Actions</div>
</th>
`;
}
})()}
2021-10-07 16:01:05 +00:00
</tr>
2023-09-04 17:28:50 +00:00
${this.data.map((itemArg) => {
const transformedItem = this.displayFunction(itemArg);
const getTr = (elementArg: HTMLElement): HTMLElement => {
if (elementArg.tagName === 'TR') {
return elementArg;
} else {
return getTr(elementArg.parentElement);
}
};
return html`
2021-10-07 16:47:36 +00:00
<tr
@click=${() => {
this.selectedDataRow = itemArg;
}}
2022-12-07 01:28:31 +00:00
@dragenter=${async (eventArg: DragEvent) => {
2022-12-06 12:11:06 +00:00
eventArg.preventDefault();
eventArg.stopPropagation();
2023-09-04 17:28:50 +00:00
const realTarget = getTr(eventArg.target as HTMLElement);
console.log('dragenter');
console.log(realTarget);
setTimeout(() => {
realTarget.style.background = 'green';
}, 0);
2022-12-06 12:11:06 +00:00
}}
2022-12-07 01:28:31 +00:00
@dragleave=${async (eventArg: DragEvent) => {
2022-12-06 12:11:06 +00:00
eventArg.preventDefault();
eventArg.stopPropagation();
2023-09-04 17:28:50 +00:00
const realTarget = getTr(eventArg.target as HTMLElement);
realTarget.style.background = 'none';
2022-12-06 12:11:06 +00:00
}}
2022-12-07 01:28:31 +00:00
@dragover=${async (eventArg: DragEvent) => {
2022-12-06 12:11:06 +00:00
eventArg.preventDefault();
2022-12-07 01:28:31 +00:00
}}
@drop=${async (eventArg: DragEvent) => {
eventArg.preventDefault();
2023-09-04 17:28:50 +00:00
const newFiles = [];
2022-12-07 01:28:31 +00:00
for (const file of Array.from(eventArg.dataTransfer.files)) {
this.files.push(file);
newFiles.push(file);
this.requestUpdate();
}
const result: File[] = this.fileWeakMap.get(itemArg as object);
if (!result) {
2023-09-04 17:28:50 +00:00
this.fileWeakMap.set(itemArg as object, newFiles);
2022-12-07 01:28:31 +00:00
} else {
result.push(...newFiles);
}
2022-12-06 12:11:06 +00:00
}}
2023-09-04 17:28:50 +00:00
@contextmenu=${async (eventArg: MouseEvent) => {
DeesContextmenu.openContextMenuWithOptions(eventArg, [
{
name: 'copy',
iconName: 'copySolid',
action: async () => {
return null;
},
},
{
name: 'edit',
iconName: 'penToSquare',
action: async () => {
return null;
},
},{
name: 'paste',
iconName: 'pasteSolid',
action: async () => {
return null;
},
},
])
}}
2021-10-07 16:47:36 +00:00
class="${itemArg === this.selectedDataRow ? 'selected' : ''}"
>
${headings.map(
(headingArg) => html`
<td>
2023-09-04 17:28:50 +00:00
<div class="innerCellContainer">${transformedItem[headingArg]}</div>
2021-10-07 16:47:36 +00:00
</td>
`
)}
2022-12-06 12:11:06 +00:00
${(() => {
2022-12-11 16:24:12 +00:00
if (this.dataActions && this.dataActions.length > 0) {
2022-12-06 12:11:06 +00:00
return html`
<td>
<div class="innerCellContainer">
${(() => {
const actions: TemplateResult[] = [];
for (const action of this.dataActions) {
2023-09-04 17:28:50 +00:00
if (action.type !== 'inRow') continue;
actions.push(
html`<div
class="action"
@click=${() => action.actionFunc(itemArg)}
>
${action.iconName
? html`
<dees-icon .iconFA=${action.iconName}></dees-icon>
`
: action.name}
</div>`
);
2022-12-06 12:11:06 +00:00
}
return actions;
})()}
</div>
</td>
`;
}
})()}
2021-10-07 16:47:36 +00:00
</tr>
2023-09-04 17:28:50 +00:00
`;
})}
2021-10-07 16:01:05 +00:00
</table>
`;
})()
: html` <div class="noDataSet">No data set!</div> `}
2023-09-04 17:28:50 +00:00
<div class="footer">
<div class="tableStatistics">
${this.data.length} ${this.dataName || 'data rows'} (total) |
${this.selectedDataRow ? '# ' + `${this.data.indexOf(this.selectedDataRow) + 1}` : `No`}
selected
</div>
<div class="footerActions">
${resolveExec(async () => {
const resultArray: TemplateResult[] = [];
for (const action of this.dataActions) {
if (action.type !== 'footer') continue;
resultArray.push(
html`<div
class="footerAction"
@click=${() => {
action.actionFunc(this.selectedDataRow);
}}
>
${action.iconName
? html`<dees-icon .iconSize=${14} .iconFA=${action.iconName}></dees-icon>
${action.name}`
: action.name}
</div>`
);
}
return resultArray;
})}
</div>
2021-10-07 16:01:05 +00:00
</div>
</div>
`;
}
public async firstUpdated() {}
}