@@ -2,32 +2,51 @@ import { PlainJs, Serializable } from "@mendix/filter-commons/typings/settings";
2
2
import { generateUUID } from "@mendix/widget-plugin-platform/framework/generate-uuid" ;
3
3
import { action , computed , makeObservable , observable } from "mobx" ;
4
4
import { BasicSortStore , Option , SortInstruction } from "../types/store" ;
5
+ import { AttributeMetaData , DynamicValue } from "mendix" ;
5
6
6
7
type StorableState = Array < [ number , "asc" | "desc" ] > ;
7
8
9
+ type Props = {
10
+ attributes : Array < {
11
+ attribute : AttributeMetaData ;
12
+ caption ?: DynamicValue < string > ;
13
+ } > ;
14
+ } ;
15
+
8
16
export class SortOrderStore implements BasicSortStore , Serializable {
9
17
private readonly _sortOrder : SortInstruction [ ] = [ ] ;
10
18
11
19
readonly id = `SortOrderStore@${ generateUUID ( ) } ` ;
12
- readonly options : Option [ ] ;
13
- readonly idToIndex : Map < string , number > ;
14
-
15
- constructor ( spec : { options ?: Option [ ] ; initSortOrder ?: SortInstruction [ ] } = { } ) {
16
- const { options = [ ] , initSortOrder = [ ] } = spec ;
20
+ options : Option [ ] = [ ] ;
21
+ readonly idToIndex : Map < string , number > = new Map ( ) ;
17
22
18
- this . options = [ ... options ] ;
19
- this . idToIndex = new Map ( options . map ( ( option , index ) => [ option . value , index ] ) ) ;
23
+ constructor ( spec : { initSortOrder ?: SortInstruction [ ] } ) {
24
+ const { initSortOrder = [ ] } = spec ;
20
25
this . _sortOrder = [ ...initSortOrder ] ;
21
26
22
27
makeObservable < this, "_sortOrder" > ( this , {
23
28
_sortOrder : observable ,
29
+ options : observable . struct ,
24
30
sortOrder : computed ,
25
31
setSortOrder : action ,
32
+ setProps : action ,
26
33
push : action ,
27
34
remove : action
28
35
} ) ;
29
36
}
30
37
38
+ setProps ( props : Props ) : void {
39
+ this . options = props . attributes . map ( item => ( {
40
+ value : item . attribute . id ,
41
+ caption : item . caption ?. value ?? "<empty>"
42
+ } ) ) ;
43
+
44
+ this . idToIndex . clear ( ) ;
45
+ this . options . forEach ( ( option , index ) => {
46
+ this . idToIndex . set ( option . value , index ) ;
47
+ } ) ;
48
+ }
49
+
31
50
get sortOrder ( ) : SortInstruction [ ] {
32
51
return [ ...this . _sortOrder ] ;
33
52
}
0 commit comments