Source file table.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
open! Core
open Poly
open! Import
open Vdom
include Table_intf
let cutoff compare x =
Incr.set_cutoff x (Incr.Cutoff.of_compare compare);
x
;;
module Make (Row_id : Id) (Column_id : Id) (Sort_spec : Sort_spec) = struct
include Util
module Row_id = Row_id
module Column_id = Column_id
module Sort_spec = Sort_spec
module Sort_key = Sort_spec.Sort_key
module Sort_dir = Sort_spec.Sort_dir
module Sort_criteria = struct
module By_column = struct
type 'a t =
{ column : 'a
; dir : Sort_dir.t
}
[@@deriving fields ~getters, compare, sexp]
end
type 'a t = 'a By_column.t list [@@deriving compare, sexp]
let map : 'a t -> f:('a -> 'b) -> 'b t =
fun t ~f ->
List.map t ~f:(fun (by_column : _ By_column.t) ->
{ by_column with column = f by_column.column })
;;
let filter_map : 'a t -> f:('a -> 'b option) -> 'b t =
fun t ~f ->
List.filter_map t ~f:(fun (by_column : _ By_column.t) ->
Option.map (f by_column.column) ~f:(fun column -> { by_column with column }))
;;
end
module Base_sort_criteria = struct
type t = Column_id.t Sort_criteria.By_column.t list [@@deriving compare, sexp]
let none = []
let find_precedence_and_dir t target =
List.find_mapi t ~f:(fun i { Sort_criteria.By_column.column; dir } ->
Option.some_if (Column_id.equal column target) (i + 1, dir))
;;
let remove t target =
List.filter t ~f:(fun { Sort_criteria.By_column.column; dir = _ } ->
not (Column_id.equal column target))
;;
let add_to_front t column dir = { Sort_criteria.By_column.column; dir } :: t
end
module Column = struct
type 'a t =
{ : Node.t
; : Css_gen.t
; group : string option
; sort_by : (Row_id.t -> 'a -> Sort_key.t) option
}
[@@deriving fields ~getters]
let create ?group ?sort_by ?( = Css_gen.empty) ~ () =
{ header; header_style; group; sort_by }
;;
end
module Row_node_spec = Row_node_spec
module Visibility_info = struct
type t =
{ tbody_rect : float Js_misc.Rect.t
; view_rect : float Js_misc.Rect.t
}
[@@deriving compare, sexp, fields ~getters]
end
let short_circuiting_list_compare f a b =
if phys_equal a b then 0 else List.compare f a b
;;
module Key = struct
module T = struct
type t =
{ sort_criteria : Sort_key.t option Lazy.t Sort_criteria.t
; row_id : Row_id.t
}
[@@deriving sexp, fields ~getters]
let sort_keys t = List.map t.sort_criteria ~f:Sort_criteria.By_column.column
let sort_dirs t = List.map t.sort_criteria ~f:Sort_criteria.By_column.dir
module B = Sort_criteria.By_column
let compare_by_col b1 b2 =
match Sort_dir.compare b1.B.dir b2.B.dir with
| 0 ->
(match force b1.B.column, force b2.B.column with
| None, None -> 0
| Some _, None -> -1
| None, Some _ -> 1
| Some k1, Some k2 -> Sort_spec.compare_keys b1.B.dir k1 k2)
| other -> other
;;
(** The comparison function here is written so that any two keys with the same
sort_dir sort according to that sort_dir; but keys with different sort_dirs just
have a stable relation between them. This allows us to have one Key type that is
used by all the different sorting situations, without needing to change
comparators. *)
let compare t1 t2 =
match t1.sort_criteria, t2.sort_criteria with
| [], [] -> Row_id.compare t1.row_id t2.row_id
| _ :: _, [] -> -1
| [], _ :: _ -> 1
| b :: _, _ :: _ ->
(match
short_circuiting_list_compare
compare_by_col
t1.sort_criteria
t2.sort_criteria
with
| 0 ->
Sort_spec.compare_rows_if_equal_keys
~cmp_row_id:Row_id.compare
b.B.dir
t1.row_id
t2.row_id
| other -> other)
;;
let create sort_criteria row_id = { sort_criteria; row_id }
end
include T
include Comparable.Make (T)
let convert_sort_criteria sort_criteria row_id row =
Sort_criteria.map sort_criteria ~f:(fun column ->
lazy (Option.map (Column.sort_by column) ~f:(fun sort_by -> sort_by row_id row)))
;;
let sort sort_criteria ~(rows : _ Row_id.Map.t Incr.t) =
let create_key row_id data =
create (convert_sort_criteria sort_criteria row_id data) row_id
in
Incr.Map.unordered_fold
rows
~instrumentation:(instrument "sort")
~init:Map.empty
~add:(fun ~key:row_id ~data acc ->
let key = create_key row_id data in
Core.Map.set acc ~key ~data)
~remove:(fun ~key:row_id ~data acc ->
let key = create_key row_id data in
Core.Map.remove acc key)
;;
end
module Html_id = struct
type t = string [@@deriving compare, sexp]
let table table_id = "table-" ^ Table_id.to_string table_id
let tbody table_id = table table_id ^ "-body"
let thead table_id = table table_id ^ "-header"
let column_group table_id = table table_id ^ "-column-group"
let column_header table_id = table table_id ^ "-column-header"
let column_header_cell table_id column_id =
table table_id ^ "-header-cell-" ^ Column_id.to_string column_id
;;
let row table_id row_id = table table_id ^ "-row-" ^ Row_id.to_string row_id
let cell_of_parts row_html_id column_id_str = row_html_id ^ "-col-" ^ column_id_str
let cell table_id row_id column_id =
cell_of_parts (row table_id row_id) (Column_id.to_string column_id)
;;
let spacer key = "spacer-" ^ key
end
module Row_view = Partial_render_list.Make (Row_id) (Key)
module Model = struct
type t =
{ id : Table_id.t
; : Float_type.t
; float_first_col : Float_type.t
; scroll_margin : Margin.t
; scroll_region : Scroll_region.Id.t
; focus_row : Row_id.t option
; focus_col : Column_id.t option
; sort_criteria : Base_sort_criteria.t
; height_cache : Row_view.Height_cache.t
; visibility_info : Visibility_info.t option
; col_group_row_height : int
; tbody_html_id : Html_id.t
; thead_html_id : Html_id.t
; column_group_html_id : Html_id.t
; column_header_html_id : Html_id.t
}
[@@deriving fields ~getters ~fields, compare, sexp_of]
let create
~scroll_margin
~scroll_region
~
~float_first_col
~height_guess
?id
?(initial_sort = Base_sort_criteria.none)
?initial_focus_row
?initial_focus_col
()
=
let id =
match id with
| Some id -> id
| None -> Table_id.create ()
in
{ id
; float_header
; float_first_col
; scroll_margin
; scroll_region
; focus_row = initial_focus_row
; focus_col = initial_focus_col
; sort_criteria = initial_sort
; height_cache = Row_view.Height_cache.empty ~height_guess
; visibility_info = None
; col_group_row_height = 0
; tbody_html_id = Html_id.tbody id
; thead_html_id = Html_id.thead id
; column_group_html_id = Html_id.column_group id
; column_header_html_id = Html_id.column_header id
}
;;
let sort_dirs t = List.map t.sort_criteria ~f:Sort_criteria.By_column.dir
let sort_columns t = List.map t.sort_criteria ~f:Sort_criteria.By_column.column
let set_float_first_col = Field.fset Fields.float_first_col
let = Field.fset Fields.float_header
let set_scroll_margin = Field.fset Fields.scroll_margin
let set_scroll_region = Field.fset Fields.scroll_region
let set_sort_criteria = Field.fset Fields.sort_criteria
let cycle_sorting ?keep_existing_cols t column_id ~next_dir =
let prev_dir =
Base_sort_criteria.find_precedence_and_dir t.sort_criteria column_id
|> Option.map ~f:snd
in
let cleared_sort_criteria =
match keep_existing_cols with
| None -> Base_sort_criteria.none
| Some () -> Base_sort_criteria.remove t.sort_criteria column_id
in
let sort_criteria =
match next_dir prev_dir with
| None -> cleared_sort_criteria
| Some dir -> Base_sort_criteria.add_to_front cleared_sort_criteria column_id dir
in
{ t with sort_criteria }
;;
let get_tbody_rect t = Option.map t.visibility_info ~f:Visibility_info.tbody_rect
end
module Action = struct
type t =
| Sort_column_clicked of Column_id.t
| Move_focus_row of Focus_dir.t
| Move_focus_col of Focus_dir.t
| Set_focus_row of Row_id.t option
| Set_focus_col of Column_id.t option
| Page_focus_row of Focus_dir.t
[@@deriving sexp, compare, variants]
end
type 'a row_renderer = row_id:Row_id.t -> row:'a Incr.t -> Row_node_spec.t Incr.t
let set_focus_row (m : Model.t) row_id =
if [%compare.equal: Row_id.t option] m.focus_row row_id
then m
else { m with focus_row = row_id }
;;
let set_focus_col (m : Model.t) col_id =
if [%compare.equal: Column_id.t option] m.focus_col col_id
then m
else { m with focus_col = col_id }
;;
let on_display ~(old_model : Model.t option Incr.t) (model : Model.t Incr.t) =
let%map old_focus_row = old_model >>| Option.map ~f:Model.focus_row
and focus_row = model >>| Model.focus_row
and old_focus_col = old_model >>| Option.map ~f:Model.focus_col
and focus_col = model >>| Model.focus_col
and = extra
and model = model in
fun () ->
if old_focus_row <> Some focus_row || old_focus_col <> Some focus_col
then (
let maybe_scroll x f = Option.iter x ~f:(fun x -> ignore (f extra x)) in
maybe_scroll focus_row (Extra.scroll_row_into_scroll_region model);
maybe_scroll focus_col (Extra.scroll_col_into_scroll_region model))
;;
let sort_column_clicked = Model.cycle_sorting ~next_dir:Sort_dir.next
let apply_action m =
let open Extra in
let%map m = m
and = extra in
fun (action : Action.t) ->
match action with
| Sort_column_clicked column_id -> sort_column_clicked m column_id
| Move_focus_row dir -> move_focus_row m extra ~dir
| Move_focus_col dir -> move_focus_col m extra ~dir
| Set_focus_row row_id -> set_focus_row m row_id
| Set_focus_col col_id -> set_focus_col m col_id
| Page_focus_row dir -> page_focus_row m extra ~dir
;;
let update_visibility (m : Model.t Incr.t) =
let%map m = m
and = extra in
fun ~schedule_action:_ ->
let visibility_info = Extra.update_visibility_info m extra in
let height_cache = Extra.update_height_cache m extra in
let col_group_row_height = Extra.update_col_group_row_height m extra in
if [%compare.equal: Visibility_info.t option] visibility_info m.visibility_info
&& [%compare.equal: Row_view.Height_cache.t] height_cache m.height_cache
&& [%compare.equal: int] col_group_row_height m.col_group_row_height
then m
else { m with visibility_info; height_cache; col_group_row_height }
;;
let spacer ~key =
let id_attr = Attr.id (Html_id.spacer key) in
stage (fun height ->
[ Node.tr
~key
~attrs:
[ Attr.many_without_merge
[ id_attr
; Attr.style (Css_gen.height (`Px (Float.iround_nearest_exn height)))
]
]
[]
])
;;
let sticky_pos (pos : Float_type.t Incr.t) = pos >>| Float_type.px_from_edge
let finalize_sticky_pos sticky_pos = Option.map sticky_pos ~f:(fun pos -> `Px pos)
let sticky_style ?left_sticky_pos ?top_sticky_pos ~z_index_name ~z_index_default () =
let sticky_style =
match left_sticky_pos, top_sticky_pos with
| None, None -> Css_gen.empty
| left, top -> Css_gen.position ?top ?left `Sticky
in
Css_gen.(
create
~field:"z-index"
~value:[%string "var(--%{z_index_name}, %{z_index_default#Int})"]
@> sticky_style)
;;
let ?override_on_click ~inject ~columns ~top_sticky_pos ~left_sticky_pos m =
let get_sticky_style ~ ~top_sticky_pos =
let first_cell =
sticky_style
?left_sticky_pos
?top_sticky_pos
~z_index_name:
(if is_grouped_header
then "prtable-leftmostgroupedheader-zindex"
else "prtable-leftmostheader-zindex")
~z_index_default:3
()
in
let default =
sticky_style
?top_sticky_pos
~z_index_name:
(if is_grouped_header
then "prtable-groupedheader-zindex"
else "prtable-header-zindex")
~z_index_default:2
()
in
first_cell, default
in
let get_sticky_attrs ~ ~top_sticky_pos =
let first_cell, default = get_sticky_style ~is_grouped_header ~top_sticky_pos in
Attr.style first_cell, Attr.style default
in
let =
let%map sort_criteria = m >>| Model.sort_criteria
and id = m >>| Model.id
and focused_col = m >>| Model.focus_col
and columns = columns
and top_sticky_pos =
match top_sticky_pos with
| None -> Incr.return None
| Some px ->
let%map col_group_row_height = m >>| Model.col_group_row_height in
finalize_sticky_pos (Some (px + col_group_row_height))
in
let first_cell_sticky_style, default_sticky_style =
get_sticky_style ~is_grouped_header:false ~top_sticky_pos
in
List.mapi (Map.data columns) ~f:(fun i (key, data) ->
let sticky_style =
if i = 0 then first_cell_sticky_style else default_sticky_style
in
let precedence_and_dir =
Base_sort_criteria.find_precedence_and_dir sort_criteria key
in
let sort_direction_indicator =
match precedence_and_dir with
| None -> Node.none
| Some (precedence, dir) ->
(match Sort_dir.indicator dir ~precedence with
| None -> Node.none
| Some indicator ->
let indicator_attrs =
Sort_dir.indicator_class dir ~precedence
|> Option.map ~f:Attr.class_
|> Option.to_list
in
Node.span
~attrs:[ Attr.many_without_merge indicator_attrs ]
[ Node.text (sprintf " %s" indicator) ])
in
let sort_direction_classes =
match precedence_and_dir with
| None -> []
| Some (precedence, dir) ->
List.filter_opt [ Some "sorted"; Sort_dir.header_class dir ~precedence ]
in
let on_click =
Option.value_map (Column.sort_by data) ~default:[] ~f:(fun _ ->
[ Attr.on_click (fun ev ->
match override_on_click with
| Some on_click -> on_click key ev
| None -> inject (Action.Sort_column_clicked key))
])
in
let col_is_focused =
[%compare.equal: Column_id.t option] focused_col (Some key)
in
let attrs =
[ Attr.id (Html_id.column_header_cell id key)
; Attr.classes
((if col_is_focused then "focused-col" else "")
:: "column-header"
:: sort_direction_classes)
]
@ on_click
@ [ Attr.style (Css_gen.concat [ sticky_style; data.Column.header_style ]) ]
in
Node.th
~attrs:[ Attr.many_without_merge attrs ]
[ data.Column.header; sort_direction_indicator ])
in
let group_nodes =
let top_sticky_pos = finalize_sticky_pos top_sticky_pos in
let first_cell_sticky_attr, default_sticky_attr =
get_sticky_attrs ~is_grouped_header:true ~top_sticky_pos
in
let%map columns = columns in
let groups = List.map (Map.data columns) ~f:(fun c -> (snd c).Column.group) in
if List.for_all groups ~f:Option.is_none
then None
else (
let grouped =
List.groupi groups ~break:(fun i x y ->
(i = 1 && Option.is_some left_sticky_pos)
|| not (Option.equal String.equal x y))
in
List.mapi grouped ~f:(fun i l ->
let sticky_attr =
if i = 0 then [ first_cell_sticky_attr ] else [ default_sticky_attr ]
in
let text, class_ =
match Option.join (List.hd l) with
| None -> [], "column-group-empty"
| Some s -> [ Node.text s ], "column-group-full"
in
Node.th
~attrs:
[ Attr.many_without_merge
([ Attr.classes [ "column-group"; class_ ]
; Attr.create "colspan" (Int.to_string (List.length l))
]
@ sticky_attr)
]
text)
|> Option.some)
in
let group_attrs =
let%map column_group_html_id = m >>| Model.column_group_html_id in
[ Attr.id column_group_html_id ]
in
let =
let%map column_header_html_id = m >>| Model.column_header_html_id in
[ Attr.id column_header_html_id ]
in
let%map group_nodes = group_nodes
and = header_nodes
and group_attrs = group_attrs
and = header_attrs in
[ Option.map group_nodes ~f:(fun n ->
Node.tr ~attrs:[ Attr.many_without_merge group_attrs ] n)
; Some (Node.tr ~attrs:[ Attr.many_without_merge header_attrs ] header_nodes)
]
|> List.filter_opt
;;
type row_html_ids =
{ row_html_id : Html_id.t
; cell_html_ids : Html_id.t list
}
let view_rendered_rows ~table_id ~column_ids ~row_view ~render_row ~left_sticky_pos =
let non_sticky_style =
sticky_style ~z_index_name:"prtable-cell-zindex" ~z_index_default:1 ()
in
let sticky_style =
sticky_style
?left_sticky_pos
~z_index_name:"prtable-leftmostcell-zindex"
~z_index_default:2
()
in
let%bind column_ids = column_ids in
let column_id_strs = List.map column_ids ~f:Column_id.to_string in
let rows_to_render_with_html_ids =
Incr.Map.unordered_fold
(row_view >>| Row_view.rows_to_render)
~instrumentation:(instrument "rows_to_render_with_html_ids")
~init:Key.Map.empty
~add:(fun ~key ~data:row acc ->
let row_html_id = Html_id.row table_id key.row_id in
let cell_html_ids =
List.map column_id_strs ~f:(Html_id.cell_of_parts row_html_id)
in
Map.set acc ~key ~data:({ row_html_id; cell_html_ids }, row))
~remove:(fun ~key ~data:_ acc -> Map.remove acc key)
~update:(fun ~key ~old_data:_ ~new_data:row acc ->
Map.change acc key ~f:(Option.map ~f:(Tuple2.map_snd ~f:(fun _ -> row))))
in
Incr.Map.mapi'
rows_to_render_with_html_ids
~instrumentation:(instrument "view_rendered_rows")
~f:(fun ~key ~data ->
let%bind { row_html_id; cell_html_ids } = data >>| fst in
let%map { Row_node_spec.row_attrs; cells } =
render_row ~row_id:key.row_id ~row:(data >>| snd)
in
let cells =
List.zip_exn cell_html_ids cells
|> List.mapi ~f:(fun i (cell_html_id, { Row_node_spec.Cell.attrs; nodes }) ->
let sticky_style = if i = 0 then sticky_style else non_sticky_style in
let attrs =
[ Attr.style sticky_style; Attr.id cell_html_id ] @ attrs
|> Attrs.merge_classes_and_styles
in
Node.td ~attrs:[ Attr.many_without_merge attrs ] nodes)
in
Node.tr
~key:row_html_id
~attrs:[ Attr.many_without_merge (row_attrs @ [ Attr.id row_html_id ]) ]
cells)
;;
let view ? m d ~render_row ~inject ~attrs =
let spacer_before = unstage (spacer ~key:"before") in
let spacer_after = unstage (spacer ~key:"after") in
let columns = d >>| Extra.columns in
let column_ids =
let%map column_ids = d >>| Extra.columns in
List.map (Map.data column_ids) ~f:fst
in
let is_single_row_attr =
let%map row_count = d >>| fun d -> Map.length (Extra.rows d) in
if row_count = 1 then Attr.class_ "single-row" else Attr.empty
in
let row_view = d >>| Extra.row_view in
let%bind table_id = m >>| Model.id
and top_sticky_pos = sticky_pos (m >>| Model.float_header)
and left_sticky_pos = sticky_pos (m >>| Model.float_first_col) in
let left_sticky_pos = finalize_sticky_pos left_sticky_pos in
let%map =
view_header
?override_on_click:override_header_on_click
~inject
~columns
~top_sticky_pos
~left_sticky_pos
m
and rendered_rows =
view_rendered_rows ~table_id ~column_ids ~row_view ~render_row ~left_sticky_pos
and before_height, after_height = Row_view.spacer_heights row_view
and is_single_row_attr = is_single_row_attr in
Node.table
~attrs:[ Attr.many_without_merge attrs ]
[ Node.thead
~attrs:
[ Attr.many_without_merge
[ Attr.id (Html_id.thead table_id)
; Attr.style (Css_gen.background_color `Inherit)
]
]
header
; Node.tbody
~attrs:[ Attr.id (Html_id.tbody table_id); is_single_row_attr ]
(spacer_before before_height
@ Map.data rendered_rows
@ spacer_after after_height)
]
;;
type 'a t = (Action.t, Model.t, unit, 'a Extra.t) Component.with_extra
let create
?
model
~old_model
~inject
~rows
~columns
~render_row
~attrs
=
let = Extra.create model ~rows ~columns in
let%map apply_action =
let%map apply_action = apply_action model extra in
fun action _ ~schedule_action:_ -> apply_action action
and on_display =
let%map on_display = on_display ~old_model model extra in
fun _ ~schedule_action:_ -> on_display ()
and update_visibility = update_visibility model extra
and view = view ?override_header_on_click model extra ~render_row ~inject ~attrs
and = extra
and model = model in
Component.create_with_extra
~on_display
~update_visibility
~apply_action
~extra
model
view
;;
let on_display ~(old_model : Model.t) (m : Model.t) d =
if old_model.focus_row <> m.focus_row || old_model.focus_col <> m.focus_col
then (
let maybe_scroll x f = Option.iter x ~f:(fun x -> ignore (f d x)) in
maybe_scroll m.focus_row (Extra.scroll_row_into_scroll_region m);
maybe_scroll m.focus_col (Extra.scroll_col_into_scroll_region m))
;;
let apply_action m d (action : Action.t) =
match action with
| Sort_column_clicked column_id -> sort_column_clicked m column_id
| Move_focus_row dir -> Extra.move_focus_row m d ~dir
| Move_focus_col dir -> Extra.move_focus_col m d ~dir
| Set_focus_row row_id -> set_focus_row m row_id
| Set_focus_col col_id -> set_focus_col m col_id
| Page_focus_row dir -> Extra.page_focus_row m d ~dir
;;
let update_visibility m d =
let visibility_info = Extra.update_visibility_info m d in
let height_cache = Extra.update_height_cache m d in
let col_group_row_height = Extra.update_col_group_row_height m d in
if [%compare.equal: Visibility_info.t option] visibility_info m.visibility_info
&& [%compare.equal: Row_view.Height_cache.t] height_cache m.height_cache
&& [%compare.equal: int] col_group_row_height m.col_group_row_height
then m
else { m with visibility_info; height_cache; col_group_row_height }
;;
end
module Default_sort_spec = struct
module Sort_key = struct
type t =
| String of string
| Float of float
| Integer of Int63.t
| Null
[@@deriving compare, sexp]
end
module Sort_dir = struct
type t =
| Ascending
| Descending
[@@deriving sexp, compare]
let next = function
| None -> Some Ascending
| Some Ascending -> Some Descending
| Some Descending -> None
;;
let indicator t ~precedence =
let dir_ind =
match t with
| Ascending -> "▲"
| Descending -> "▼"
in
let precedence_ind =
match precedence with
| 1 -> ""
| p -> sprintf "(%d)" p
in
Some (dir_ind ^ precedence_ind)
;;
let t ~precedence:_ =
match t with
| Ascending -> Some "sorted-asc"
| Descending -> Some "sorted-desc"
;;
let indicator_class _ ~precedence:_ = Some "sorted-indicator"
let sign = function
| Ascending -> 1
| Descending -> -1
;;
end
let compare_keys dir k1 k2 =
match (k1 : Sort_key.t), (k2 : Sort_key.t) with
| Null, _ | _, Null -> Sort_key.compare k1 k2
| _, _ -> Sort_key.compare k1 k2 * Sort_dir.sign dir
;;
let compare_rows_if_equal_keys ~cmp_row_id dir r1 r2 =
cmp_row_id r1 r2 * Sort_dir.sign dir
;;
end