@timlassiter11/yatl 1.0.0 → 1.0.2

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/index.mjs CHANGED
@@ -106,6 +106,14 @@ var _YatlSearchEvent = class _YatlSearchEvent extends YatlEvent {
106
106
  };
107
107
  _YatlSearchEvent.EVENT_NAME = "yatl-search";
108
108
  var YatlSearchEvent = _YatlSearchEvent;
109
+ var _YatlStateChangeEvent = class _YatlStateChangeEvent extends YatlEvent {
110
+ constructor(state2, triggers) {
111
+ super(_YatlStateChangeEvent.EVENT_NAME, { state: state2, triggers });
112
+ this.triggers = triggers;
113
+ }
114
+ };
115
+ _YatlStateChangeEvent.EVENT_NAME = "yatl-state-change";
116
+ var YatlStateChangeEvent = _YatlStateChangeEvent;
109
117
 
110
118
  // src/utils.ts
111
119
  import { html } from "lit";
@@ -182,23 +190,6 @@ function highlightText(text, ranges) {
182
190
  function widthsToGridTemplates(widths, defaultWidth = "1fr") {
183
191
  return widths.map((width) => width ? `${width}px` : defaultWidth);
184
192
  }
185
- function didSortStateChange(newState, oldState) {
186
- if (!oldState) {
187
- return true;
188
- }
189
- const allKeys = /* @__PURE__ */ new Set([
190
- ...oldState.map((s) => s.field),
191
- ...newState.map((s) => s.field)
192
- ]);
193
- for (const key of allKeys) {
194
- const oldSort = findColumn(key, oldState)?.sortState;
195
- const newSort = findColumn(key, newState)?.sortState;
196
- if (oldSort?.order !== newSort?.order || oldSort?.priority !== newSort?.priority) {
197
- return true;
198
- }
199
- }
200
- return false;
201
- }
202
193
  function isCompareable(value) {
203
194
  return typeof value === "string" || typeof value === "number" || typeof value === "boolean" || value instanceof Date;
204
195
  }
@@ -524,10 +515,11 @@ var DEFAULT_STORAGE_OPTIONS = {
524
515
  var SAVE_TRIGGERS = /* @__PURE__ */ new Set([
525
516
  "searchQuery",
526
517
  "filters",
527
- // Covers column order
528
518
  "columns",
529
- // Covers sort, visibility, and width
530
- "columnStates",
519
+ "columnSort",
520
+ "columnOrder",
521
+ "columnVisibility",
522
+ "columnWidths",
531
523
  "storageOptions"
532
524
  ]);
533
525
  var MATCH_WEIGHTS = {
@@ -544,6 +536,7 @@ var YatlTable = class extends LitElement {
544
536
  this._enableSearchScoring = false;
545
537
  this._columns = [];
546
538
  this._columnStates = [];
539
+ this._columnOrder = [];
547
540
  this._storageOptions = null;
548
541
  this._data = [];
549
542
  this._searchQuery = "";
@@ -586,12 +579,12 @@ var YatlTable = class extends LitElement {
586
579
  return;
587
580
  }
588
581
  const multiSort = event.shiftKey;
589
- const state2 = findColumn(column.field, this._columnStates);
590
- if (!state2?.sortState) {
582
+ const state2 = this.getColumnState(column.field);
583
+ if (!state2?.sort) {
591
584
  this.sort(column.field, "asc", !multiSort);
592
- } else if (state2.sortState.order === "asc") {
585
+ } else if (state2.sort.order === "asc") {
593
586
  this.sort(column.field, "desc", !multiSort);
594
- } else if (state2.sortState.order) {
587
+ } else if (state2.sort.order) {
595
588
  this.sort(column.field, null, !multiSort);
596
589
  }
597
590
  };
@@ -621,18 +614,16 @@ var YatlTable = class extends LitElement {
621
614
  const finalWidth = parseFloat(
622
615
  this.resizeState.currentWidths[this.resizeState.columnIndex]
623
616
  );
624
- const columnStates = this.columnStates;
625
- const state2 = findColumn(this.resizeState.columnField, columnStates);
617
+ const columnWidths = this.columnWidths;
618
+ const state2 = findColumn(this.resizeState.columnField, columnWidths);
626
619
  state2.width = finalWidth;
627
- this.columnStates = columnStates;
620
+ this.columnWidths = columnWidths;
628
621
  this.dispatchEvent(new YatlColumnResizeEvent(state2.field, state2.width));
629
622
  }
630
623
  this.resizeState = null;
631
624
  };
632
625
  this.handleDragColumnStart = (event, field) => {
633
626
  const target = event.target;
634
- console.log("Starting drag event");
635
- console.log(target);
636
627
  if (target?.classList.contains("resizer")) {
637
628
  event.preventDefault();
638
629
  return;
@@ -667,24 +658,23 @@ var YatlTable = class extends LitElement {
667
658
  }
668
659
  event.preventDefault();
669
660
  event.stopPropagation();
670
- const columns = [...this.columns];
671
- const dragIndex = columns.findIndex((col) => col.field === this.dragColumn);
672
- const dropIndex = columns.findIndex((col) => col.field === field);
661
+ const newColumnOrder = [...this.columnOrder];
662
+ const dragIndex = newColumnOrder.findIndex((col) => col === this.dragColumn);
663
+ const dropIndex = newColumnOrder.findIndex((col) => col === field);
673
664
  if (dragIndex > -1 && dropIndex > -1) {
674
- const [draggedColumn] = columns.splice(dragIndex, 1);
665
+ const [draggedColumn] = newColumnOrder.splice(dragIndex, 1);
675
666
  const droppedColumn = findColumn(field, this.columns);
676
667
  if (!droppedColumn) return;
677
- columns.splice(dropIndex, 0, draggedColumn);
678
- const newColumnOrder = columns.map((col) => col.field);
668
+ newColumnOrder.splice(dropIndex, 0, draggedColumn);
679
669
  const reorderEvent = new YatlColumnReorderEvent(
680
- draggedColumn.field,
670
+ draggedColumn,
681
671
  droppedColumn.field,
682
672
  newColumnOrder
683
673
  );
684
674
  if (!this.dispatchEvent(reorderEvent)) {
685
675
  return;
686
676
  }
687
- this.setColumnOrder(newColumnOrder);
677
+ this.columnOrder = [...newColumnOrder];
688
678
  }
689
679
  };
690
680
  this.handleDragColumnEnd = () => {
@@ -726,26 +716,100 @@ var YatlTable = class extends LitElement {
726
716
  }
727
717
  const oldValue = this._columns;
728
718
  this._columns = columns;
729
- this.createColumnStates();
730
719
  this.filterDirty = true;
731
720
  this.requestUpdate("columns", oldValue);
732
721
  }
733
- get columnStates() {
734
- return this._columnStates.map((state2) => ({
735
- ...state2,
736
- sortState: state2.sortState ? { ...state2.sortState } : void 0
722
+ get columnOrder() {
723
+ const finalOrder = /* @__PURE__ */ new Set();
724
+ for (const field of this._columnOrder) {
725
+ const col = findColumn(field, this.columns);
726
+ if (col) {
727
+ finalOrder.add(field);
728
+ }
729
+ }
730
+ for (const col of this.columns) {
731
+ if (!finalOrder.has(col.field)) {
732
+ finalOrder.add(col.field);
733
+ }
734
+ }
735
+ return [...finalOrder];
736
+ }
737
+ set columnOrder(columns) {
738
+ if (this._columnOrder === columns) {
739
+ return;
740
+ }
741
+ const oldValue = this._columnOrder;
742
+ this._columnOrder = [...columns];
743
+ this.requestUpdate("columnOrder", oldValue);
744
+ }
745
+ get columnVisibility() {
746
+ return this.columnOrder.map((field) => ({
747
+ field,
748
+ visible: this.getColumnState(field).visible
737
749
  }));
738
750
  }
739
- set columnStates(states) {
740
- if (this._columnStates === states) {
751
+ set columnVisibility(columns) {
752
+ const oldValue = this.columnVisibility;
753
+ let changed = false;
754
+ for (const column of columns) {
755
+ const columnState = this.getColumnState(column.field);
756
+ if (columnState && columnState.visible !== column.visible) {
757
+ changed = true;
758
+ columnState.visible = column.visible;
759
+ }
760
+ }
761
+ if (!changed) {
762
+ return;
763
+ }
764
+ this.requestUpdate("columnVisibility", oldValue);
765
+ }
766
+ get columnSort() {
767
+ return this.columnOrder.map((field) => {
768
+ const sortState = this.getColumnState(field).sort;
769
+ return {
770
+ field,
771
+ // We need to make a copy of sort state so
772
+ // if the user changes it, it doesn't change our copy.
773
+ sort: sortState ? { ...sortState } : null
774
+ };
775
+ });
776
+ }
777
+ set columnSort(columns) {
778
+ const oldValue = this.columnSort;
779
+ let changed = false;
780
+ for (const column of columns) {
781
+ const columnState = this.getColumnState(column.field);
782
+ if (columnState && (columnState.sort?.order !== column.sort?.order || columnState.sort?.priority !== column.sort?.priority)) {
783
+ changed = true;
784
+ columnState.sort = column.sort;
785
+ }
786
+ }
787
+ if (!changed) {
741
788
  return;
742
789
  }
743
- const oldValue = this._columnStates;
744
- this._columnStates = states;
745
- if (didSortStateChange(this._columnStates, oldValue)) {
746
- this.sortDirty = true;
790
+ this.sortDirty = true;
791
+ this.requestUpdate("columnSort", oldValue);
792
+ }
793
+ get columnWidths() {
794
+ return this.columnOrder.map((field) => ({
795
+ field,
796
+ width: this.getColumnState(field).width
797
+ }));
798
+ }
799
+ set columnWidths(columns) {
800
+ const oldValue = this.columnWidths;
801
+ let changed = false;
802
+ for (const column of columns) {
803
+ const columnState = this.getColumnState(column.field);
804
+ if (columnState && columnState.width !== column.width) {
805
+ changed = true;
806
+ columnState.width = column.width;
807
+ }
808
+ }
809
+ if (!changed) {
810
+ return;
747
811
  }
748
- this.requestUpdate("columnStates", oldValue);
812
+ this.requestUpdate("columnWidths", oldValue);
749
813
  }
750
814
  get searchQuery() {
751
815
  return this._searchQuery;
@@ -837,18 +901,17 @@ var YatlTable = class extends LitElement {
837
901
  * Gets a copy of the current state of the table.
838
902
  */
839
903
  getState() {
840
- const states = this.columnStates;
841
904
  return {
842
905
  searchQuery: this.searchQuery,
843
906
  filters: this.filters,
844
- columnOrder: this.columns.map((column) => column.field),
907
+ columnOrder: this.columnOrder,
845
908
  columns: this.columns.map((column) => {
846
- const columnState = findColumn(column.field, states);
909
+ const state2 = this.getColumnState(column.field);
847
910
  return {
848
911
  field: column.field,
849
- visible: columnState?.visible ?? true,
850
- sortState: columnState?.sortState,
851
- width: columnState?.width
912
+ visible: state2.visible,
913
+ sort: state2.sort,
914
+ width: state2.width
852
915
  };
853
916
  })
854
917
  };
@@ -865,15 +928,12 @@ var YatlTable = class extends LitElement {
865
928
  this.filters = state2.filters;
866
929
  }
867
930
  if ("columnOrder" in state2 && state2.columnOrder !== void 0) {
868
- this.setColumnOrder(state2.columnOrder);
931
+ this.columnOrder = state2.columnOrder;
869
932
  }
870
933
  if ("columns" in state2 && state2.columns !== void 0) {
871
934
  const newColumnStates = [];
872
935
  for (const newState of state2.columns) {
873
- const currentState = findColumn(newState.field, this._columnStates) ?? {
874
- field: newState.field,
875
- visible: true
876
- };
936
+ const currentState = this.getColumnState(newState.field);
877
937
  newColumnStates.push(currentState);
878
938
  if (!newState) {
879
939
  continue;
@@ -881,14 +941,15 @@ var YatlTable = class extends LitElement {
881
941
  if ("visible" in newState && newState.visible !== void 0) {
882
942
  currentState.visible = newState.visible;
883
943
  }
884
- if ("sortState" in newState && newState.sortState !== void 0) {
885
- currentState.sortState = newState.sortState;
944
+ if ("sort" in newState && newState.sort !== void 0) {
945
+ currentState.sort = newState.sort;
886
946
  }
887
947
  if ("width" in newState && newState.width !== void 0) {
888
948
  currentState.width = newState.width;
889
949
  }
890
950
  }
891
- this.columnStates = newColumnStates;
951
+ this._columnStates = newColumnStates;
952
+ this.requestUpdate();
892
953
  }
893
954
  }
894
955
  /**
@@ -899,35 +960,35 @@ var YatlTable = class extends LitElement {
899
960
  * @param clear - Clear all other sorting
900
961
  */
901
962
  sort(field, order, clear = true) {
902
- const columnStates = this.columnStates;
903
- const state2 = findColumn(field, columnStates);
963
+ const sortStates = this.columnSort;
964
+ const state2 = findColumn(field, sortStates);
904
965
  if (!state2) {
905
966
  throw new Error(`Cannot get options for non-existent column "${field}"`);
906
967
  }
907
- if (order === state2.sortState?.order) {
968
+ if (order === state2.sort?.order) {
908
969
  return;
909
970
  }
910
971
  if (!this.dispatchEvent(new YatlSortEvent(field, order))) {
911
972
  return;
912
973
  }
913
- if (order && !state2.sortState) {
914
- const priorities = columnStates.map((col) => col.sortState?.priority).filter((priority2) => priority2 !== void 0);
974
+ if (order && !state2.sort) {
975
+ const priorities = sortStates.map((col) => col.sort?.priority).filter((priority2) => priority2 !== void 0);
915
976
  const maxPriority = this.columns.length + 1;
916
977
  const priority = Math.min(maxPriority, ...priorities) - 1;
917
- state2.sortState = { order, priority };
918
- } else if (order && state2.sortState) {
919
- state2.sortState.order = order;
978
+ state2.sort = { order, priority };
979
+ } else if (order && state2.sort) {
980
+ state2.sort.order = order;
920
981
  } else {
921
- state2.sortState = null;
982
+ state2.sort = null;
922
983
  }
923
984
  if (clear) {
924
- for (const state3 of columnStates) {
985
+ for (const state3 of sortStates) {
925
986
  if (state3.field !== field) {
926
- state3.sortState = null;
987
+ state3.sort = null;
927
988
  }
928
989
  }
929
990
  }
930
- this.columnStates = columnStates;
991
+ this.columnSort = [...sortStates];
931
992
  }
932
993
  /**
933
994
  * Sets the visibility of a specified column.
@@ -935,8 +996,8 @@ var YatlTable = class extends LitElement {
935
996
  * @param visible - `true` to show the column, `false` to hide it.
936
997
  */
937
998
  setColumnVisibility(field, visible) {
938
- const columnStates = this.columnStates;
939
- const state2 = findColumn(field, columnStates);
999
+ const visibilityStates = this.columnVisibility;
1000
+ const state2 = findColumn(field, visibilityStates);
940
1001
  if (!state2) {
941
1002
  throw new Error(`Cannot get options for non-existent column "${field}"`);
942
1003
  }
@@ -947,14 +1008,14 @@ var YatlTable = class extends LitElement {
947
1008
  return;
948
1009
  }
949
1010
  state2.visible = visible;
950
- this.columnStates = columnStates;
1011
+ this.columnVisibility = visibilityStates;
951
1012
  }
952
1013
  /**
953
1014
  * Toggles the visibility of hte specified column
954
1015
  * @param field - The field name of the column to toggle.
955
1016
  */
956
1017
  toggleColumnVisibility(field) {
957
- const state2 = findColumn(field, this._columnStates);
1018
+ const state2 = this.getColumnState(field);
958
1019
  this.setColumnVisibility(field, !state2);
959
1020
  }
960
1021
  /**
@@ -1059,27 +1120,6 @@ var YatlTable = class extends LitElement {
1059
1120
  this.tableElement.scrollTop = px;
1060
1121
  }
1061
1122
  }
1062
- /**
1063
- * Sets the display order of the columns in the table.
1064
- *
1065
- * @param fields - An array of field names representing the new order of columns. Columns not included in the array will be placed at the end.
1066
- * @throws {TypeError} If `fields` is not an array.
1067
- */
1068
- setColumnOrder(fields) {
1069
- const newColumns = [];
1070
- for (const field of fields) {
1071
- const col = findColumn(field, this.columns);
1072
- if (col) {
1073
- newColumns.push(col);
1074
- }
1075
- }
1076
- for (const col of this.columns) {
1077
- if (!findColumn(col.field, newColumns)) {
1078
- newColumns.push(col);
1079
- }
1080
- }
1081
- this.columns = [...newColumns];
1082
- }
1083
1123
  /**
1084
1124
  * Finds the first row
1085
1125
  * @param field
@@ -1147,8 +1187,8 @@ var YatlTable = class extends LitElement {
1147
1187
  part="header-sort-icon"
1148
1188
  class=${classMap({
1149
1189
  "sort-icon": true,
1150
- ascending: state2.sortState?.order === "asc",
1151
- descending: state2.sortState?.order === "desc"
1190
+ ascending: state2.sort?.order === "asc",
1191
+ descending: state2.sort?.order === "desc"
1152
1192
  })}
1153
1193
  ></div>` : nothing;
1154
1194
  }
@@ -1160,8 +1200,9 @@ var YatlTable = class extends LitElement {
1160
1200
  @mousedown=${(event) => this.handleResizeMouseDown(event, column.field)}
1161
1201
  ></div>` : nothing;
1162
1202
  }
1163
- renderHeaderCell(column) {
1164
- const state2 = findColumn(column.field, this._columnStates);
1203
+ renderHeaderCell(field) {
1204
+ const column = findColumn(field, this.columns);
1205
+ const state2 = this.getColumnState(column.field);
1165
1206
  if (state2.visible == false) {
1166
1207
  return nothing;
1167
1208
  }
@@ -1196,7 +1237,7 @@ var YatlTable = class extends LitElement {
1196
1237
  renderHeader() {
1197
1238
  return html2`
1198
1239
  <div part="header" class="header row">
1199
- ${this.columns.map((column) => this.renderHeaderCell(column))}
1240
+ ${this.columnOrder.map((field) => this.renderHeaderCell(field))}
1200
1241
  </div>
1201
1242
  `;
1202
1243
  }
@@ -1210,9 +1251,10 @@ var YatlTable = class extends LitElement {
1210
1251
  const indices = this.rowMetadata.get(row).highlightIndices;
1211
1252
  return this.enableSearchHighlight && indices ? highlightText(String(value), indices[column.field]) : value;
1212
1253
  }
1213
- renderCell(column, row) {
1214
- const state2 = findColumn(column.field, this._columnStates);
1215
- if (state2?.visible == false) {
1254
+ renderCell(field, row) {
1255
+ const column = findColumn(field, this.columns);
1256
+ const state2 = this.getColumnState(column.field);
1257
+ if (!state2.visible) {
1216
1258
  return nothing;
1217
1259
  }
1218
1260
  let value = getNestedValue(row, column.field);
@@ -1250,7 +1292,7 @@ var YatlTable = class extends LitElement {
1250
1292
  data-index=${metadata.index}
1251
1293
  data-filtered-index=${index}
1252
1294
  >
1253
- ${this.columns.map((column) => this.renderCell(column, row))}
1295
+ ${this.columnOrder.map((field) => this.renderCell(field, row))}
1254
1296
  </div>
1255
1297
  `;
1256
1298
  }
@@ -1311,7 +1353,8 @@ var YatlTable = class extends LitElement {
1311
1353
  `;
1312
1354
  }
1313
1355
  render() {
1314
- const gridTemplate = widthsToGridTemplates(this.columnWidths).join(" ");
1356
+ const gridWidths = this.getGridWidths();
1357
+ const gridTemplate = widthsToGridTemplates(gridWidths).join(" ");
1315
1358
  return html2`
1316
1359
  <div
1317
1360
  part="table"
@@ -1326,12 +1369,30 @@ var YatlTable = class extends LitElement {
1326
1369
  // #region --- Lifecycle Methods ---
1327
1370
  updated(changedProperties) {
1328
1371
  super.updated(changedProperties);
1329
- if (!this.storageOptions?.key) return;
1330
- const shouldSave = Array.from(changedProperties.keys()).some(
1331
- (prop) => SAVE_TRIGGERS.has(prop)
1372
+ super.updated(changedProperties);
1373
+ const stateProps = [
1374
+ "columnOrder",
1375
+ "columnVisibility",
1376
+ "columnSort",
1377
+ "columnWidths",
1378
+ "searchQuery",
1379
+ "filters"
1380
+ ];
1381
+ const changedStateProp = stateProps.filter(
1382
+ (prop) => changedProperties.has(prop)
1332
1383
  );
1333
- if (shouldSave) {
1334
- this.scheduleSave();
1384
+ if (changedStateProp.length) {
1385
+ this.dispatchEvent(
1386
+ new YatlStateChangeEvent(this.getState(), changedStateProp)
1387
+ );
1388
+ }
1389
+ if (this.storageOptions?.key) {
1390
+ const shouldSave = Array.from(changedProperties.keys()).some(
1391
+ (prop) => SAVE_TRIGGERS.has(prop)
1392
+ );
1393
+ if (shouldSave) {
1394
+ this.scheduleSave();
1395
+ }
1335
1396
  }
1336
1397
  }
1337
1398
  disconnectedCallback() {
@@ -1520,12 +1581,12 @@ var YatlTable = class extends LitElement {
1520
1581
  compareRows(a, b, field) {
1521
1582
  let aValue, bValue;
1522
1583
  const columnData = findColumn(field, this.columnData);
1523
- if (!columnData.state.sortState) {
1584
+ if (!columnData.state.sort) {
1524
1585
  return 0;
1525
1586
  }
1526
1587
  const aMetadata = this.rowMetadata.get(a);
1527
1588
  const bMetadata = this.rowMetadata.get(b);
1528
- if (columnData.state.sortState?.order === "asc") {
1589
+ if (columnData.state.sort?.order === "asc") {
1529
1590
  aValue = aMetadata.sortValues[columnData.field];
1530
1591
  bValue = bMetadata.sortValues[columnData.field];
1531
1592
  } else {
@@ -1549,9 +1610,7 @@ var YatlTable = class extends LitElement {
1549
1610
  this.filterRows();
1550
1611
  return;
1551
1612
  }
1552
- const sortedColumns = this.columnData.filter((col) => col.state.visible && col.state.sortState).sort(
1553
- (a, b) => b.state.sortState.priority - a.state.sortState.priority
1554
- );
1613
+ const sortedColumns = this.columnData.filter((col) => col.state.visible && col.state.sort).sort((a, b) => b.state.sort.priority - a.state.sort.priority);
1555
1614
  this._filteredData = this._filteredData.toSorted((a, b) => {
1556
1615
  const aMetadata = this.rowMetadata.get(a);
1557
1616
  const bMetadata = this.rowMetadata.get(b);
@@ -1572,18 +1631,7 @@ var YatlTable = class extends LitElement {
1572
1631
  this.sortDirty = false;
1573
1632
  }
1574
1633
  // #endregion
1575
- // #region --- State Methods ---
1576
- createColumnStates() {
1577
- this.columnStates = this.columns.map((column) => {
1578
- const previousState = findColumn(column.field, this._columnStates);
1579
- return {
1580
- field: column.field,
1581
- visible: previousState?.visible ?? true,
1582
- sortState: previousState?.sortState,
1583
- width: previousState?.width
1584
- };
1585
- });
1586
- }
1634
+ // #region --- Utilities ---
1587
1635
  createMetadata() {
1588
1636
  this.rowMetadata = /* @__PURE__ */ new WeakMap();
1589
1637
  let index = 0;
@@ -1636,20 +1684,19 @@ var YatlTable = class extends LitElement {
1636
1684
  this.queryTokens.push(...this.searchTokenizer(this.searchQuery));
1637
1685
  }
1638
1686
  }
1639
- // #endregion
1640
- // #region --- Utilities ---
1641
1687
  get columnData() {
1642
1688
  return this.columns.map((column) => ({
1643
1689
  field: column.field,
1644
1690
  options: column,
1645
- state: findColumn(column.field, this._columnStates) ?? {
1646
- field: column.field,
1647
- visible: true
1648
- }
1691
+ state: this.getColumnState(column.field)
1649
1692
  }));
1650
1693
  }
1651
- get columnWidths() {
1652
- return this.columns.map((col) => findColumn(col.field, this._columnStates)).filter((state2) => state2 ? state2.visible : true).map((state2) => state2?.width ?? null);
1694
+ /**
1695
+ * Gets the width of each column in the
1696
+ * order they will appear in the grid.
1697
+ */
1698
+ getGridWidths() {
1699
+ return this.columnOrder.map((field) => this.getColumnState(field)).filter((state2) => state2.visible).map((state2) => state2.width);
1653
1700
  }
1654
1701
  scheduleSave() {
1655
1702
  window.clearTimeout(this.saveTimer);
@@ -1657,6 +1704,19 @@ var YatlTable = class extends LitElement {
1657
1704
  this.saveStateToStorage();
1658
1705
  }, STATE_SAVE_DEBOUNCE);
1659
1706
  }
1707
+ getColumnState(field) {
1708
+ let state2 = findColumn(field, this._columnStates);
1709
+ if (!state2) {
1710
+ state2 = {
1711
+ field,
1712
+ visible: true,
1713
+ sort: null,
1714
+ width: null
1715
+ };
1716
+ this._columnStates.push(state2);
1717
+ }
1718
+ return state2;
1719
+ }
1660
1720
  // #endregion
1661
1721
  // #region --- Storage Methods ---
1662
1722
  saveStateToStorage() {
@@ -1676,7 +1736,7 @@ var YatlTable = class extends LitElement {
1676
1736
  field: columnState.field
1677
1737
  };
1678
1738
  if (options.saveColumnSortOrders) {
1679
- savedColumnState.sortState = columnState.sortState;
1739
+ savedColumnState.sort = columnState.sort;
1680
1740
  }
1681
1741
  if (options.saveColumnVisibility) {
1682
1742
  savedColumnState.visible = columnState.visible;
@@ -1721,7 +1781,7 @@ var YatlTable = class extends LitElement {
1721
1781
  columnStateToRestore.width = savedColumnState.width;
1722
1782
  }
1723
1783
  if (options.saveColumnSortOrders) {
1724
- columnStateToRestore.sortState = savedColumnState.sortState;
1784
+ columnStateToRestore.sort = savedColumnState.sort;
1725
1785
  }
1726
1786
  tableStateToRestore.columns.push(columnStateToRestore);
1727
1787
  }
@@ -1740,14 +1800,14 @@ var YatlTable = class extends LitElement {
1740
1800
  if (!header) {
1741
1801
  return;
1742
1802
  }
1743
- const columnIndex = this.columns.findIndex((col) => col.field === field);
1803
+ const columnIndex = this.columnOrder.findIndex((col) => col === field);
1744
1804
  if (columnIndex < 0) {
1745
1805
  return;
1746
1806
  }
1747
1807
  this.tableElement.querySelectorAll(".header .cell").forEach((element) => {
1748
1808
  const field2 = element.dataset.field;
1749
1809
  if (field2) {
1750
- const state2 = findColumn(field2, this._columnStates);
1810
+ const state2 = this.getColumnState(field2);
1751
1811
  if (state2) {
1752
1812
  state2.width = element.getBoundingClientRect().width;
1753
1813
  }
@@ -1759,7 +1819,7 @@ var YatlTable = class extends LitElement {
1759
1819
  startWidth: header.getBoundingClientRect().width,
1760
1820
  columnIndex,
1761
1821
  columnField: field,
1762
- currentWidths: widthsToGridTemplates(this.columnWidths)
1822
+ currentWidths: widthsToGridTemplates(this.getGridWidths())
1763
1823
  };
1764
1824
  this.tableElement.style.setProperty(
1765
1825
  "--grid-template",
@@ -1826,7 +1886,16 @@ __decorateClass([
1826
1886
  ], YatlTable.prototype, "columns", 1);
1827
1887
  __decorateClass([
1828
1888
  property({ attribute: false })
1829
- ], YatlTable.prototype, "columnStates", 1);
1889
+ ], YatlTable.prototype, "columnOrder", 1);
1890
+ __decorateClass([
1891
+ property({ attribute: false })
1892
+ ], YatlTable.prototype, "columnVisibility", 1);
1893
+ __decorateClass([
1894
+ property({ attribute: false })
1895
+ ], YatlTable.prototype, "columnSort", 1);
1896
+ __decorateClass([
1897
+ property({ attribute: false })
1898
+ ], YatlTable.prototype, "columnWidths", 1);
1830
1899
  __decorateClass([
1831
1900
  property({ type: String, attribute: "search-query" })
1832
1901
  ], YatlTable.prototype, "searchQuery", 1);
@@ -1860,6 +1929,7 @@ export {
1860
1929
  YatlRowClickEvent,
1861
1930
  YatlSearchEvent,
1862
1931
  YatlSortEvent,
1932
+ YatlStateChangeEvent,
1863
1933
  YatlTable,
1864
1934
  createRegexTokenizer,
1865
1935
  findColumn,