|
1 | | -import { LitElement, html } from 'lit'; |
| 1 | +import { LitElement, html, css } from 'lit'; |
2 | 2 | import { Model } from '../core/Model'; |
3 | 3 | import { EventType } from '../core/EventType'; |
| 4 | +import { TableHeadElement } from './TableHeadElement'; |
4 | 5 |
|
5 | 6 | /** |
6 | 7 | * @class TableBodyElement |
7 | 8 | * |
8 | 9 | * This class provides a table body element. |
9 | 10 | * |
10 | 11 | * @example |
11 | | - * <js-table-body></js-table-body> |
| 12 | + * <js-tablebody data="#data-source-id"></js-tablebody> |
12 | 13 | */ |
13 | 14 | export class TableBodyElement extends LitElement { |
| 15 | + #data = null; |
| 16 | + #head = null; |
| 17 | + |
14 | 18 | static get localName() { |
15 | | - return 'js-table-body'; |
| 19 | + return 'js-tablebody'; |
16 | 20 | } |
17 | 21 |
|
18 | 22 | static get properties() { |
19 | 23 | return { |
20 | | - model: { |
21 | | - type: Model, reflect: true, converter: { |
22 | | - fromAttribute: (value, type) => { |
23 | | - console.log(`TableBodyElement.model.fromAttribute(${value}, ${type})`); |
24 | | - return value; |
25 | | - }, |
26 | | - toAttribute: (value, type) => { |
27 | | - console.log(value); |
28 | | - return value.data; |
29 | | - } |
30 | | - } |
31 | | - } |
| 24 | + data: { type: String, reflect: true }, |
| 25 | + columns: { type: Array, reflect: true }, |
32 | 26 | }; |
33 | 27 | } |
34 | 28 |
|
35 | | - render() { |
36 | | - console.log(`TableBodyElement.render() ${this.model ? this.model.data : '(not a model)'}`); |
37 | | - return html` |
38 | | - <tbody> |
39 | | - <tr> |
40 | | - <td>A</td> |
41 | | - <td>B</td> |
42 | | - </tr> |
43 | | - </tbody> |
| 29 | + static get styles() { |
| 30 | + return css` |
| 31 | + :host { |
| 32 | + display: block; |
| 33 | + } |
| 34 | + table { |
| 35 | + border-spacing: 0; |
| 36 | + } |
| 37 | + td, th { |
| 38 | + vertical-align: top; |
| 39 | + border-left: 1px solid #aaa; |
| 40 | + border-top: 1px solid #aaa; |
| 41 | + } |
| 42 | + td:last-child, th:last-child { |
| 43 | + border-right: 1px solid #aaa; |
| 44 | + } |
| 45 | + tr:last-child td { |
| 46 | + border-bottom: 1px solid #aaa; |
| 47 | + } |
| 48 | + th { |
| 49 | + text-transform: capitalize; |
| 50 | + } |
| 51 | + .wrap { |
| 52 | + max-height: 40px; |
| 53 | + overflow: hidden; |
| 54 | + } |
44 | 55 | `; |
45 | 56 | } |
46 | 57 |
|
47 | | - updated(changedProperties) { |
48 | | - if (changedProperties.has('model')) { |
49 | | - this.#modelChangedSetListeners(this.model, changedProperties.get('model')); |
| 58 | + attributeChangedCallback(name, oldVal, newVal) { |
| 59 | + super.attributeChangedCallback(name, oldVal, newVal); |
| 60 | + if (name === 'data') { |
| 61 | + this.#dataChanged(newVal, oldVal); |
50 | 62 | } |
51 | 63 | } |
52 | 64 |
|
53 | | - #modelChangedSetListeners(newVal, oldVal) { |
54 | | - if (oldVal) { |
55 | | - oldVal.removeEventListener(EventType.CHANGE, this.#modelChanged.bind(this)); |
| 65 | + #dataChanged(newVal, oldVal) { |
| 66 | + if (oldVal != null && this.#data && newVal !== oldVal) { |
| 67 | + this.#data.removeEventListener(EventType.CHANGE, this.#dataUpdate.bind(this)); |
| 68 | + this.#data = null; |
| 69 | + this.columns = null; |
56 | 70 | } |
57 | | - if (newVal) { |
58 | | - newVal.addEventListener(EventType.CHANGE, this.#modelChanged.bind(this)); |
| 71 | + if (newVal != null && newVal !== oldVal) { |
| 72 | + this.#data = document.querySelector(newVal); |
| 73 | + this.columns = new Array(); |
| 74 | + if (this.#data) { |
| 75 | + this.#data.addEventListener(EventType.CHANGE, this.#dataUpdate.bind(this)); |
| 76 | + } else { |
| 77 | + throw new Error(`Data Source "${newVal}" not found`); |
| 78 | + } |
59 | 79 | } |
60 | 80 | } |
61 | 81 |
|
62 | | - #modelChanged() { |
63 | | - console.log(`TableBodyElement.#modelChanged`); |
| 82 | + #dataUpdate() { |
64 | 83 | this.requestUpdate(); |
| 84 | + this.dispatchEvent(new CustomEvent(EventType.CHANGE, { |
| 85 | + detail: this |
| 86 | + })); |
| 87 | + } |
| 88 | + |
| 89 | + firstUpdated() { |
| 90 | + // Set the table header |
| 91 | + this.#head = this.querySelector(TableHeadElement.localName); |
| 92 | + } |
| 93 | + |
| 94 | + render() { |
| 95 | + const rows = this.#renderRows(); |
| 96 | + const head = this.#head ? this.#head.render() : html``; |
| 97 | + return [html`<table>${head}<tbody>${rows}</tbody></table>`]; |
| 98 | + } |
| 99 | + |
| 100 | + #renderRows() { |
| 101 | + if (!this.#data) { |
| 102 | + return html``; |
| 103 | + } |
| 104 | + let rows = []; |
| 105 | + for (let i = 0; i < this.#data.length; i++) { |
| 106 | + rows.push(html`<tr>${this.#renderColumns(this.#data.at(i))}</tr>`); |
| 107 | + } |
| 108 | + return rows; |
| 109 | + } |
| 110 | + |
| 111 | + #renderColumns(row) { |
| 112 | + if (!row) { |
| 113 | + return html``; |
| 114 | + } |
| 115 | + if (row instanceof Object) { |
| 116 | + let columns = []; |
| 117 | + for (let key in row) { |
| 118 | + if (this.columns.indexOf(key) === -1) { |
| 119 | + this.columns.push(key); |
| 120 | + } |
| 121 | + columns.push(html`<td><div class="wrap">${this.#renderCell(row[key])}</div></td>`); |
| 122 | + } |
| 123 | + return columns; |
| 124 | + } |
| 125 | + |
| 126 | + // TODO: Other types |
| 127 | + return html``; |
| 128 | + } |
| 129 | + |
| 130 | + #renderCell(cell) { |
| 131 | + if (cell instanceof Object) { |
| 132 | + return html`<code>${JSON.stringify(cell)}</code>`; |
| 133 | + } |
| 134 | + return html`${cell}`; |
65 | 135 | } |
66 | 136 | } |
0 commit comments