@internetarchive/ads-table 0.0.6 → 0.0.8

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.js CHANGED
@@ -510,6 +510,10 @@ class AdsTable extends s {
510
510
  }
511
511
  get sortedRows() {
512
512
  var _a;
513
+ // if sorting has been done already, return the rows as-is.
514
+ if (this.selectedColumn.preSorted) {
515
+ return this.rows;
516
+ }
513
517
  const sortByPrimaryKey = this.getSortFunction(this.sortColumnDataType, this.sortDirection);
514
518
  const sortBySecondaryKey = this.getSortFunction(this.secondarySortDataType, ((_a = this.secondarySortDataType) === null || _a === void 0 ? void 0 : _a.defaultSortDirection) || this.sortDirection);
515
519
  return this.rows.sort((rowA, rowB) => {
@@ -524,9 +528,14 @@ class AdsTable extends s {
524
528
  }
525
529
  });
526
530
  }
531
+ // rows highlighted by user
527
532
  get selectedRows() {
528
533
  return this.selectedRowIds.map((id) => this.rowsById[id]).filter((x) => x);
529
534
  }
535
+ // the column you are currently sorting by
536
+ get selectedColumn() {
537
+ return this.columnByKey[this.sortKey];
538
+ }
530
539
  // handler for when a user clicks on a column header
531
540
  onColumnClick(column, defaultSortDirection = this
532
541
  .defaultSortDirection) {
@@ -541,15 +550,20 @@ class AdsTable extends s {
541
550
  this.sortDirection =
542
551
  column.dataType.defaultSortDirection || defaultSortDirection;
543
552
  }
553
+ this.emitEvent("column-click", {
554
+ column,
555
+ sortKey: this.sortKey,
556
+ sortDirection: this.sortDirection,
557
+ });
544
558
  }
545
559
  // a map of column keys to their respective data types so we don't have to call .find everywhere
546
- get columnKeyToDataType() {
547
- return Object.fromEntries(this.columns.map((col) => [col.key, col.dataType]));
560
+ get columnByKey() {
561
+ return Object.fromEntries(this.columns.map((col) => [col.key, col]));
548
562
  }
549
563
  // data type of the currently sorted column
550
564
  get sortColumnDataType() {
551
565
  if (this.sortKey) {
552
- return this.columnKeyToDataType[this.sortKey];
566
+ return this.columnByKey[this.sortKey].dataType;
553
567
  }
554
568
  else {
555
569
  return undefined;
@@ -558,7 +572,7 @@ class AdsTable extends s {
558
572
  // data type of the secondary sort column
559
573
  get secondarySortDataType() {
560
574
  if (this.secondarySortKey) {
561
- return this.columnKeyToDataType[this.secondarySortKey];
575
+ return this.columnByKey[this.secondarySortKey].dataType;
562
576
  }
563
577
  else {
564
578
  return undefined;
@@ -899,41 +913,13 @@ const NumberDataType = {
899
913
  defaultSortDirection: "descending",
900
914
  };
901
915
  // for comparing and formatting dates - Note, date formatting operation
902
- // is considerably slower than any other ones here, so we keep an
903
- // internal rolling cache of 1k values since date values in general
904
- // are not liable to change often, and tables should not have more
905
- // items than this.
906
- class DateFormatCache {
907
- // get a formatted date string from the cache
908
- static get(date) {
909
- return DateFormatCache.cache[date.toString()];
910
- }
911
- // note - date references always serialize to identical keys, probably
912
- // using their internal millisecond values.
913
- static getOrAdd(date) {
914
- const cachedValue = DateFormatCache.get(date) || formatDate(date);
915
- DateFormatCache.pruneCache();
916
- DateFormatCache.cache[date.toString()] = cachedValue; // set idempotently
917
- return cachedValue;
918
- }
919
- static pruneCache() {
920
- const cacheKeys = Object.keys(DateFormatCache.cache);
921
- if (cacheKeys.length >= DateFormatCache.MAX_CACHE_SIZE) {
922
- const randomDeleteIndex = Math.floor(Math.random() * cacheKeys.length);
923
- const deleteKey = cacheKeys[randomDeleteIndex];
924
- console.log("deleting from cache", cacheKeys.length, deleteKey, DateFormatCache.cache);
925
- delete DateFormatCache.cache[deleteKey];
926
- }
927
- }
928
- }
929
- DateFormatCache.MAX_CACHE_SIZE = 1000;
930
- DateFormatCache.cache = {};
916
+ // is SLOW relative to some other operations.
931
917
  const DateDataType = {
932
918
  compare: (a, b) => {
933
919
  var _a;
934
920
  return ((_a = NumberDataType.compare) === null || _a === void 0 ? void 0 : _a.call(NumberDataType, a.getTime(), b.getTime())) || 0;
935
921
  },
936
- format: (date) => DateFormatCache.getOrAdd(date),
922
+ format: (date) => formatDate(date),
937
923
  defaultSortDirection: "descending",
938
924
  };
939
925
  // for comparing and formatting numbers that represent bytes