@exmg/exm-grid 1.2.2 → 1.2.4

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.
@@ -0,0 +1,331 @@
1
+ import { __decorate } from 'tslib';
2
+ import { html } from 'lit';
3
+ import { ExmgElement } from '@exmg/lit-base/index.js';
4
+ import { property, state } from 'lit/decorators.js';
5
+ import { cache } from 'lit/directives/cache.js';
6
+ import { ExmRowSelectable } from './featrues/exm-row-selectable.js';
7
+ import { ExmQuerySelectors } from './utils/exm-query-selectors.js';
8
+ import { ExmRowExpandable } from './featrues/exm-row-expandable.js';
9
+ import { ExmColumnSortable } from './featrues/exm-column-sortable.js';
10
+ import { ExmRowSortable } from './featrues/exm-row-sortable.js';
11
+ import '@exmg/exm-sortable/exm-sortable.js';
12
+
13
+ /**
14
+ * ### Styling
15
+ * The following custom properties and mixins are available for styling:
16
+ *
17
+ * Custom property | Description | Default
18
+ * ----------------|-------------|----------
19
+ * `--exm-arrow-upward-url` | Url to icon that is used for soring direction indicator | `url('/assets/arrow-upward.svg');`
20
+ * `--exm-table-card-width` | table card width | `100%;`
21
+ * `--exm-table-card-margin-bottom` | table bottom margin | `5px;`
22
+ * `--exm-table-color` | table text color | `#02182b;`
23
+ * `--exm-table-background-color` | table background color | `#ffffff;`
24
+ * `--exm-table-box-shadow` | table box shadow | `#{0px 1px 5px 0px rgba($onSurface, .2), 0px 2px 2px 0px rgba($onSurface, .14), 0px 3px 1px -2px rgba($onSurface, .12)},`
25
+ * `--exm-table-row-divider-color` | table rows separator color | `#dbdbdb;`
26
+ * `--exm-table-row-selected-color` | selected row text color | `#02182b;`
27
+ * `--exm-table-row-alternate-background-color` | alternate row background color | `#cbc9d420;`
28
+ * `--exm-table-row-selected-background-color` | selected row background color | `#e2f1fe;`
29
+ * `--exm-table-row-hover-color` | row hover text color | `#02182b;`
30
+ * `--exm-table-row-hover-background-color` | row hover background color | `#f1f1f1;`
31
+ * `--exm-table-row-dragged-background-color` | sortable row background color when dragged | `#f1f1f1;`
32
+ * `--exm-table-rows-expanded-divider-border` | border between row and expanded row detail | `none;`
33
+ * `--exm-table-rows-expanded-border` | border around row and expanded row detail | `1px solid #dbdbdb;`
34
+ * `--exm-table-rows-expanded-background-color` | background color of row and expanded row detail | `#e2f1fe;`
35
+ * `--exm-table-rows-expanded-color` | text color of row and expanded row detail | `#02182b;`
36
+ * `--exm-table-th-color` | header text color | `#0071dc;`
37
+ * `--exm-table-columns-background-color` | header background color | `#ffffff;`
38
+ * `--exm-table-th-sortable-hover-color` | sortable header hover text color | `#02182b;`
39
+ * `--exm-table-th-height` | header height | `48px;`
40
+ * `--exm-table-th-sort-margin-left` | header margin after text but before icon | `0px;`
41
+ * `--exm-table-td-height` | row cell height | `48px;`
42
+ * `--exm-table-th-sort-icon-height` | sort icon height | `1em;`
43
+ * `--exm-table-th-sort-icon-width` | sort icon width | `1em;`
44
+ * `--exm-table-col-number-padding-right` | right padding of number column | `10px;`
45
+ * `--exm-table-checkbox-cell-width` | width of cell with checkbox | `24px;`
46
+ */
47
+ class ExmGridBase extends ExmgElement {
48
+ constructor() {
49
+ super(...arguments);
50
+ /**
51
+ * Array of data which mapped to rows
52
+ */
53
+ this.items = [];
54
+ /**
55
+ * Feature that turn on sort by column
56
+ */
57
+ this.sortable = false;
58
+ /**
59
+ * Feature that allow sort rows
60
+ * If table has turn on feature `selectable` then it takes precedence and `rowSelectable` will be ignored
61
+ */
62
+ this.rowsSortable = false;
63
+ /**
64
+ * Feature that allow select rows
65
+ */
66
+ this.rowsSelectable = false;
67
+ /**
68
+ * By default a ros is also selactable by clicking anywhere inside the row when the rowSElectable option is enabled
69
+ */
70
+ this.disableRowClickSelection = false;
71
+ /**
72
+ * Map of column names that should be hidden
73
+ */
74
+ this.hiddenColumnNames = {};
75
+ /**
76
+ * Map of row id and selection state
77
+ * Useful for setup default selection or manipulating programmatically
78
+ */
79
+ this.selectedRowIds = {};
80
+ /**
81
+ * Map of row id and expandable row state
82
+ * Useful for setup default expanded rows or manipulating programmatically
83
+ */
84
+ this.expandedRowIds = {};
85
+ /**
86
+ * Set table layout. If fixed then text overflow will be hidden and ellipsis added.
87
+ */
88
+ this.tableLayout = 'auto';
89
+ this.withToolbar = false;
90
+ this.componentReady = false;
91
+ }
92
+ getQuerySelectors() {
93
+ if (!this.querySelectors) {
94
+ throw new Error('ExmQuerySelector not initialized yet');
95
+ }
96
+ return this.querySelectors;
97
+ }
98
+ getTable() {
99
+ return this.getQuerySelectors().getTable();
100
+ }
101
+ getTableBody() {
102
+ return this.getQuerySelectors().getTableBody();
103
+ }
104
+ findTableBody() {
105
+ if (this.querySelectors) {
106
+ return this.getTableBody() || undefined;
107
+ }
108
+ return undefined;
109
+ }
110
+ // eslint-disable-next-line
111
+ getColumns(selector = 'th') {
112
+ return this.getQuerySelectors().getColumns(selector);
113
+ }
114
+ getBodyRowSelector(selector = '') {
115
+ return this.getQuerySelectors().getBodyRowSelector(selector);
116
+ }
117
+ canSortRows() {
118
+ return !this.sortable && this.rowsSortable;
119
+ }
120
+ rowsOrderChange(e) {
121
+ setTimeout(() => {
122
+ const { sourceIndex, targetIndex } = e.detail;
123
+ const items = [...this.items];
124
+ const movedElement = items[sourceIndex];
125
+ items.splice(sourceIndex, 1);
126
+ items.splice(targetIndex, 0, movedElement);
127
+ this.dispatchEvent(new CustomEvent('exm-grid-rows-order-updated', {
128
+ composed: true,
129
+ bubbles: true,
130
+ detail: { sourceIndex, targetIndex },
131
+ }));
132
+ this.dispatchEvent(new CustomEvent('exm-grid-rows-order-changed', {
133
+ composed: true,
134
+ bubbles: true,
135
+ detail: { items },
136
+ }));
137
+ }, 0);
138
+ }
139
+ updateColumnVisibility(previousHiddenColumnNames = {}) {
140
+ let visibleColumns = 0;
141
+ this.getColumns().forEach((column, index) => {
142
+ const columnKey = column.getAttribute('data-column-key');
143
+ visibleColumns += this.hiddenColumnNames[columnKey || ''] ? 0 : 1;
144
+ if (columnKey && previousHiddenColumnNames[columnKey] !== this.hiddenColumnNames[columnKey]) {
145
+ const nextDisplayValue = this.hiddenColumnNames[columnKey] ? 'none' : 'table-cell';
146
+ column.style.display = nextDisplayValue;
147
+ this.getTable()
148
+ .querySelectorAll(`tbody.grid-data tr:not(.grid-row-detail) td:nth-child(${index + 1})`)
149
+ .forEach((cell) => {
150
+ cell.style.display = nextDisplayValue;
151
+ });
152
+ }
153
+ });
154
+ this.updateAutoColspan(visibleColumns);
155
+ }
156
+ updateAutoColspan(visibleColumns) {
157
+ this.getTable()
158
+ .querySelectorAll('[data-auto-colspan]')
159
+ .forEach((element) => {
160
+ const offset = Number.parseInt(element.getAttribute('data-auto-span') || '0', 10);
161
+ element.setAttribute('colspan', (visibleColumns - offset).toString());
162
+ });
163
+ }
164
+ observeExpandedRowIds(changedProps) {
165
+ if (changedProps.has('expandedRowIds')) {
166
+ Object.entries(this.expandedRowIds).forEach(([rowId, nextExpandedState]) => {
167
+ const expendableToggle = this.getTableBody().querySelector(this.getBodyRowSelector(`[data-row-key="${rowId}"] ${this.expandableToggleSelector}`));
168
+ if (expendableToggle) {
169
+ const isExpanded = expendableToggle.hasAttribute('data-is-expanded');
170
+ if (isExpanded !== nextExpandedState) {
171
+ expendableToggle.dispatchEvent(new MouseEvent('click'));
172
+ }
173
+ }
174
+ });
175
+ }
176
+ }
177
+ observeSelectedRowIds(changedProps) {
178
+ if (changedProps.has('selectedRowIds')) {
179
+ Object.entries(this.selectedRowIds).forEach(([rowId, nextSelectionState]) => {
180
+ const row = this.getTableBody().querySelector(this.getBodyRowSelector(`[data-row-key="${rowId}"]`));
181
+ if (row) {
182
+ const isSelected = row.hasAttribute('data-selected');
183
+ if (isSelected !== nextSelectionState) {
184
+ row.dispatchEvent(new MouseEvent('click'));
185
+ }
186
+ }
187
+ });
188
+ }
189
+ }
190
+ observeItems(changedProps) {
191
+ if (changedProps.has('items') && this.rowSelectableFeature) {
192
+ this.rowSelectableFeature.syncSelectedItems();
193
+ }
194
+ }
195
+ async initGridAttributes() {
196
+ await this.updateComplete;
197
+ const toolbarSlot = this.shadowRoot.querySelector('slot[name="toolbar"]');
198
+ if (toolbarSlot && toolbarSlot.assignedNodes && toolbarSlot.assignedNodes().length) {
199
+ // make TS happy - this.withToolbar is declared but never read
200
+ this.withToolbar = this.withToolbar || true;
201
+ }
202
+ }
203
+ async firstUpdated() {
204
+ const table = this.shadowRoot.host.querySelector('table');
205
+ const tableBody = table.querySelector('tbody.grid-data');
206
+ this.querySelectors = new ExmQuerySelectors(table, tableBody);
207
+ this.initGridAttributes();
208
+ const bodyRows = this.querySelectors.getBodyRows();
209
+ if (this.sortable) {
210
+ this.columnSortableFeature = new ExmColumnSortable(this.querySelectors, (event) => this.dispatchEvent(event), this.defaultSortColumn, this.defaultSortDirection);
211
+ this.columnSortableFeature.initFeature();
212
+ }
213
+ if (this.canSortRows()) {
214
+ this.rowSortableFeature = new ExmRowSortable(this.querySelectors);
215
+ this.rowSortableFeature.initFeature();
216
+ }
217
+ if (this.rowsSelectable) {
218
+ this.rowSelectableFeature = new ExmRowSelectable(this.querySelectors, (event) => this.dispatchEvent(event), this.disableRowClickSelection, this.selectableCheckboxSelector);
219
+ this.rowSelectableFeature.initFeature(bodyRows);
220
+ }
221
+ if (this.expandableToggleSelector) {
222
+ this.rowExpandableFeature = new ExmRowExpandable(this.querySelectors, this.expandableToggleSelector);
223
+ this.rowExpandableFeature.initFeature();
224
+ }
225
+ this.updateColumnVisibility();
226
+ bodyRows.forEach((row) => row.setAttribute('data-initialized', ''));
227
+ this.querySelectors.getColumns('th:not([title])').forEach((col) => col.setAttribute('title', col.innerText));
228
+ this.querySelectors.getTable().setAttribute('data-table-layout', this.tableLayout);
229
+ await this.updateComplete;
230
+ this.componentReady = true;
231
+ }
232
+ updated(changedProps) {
233
+ if (changedProps.has('hiddenColumnNames') || changedProps.has('items')) {
234
+ this.updateColumnVisibility(changedProps.get('hiddenColumnNames'));
235
+ }
236
+ this.observeItems(changedProps);
237
+ this.observeExpandedRowIds(changedProps);
238
+ this.observeSelectedRowIds(changedProps);
239
+ if (this.componentReady && changedProps.has('items')) {
240
+ const bodyRows = this.querySelectors.getBodyRowsNotInitialized();
241
+ if (this.rowSelectableFeature) {
242
+ this.rowSelectableFeature.updateFeature(bodyRows);
243
+ }
244
+ if (this.rowExpandableFeature) {
245
+ this.rowExpandableFeature.updateFeature();
246
+ }
247
+ if (this.rowSortableFeature) {
248
+ this.rowSortableFeature.updateFeature();
249
+ }
250
+ bodyRows.forEach((row) => row.setAttribute('data-initialized', ''));
251
+ }
252
+ }
253
+ renderWithoutSortable() {
254
+ return html ` <slot></slot> `;
255
+ }
256
+ renderWithSortable() {
257
+ return html `
258
+ <exm-sortable
259
+ orientation="${'vertical'}"
260
+ animation-enabled
261
+ item-selector="tbody.grid-data &gt; tr:not(.grid-row-detail)"
262
+ handle-selector=".grid-row-drag-handler"
263
+ .sortableHostNode="${this.findTableBody()}"
264
+ @dom-order-change="${this.rowsOrderChange}"
265
+ >
266
+ <slot></slot>
267
+ </exm-sortable>
268
+ `;
269
+ }
270
+ render() {
271
+ return html `
272
+ <div class="table-card-container">
273
+ <slot name="toolbar"></slot>
274
+ <div class="table-card">
275
+ <div class="table-container">
276
+ ${cache(this.canSortRows() ? this.renderWithSortable() : this.renderWithoutSortable())}
277
+ </div>
278
+ <slot name="pagination"></slot>
279
+ </div>
280
+ </div>
281
+ `;
282
+ }
283
+ }
284
+ __decorate([
285
+ property({ type: Array })
286
+ ], ExmGridBase.prototype, "items", void 0);
287
+ __decorate([
288
+ property({ type: Boolean, reflect: true })
289
+ ], ExmGridBase.prototype, "sortable", void 0);
290
+ __decorate([
291
+ property({ type: String, attribute: 'default-sort-column' })
292
+ ], ExmGridBase.prototype, "defaultSortColumn", void 0);
293
+ __decorate([
294
+ property({ type: String, attribute: 'default-sort-direction' })
295
+ ], ExmGridBase.prototype, "defaultSortDirection", void 0);
296
+ __decorate([
297
+ property({ type: Boolean, reflect: true, attribute: 'rows-sortable' })
298
+ ], ExmGridBase.prototype, "rowsSortable", void 0);
299
+ __decorate([
300
+ property({ type: Boolean, attribute: 'rows-selectable' })
301
+ ], ExmGridBase.prototype, "rowsSelectable", void 0);
302
+ __decorate([
303
+ property({ type: Boolean, attribute: 'disable-row-click-selection' })
304
+ ], ExmGridBase.prototype, "disableRowClickSelection", void 0);
305
+ __decorate([
306
+ property({ type: String, attribute: 'selectable-checkbox-selector' })
307
+ ], ExmGridBase.prototype, "selectableCheckboxSelector", void 0);
308
+ __decorate([
309
+ property({ type: Object })
310
+ ], ExmGridBase.prototype, "hiddenColumnNames", void 0);
311
+ __decorate([
312
+ property({ type: Object })
313
+ ], ExmGridBase.prototype, "selectedRowIds", void 0);
314
+ __decorate([
315
+ property({ type: Object })
316
+ ], ExmGridBase.prototype, "expandedRowIds", void 0);
317
+ __decorate([
318
+ property({ type: String, attribute: 'expandable-toggle-selector', reflect: true })
319
+ ], ExmGridBase.prototype, "expandableToggleSelector", void 0);
320
+ __decorate([
321
+ property({ type: String, attribute: 'table-layout', reflect: true })
322
+ ], ExmGridBase.prototype, "tableLayout", void 0);
323
+ __decorate([
324
+ state()
325
+ ], ExmGridBase.prototype, "querySelectors", void 0);
326
+ __decorate([
327
+ property({ type: Boolean, reflect: true, attribute: 'data-with-toolbar' })
328
+ ], ExmGridBase.prototype, "withToolbar", void 0);
329
+
330
+ export { ExmGridBase };
331
+ //# sourceMappingURL=exm-grid-base.js.map
@@ -1,130 +1,9 @@
1
- import { ExmgElement } from '@exmg/lit-base/index.js';
2
- import '@exmg/exm-sortable/exm-sortable.js';
3
- import { SORT_DIRECTION } from './types/exm-grid-types.js';
4
- type GenericPropertyValues<T, V = unknown> = Map<T, V>;
5
- type Props = Exclude<keyof ExmGrid, number | symbol>;
6
- type SmartPropertyValue = GenericPropertyValues<Props>;
7
- /**
8
- * ### Styling
9
- * The following custom properties and mixins are available for styling:
10
- *
11
- * Custom property | Description | Default
12
- * ----------------|-------------|----------
13
- * `--exm-arrow-upward-url` | Url to icon that is used for soring direction indicator | `url('/assets/arrow-upward.svg');`
14
- * `--exm-table-card-width` | table card width | `100%;`
15
- * `--exm-table-card-margin-bottom` | table bottom margin | `5px;`
16
- * `--exm-table-color` | table text color | `#02182b;`
17
- * `--exm-table-background-color` | table background color | `#ffffff;`
18
- * `--exm-table-box-shadow` | table box shadow | `#{0px 1px 5px 0px rgba($onSurface, .2), 0px 2px 2px 0px rgba($onSurface, .14), 0px 3px 1px -2px rgba($onSurface, .12)},`
19
- * `--exm-table-row-divider-color` | table rows separator color | `#dbdbdb;`
20
- * `--exm-table-row-selected-color` | selected row text color | `#02182b;`
21
- * `--exm-table-row-selected-background-color` | selected row background color | `#e2f1fe;`
22
- * `--exm-table-row-hover-color` | row hover text color | `#02182b;`
23
- * `--exm-table-row-hover-background-color` | row hover background color | `#f1f1f1;`
24
- * `--exm-table-row-dragged-background-color` | sortable row background color when dragged | `#f1f1f1;`
25
- * `--exm-table-rows-expanded-divider-border` | border between row and expanded row detail | `none;`
26
- * `--exm-table-rows-expanded-border` | border around row and expanded row detail | `1px solid #dbdbdb;`
27
- * `--exm-table-rows-expanded-background-color` | background color of row and expanded row detail | `#e2f1fe;`
28
- * `--exm-table-rows-expanded-color` | text color of row and expanded row detail | `#02182b;`
29
- * `--exm-table-th-color` | header text color | `#0071dc;`
30
- * `--exm-table-columns-background-color` | header background color | `#ffffff;`
31
- * `--exm-table-th-sortable-hover-color` | sortable header hover text color | `#02182b;`
32
- * `--exm-table-th-height` | header height | `48px;`
33
- * `--exm-table-th-sort-margin-left` | header margin after text but before icon | `0px;`
34
- * `--exm-table-td-height` | row cell height | `48px;`
35
- * `--exm-table-th-sort-icon-height` | sort icon height | `1em;`
36
- * `--exm-table-th-sort-icon-width` | sort icon width | `1em;`
37
- * `--exm-table-col-number-padding-right` | right padding of number column | `10px;`
38
- * `--exm-table-checkbox-cell-width` | width of cell with checkbox | `24px;`
39
- */
40
- export declare class ExmGrid extends ExmgElement {
1
+ import { ExmGridBase } from './exm-grid-base.js';
2
+ export declare class ExmGrid extends ExmGridBase {
41
3
  static styles: import("lit").CSSResult[];
42
- /**
43
- * Array of data which mapped to rows
44
- */
45
- items: unknown[];
46
- /**
47
- * Feature that turn on sort by column
48
- */
49
- sortable: boolean;
50
- /**
51
- * Name of sort column which should be sorted by default
52
- */
53
- defaultSortColumn?: string;
54
- /**
55
- * Default sort direction
56
- */
57
- defaultSortDirection?: SORT_DIRECTION;
58
- /**
59
- * Feature that allow sort rows
60
- * If table has turn on feature `selectable` then it takes precedence and `rowSelectable` will be ignored
61
- */
62
- rowsSortable: boolean;
63
- /**
64
- * Feature that allow select rows
65
- */
66
- rowsSelectable: boolean;
67
- /**
68
- * By default a ros is also selactable by clicking anywhere inside the row when the rowSElectable option is enabled
69
- */
70
- disableRowClickSelection: boolean;
71
- /**
72
- * If rows are selectable you can also pass selector to checkboxes.
73
- * We can have checkboxes in thead or / and tbody.
74
- */
75
- selectableCheckboxSelector?: string;
76
- /**
77
- * Map of column names that should be hidden
78
- */
79
- hiddenColumnNames: Record<string, string>;
80
- /**
81
- * Map of row id and selection state
82
- * Useful for setup default selection or manipulating programmatically
83
- */
84
- selectedRowIds: Record<string, boolean>;
85
- /**
86
- * Map of row id and expandable row state
87
- * Useful for setup default expanded rows or manipulating programmatically
88
- */
89
- expandedRowIds: Record<string, boolean>;
90
- /**
91
- * Selector to element inside row which will trigger expand / collapse action on related row detail
92
- */
93
- expandableToggleSelector?: string;
94
- /**
95
- * Set table layout. If fixed then text overflow will be hidden and ellipsis added.
96
- */
97
- tableLayout: 'fixed' | 'auto';
98
- private querySelectors?;
99
- withToolbar: boolean;
100
- private rowSelectableFeature?;
101
- private rowExpandableFeature?;
102
- private rowSortableFeature?;
103
- private columnSortableFeature?;
104
- private componentReady;
105
- private getQuerySelectors;
106
- private getTable;
107
- private getTableBody;
108
- private findTableBody;
109
- private getColumns;
110
- private getBodyRowSelector;
111
- private canSortRows;
112
- private rowsOrderChange;
113
- private updateColumnVisibility;
114
- private updateAutoColspan;
115
- private observeExpandedRowIds;
116
- private observeSelectedRowIds;
117
- private observeItems;
118
- private initGridAttributes;
119
- protected firstUpdated(): Promise<void>;
120
- protected updated(changedProps: SmartPropertyValue): void;
121
- private renderWithoutSortable;
122
- private renderWithSortable;
123
- protected render(): import("lit-html").TemplateResult<1>;
124
4
  }
125
5
  declare global {
126
6
  interface HTMLElementTagNameMap {
127
7
  'exm-grid': ExmGrid;
128
8
  }
129
9
  }
130
- export {};