package bonsai

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file bonsai_web_ui_partial_render_table_protocol.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
open Core

module Stable = struct
  open Core.Core_stable

  module Dir = struct
    module V1 = struct
      type t =
        [ `Asc
        | `Desc
        ]
      [@@deriving sexp, bin_io, equal, compare]
    end
  end

  module Order = struct
    module V1 = struct
      type 'col_id t = ('col_id * Dir.V1.t) list [@@deriving sexp, bin_io, equal, compare]
    end
  end
end

module type Col_id = sig
  type t [@@deriving equal, sexp, bin_io]
end

module Dir = struct
  include Stable.Dir.V1
end

module Order = struct
  include Stable.Order.V1

  module Action = struct
    type 'col_id t =
      | Set_sort of 'col_id
      | Add_sort of 'col_id
    [@@deriving sexp_of]
  end

  let apply_action
        (type col_id)
        t
        (module Col_id : Col_id with type t = col_id)
        (action : col_id Action.t)
    =
    let equal = Col_id.equal in
    let cycle_sort_direction id =
      match List.Assoc.find ~equal t id with
      | None -> [ id, `Asc ]
      | Some `Asc -> [ id, `Desc ]
      | Some `Desc -> []
    in
    match action with
    | Set_sort id -> cycle_sort_direction id
    | Add_sort id -> cycle_sort_direction id @ List.Assoc.remove ~equal t id
  ;;

  let to_compare t ~sorters ~default_sort : _ Incr_map_collate.Compare.t =
    match t, default_sort with
    | [], None -> Unchanged
    | [], Some compare -> Custom_by_key_and_value { compare }
    | t, default_sort ->
      let l =
        List.filter_map t ~f:(fun (id, direction) ->
          let open Option.Let_syntax in
          let%map compare = Map.find sorters id in
          match direction with
          | `Asc -> compare
          | `Desc -> fun a b -> Comparable.reverse compare a b)
      in
      let compare =
        List.append l (Option.to_list default_sort)
        |> fun cmps a b -> Comparable.lexicographic cmps a b
      in
      Custom_by_key_and_value { compare }
  ;;

  let default = []
end