1- type Sort = {
2- property : string ;
3- descending : boolean ;
1+ import { z } from "zod" ;
2+
3+ export const SortSchema = z . object ( {
4+ property : z . string ( ) ,
5+ descending : z . boolean ( ) ,
6+ } ) ;
7+ export type Sort = z . infer < typeof SortSchema > ;
8+
9+ type Persistence = {
10+ get : ( ) => Sort ;
11+ set : ( sort : Sort ) => boolean ;
412} ;
513
614type SortedTableOptions < T > = {
715 table : string ;
816 data ?: T [ ] ;
917 buildRow : ( entry : T ) => HTMLTableRowElement ;
10- initialSort ?: Sort ;
11- } ;
18+ } & (
19+ | { initialSort ?: Sort ; persistence ?: never }
20+ | { persistence ?: Persistence ; initialSort ?: never }
21+ ) ;
22+
1223export class SortedTable < T > {
13- protected data : { source : T ; element ?: HTMLTableRowElement } [ ] ;
24+ protected data : { source : T ; element ?: HTMLTableRowElement } [ ] = [ ] ;
1425 private table : JQuery < HTMLTableElement > ;
1526 private buildRow : ( entry : T ) => HTMLTableRowElement ;
1627 private sort ?: Sort ;
28+ private persistence ?: Persistence ;
1729
18- constructor ( { table , data , buildRow , initialSort } : SortedTableOptions < T > ) {
19- this . table = $ ( table ) ;
30+ constructor ( options : SortedTableOptions < T > ) {
31+ this . table = $ ( options . table ) ;
2032 if ( this . table === undefined ) {
21- throw new Error ( `No element found for ${ table } ` ) ;
33+ throw new Error ( `No element found for ${ options . table } ` ) ;
2234 }
2335
24- this . buildRow = buildRow ;
25- this . data = [ ] ;
26- if ( data !== undefined ) {
27- this . setData ( data ) ;
36+ this . buildRow = options . buildRow ;
37+
38+ if ( options . data !== undefined ) {
39+ this . setData ( options . data ) ;
2840 }
2941
30- if ( initialSort !== undefined ) {
31- this . sort = initialSort ;
42+ if ( "persistence" in options && options . persistence !== undefined ) {
43+ this . persistence = options . persistence ;
44+ this . sort = this . persistence . get ( ) ;
45+ this . doSort ( ) ;
46+ } else if ( "initialSort" in options && options . initialSort !== undefined ) {
47+ this . sort = options . initialSort ;
3248 this . doSort ( ) ;
3349 }
3450
@@ -43,20 +59,23 @@ export class SortedTable<T> {
4359 target . dataset [ "sortDefaultDirection" ] === "desc" ;
4460 if ( property === undefined ) return ;
4561
46- if ( this . sort === undefined || property !== this . sort . property ) {
47- this . sort = { property, descending : defaultDirection } ;
62+ let updatedSort = this . sort ;
63+
64+ if ( updatedSort === undefined || property !== updatedSort . property ) {
65+ updatedSort = { property, descending : defaultDirection } ;
4866 } else {
49- this . sort . descending = ! this . sort ?. descending ;
67+ updatedSort . descending = ! updatedSort ?. descending ;
5068 }
69+ this . setSort ( updatedSort ) ;
5170
52- this . doSort ( ) ;
5371 this . updateBody ( ) ;
5472 } ;
5573 }
5674 }
5775
5876 public setSort ( sort : Partial < Sort > ) : void {
5977 this . sort = { ...this . sort , ...sort } as Sort ;
78+ this . persistence ?. set ( this . sort ) ;
6079 this . doSort ( ) ;
6180 }
6281
0 commit comments